<template>
  <div class="app-container">
    <!-- 表单筛选 -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      inline
      v-show="showSearch"
      label-width="80px"
    >
      <el-form-item label="分类名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入分类名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
        >
          <!-- dict in dict.type.sys_user_sex -->
          <el-option
            v-for="dict in statusOption"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 100%"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <!-- 按钮组 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <!-- v-hasPermi="['abuwx:wxuser:add']" -->
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="selectedRow.length !== 1"
          @click="handleUpdate"
          >修改</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="selectedRow.length < 1"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getTableList"
      ></right-toolbar>
    </el-row>
    <!-- 表格 -->
    <el-table
      v-loading="loading"
      :data="tableList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
        :selectable="selectable"
      />
      <el-table-column label="分类名称" align="center" prop="name" />
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="显示状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag
            effect="dark"
            :type="scope.row.status === '0' ? 'success' : 'danger'"
          >
            {{ scope.row.status === "0" ? "显示" : "隐藏" }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="备注"
        align="center"
        prop="remark"
        show-overflow-tooltip
      />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="修改时间" align="center" prop="updateTime" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            v-if="scope.row.status === '1'"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @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="getTableList"
    />
    <!-- 新增编辑弹窗 -->
    <el-dialog
      :title="title"
      :visible.sync="dlgVisible"
      append-to-body
      width="30%"
    >
      <AddAndEdit
        :loading="dlgLoading"
        :data="formData"
        @confirm="handleConfirm"
        @cancel="handleCancel"
      ></AddAndEdit>
    </el-dialog>
  </div>
</template>

<script>
import {
  getList,
  getDetail,
  add,
  update,
  del,
} from "@/api/infoManage/classification";
import AddAndEdit from "./components/addAndEdit.vue";
export default {
  name: "Classification",
  components: {
    AddAndEdit,
  },
  data() {
    return {
      // 显示搜索条件
      showSearch: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: "",
        status: "",
      },
      // 时间
      dateRange: [],
      // 状态组
      statusOption: [
        { value: "0", label: "显示" },
        { value: "1", label: "隐藏" },
      ],
      // 表格相关
      loading: false,
      tableList: [],
      selectedRow: [],
      total: 0,
      //  弹窗相关
      title: "",
      dlgVisible: false,
      formData: {},
      dlgLoading: false,
    };
  },
  created() {
    this.getTableList();
  },
  methods: {
    // 搜索
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getTableList();
    },
    // 重置
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.getTableList();
    },
    // 新增
    handleAdd() {
      this.title = "新增";
      this.dlgVisible = true;
      this.formData = {};
    },
    // 修改
    handleUpdate(row) {
      this.title = "编辑";
      this.dlgVisible = true;
      this.dlgLoading = true;
      // const id = row.id || this.selectedRow[0].id;
      getDetail(row.id)
        .then((res) => {
          if (res.code === 200) {
            this.formData = res.data;
          }
        })
        .finally(() => (this.dlgLoading = false));
    },
    // 弹窗确认
    handleConfirm(val) {
      if (val.id) {
        update(val).then((res) => {
          if (res.code === 200) {
            this.$modal.msgSuccess("编辑成功");
            this.handleCancel();
            this.getTableList();
          }
        });
      } else {
        add(val).then((res) => {
          if (res.code === 200) {
            this.$modal.msgSuccess("新增成功");
            this.handleCancel();
            this.getTableList();
          }
        });
      }
    },
    // 弹窗取消
    handleCancel() {
      this.dlgVisible = false;
    },
    // 删除
    handleDelete(row) {
      const selectIds = this.selectedRow.map((item) => item.id);
      const ids = row.id || selectIds;
      this.$modal
        .confirm("是否确认删除数据？")
        .then(function () {
          return del(ids);
        })
        .then(() => {
          this.getTableList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    // 表格选中
    handleSelectionChange(selection) {
      this.selectedRow = selection;
    },
    // 表格当前行是否可选
    selectable(row, index) {
      return row.status === "1";
    },
    // 查询列表
    getTableList() {
      let params = this.addDateRange(this.queryParams, this.dateRange);
      console.log(params);
      this.loading = true;
      getList(params)
        .then((res) => {
          if (res.code === 200) {
            this.tableList = res.rows;
            this.total = res.total;
          }
        })
        .finally(() => (this.loading = false));
    },
  },
};
</script>

<style lang="scss" scoped></style>
