<template>
  <div class="container">
    <el-page-header @back="$emit('back')" :content="orgMap.orgName">
    </el-page-header>
    <direct-search
      ref="form"
      :label-position="'right'"
      :forms="searchList"
      :style="{ textAlign: 'left', marginTop: '20px' }"
      @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="title"
      :form-edit="formEdit"
      :form-data="formData"
      @handleConfirm="handleConfirm"
    ></dialog-form>
  </div>
</template>

<script>
import paginationMixin from "@/components/TabComponents/mixin"
import { getDepartPage, addDepart, delDepart } from "@/api/org.js"

export default {
  name: "OrgManage",
  props: ["orgMap"],
  data() {
    return {
      listLoading: false,
      // 查询列表
      searchList: [
        {
          label: "科室名称",
          type: "input",
          prop: "deptName",
          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: "deptName",
        },
        {
          label: "科室代码",
          minWidth: 120,
          value: "deptCode",
        },
        {
          label: "操作",
          width: 280,
          fixed: "right",
          operType: "button",
          operations: [
            {
              func: this.handleAdd,
              formatter(row) {
                return {
                  label: "编辑",
                  type: "primary",
                }
              },
            },
            {
              func: this.handleDel,
              formatter(row) {
                return {
                  label: "删除",
                  type: "warning",
                }
              },
            },
          ],
        },
      ],
      tableData: [],
      cacheForm: {},
      title: "",
      formData: [
        {
          type: "input",
          label: "科室名称",
          placeholder: "请输入科室名称",
          prop: "deptName",
          rules: [{ required: true, message: "请输入科室名称" }],
        },
        {
          type: "input",
          label: "科室代码",
          placeholder: "请输入科室代码",
          prop: "deptCode",
          rules: [{ required: true, message: "请输入科室代码" }],
        },
      ],
      formEdit: {},
    }
  },
  mixins: [paginationMixin],

  methods: {
    handleAdd({ id, deptName, deptCode }) {
      this.title = id ? "编辑科室" : "新增科室"
      this.formEdit = id ? { id, deptName, deptCode } : {}
      this.$refs.dialog.open()
    },
    handleConfirm(form) {
      const data = Object.assign({ ...this.orgMap }, form)
      const msg = data.id ? "编辑成功" : "新增成功"
      addDepart(data).then((res) => {
        if (res.code === 1) {
          this.$message.success(msg)
          this.handleSearch()
          this.$refs.dialog.close()
        }
      })
    },
    handleDel(row) {
      this.$confirm(`是否删除【${row.deptName}】?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delDepart(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) {
      this.cacheForm = Object.assign(this.cacheForm, form)
      this.listLoading = true
      const params = Object.assign({ orgId: this.orgMap.orgId }, this.cacheForm)
      for (let key in params) {
        if (params[key] === "") {
          delete params[key]
        }
      }
      params.current = this.pageIndex
      params.size = this.pageSize
      getDepartPage(params).then((res) => {
        this.listLoading = false
        if (res.code === 1) {
          const d = res.data
          this.tableData = d.records || []
          this.total = Number(d.total)
        }
      })
    },
  },
  created() {
    this.handleFormSearch()
  },
}
</script>
