<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="院区" prop="wardId">
        <el-select v-model="queryParams.wardId" filterable clearable style="width: 240px">
          <el-option label="全部" value=""></el-option>
          <el-option
            v-for="item in wardIdList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="科室名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入科室名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">
          搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">
          新增科室
        </el-button>
      </el-col>
    </el-row>

    <el-table ref="table" v-loading="loading" :data="pageList" row-key="id" border>
      <el-table-column
        align="center"
        label="序号"
        type="index"
        :index="(index) => (queryParams.pageNum - 1) * queryParams.pageSize + index + 1"
        width="120"
      />
      <el-table-column
        align="center"
        label="院区名称"
        prop="wardName"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <!-- 一级科室或者搜索时才展示 -->
          {{ scope.row.parentId === '0' || isSearch ? scope.row.wardName : "" }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="一级科室名称"
        prop="name"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          {{ scope.row.parentId === '0' ? scope.row.name : "" }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="二级科室名称"
        prop="level2name"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          {{ scope.row.parentId !== '0' ? scope.row.name : "" }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="操作"
        class-name="small-padding fixed-width"
        fixed="right"
      >
        <template slot-scope="scope">
          <!-- <el-button size="mini" type="text" @click="handleEdit(scope.row)">置顶</el-button>
          <el-button size="mini" type="text" @click="handleEdit(scope.row)">置底</el-button>
          <el-button size="mini" type="text" @click="handleEdit(scope.row)">上移</el-button>
          <el-button size="mini" type="text" @click="handleEdit(scope.row)">下移</el-button> -->
          <el-button size="mini" type="text" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="text" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <CrudPopup ref="crud" />
  </div>
</template>

<script>
import { deptPageList, deleteDept, checkDeptUse, getAllHospital } from "@/api/admin";
import CrudPopup from "./components/CrudPopup/index.vue";

export default {
  name: "DeptManage",
  components: {
    CrudPopup,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 日期范围
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: "",
        wardId: ""
      },
      pageList: [],
      isSearch: false,
      wardIdList: [] // 院区列表
    };
  },
  created() {
    this.loadData();
  },
  activated() {
    this.loadData();
  },
  watch: {
    "queryParams.wardId"(val) {
      const wardId = val;
      if (wardId) {
        this.getList(wardId);
      } else {
        this.pageList = [];
      }
    },
  },
  methods: {
    loadData() {
      this.loadWardIdList();
      this.getList();
    },
    /*获取院区列表*/
    async loadWardIdList() {
      const res = await getAllHospital();
      this.wardIdList = res.data;
    },
    /** 查询角色列表 */
    getList() {
      this.loading = true;
      return deptPageList(this.queryParams)
        .then((res) => {
          const { list, page } = res.data;
          this.pageList = list || [];
          this.total = Number(page.total);
          this.isSearch = !!this.queryParams.name;
        })
        .finally(() => {
          this.loading = false;
        });
    },

    // 表单重置
    reset() {
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增 */
    handleAdd() {
      this.$refs.crud.open().then((res) => {
        this.getList().then(() => {
          // 二级科室，自动展示当前科室
          if (res.parentId && res.parentId !== '0') {
            const row = this.pageList.find((item) => item.id === res.parentId);
            if (row) {
              this.$refs.table.toggleRowExpansion(row);
            }
          }
        });
      });
    },
    /** 编辑 */
    handleEdit(item) {
      this.$refs.crud
        .open({
          ...item,
          parentId: item.parentId || undefined,
        })
        .then(() => {
          this.getList();
        });
    },
    handleDelete(item) {
      this.$modal
        .confirm("是否确认删除？")
        .then(async () => {
          await checkDeptUse(item.id); // 删除前，校验科室是否已被使用
          await deleteDept(item.id);
          this.$message.success("操作成功");
          this.getList();
        })
        .catch(() => {});
    },
  },
};
</script>
<style lang="scss" scoped></style>
