<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: "input",
          prop: "code",
          placeholder: "请输入参数代码",
        },
        {
          label: "参数值",
          type: "input",
          prop: "value",
          placeholder: "请输入参数值",
        },
        {
          label: "类型",
          type: "input",
          prop: "type",
          placeholder: "请输入类型",
        },
        {
          type: "button",
          value: "查询",
          icon: "el-icon-search",
        },
        {
          type: "button",
          color: "primary",
          icon: "el-icon-plus",
          value: "添加",
          func: this.handleAdd,
        },
      ],
      columns: [
        {
          label: "参数代码",
          minWidth: 120,
          value: "code",
        },

        {
          label: "参数值",
          minWidth: 120,
          value: "value",
        },
        {
          label: "类型",
          minWidth: 100,
          value: "type",
        },
        {
          label: "目标值名称",
          minWidth: 100,
          value: "targetName",
        },
        {
          label: "目标值",
          minWidth: 100,
          value: "targetValue",
        },
        {
          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: "input",
          label: "参数代码",
          placeholder: "请输入参数代码",
          prop: "code",
          disabled: false,
          rules: [{ required: true, message: "参数代码" }],
        },
        {
          type: "input",
          label: "参数值",
          placeholder: "请输入参数值",
          prop: "value",
          rules: [{ required: true, message: "请输入参数值" }],
        },

        {
          type: "input",
          label: "类型",
          placeholder: "请选择类型",
          prop: "type",
          rules: [{ required: true, message: "请选择类型" }],
        },
        {
          type: "input",
          label: "目标值名称",
          placeholder: "请选择目标值名称",
          prop: "targetName",
        },
        {
          type: "input",
          label: "目标值",
          placeholder: "请选择目标值",
          prop: "targetValue",
        },
      ],
      formEdit: {},
    }
  },
  watch: {
    groupId(groupId) {
      if (groupId) {
        this.handleFormSearch({ groupId })
      } else {
        this.tableData = []
      }
    },
  },

  methods: {
    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()
        }
      })
    },
    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>
