<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="70px">
      <el-form-item label="姓名" prop="patientName">
        <el-input v-model="queryParams.patientName" placeholder="请输入患者姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="电话" prop="patientPhone">
        <el-input v-model="queryParams.patientPhone" placeholder="请输入患者电话" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="医院" prop="hospitalAgencyId">
        <el-select v-model="queryParams.hospitalAgencyId" filterable placeholder="请选择医院" style="width:200px" clearable
          @clear="clearhospitalAgency" @change="changehospitalAgency">
          <el-option v-for="item in hospitalAgencylist" :key="item.id" :label="item.agencyName" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="院区" prop="campusAgencyId" v-if="queryParams.hospitalAgencyId">
        <el-select v-model="queryParams.campusAgencyId" filterable placeholder="请选择院区" style="width:200px" clearable
          @clear="clearcampusAgency" @change="changecampusAgency">
          <el-option v-for="item in campusAgencylist" :key="item.id" :label="item.agencyName" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="科室" prop="departmentId" v-if="queryParams.hospitalAgencyId">
        <el-select v-model="queryParams.departmentId" filterable placeholder="请选择科室" style="width:200px" clearable>
          <el-option v-for="item in departmentlist" :key="item.id" :label="item.departmentName" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="异常原因" prop="abnormalCauseValue">
        <el-select v-model="queryParams.abnormalCauseValue" filterable placeholder="请输入异常原因" style="width:200px"
          clearable>
          <el-option v-for="item in abnormalCauselist" :key="item.id" :label="item.abnormalCauseValue" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <!-- (CALL_DETECTION：通话检测，MANUAL_BLACKOUT：手动拉黑,MANUAL_ADDITION：手动添加，HIS_CONNECT：HIS对接) -->
      <!-- <el-form-item label="来源" prop="blacklistSource">
        <el-select v-model="queryParams.blacklistSource" placeholder="请选择来源" clearable>
          <el-option v-for="dict in dict.type.blacklist_source" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </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"
          v-hasPermi="['manage:blacklist:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['manage:blacklist:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['manage:blacklist:remove']">删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['manage:blacklist:export']">导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="blacklistList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" />
      <el-table-column label="序号" type="index" width="55" align="center" />
      <el-table-column label="姓名" align="center" prop="patientName" />
      <el-table-column label="电话" align="center" prop="patientPhone" width='120' />
      <el-table-column label="医院" align="center" prop="hospitalAgencyName" width='130' />
      <el-table-column label="科室名称" align="center" prop="departmentName" width='130' />
      <el-table-column label="异常原因" align="center" prop="abnormalCauseValue" />
      <!-- <el-table-column label="来源" align="center" prop="blacklistSource">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.blacklist_source" :value="scope.row.blacklistSource" />
        </template>
</el-table-column> -->
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleUpdate(scope.row)"
            v-hasPermi="['manage:blacklist:edit']">修改</el-button>
          <el-button size="mini" type="text" @click="handleDelete(scope.row)"
            v-hasPermi="['manage:blacklist:remove']">删除</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" />
    <!-- 添加或修改患者-黑明单关系对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="patientName">
          <el-input v-model="form.patientName" placeholder="请输入姓名" :disabled="title == '修改黑名单'" />
        </el-form-item>
        <el-form-item label="身份证" prop="cardNo">
          <el-input v-model="form.cardNo" placeholder="请输入身份证" :disabled="title == '修改黑名单'" maxlength="18" />
        </el-form-item>
        <el-form-item label="电话" prop="patientPhone">
          <el-input v-model="form.patientPhone" placeholder="请输入电话" :disabled="title == '修改黑名单'" maxlength="11" />
        </el-form-item>
        <el-form-item label="医院" prop="hospitalAgencyId">
          <el-select v-model="form.hospitalAgencyId" filterable placeholder="请选择医院" style="width:380px" clearable
            :disabled="title == '修改黑名单'" @clear="clearhospitalAgency(1)" @change="changehospitalAgency($event, 1)">
            <el-option v-for="item in hospitalAgencylist" :key="item.id" :label="item.agencyName" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="院区" prop="campusAgencyId" v-if="form.hospitalAgencyId">
          <el-select v-model="form.campusAgencyId" filterable placeholder="请选择院区" style="width:380px" clearable
            :disabled="title == '修改黑名单'" @clear="clearcampusAgency(1)" @change="changecampusAgency($event, 1)">
            <el-option v-for="item in form_campusAgencylist" :key="item.id" :label="item.agencyName" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="科室名称" prop="departmentId" v-if="form.hospitalAgencyId">
          <el-select v-model="form.departmentId" filterable placeholder="请选择科室" style="width:380px" clearable
            :disabled="title == '修改黑名单'">
            <el-option v-for="item in form_departmentlist" :key="item.id" :label="item.departmentName" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="异常原因" prop="abnormalCauseId">
          <el-select v-model="form.abnormalCauseId" filterable placeholder="请输入异常原因" style="width:380px" clearable
            @clear="clearabnormalCauseValue" @change="changeabnormalCauseValue">
            <el-option v-for="item in abnormalCauselist" :key="item.id" :label="item.abnormalCauseValue"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="补充说明" prop="supplementIllustrateId">
          <el-select v-model="form.supplementIllustrateId" filterable placeholder="请输入异常原因" style="width:380px"
            clearable>
            <el-option v-for="item in supplementIllustratelist" :key="item.id" :label="item.abnormalCauseValue"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="功能限制" prop="functionLimitation">
          <el-input v-model="form.functionLimitation" placeholder="终止所有服务" disabled />
        </el-form-item>
        <!-- <el-form-item label="来源" prop="blacklistSource">
          <el-select v-model="form.blacklistSource" placeholder="请选择来源">
            <el-option v-for="dict in dict.type.blacklist_source" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBlacklist, getBlacklist, delBlacklist, addBlacklist, updateBlacklist, causegetlist } from "@/api/manage/blacklist";
