<template>
  <div class="mall-classify-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" size="small">
      <el-form-item label="分类名称" prop="categoryName">
        <el-input v-model="queryParams.categoryName" placeholder="请输入分类名称"></el-input>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker v-model="daterangeCreateTime" size="small" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="上架状态" prop="shelvedStatus">
        <el-select v-model="queryParams.shelvedStatus" placeholder="请选择上架状态">
          <el-option label="上架" :value="true"></el-option>
          <el-option label="下架" :value="false"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery">查询</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增分类</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="list">
      <el-table-column type="index" label="序号" width="80" align="center">
        <template slot-scope="scope">
          <div>
            {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="categoryName" label="分类名称" align="center"></el-table-column>
      <el-table-column prop="categorySort" label="分类排序" align="center"></el-table-column>
      <el-table-column prop="shelvedStatus" label="上架状态" align="center">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.shelvedStatus">上架</el-tag>
          <el-tag type="danger" v-if="!scope.row.shelvedStatus">下架</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button type="text" @click="setStatus(scope.row)">{{
            scope.row.shelvedStatus ? "下架" : "上架"
          }}</el-button>
          <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
    <!-- 新增分类抽屉 -->
    <el-drawer :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="classifyRules" label-width="100px" size="small" class="drawer-form">
        <el-form-item label="分类名称" prop="categoryName">
          <el-input style="width: 300px" v-model="form.categoryName" maxlength="20" placeholder="请输入分类名称"></el-input>
        </el-form-item>
        <el-form-item label="分类排序" prop="categorySort">
          <el-input-number v-model="form.categorySort" :min="1" label="排序" :precision="0"></el-input-number>
        </el-form-item>
        <el-form-item label="上架状态" prop="shelvedStatus">
          <el-select style="width: 300px" v-model="form.shelvedStatus" placeholder="请选择上架状态">
            <el-option label="上架" :value="true"></el-option>
            <el-option label="下架" :value="false"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div class="drawer-footer">
        <el-button size="small" @click="cancel">取 消</el-button>
        <el-button size="small" :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { productCategoryAdd, productCategoryEdit, productCategoryPage, productCategoryInfo, productCategoryDel, productCategoryShelvedStatus } from "@/api/pointsMall"
export default {
  name: "MallClassify",
  data () {
    return {
      //按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 测试单表表格数据
      list: [],
      title: "新增分类",
      daterangeCreateTime: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        categoryName: undefined,
        shelvedStatus: undefined,
      },
      drawer: {
        visible: false,
      },
      form: {
        categoryName: undefined,
        shelvedStatus: true,
        categorySort: 1
      },
      classifyRules: {
        categoryName: [{ required: true, message: "请输入分类名称", trigger: "blur" }],
        categorySort: [{ required: true, message: "请输入排序", trigger: "blur" }],
        shelvedStatus: [{ required: true, message: "请选择状态", trigger: "change" }],
      },
    };
  },
  created () {
    this.getList();
  },
  methods: {
    // 表单重置
    reset () {
      this.form = {
        categoryName: undefined,
        shelvedStatus: true,
        categorySort: 1
      }
      this.resetForm("form");
    },
    /** 上下架按钮操作 */
    setStatus (row) {
      const ids = row.categoryId || this.ids;
      let statusText = row.shelvedStatus ? '下架' : '上架';
      this.$modal.confirm('是否确认' + statusText + '分类名称为"' + row.categoryName + '"的数据项？').then(() => {
        this.loading = true;
        return productCategoryShelvedStatus(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess(statusText + "成功");
      }).finally(() => {
        this.loading = false;
      });
    },
    getList () {
      this.loading = true;
      this.queryParams["startTime"] = undefined;
      this.queryParams["endTime"] = undefined;
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams["startTime"] = this.daterangeCreateTime[0];
        this.queryParams["endTime"] = this.daterangeCreateTime[1];
      }
      productCategoryPage(this.queryParams).then(response => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    /** 搜索按钮操作 */
    handlePage () {
      this.queryParams.pageNum = 1;
      this.getPage();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "新增分类";
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.loading = true;
      this.reset();
      const id = row.categoryId || this.ids
      productCategoryInfo(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改分类";
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const ids = row.categoryId || this.ids;
      this.$modal.confirm('是否确认删除分类名称为"' + row.categoryName + '"的数据项？').then(() => {
        this.loading = true;
        return productCategoryDel(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.categoryId != null) {
            productCategoryEdit(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            productCategoryAdd(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.mall-classify-container {
  padding: 20px;
  background: #fff;

  .search-area {
    display: flex;
    align-items: center;
    padding: 15px 0;
    flex-shrink: 0;

    .el-form {
      width: 100%;
    }

    .el-form-item {
      margin-bottom: 0;
      margin-right: 20px;

      &:last-child {
        margin-right: 0;
        margin-left: auto;
      }
    }
  }
  .search-bar {
    margin-bottom: 20px;
  }

  .pagination {
    margin-top: 20px;
    text-align: right;
  }
}

.drawer-form {
  padding-left: 20px;
}
.drawer-footer {
  margin-left: 120px;
}
</style>
