<template>
  <page-layer>
    <template #header>
      <el-input size="small" v-model="patNo" clearable style="width: 120px" placeholder="住院/门诊号"></el-input>
      <el-date-picker
          v-model="dateRange"
          type="daterange"
          :shortcuts="maxHalfYear"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          size="small"
          style="width: 240px"
      ></el-date-picker>
      <el-input v-show="benYuanFlag" size="small" v-model="name" clearable style="width: 120px"
                placeholder="姓名"></el-input>
      <el-input v-show="benYuanFlag" size="small" v-model="certno" clearable style="width: 120px"
                placeholder="身份证"></el-input>
      <el-divider direction="vertical"></el-divider>
      <el-checkbox v-model="benYuanFlag">查询本院</el-checkbox>
      <el-divider direction="vertical"></el-divider>
      <el-button type="primary" icon="Search" @click="getInhospInfo">查询</el-button>
      <el-divider direction="vertical"></el-divider>
      <div style="display: inline-flex" v-if="benYuanFlag">
        <el-button type="primary" icon="Download" @click="benYuanDownload" :disabled="inhosps.length === 0">导出
        </el-button>
      </div>
      <div style="display: inline-flex" v-else>
        <el-button type="primary" @click="beforeGetCumulativeInfo">累计信息查询</el-button>
        <el-button type="success" @click="beforeGetRefMedins">转院查询</el-button>
        <el-button type="success" @click="beforeTransHospRcd">转院备案</el-button>
        <el-button type="success" @click="beforeCancelTransHospRcd">转院备案撤销</el-button>
      </div>
    </template>
    <template #main>
      <el-table v-if="benYuanFlag" :data="inhosps.slice((currentPage - 1) * pageSize, currentPage * pageSize)" stripe
                highlight-current-row :height="tableHeight">
        <el-table-column prop="smallDeptName" label="入院科室"></el-table-column>
        <el-table-column prop="zkWardName" label="转科科室"></el-table-column>
        <el-table-column prop="inpatientNo" label="住院号" width="60"></el-table-column>
        <el-table-column prop="name" label="姓名" width="60"></el-table-column>
        <el-table-column prop="sexName" label="性别" width="40"></el-table-column>
        <el-table-column prop="certno" label="身份证"></el-table-column>
        <el-table-column prop="insuplcAdmdvsName" label="参保地"></el-table-column>
        <el-table-column prop="admissTimes" label="次数" width="40"></el-table-column>
        <el-table-column prop="disDiag" label="诊断代码"></el-table-column>
        <el-table-column prop="disDiagComment" label="诊断名称" width="220"></el-table-column>
        <el-table-column prop="totalCharge" label="总费用"></el-table-column>
        <el-table-column prop="referPhysicianName" label="管床医生"></el-table-column>
        <el-table-column prop="responceTypeName" label="医保身份"></el-table-column>
      </el-table>
      <el-table
          v-else
          :data="inhosps.slice((currentPage - 1) * pageSize, currentPage * pageSize)"
          stripe
          highlight-current-row
          :height="tableHeight"
          @row-click="handleClickInHospInfo"
      >
        <el-table-column prop="mdtrtId" label="就诊Id"></el-table-column>
        <el-table-column prop="psnNo" label="人员编号"></el-table-column>
        <el-table-column prop="psnCertTypeName" label="人员证件类型"></el-table-column>
        <el-table-column prop="certno" label="证件号码"></el-table-column>
        <el-table-column prop="psnName" label="人员姓名"></el-table-column>
        <el-table-column prop="gendName" label="性别"></el-table-column>
        <el-table-column prop="brdy" label="出生日期"></el-table-column>
        <el-table-column prop="insutypeName" label="险种类型"></el-table-column>
        <el-table-column prop="begndate" label="开始日期"></el-table-column>
        <el-table-column prop="medTypeName" label="医疗类别"></el-table-column>
        <el-table-column prop="iptOtpNo" label="住院/门诊号"></el-table-column>
        <el-table-column prop="outFlagName" label="异地标志"></el-table-column>
      </el-table>
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[15, 30, 45, 70, 100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="inhosps.length"
          style="margin-top: 5px"
      ></el-pagination>
      <el-dialog v-model="showTrsHspRcdInfo" title="转院备案" width="70%" :close-on-click-modal="false">
        <el-row :gutter="5">
          <el-col :span="6">
            <i class="required">*</i>人员编号：
            <el-input v-model="trsHspRcdInfo.psnNo" disabled></el-input>
          </el-col>
          <el-col :span="6">
            险种类型：
            <el-input v-model="trsHspRcdInfo.insutypeName" disabled></el-input>
          </el-col>
          <el-col :span="6">
            联系电话：
            <el-input v-model="trsHspRcdInfo.tel" clearable></el-input>
          </el-col>
          <el-col :span="6">
            联系地址：
            <el-input v-model="trsHspRcdInfo.addr" clearable></el-input>
          </el-col>
        </el-row>
        <div style="height: 16px"></div>
        <el-row :gutter="5">
          <el-col :span="6">
            <i class="required">*</i>参保机构医保区划：
            <el-input v-model="trsHspRcdInfo.insuOptins" clearable></el-input>
          </el-col>
          <el-col :span="6">
            <i class="required">*</i>诊断代码：
            <el-input v-model="trsHspRcdInfo.diagCode" clearable></el-input>
          </el-col>
          <el-col :span="6">
            <i class="required">*</i>诊断名称：
            <el-input v-model="trsHspRcdInfo.diagName" clearable></el-input>
          </el-col>
          <el-col :span="6">
            疾病病情描述：
            <el-input v-model="trsHspRcdInfo.diseCondDscr" clearable></el-input>
          </el-col>
        </el-row>
        <div style="height: 16px"></div>
        <el-row :gutter="5">
          <el-col :span="6">
            <i class="required">*</i>转往定点医药机构编号：
            <el-input v-model="trsHspRcdInfo.reflinMedinsNo" readonly @click="showSearchHospital = true"></el-input>
          </el-col>
          <el-col :span="6">
            <i class="required">*</i>转往医院名称：
            <el-input v-model="trsHspRcdInfo.reflinMedinsName" readonly @click="showSearchHospital = true"></el-input>
          </el-col>
          <el-col :span="6">
            <i class="required">*</i>就医地行政区划：
            <el-input v-model="trsHspRcdInfo.mdtrtareaAdmdvs" disabled></el-input>
          </el-col>
          <el-col :span="6">
            医院同意转院标志：
            <el-select v-model="trsHspRcdInfo.hospAgreReflFlag" style="width: 100%">
              <el-option label="是" value="1"></el-option>
              <el-option label="否" value="0"></el-option>
            </el-select>
          </el-col>
        </el-row>
        <div style="height: 16px"></div>
        <el-row :gutter="5">
          <el-col :span="6">
            <i class="required">*</i>转院类型：
            <el-select v-model="trsHspRcdInfo.reflType" style="width: 100%">
              <el-option v-for="item in reflTypes" :key="item.code" :value="item.code" :label="item.name"></el-option>
            </el-select>
          </el-col>
          <el-col :span="6">
            <i class="required">*</i>转院日期：
            <el-date-picker type="date" v-model="trsHspRcdInfo.reflDate" clearable style="width: 100%"></el-date-picker>
          </el-col>
          <el-col :span="6">
            <i class="required">*</i>转院原因：
            <el-input v-model="trsHspRcdInfo.reflRea" clearable></el-input>
          </el-col>
          <el-col :span="6">
            <i class="required">*</i>转院意见：
            <el-input v-model="trsHspRcdInfo.reflOpnn" clearable></el-input>
          </el-col>
        </el-row>
        <div style="height: 16px"></div>
        <el-row :gutter="5">
          <el-col :span="6">
            <i class="required">*</i>开始日期：
            <el-date-picker type="date" v-model="trsHspRcdInfo.begndate" clearable style="width: 100%"></el-date-picker>
          </el-col>
          <el-col :span="6">
            <i class="required">*</i>结束日期：
            <el-date-picker type="date" v-model="trsHspRcdInfo.enddate" clearable style="width: 100%"></el-date-picker>
          </el-col>
          <el-col :span="6">
            转诊使用标志：
            <el-input v-model="trsHspRcdInfo.reflUsedFlag" clearable></el-input>
          </el-col>
        </el-row>

        <el-dialog width="45%" title="定点医疗机构查询" v-model="showSearchHospital" append-to-body>
          <div>
            <el-select v-model="hospType" placeholder="请选择医疗机构类别">
              <el-option v-for="item in hospTypes" :key="item.code" :value="item.code" :label="item.name"></el-option>
            </el-select>
            <el-input clearable v-model="fuzzyHospName" style="width: 280px"
                      placeholder="输入医疗机名称关键字模糊查询"></el-input>
            <el-button icon="Search" type="primary" @click="searchHospitals">查询</el-button>
          </div>
          <el-divider></el-divider>
          <el-tag type="danger">请双击转院目标医疗机构</el-tag>
          <el-table :data="hospitals" stripe height="300" highlight-current-row @row-dblclick="dbClickMedins">
            <el-table-column prop="fixmedinsCode" label="医药机构编号"></el-table-column>
            <el-table-column prop="fixmedinsName" label="医药机构名称"></el-table-column>
            <el-table-column prop="uscc" label="统一社会信用代码"></el-table-column>
            <el-table-column prop="fixmedinsTypeName" label="医疗服务机构类型"></el-table-column>
            <el-table-column prop="hospLvName" label="医院等级"></el-table-column>
            <el-table-column prop="fixmedinsAdmdvsName" label="医药机构地区"></el-table-column>
          </el-table>
        </el-dialog>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="showTrsHspRcdInfo = false" icon="Close">取消</el-button>
            <el-button type="primary" @click="executeTransHospRcd" icon="Check">提交备案</el-button>
          </div>
        </template>
      </el-dialog>
      <el-dialog title="人员转院备案信息" v-model="showPsnTrsHspRcds" width="70%">
        <el-table :data="psnTrsHspRcds" stripe highlight-current-row height="300">
          <el-table-column prop="reflinMedinsNo" label="转往定点医药机构编号"></el-table-column>
          <el-table-column prop="reflinMedinsName" label="转往定点医药机构名称"></el-table-column>
          <el-table-column prop="diagCode" label="诊断编码"></el-table-column>
          <el-table-column prop="diagName" label="诊断名称"></el-table-column>
          <el-table-column prop="reflRea" label="转院原因"></el-table-column>
          <el-table-column prop="reflOpnn" label="转院意见"></el-table-column>
          <el-table-column prop="reflDate" label="转院日期"></el-table-column>
          <el-table-column prop="begndate" label="开始日期"></el-table-column>
          <el-table-column prop="enddate" label="结束日期"></el-table-column>
          <el-table-column label="操作">
            <template #default="scope">
              <el-button type="warning" icon="RefreshLeft" @click="executeCancelTransHospRcd(scope.row)">撤销
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-dialog>
      <el-dialog title="人员累计信息查询" v-model="showCumInfo" width="50%">
        <Cuminfo :psn-no="currentInHospInfo.psnNo"/>
      </el-dialog>
      <el-dialog title="转院信息查询" v-model="showRefMedins" width="70%">
        <el-date-picker
            v-model="refMedinsRange"
            type="daterange"
            :shortcuts="maxHalfYear"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            size="small"
            style="width: 240px"
        ></el-date-picker>
        <el-divider direction="vertical"></el-divider>
        <el-button type="primary" icon="Search" @click="executeGetRefMedins">查询</el-button>
        <el-table :data="refMedins" stripe height="300">
          <el-table-column prop="insutypeName" label="险种类型"></el-table-column>
          <el-table-column prop="dclaSoucName" label="申报来源"></el-table-column>
          <el-table-column prop="psnNo" label="人员编号"></el-table-column>
          <el-table-column prop="psnCertTypeName" label="人员证件类型"></el-table-column>
          <el-table-column prop="certno" label="证件号码"></el-table-column>
          <el-table-column prop="psnName" label=" 人员姓名"></el-table-column>
          <el-table-column prop="gendName" label="性别"></el-table-column>
          <el-table-column prop="brdy" label="出生日期"></el-table-column>
          <el-table-column prop="tel" label="联系电话"></el-table-column>
          <el-table-column prop="addr" label="联系地址"></el-table-column>
          <el-table-column prop="insuOptinsName" label="参保地区"></el-table-column>
          <el-table-column prop="empName" label="单位名称"></el-table-column>
          <el-table-column prop="diagCode" label="诊断代码"></el-table-column>
          <el-table-column prop="diagName" label="诊断名称"></el-table-column>
          <el-table-column prop="diseCondDscr" label="疾病病情描述"></el-table-column>
          <el-table-column prop="reflinMedinsNo" label="转往定点医药机构编号"></el-table-column>
          <el-table-column prop="reflinMedinsName" label="转往医院名称"></el-table-column>
          <el-table-column prop="outFlagName" label="异地标志"></el-table-column>
          <el-table-column prop="reflDate" label="转院日期"></el-table-column>
          <el-table-column prop="reflRea" label="转院原因"></el-table-column>
          <el-table-column prop="begndate" label="开始日期"></el-table-column>
          <el-table-column prop="enddate" label="结束日期"></el-table-column>
          <el-table-column prop="hospAgreReflFlagName" label="医院同意转院标志"></el-table-column>
          <el-table-column prop="opterId" label="经办人ID"></el-table-column>
          <el-table-column prop="opterName" label="经办人姓名"></el-table-column>
          <el-table-column prop="optTime" label="经办时间"></el-table-column>
        </el-table>
      </el-dialog>
    </template>
  </page-layer>
</template>

<script>
import {onMounted, ref, watch} from 'vue'
import {maxHalfYear} from '@/data/shortcuts'
import {ElMessage} from 'element-plus'
import {
  obtainMedicalInstitutionInfo,
  queryInHospitalPatientsInfo,
  queryInsuplcAdmdvsByMdtrtId,
  queryPersonnelTransferHospitalInfo,
  weiJieSuanZaiYuanHuanZhe,
} from '../../../api/medical-insurance/si-query'
import {formatDatetime} from '@/utils/date'
import store from '@/store'
import {getHospTypes, getReflTypes} from '@/api/medical-insurance/si-dict'
import {
  getAllTrsHospRcdByPsnNo,
  revokeTransferHospitalRecord,
  transferHospitalRecord
} from '@/api/medical-insurance/si-psn-rcd'
import Cuminfo from '../../../components/medical-insurance/cuminfo/Index.vue'
import {getDateRangeFormatDate} from '../../../utils/date'
import {cptSex} from '../../../utils/computed'
import {ExcelName} from '../../../utils/ExportExcel'
import PageLayer from "@/layout/PageLayer";

export default {
  components: {
    PageLayer,
    Cuminfo,
  },
  setup() {
    const windowSize = store.state.app.windowSize
    const tableHeight = windowSize.h - 55
    const patNo = ref('')
    const dateRange = ref(null)
    const inhosps = ref([])
    const pageSize = ref(30)
    const currentPage = ref(1)
    const currentInHospInfo = ref({})
    const trsHspRcdInfo = ref({})
    const showTrsHspRcdInfo = ref(false)
    const hospTypes = ref([])
    const showSearchHospital = ref(false)
    const hospType = ref(null)
    const reflTypes = ref([])
    const fuzzyHospName = ref(null)
    const hospitals = ref([])
    const psnTrsHspRcds = ref([])
    const showPsnTrsHspRcds = ref(false)
    const showCumInfo = ref(false)
    const refMedins = ref([])
    const showRefMedins = ref(false)
    const refMedinsRange = ref(null)
    const benYuanFlag = ref(false)
    const name = ref('')
    const certno = ref('') // 身份证
    const nullDateRange = () => {
      if (!dateRange.value) {
        ElMessage({
          message: '请先选择日期范围',
          type: 'warning',
          duration: 2000,
          showClose: true,
        })
        return true
      }
      return false
    }
    const nullInHospInfo = () => {
      if (!currentInHospInfo.value.psnNo) {
        ElMessage({
          message: '请先选择在院信息',
          type: 'warning',
          duration: 2000,
          showClose: true,
        })
        return true
      }
      return false
    }
    const handleSizeChange = (val) => {
      pageSize.value = val
    }
    const handleCurrentChange = (val) => {
      currentPage.value = val
    }
    const getInhospInfo = () => {
      if (benYuanFlag.value) {
        let dateS = getDateRangeFormatDate(dateRange.value)
        weiJieSuanZaiYuanHuanZhe(patNo.value, dateS.startTime, dateS.endTime, name.value, certno.value).then((res) => {
          inhosps.value = res
        })
      } else {
        if (nullDateRange()) {
          return
        }
        const param = {
          patNo: patNo.value,
          begntime: formatDatetime(dateRange.value[0]),
          endtime: formatDatetime(dateRange.value[1]),
        }
        queryInHospitalPatientsInfo(param).then((res) => {
          inhosps.value = res
        })
      }
    }

    const handleClickInHospInfo = (row) => {
      currentInHospInfo.value = row
    }

    const beforeGetCumulativeInfo = () => {
      if (nullInHospInfo()) {
        return
      }
      showCumInfo.value = true
    }

    const beforeGetRefMedins = () => {
      if (nullInHospInfo()) {
        return
      }
      showRefMedins.value = true
    }

    const executeGetRefMedins = () => {
      if (!refMedinsRange.value) {
        ElMessage({
          message: '请先选择时间范围',
          type: 'warning',
          duration: 2000,
          showClose: true,
        })
        return
      }
      const param = {
        patNo: patNo.value,
        psnNo: currentInHospInfo.value.psnNo,
        begntime: formatDatetime(refMedinsRange.value[0]),
        endtime: formatDatetime(refMedinsRange.value[1]),
      }
      queryPersonnelTransferHospitalInfo(param).then((res) => {
        refMedins.value = res
      })
    }

    const beforeTransHospRcd = () => {
      if (nullInHospInfo()) {
        return
      }
      trsHspRcdInfo.value.psnNo = currentInHospInfo.value.psnNo
      trsHspRcdInfo.value.insutype = currentInHospInfo.value.insutype
      trsHspRcdInfo.value.insutypeName = currentInHospInfo.value.insutypeName
      queryInsuplcAdmdvsByMdtrtId(currentInHospInfo.value.mdtrtId).then((res) => {
        trsHspRcdInfo.value.insuOptins = res
        showTrsHspRcdInfo.value = true
      })
    }

    const executeTransHospRcd = () => {
      transferHospitalRecord(trsHspRcdInfo.value).then((res) => {
        ElMessage({
          message: res,
          type: 'success',
          showClose: true,
        })
        showTrsHspRcdInfo.value = false
      })
    }

    const beforeCancelTransHospRcd = () => {
      if (nullInHospInfo()) {
        return
      }
      getAllTrsHospRcdByPsnNo(currentInHospInfo.value.psnNo).then((res) => {
        psnTrsHspRcds.value = res
        showPsnTrsHspRcds.value = true
      })
    }

    const executeCancelTransHospRcd = (row) => {
      revokeTransferHospitalRecord(row).then((res) => {
        ElMessage({
          message: res,
          type: 'success',
          showClose: true,
        })
        showPsnTrsHspRcds.value = false
      })
    }

    const searchHospitals = () => {
      if (!hospType.value) {
        ElMessage({
          message: '请选择医疗机构类别！',
          type: 'warning',
          duration: 2000,
          showClose: true,
        })
        return
      }

      if (!fuzzyHospName.value) {
        ElMessage({
          message: '请输入医疗机构关键字名称！',
          type: 'warning',
          duration: 2000,
          showClose: true,
        })
        return
      }
      obtainMedicalInstitutionInfo(hospType.value, fuzzyHospName.value).then((res) => {
        hospitals.value = res
      })
    }

    const dbClickMedins = (row) => {
      trsHspRcdInfo.value.reflinMedinsNo = row.fixmedinsCode
      trsHspRcdInfo.value.reflinMedinsName = row.fixmedinsName
      trsHspRcdInfo.value.mdtrtareaAdmdvs = row.fixmedinsAdmdvs
      showSearchHospital.value = false
    }

    const bizAppyTypes = ref([])
    const certTypes = ref([])
    const agnterRlts = ref([])
    onMounted(() => {
      getHospTypes().then((res) => {
        hospTypes.value = res
      })
      getReflTypes().then((res) => {
        reflTypes.value = res
      })
      // getBizAppyTypes().then((res) => {
      //   bizAppyTypes.value = res
      // })
      // getCertTypes().then((res) => {
      //   certTypes.value = res
      // })
      // getAgnterRlts().then((res) => {
      //   agnterRlts.value = res
      // })
    })

    watch(
        () => benYuanFlag.value,
        () => {
          inhosps.value = []
        }
    )

    const benYuanDownload = () => {
      let title = {
        smallDeptName: '入院科室',
        zkWardName: '转科科室',
        inpatientNo: '住院号',
        name: '姓名',
        sexName: '性别',
        certno: '身份证',
        admissTimes: '住院次数',
        responceTypeName: '医保身份',
        disDiag: '诊断代码',
        disDiagComment: '诊断名称',
        totalCharge: '总费用',
        referPhysicianName: '管床医生',
        insuplcAdmdvsName: '参保地',
      }
      ExcelName(inhosps.value, title)
    }

    return {
      tableHeight,
      patNo,
      dateRange,
      maxHalfYear,
      inhosps,
      psnTrsHspRcds,
      showPsnTrsHspRcds,
      bizAppyTypes,
      certTypes,
      agnterRlts,
      showCumInfo,
      refMedins,
      showRefMedins,
      refMedinsRange,
      pageSize,
      currentPage,
      currentInHospInfo,
      trsHspRcdInfo,
      showTrsHspRcdInfo,
      showSearchHospital,
      fuzzyHospName,
      hospitals,
      hospTypes,
      hospType,
      reflTypes,
      handleSizeChange,
      handleCurrentChange,
      handleClickInHospInfo,
      beforeGetCumulativeInfo,
      beforeGetRefMedins,
      beforeTransHospRcd,
      executeTransHospRcd,
      beforeCancelTransHospRcd,
      executeCancelTransHospRcd,
      getInhospInfo,
      searchHospitals,
      dbClickMedins,
      executeGetRefMedins,
      benYuanFlag,
      name,
      certno,
      cptSex,
      benYuanDownload,
    }
  },
}
</script>

<style scoped>
.required {
  color: red;
}

.benYuanFlagDiv {
  display: inline-flex;
  padding: 0 5px;
  height: 28px;
  cursor: pointer;
}

.benYuanFlagDiv:hover {
  color: #7373e7;
}
</style>
