<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="患者表id" prop="patientId">
        <el-input
          v-model="queryParams.patientId"
          placeholder="请输入患者表id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="cardNo">
        <el-input
          v-model="queryParams.cardNo"
          placeholder="请输入身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <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="familyMemberPhone">
        <el-input
          v-model="queryParams.familyMemberPhone"
          placeholder="请输入家属电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出生日期，格式：yyyy-MM-dd" prop="birthDate">
        <el-date-picker clearable
          v-model="queryParams.birthDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择出生日期，格式：yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="就诊类型，门诊：OUTPATIENT_SERVICE，住院：BE_IN_HOSPITAL" prop="visitType">
        <el-select v-model="queryParams.visitType" placeholder="请选择就诊类型，门诊：OUTPATIENT_SERVICE，住院：BE_IN_HOSPITAL" clearable>
          <el-option
            v-for="dict in dict.type.visit_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item label="医院" prop="hospitalAgencyId">
        <el-select v-model="queryParams.hospitalAgencyId" filterable placeholder="请选择医院" style="width:208px" clearable>
          <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="所属院区id" prop="campusAgencyId">
        <el-input v-model="queryParams.campusAgencyId" placeholder="请输入所属院区id" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="所属科室id" prop="departmentId">
        <el-input v-model="queryParams.departmentId" placeholder="请输入所属科室id" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="所属病区id" prop="wardId">
        <el-input v-model="queryParams.wardId" placeholder="请输入所属病区id" clearable @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"
          v-hasPermi="['unitconfig:visitRecord: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="['unitconfig:visitRecord: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="['unitconfig:visitRecord: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="['unitconfig:visitRecord:export']">导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="visitRecordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" />
      <el-table-column label="序号" type="index" width="55" align="center" />
      <el-table-column label="pat_id" align="center" prop="id" />
      <el-table-column label="姓名" align="center" prop="patientName" />
      <el-table-column label="电话" align="center" prop="patientPhone" />
      <el-table-column label="身份证号" align="center" prop="cardNo" />
      <!-- <el-table-column label="家属电话" align="center" prop="familyMemberPhone" /> -->
      <!-- <el-table-column label="地址" align="center" prop="address" /> -->
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <span>{{ scope.row.sex == 'MALE' ? '男' : '' }}
            {{ scope.row.sex == 'FEMALE' ? '女' : '' }}
          </span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="出生日期，格式：yyyy-MM-dd" align="center" prop="birthDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birthDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="就诊类型，门诊：OUTPATIENT_SERVICE，住院：BE_HOSPITALIZED" align="center" prop="visitType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.visit_type" :value="scope.row.visitType" />
        </template>
      </el-table-column> 
      <el-table-column label="所属医院名称" align="center" prop="hospitalAgencyName" />
      <el-table-column label="所属院区名称" align="center" prop="campusAgencyName" />
      <el-table-column label="所属科室名称" align="center" prop="departmentName" />
      <el-table-column label="所属病区名称" align="center" prop="wardName" />-->
      <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="['unitconfig:visitRecord:edit']">编辑</el-button>
          <el-button size="mini" type="text" @click="goPatientConfigurationVisitRecords(scope.row)">就诊记录</el-button>
          <!-- <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['unitconfig:visitRecord: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="请输入患者姓名" />
        </el-form-item>
        <el-form-item label="患者电话" prop="patientPhone">
          <el-input v-model="form.patientPhone" placeholder="请输入患者电话" />
        </el-form-item>
        <el-form-item label="身份证号" prop="cardNo">
          <el-input v-model="form.cardNo" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="所属医院" prop="hospitalAgencyId">
          <el-select v-model="form.hospitalAgencyId" filterable placeholder="请选择所属医院" style="width:380px" clearable>
            <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="pat_id" prop="patientId">
          <el-input v-model="form.patientId" placeholder="" disabled />
        </el-form-item> -->
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </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 { listVisitRecord, getVisitRecord, delVisitRecord, addVisitRecord, updateVisitRecord } from "@/api/unitconfig/patientConfiguration";
import { selectAgencyList } from "@/api/manage/selectAgencyList";
import { getAge } from "@/utils/age";

export default {
  name: "patientConfiguration",
  dicts: ['visit_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 患者就诊记录基本信息表格数据
      visitRecordList: [],
      //医院
      hospitalAgencylist: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        patientId: null,
        cardNo: null,
        patientName: null,
        patientPhone: null,
        familyMemberPhone: null,
        address: null,
        sex: null,
        birthDate: null,
        visitMethod: null,
        hospitalAgencyId: null,
        campusAgencyId: null,
        departmentId: null,
        wardId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        hospitalAgencyId: [
          { required: true, message: "所属医院不能为空", trigger: "blur" }
        ],
        patientName: [
          { required: true, message: "患者姓名不能为空", trigger: "blur" }
        ],
        patientPhone: [
          { required: true, message: "患者电话不能为空", trigger: "blur" }
        ],
        cardNo: [
          { required: true, message: "身份证号不能为空", trigger: "blur" }
        ],
        // patientId: [
        //   { required: true, message: "患者表id不能为空", trigger: "blur" }
        // ],
      }
    };
  },
  created() {
    this.getList();
    this.selectAgencyinfo();
  },
  methods: {
    /** 查询患者就诊记录基本信息列表 */
    getList() {
      this.loading = true;
      listVisitRecord(this.queryParams).then(response => {
        this.visitRecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //获取医院list
    selectAgencyinfo() {
      let query = {
        agencyStatus: 'ON',
        nodeType: 'HOSPITAL',
      }
      selectAgencyList(query).then(res => {
        this.hospitalAgencylist = res.data
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        patientId: null,
        cardNo: null,
        patientName: null,
        patientPhone: null,
        familyMemberPhone: null,
        address: null,
        sex: null,
        birthDate: null,
        age: null,
        nation: null,
        visitMethod: null,
        visitName: null,
        hospitalAgencyId: null,
        hospitalAgencyName: null,
        campusAgencyId: null,
        campusAgencyName: null,
        departmentId: null,
        departmentName: null,
        wardId: null,
        wardName: null,
        attendingPhysicianId: null,
        attendingPhysicianName: null,
        mainDiagnosis: null,
        marriage: null,
        medicalHistoryNarrator: null,
        admissionTime: null,
        dischargeTime: null,
        recordTime: null,
        outpatientVisitInfo: null,
        hospitalizationDays: null,
        inHospitalInfo: null,
        outHospitalInfo: null,
        visitSerialNumber: null,
        inHospitalNumber: null,
        responsibleNurse: null,
        surgicalName: 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
      getVisitRecord(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改患者就诊记录基本信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateVisitRecord(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVisitRecord(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除患者就诊记录基本信息编号为"' + ids + '"的数据项？').then(function () {
        return delVisitRecord(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('manage/visitRecord/export', {
        ...this.queryParams
      }, `visitRecord_${new Date().getTime()}.xlsx`)
    },
    // 跳转就诊记录
    goPatientConfigurationVisitRecords(row) {
      this.$router.push({
        path: "/unitconfig/PatientConfigurationVisitRecords",
        query: {
          patientId: row.id,
          cardNo: row.cardNo,
          patientName: row.patientName,
          patientPhone: row.patientPhone,
          sex: row.sex,
          age: row.birthDate ? getAge(row.birthDate) : '',
          hospitalAgencyId: row.hospitalAgencyId,
          hospitalAgencyName: row.hospitalAgencyName
        },
      });
    },
  }
};
</script>
