<template>
  <div class="app-container">
    <el-form size="small" label-width="70px" class="mb8">
      <el-row :gutter="10">
        <el-col :xs="24" :sm="12" :md="12" :lg="6" :xl="4">
          <el-form-item label="应用科室">
            <TreeDept
              v-model="formParams.deptId"
              clearable
              style="width: 100%"
              :options="deptList"
              :autoFetch="false"
              :props="{ emitPath: false }"
              @change="onChangeDept"
            />
            <!-- <el-select v-model="formParams.deptId" placeholder="请选择应用科室"></el-select> -->
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="12" :lg="6" :xl="4">
          <el-form-item label="应用疾病">
            <el-select
              v-model="formParams.labelId"
              placeholder="请选择应用疾病"
              filterable
              clearable
            >
              <el-option
                v-for="item in diseaseList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="12" :lg="6" :xl="4">
          <el-form-item label="提醒类型">
            <el-select
              v-model="formParams.categoryId"
              placeholder="请选择提醒类型"
              filterable
              clearable
            >
              <el-option
                v-for="item in categoryList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="12" :lg="6" :xl="4">
          <el-form-item label="提醒名称">
            <el-input v-model="formParams.name" placeholder="请输入提醒名称" />
          </el-form-item>
        </el-col>
        <el-col :xs="12" :sm="6" :md="6" :lg="4" :xl="4">
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="onSearch"
            :disabled="loading"
          >
            搜索
          </el-button>
          <el-button icon="el-icon-refresh" size="mini" :disabled="loading" @click="handleReset">
            重置
          </el-button>
        </el-col>
      </el-row>
    </el-form>
    <div style="display: flex; justify-content: flex-end; padding-bottom: 8px">
      <el-button icon="el-icon-plus" type="primary" size="mini" plain @click="onAdd">
        新增提醒
      </el-button>
    </div>
    <el-table v-loading="loading" align="center" :data="tableData">
      <el-table-column type="index" label="序号" width="100px" align="center"></el-table-column>
      <el-table-column label="提醒名称" align="center" prop="name"></el-table-column>
      <el-table-column label="提醒类型" align="center" prop="categoryName"></el-table-column>
      <el-table-column
        label="最后编辑时间"
        align="center"
        prop="updateTime"
        min-width="160px"
      ></el-table-column>
      <el-table-column label="应用科室" align="center" prop="deptName"></el-table-column>
      <el-table-column label="应用疾病" align="center" prop="labelName"></el-table-column>
      <el-table-column label="操作" align="center" fixed="right">
        <template slot-scope="{ row }">
          <el-button size="mini" type="text" @click="onEdit(row)">编辑</el-button>
          <el-button size="mini" type="text" @click="onDel(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :total="pageable.total"
      :page.sync="pageable.pageNum"
      :limit.sync="pageable.pageSize"
      @pagination="getList"
    />
    <AddDialog ref="dialogRef" @onOk="onOk" />
  </div>
</template>
<script>
import { TableMixin } from "@/mixins/tableMixin2";
import {
  remindTemplateListApi,
  deleteRemindTemplateApi,
} from "@/api/hospital/remindManage/remindList";
import AddDialog from "./components/addDialog/index.vue";
import TreeDept from "@/components/TreeDept";
import { getTreeHospDeptByPatient } from "@/api/admin";
import { getReminderListApi } from "@/api/common/dictManage/reminder";
import { getMedicalLabel } from "@/api/hospital";
import { getClientId } from "@/utils/auth";

export default {
  name: "RemindManage",
  components: {
    AddDialog,
    TreeDept,
  },
  mixins: [TableMixin],
  data() {
    return {
      apiMethod: remindTemplateListApi,
      formParams: {
        deptId: "",
        labelId: null,
        categoryId: null,
        name: "",
      },
      deptList: [],
      diseaseList: [],
      categoryList: [],
    };
  },
  methods: {
    /*获取科室数据*/
    async getDeptList() {
      const res = await getTreeHospDeptByPatient();
      this.deptList = res.data || [];
    },
    // 获取病种数据
    async loadMedicalLabel() {
      const { deptId } = this.formParams;
      const res = await getMedicalLabel({
        deptId,
      });
      this.diseaseList = res.data || [];
    },
    // 获取提醒分类列表
    async getRemindTypeList() {
      const {
        data: { list },
      } = await getReminderListApi({
        pageNum: 1,
        pageSize: 1000,
        clientId: getClientId(),
      });
      this.categoryList = list || [];
    },
    loadOptions() {
      this.getDeptList();
      this.loadMedicalLabel();
      this.getRemindTypeList();
    },
    onChangeDept() {
      this.formParams.labelId = null;
      this.loadMedicalLabel();
    },
    paramsCallback() {
      const { deptId, labelId, categoryId, name } = this.formParams;
      return {
        pageNum: this.pageable.pageNum,
        pageSize: this.pageable.pageSize,
        deptId,
        labelId,
        categoryId,
        name,
      };
    },
    onAdd() {
      this.$nextTick(() => {
        this.$refs.dialogRef.openDialog();
      });
    },
    onEdit(row) {
      this.$nextTick(() => {
        this.$refs.dialogRef.openDialog(row);
      });
    },
    onDel(row) {
      this.$modal.confirm("是否确认删除？").then(async () => {
        await deleteRemindTemplateApi({ id: row.id });
        this.$message.success("操作成功");
        this.pageable.pageNum = 1;
        this.getList();
      });
    },
    onOk() {
      this.pageable.pageNum = 1;
      this.formParams = {
        deptId: "",
        labelId: null,
        categoryId: null,
        remark: "",
        name: "",
      };
      this.getList();
    },
    handleReset() {
      this.formParams = {
        deptId: "",
        labelId: null,
        categoryId: null,
        remark: "",
      };
      this.onSearch();
    },
  },
  mounted() {
    this.loadOptions();
  },
};
</script>
<style lang="scss" scoped>
.el-select,
.el-input {
  width: 100%;
}
</style>