import { selectAgencyList, getDepartmentList, getAgencyList } from "@/api/manage/selectAgencyList";

export default {
  name: "Blacklist",
  dicts: ['blacklist_source'],
  data() {
    //验证身份证
    var isCardId = (rule, value, callback) => {
      if (value == null) {
        callback(new Error("请输入患者身份证号"));
      } else {
        const reg =
          /^\d{6}(18|19|20)?\d{2}(0[1-9]|1[0-2])(([0-2][1-9])|10|20|30|31)\d{3}(\d|X|x)$/;
        const card = reg.test(value);
        if (!card) {
          callback(new Error("身份证号格式有误！"));
        } else {
          callback();
        }
      }
    };
    return {
      //医院list
      hospitalAgencylist: [],
      // 院区list
      campusAgencylist: [],
      //科室list
      departmentlist: [],
      //form-医院list
      form_hospitalAgencylist: [],
      // form-院区list
      form_campusAgencylist: [],
      //form-科室list
      form_departmentlist: [],
      //异常原因
      abnormalCauselist: [],
      //补充说明
      supplementIllustratelist: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 患者-黑明单关系表格数据
      blacklistList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        hospitalAgencyId: null,
        abnormalCauseId: null,
        abnormalCauseValue: null,
        supplementIllustrateValue: null,
        departmentId: null,
        campusAgencyId: null,
        blacklistSource: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        patientName: [
          { required: true, message: '请输入患者姓名', trigger: 'change' }
        ],
        cardNo: [
          { required: true, message: '', validator: isCardId, trigger: 'change' }
        ],
        hospitalAgencyId: [
          { required: true, message: '请选择医院', trigger: 'change' }
        ],
        campusAgencyId: [
          { required: true, message: '请选择院区', trigger: 'change' }
        ],
        departmentId: [
          { required: true, message: '请选择科室', trigger: 'change' }
        ],
        abnormalCauseId: [
          { required: true, message: '请输入异常原因', trigger: 'change' }
        ],
        supplementIllustrateId: [
          { required: true, message: '请输入补充说明', trigger: 'change' }
        ],
        patientPhone: [
          { required: true, message: '请输入患者手机号', trigger: 'change' },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号",
            trigger: "blur"
          }
        ],
      }
    };
  },
  created() {
    this.selectAgencyinfo();
    this.getList();
    this.causegetinfo();
  },
  methods: {
    /** 查询患者-黑明单关系列表 */
    getList() {
      this.loading = true;
      listBlacklist(this.queryParams).then(response => {
        this.blacklistList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        cardNo: null,
        campusAgencyId: null,
        departmentId: null,
        wardId: null,
        patientName: null,
        abnormalCauseId: null,
        abnormalCauseValue: null,
        supplementIllustrateId: null,
        supplementIllustrateValue: null,
        functionLimitation: '终止所有服务',
        blacklistSource: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加黑名单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBlacklist(id).then(response => {
        this.form = response.data;
        if (this.form.hospitalAgencyId) {
          this.changehospitalAgency(this.form.hospitalAgencyId, 1, 2)
        }
        if (this.form.campusAgencyId) {
          this.changecampusAgency(this.form.campusAgencyId, 1, 2)
        }
        if (this.form.abnormalCauseId) {
          this.changeabnormalCauseValue(this.form.abnormalCauseId, 1)
        }
        this.open = true;
        this.title = "修改黑名单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      if (this.form.hospitalAgencyId && this.hospitalAgencylist?.length > 0) {
        this.form.hospitalAgencyName = this.hospitalAgencylist.find(e => e.id == this.form.hospitalAgencyId)?.agencyName
      }
      if (this.form.campusAgencyId && this.form_campusAgencylist?.length > 0) {
        this.form.campusAgencyName = this.form_campusAgencylist.find(e => e.id == this.form.campusAgencyId)?.agencyName
      }
      if (this.form.departmentId && this.form_departmentlist?.length > 0) {
        this.form.departmentName = this.form_departmentlist.find(e => e.id == this.form.departmentId)?.departmentName
      }
      if (this.form.abnormalCauseId && this.abnormalCauselist?.length > 0) {
        this.form.abnormalCauseValue = this.abnormalCauselist.find(e => e.id == this.form.abnormalCauseId)?.abnormalCauseValue
      }
      if (this.form.supplementIllustrateId && this.supplementIllustratelist?.length > 0) {
        this.form.supplementIllustrateValue = this.supplementIllustratelist.find(e => e.id == this.form.supplementIllustrateId)?.abnormalCauseValue
      }
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBlacklist(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBlacklist(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除？').then(function () {
        return delBlacklist(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('manage/blacklist/export', {
        ...this.queryParams
      }, `blacklist_${new Date().getTime()}.xlsx`)
    },
    //获取医院list
    selectAgencyinfo() {
      let query = {
        agencyStatus: 'ON',
        nodeType: 'HOSPITAL',
      }
      selectAgencyList(query).then(res => {
        this.hospitalAgencylist = res.data
      })
    },
    //获取下级单位
    getAgencyListinfo(nodeType, id, type) {
      let query = {
        nodeType: nodeType,
      }
      if (nodeType == 'HOSPITAL') {
        query.hospitalId = id
      } else if (nodeType == 'CAMPUS') {
        query.campusId = id
      } else if (nodeType == 'DEPARTMENT') {
        query.departmentId = id
      }
      getAgencyList(query).then(res => {
        if (type) {
          if (nodeType == 'HOSPITAL') {
            this.form_campusAgencylist = res.data.campusList
            this.form_departmentlist = res.data.departmentList
          } else if (nodeType == 'CAMPUS') {
            this.form_departmentlist = res.data.departmentList
          }
        } else {
          if (nodeType == 'HOSPITAL') {
            this.campusAgencylist = res.data.campusList
          }
          if (nodeType == 'CAMPUS' || nodeType == 'HOSPITAL') {
            this.departmentlist = res.data.departmentList
          }
        }
      })
    },
    //选中医院获取院区
    changehospitalAgency(id, type, typetwo) {
      this.getAgencyListinfo('HOSPITAL', id, type)
      // let query = {
      //   agencyStatus: 'ON',
      //   nodeType: 'CAMPUS',
      //   parentId: id,
      // }
      // selectAgencyList(query).then(res => {
      if (type) {
        if (!typetwo) {
          this.form.campusAgencyId = null
          this.form.departmentId = null
          this.form.wardId = null
          this.form.certificateIssuingDoctorId = null
        }
      } else {
        this.queryParams.campusAgencyId = null
        this.queryParams.departmentId = null
        this.queryParams.wardId = null
        this.queryParams.certificateIssuingDoctorId = null
      }
      // })
    },
    //选中院区获取科室
    changecampusAgency(id, type, typetwo) {
      this.getAgencyListinfo('CAMPUS', id, type)
      // let query = {
      //   nodeType: 'DEPARTMENT',
      //   hospitalAgencyId: id,
      // }
      // getDepartmentList(query).then(res => {
      if (type) {
        if (!typetwo) {
          this.form.departmentId = null
          this.form.wardId = null
        }
      } else {
        this.queryParams.departmentId = null
        this.queryParams.wardId = null
      }
      // })
    },
    //清空医院
    clearhospitalAgency(type) {
      if (type) {
        this.form.campusAgencyId = null
        this.form.departmentId = null
        this.form.wardId = null
      } else {
        this.queryParams.campusAgencyId = null
        this.queryParams.departmentId = null
        this.queryParams.wardId = null
      }
    },
    //清空院区
    clearcampusAgency(type) {
      if (type) {
        this.form.departmentId = null
        this.form.wardId = null
        this.getAgencyListinfo('HOSPITAL', this.form.hospitalAgencyId, type)
      } else {
        this.queryParams.departmentId = null
        this.queryParams.wardId = null
        this.getAgencyListinfo('HOSPITAL', this.queryParams.hospitalAgencyId)
      }
    },
    //获取异常原因
    causegetinfo() {
      let query = {
        abnormalCauseLevel: 'ABNORMAL_CAUSE'
      }
      causegetlist(query).then(res => {
        this.abnormalCauselist = res.data
        this.form.supplementIllustrateValue = null
      })
    },
    //清空异常原因
    clearabnormalCauseValue() {
      this.form.supplementIllustrateValue = null
    },
    //选中异常原因
    changeabnormalCauseValue(id, type) {
      let query = {
        parentId: id,
      }
      causegetlist(query).then(res => {
        this.supplementIllustratelist = res.data
        if (!type) {
          this.form.supplementIllustrateValue = null
        }
      })
    },
  }
};
</script>
