<template>
  <div class="parameter h-full w-full">
    <TableHeader>
      <div slot="left">
        <el-button size="small" type="primary" @click="handleAdd"
          >新增参数</el-button
        >
        <el-button size="small" type="danger" @click="handleDelAll"
          >批量删除</el-button
        >
      </div>
      <div slot="right">
        <el-form ref="searchForm" :model="searchForm" inline>
          <el-form-item prop="type">
            <el-select
              size="small"
              style="width: 150px"
              v-model="searchForm.type"
            >
              <el-option label="按参数名称" value="name"></el-option>
              <el-option label="按二级组织" value="secondOrganize"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="keyword">
            <el-input
              size="small"
              v-model="searchForm.keyword"
              style="width: 200px"
              class="ml10 mr10"
              placeholder="请输入关键字搜索"
              @keyup.native.enter="handleSearch"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button size="small" type="primary" @click="handleSearch"
              >搜 索</el-button
            >
          </el-form-item>
          <el-form-item>
            <el-button size="small" @click="handleReset">重 置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </TableHeader>
    <!-- 表格 -->
    <div class="table-content">
      <y-table
        ref="MyTable"
        :loading="loading"
        :data="tableData"
        :column="column"
        border
        tooltip-effect="dark"
        :max-height="600"
        :row-key="(row) => row.id"
        @selection-change="handleSelectionChange"
      ></y-table>
    </div>
    <Pagination
      :total="total"
      :current.sync="current"
      :size.sync="size"
      @get="getParamList"
    ></Pagination>
    <!-- 新增参数 -->
    <AddParameter
      ref="AddParameter"
      :show.sync="addVisible"
      :title="title"
      :dict="dict"
      @success="getParamList"
    ></AddParameter>
  </div>
</template>

<script>
import TableHeader from "@/components/TableHeader.vue";
import AddParameter from "./components/AddParameter.vue";
import { getParamList, delParam } from "@/api/system";
export default {
  components: {
    TableHeader,
    AddParameter,
  },
  data() {
    return {
      column: [
        {
          label: "全选",
          type: "selection",
          width: "55",
          align: "center",
          reserveSelection: true,
        },
        {
          label: "序号",
          type: "index",
          width: "55",
          align: "center",
          index: (index) => {
            return (this.current - 1) * this.size + index + 1;
          },
        },

        {
          label: "参数名称",
          prop: "name",
          align: "center",
        },
        {
          label: "一级组织",
          prop: "firstOrganize",
          align: "center",
        },
        {
          label: "二级组织",
          prop: "secondOrganize",
          align: "center",
        },
        {
          label: "参数键",
          prop: "paramKey",
          align: "center",
        },
        {
          label: "参数值",
          prop: "paramValue",
          align: "center",
        },
        {
          label: "参数有效状态",
          prop: "validStatus",
          align: "center",
          formatter: (row) => {
            if (this.dict.validStatus) {
              return (
                <el-tag type="info" size="small">
                  {this.dict.validStatus[row.validStatus]}
                </el-tag>
              );
            }
          },
        },
        {
          label: "备注",
          prop: "remark",
          align: "center",
        },
        {
          label: "操作",
          align: "center",
          width: "120",
          formatter: (row) => {
            return (
              <div class="flex justify-center gap-4">
                <span
                  class="primary cursor-pointer"
                  onClick={() => this.handleEdit(row)}
                >
                  编辑
                </span>
                <span
                  class="delete cursor-pointer"
                  onClick={() => this.handleDel(row.id)}
                >
                  删除
                </span>
              </div>
            );
          },
        },
      ],
      searchForm: {
        type: "name",
        keyword: "",
      },
      tableData: [],
      current: 1,
      size: 10,
      total: 0,
      loading: false,
      selectKeys: [],
      addVisible: false,
      title: "新增参数",
      dict: {}, // 字典
    };
  },
  created() {
    this.getParamList();
  },
  computed: {},
  methods: {
    // 系统事项列表
    async getParamList() {
      this.loading = true;
      let form = {};
      let value = `%${this.searchForm.keyword}%`;
      if (this.searchForm.type == "name") {
        form.name = value;
      } else {
        form.secondOrganize = value;
      }
      let res = await getParamList({
        page: this.current,
        size: this.size,
        ...form,
      });
      if (res.data.code == 1) {
        let { data, total, dict } = res.data.data;
        this.dict = dict;
        if (!data.length && this.current > 1) {
          this.current -= 1;
          this.getParamList();
        }
        this.tableData = data;
        this.total = total;
      }
      this.loading = false;
    },

    // 批量移除
    handleDelAll() {
      if (!this.selectKeys.length) {
        this.$message.warning("请先勾选数据");
        return;
      }
      let ids = this.selectKeys.map((v) => v.id).join(",");
      this.handleDel(ids);
    },
    // 搜索
    handleSearch() {
      this.current = 1;
      this.$clearSelection("MyTable");
      this.getParamList();
    },
    // 重置
    handleReset() {
      this.current = 1;
      this.$clearSelection("MyTable");
      this.$resetForm("searchForm");
      this.getParamList();
    },
    // 选中
    handleSelectionChange(select) {
      this.selectKeys = select;
    },

    // 新增
    handleAdd() {
      this.title = "新增参数";
      this.$refs.AddParameter.onAdd();
      this.addVisible = true;
    },
    // 编辑
    handleEdit(row) {
      this.title = "编辑参数";
      this.$refs.AddParameter.onEdit(row);
      this.addVisible = true;
    },
    // 移除
    handleDel(id) {
      this.$confirm("此操作将删除所选数据，是否继续？", "系统提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        cancelButtonClass: "btn-custom-cancel",
        type: "warning",
      })
        .then(async () => {
          let res = await delParam({ id });
          let { code, msg } = res.data;
          if (code === 1) {
            this.$message.success(msg);
            this.getParamList();
            this.$clearSelection("MyTable");
          }
        })
        .catch(() => {
          console.log("取消成功！");
        });
    },
  },
};
</script>

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