<template>
  <div class="container">
    <direct-search
      ref="form"
      :label-position="'right'"
      :forms="searchList"
      :style="{ textAlign: 'left' }"
      @handleSearch="handleFormSearch"
    />
    <el-table-self
      ref="table"
      :table-data="tableData"
      :columns="columns"
      :list-loading="listLoading"
      :current-page="pageIndex"
      :total-count="total"
      :page-sizes="pageSizes"
      :page-size="pageSize"
      @pageSizeChange="handleSizeChange"
      @currentPageChange="handleCurrentChange"
    />
    <dialog-form
      width="450px"
      :close-modal="false"
      ref="dialog"
      :title="formEdit.id ? '编辑' : '新增'"
      :form-edit="formEdit"
      :form-data="formData"
      @handleConfirm="handleConfirm"
    ></dialog-form>
  </div>
</template>

<script>
import paginationMixin from "@/components/TabComponents/mixin"
import {
  getParamConfigList,
  setParamConfig,
  delParamConfig,
} from "@/api/coop-group.js"
export default {
  name: "ParameterConfig",
  mixins: [paginationMixin],
  props: {
    groupId: String,
  },
  data() {
    return {
      listLoading: false,
      // 查询列表
      searchList: [
        {
          label: "类型",
          type: "select",
          prop: "type",
          placeholder: "请输入类型",
          optsFormatter: () => {
            return this.dictMap && this.dictMap["sys_param_type"]
          },
        },
        {
          label: "参数值",
          type: "input",
          prop: "value",
          placeholder: "请输入参数值",
        },

        {
          type: "button",
          value: "查询",
          icon: "el-icon-search",
        },
        {
          type: "button",
          color: "primary",
          icon: "el-icon-plus",
          value: "添加",
          func: this.handleAdd,
        },
      ],
      columns: [
        {
          label: "类型",
          minWidth: 100,
          value: "type",
          formatter: (row) => {
            return this.$handle.formatDicList(
              this.dictMap["sys_param_type"],
              row.type
            )
          },
        },
        {
          label: "参数代码",
          minWidth: 120,
          value: "code",
          formatter: (row) => {
            const item =
              this.dictMap["sys_param_type"].find(
                (_) => _.value === row.type
              ) || {}
            const list = item.children || []
            return this.$handle.formatDicList(list, row.code)
          },
        },

        {
          label: "参数值",
          minWidth: 120,
          value: "value",
        },

        {
          label: "创建时间",
          minWidth: 180,
          value: "createTime",
        },
        {
          label: "更新时间",
          minWidth: 180,
          value: "updateTime",
        },
        {
          label: "操作",
          width: 160,
          fixed: "right",
          operType: "button",
          operations: [
            {
              func: this.handleAdd,
              formatter(row) {
                return {
                  label: "编辑",
                  type: "text",
                }
              },
            },
            {
              func: this.handleDel,
              style: {
                color: "#F56C6C",
              },
              formatter(row) {
                return {
                  label: "删除",
                  type: "text",
                }
              },
            },
          ],
        },
      ],
      tableData: [],
      cacheForm: {},
      formData: [
        {
          type: "select",
          label: "类型",
          placeholder: "请选择类型",
          prop: "type",
          rules: [{ required: true, message: "请选择类型" }],
          func: this.typeChange,
          optsFormatter: () => {
            return this.dictMap && this.dictMap["sys_param_type"]
          },
        },
        {
          type: "select",
          label: "参数代码",
          placeholder: "请输入参数代码",
          prop: "code",
          disabled: false,
          rules: [{ required: true, message: "参数代码" }],
          opts: [],
        },
        {
          type: "input",
          label: "参数值",
          placeholder: "请输入参数值",
          prop: "value",
          rules: [{ required: true, message: "请输入参数值" }],
        },

        {
          type: "input",
          label: "默认参数值",
          placeholder: "请选择默认参数值",
          prop: "defaultValue",
        },
      ],
      formEdit: {},
    }
  },
  watch: {
    groupId(groupId) {
      if (groupId) {
        this.handleFormSearch({ groupId })
      } else {
        this.tableData = []
      }
    },
  },

  methods: {
    typeChange(val) {
      this.$refs.dialog.initFields({ code: "" })
      const opt = this.dictMap["sys_param_type"].find((_) => _.value === val)
      this.formData[1].opts = (opt && opt.children) || []
    },
    handleAdd(row) {
      this.formEdit = Object.assign({}, row)
      if (!this.groupId) {
        this.$message.warning("请先选择协作组")
        return
      }
      this.$refs.dialog.open()
    },
    handleConfirm(form) {
      const data = Object.assign(form, {
        groupId: this.groupId,
      })
      setParamConfig(data)
        .then((res) => {
          if (res.code === 1) {
            this.$message.success("添加成功")
            this.handleSearch()
            this.$refs.dialog.close()
          }
        })
        .finally(() => {
          this.$refs.dialog.loading = false
        })
    },
    handleDel(row) {
      this.$confirm(`是否删除【${row.code || ""}】?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delParamConfig(row.id).then((res) => {
            if (res.code === 1) {
              this.$message({
                type: "success",
                message: "删除成功!",
              })
              this.handleSearch()
            }
          })
        })
        .catch(() => {})
    },

    // 查询
    handleFormSearch(form) {
      this.pageIndex = 1
      this.handleSearch(form)
    },
    handleSearch(form) {
      const params = Object.assign(this.cacheForm, form)
      this.listLoading = true
      for (let key in params) {
        if (params[key] === "") {
          delete params[key]
        }
      }
      params.current = this.pageIndex
      params.size = this.pageSize
      getParamConfigList(params).then((res) => {
        this.listLoading = false
        if (res.code === 1) {
          const d = res.data
          this.tableData = d.records || []
          this.total = Number(d.total)
        }
      })
    },
  },
}
</script>
