<template>
  <div class="container">
    <template v-if="!isDepart">
      <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="title"
        :form-edit="formEdit"
        :form-data="formData"
        @handleConfirm="handleConfirm"
      ></dialog-form>
    </template>
    <template v-if="isDepart">
      <depart-manage :org-map="orgMap" @back="isDepart = false"></depart-manage>
    </template>
  </div>
</template>

<script>
import paginationMixin from "@/components/TabComponents/mixin"
import DepartManage from "./DepartManage"
import { getOrgPage, addOrg, delOrg } from "@/api/org.js"

export default {
  name: "org",
  components: {
    DepartManage,
  },
  data() {
    return {
      isDepart: false,
      orgMap: {},
      listLoading: false,
      // 查询列表
      searchList: [
        {
          label: "机构名称",
          type: "input",
          prop: "orgName",
          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: "orgName",
        },
        {
          label: "机构代码",
          minWidth: 120,
          value: "orgCode",
        },
        {
          label: "操作",
          width: 280,
          fixed: "right",
          operType: "button",
          operations: [
            {
              func: this.handleDepart,
              formatter(row) {
                return {
                  label: "科室管理",
                  type: "primary",
                }
              },
            },
            {
              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: "orgName",
          rules: [{ required: true, message: "请输入机构名称" }],
        },
        {
          type: "input",
          label: "机构代码",
          placeholder: "请输入机构代码",
          prop: "orgCode",
          rules: [{ required: true, message: "请输入机构代码" }],
        },
      ],
      formEdit: {},
    }
  },
  mixins: [paginationMixin],

  methods: {
    handleDepart({ orgName, id }) {
      this.orgMap = {
        orgName,
        orgId: id,
      }
      this.isDepart = true
    },
    handleAdd({ id, orgName, orgCode }) {
      this.title = id ? "编辑机构" : "新增机构"
      this.formEdit = id ? { id, orgName, orgCode } : {}
      this.$refs.dialog.open()
    },
    handleConfirm(form) {
      const data = Object.assign({}, form)
      const msg = data.id ? "编辑成功" : "新增成功"
      addOrg(data).then((res) => {
        if (res.code === 1) {
          this.$message.success(msg)
          this.handleSearch()
          this.$refs.dialog.close()
        }
      })
    },
    handleDel(row) {
      this.$confirm(`是否删除【${row.orgName}】?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delOrg(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({}, this.cacheForm)
      for (let key in params) {
        if (params[key] === "") {
          delete params[key]
        }
      }
      params.current = this.pageIndex
      params.size = this.pageSize
      getOrgPage(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>
