<template>
    <span style="color:red;">患者数据来源病案首页</span>
    <el-button @click="newlyAddedClick" type="success" icon="Plus">新增</el-button>
    <div style="display: flex">
        <div style="width: 400px">
            <vxe-table :data="infectiousDiseasesList"
                       @cell-click="rowClick"
                       :height="getWindowSize.h / 1.2">
                <vxe-column title="传染病名称" field="contagion">
                    <template #default="{row}">
                        {{ getClassName(row)?.name }}
                    </template>
                </vxe-column>
                <vxe-column title="填报日期" field="reportDate"/>
            </vxe-table>
        </div>
        <div style="flex: 1;min-width: 800px">
            <el-scrollbar :height="getWindowSize.h / 1.2">
                <el-form ref="formRef" :model="sheetData"
                         :rules="rules"
                         label-width="100px" inline>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="报卡类别：" prop="cardNo">
                                {{ sheetData.cardNo }}
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="报卡类别：" prop="illType">
                                <el-select v-model="sheetData.illType">
                                    <el-option v-for="item in cardReportingType"
                                               :label="item.name"
                                               :value="item.code"/>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="姓名：">
                                {{ sheetData.name }}
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="家长姓名：">
                                <el-input v-model="sheetData.genearch"/>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="孕妇：">
                                <el-input v-model="sheetData.yunDays">
                                    <template #prepend>孕</template>
                                    <template #append>
                                        周
                                    </template>
                                </el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="身份证号：">
                                <el-input v-model="sheetData.socialNo"/>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="性别：" prop="sex">
                                <el-select v-model="sheetData.sex">
                                    <el-option v-for="item in sexes"
                                               :label="item.name"
                                               :value="item.code.toString()"/>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="出生日期：">
                                <el-date-picker
                                        v-model="sheetData.birthDate"
                                        format="YYYY-MM-DD"
                                        value-format="YYYY-MM-DD"/>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="年龄：">
                                {{ sheetData.age }}
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="工作单位：">
                                <el-input v-model="sheetData.corporation"/>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="联系电话：" prop="relationTel">
                                <el-input v-model="sheetData.relationTel"/>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="病人属于：" prop="scopeFlag">
                                <el-select v-model="sheetData.scopeFlag">
                                    <el-option v-for="item in thePatientBelongsTo"
                                               :label="item.name"
                                               :value="item.code"/>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="职业：" prop="occupationCode">
                                <el-select v-model="sheetData.occupationCode">
                                    <el-option v-for="item in occupation"
                                               :label="item.name"
                                               :value="item.code"/>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="现住址：">
                                <el-input v-model="sheetData.currentDistrict"/>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="现街道：" prop="currentStreet">
                                <el-input v-model="sheetData.currentStreet"/>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-alert title="病历分类（乙型肝类、血吸虫病填写）"
                                      show-icon
                                      type="warning"
                                      :closable="false"/>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="分类(1)：" prop="vaccinate">
                                <el-select v-model="sheetData.vaccinate">
                                    <el-option v-for="item in medicalRecordClassification1"
                                               :label="item.name"
                                               :value="item.code"/>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="分类(2)：" prop="vaccinate2">
                                <el-select v-model="sheetData.vaccinate2">
                                    <el-option v-for="item in medicalRecordClassification2"
                                               :label="item.name"
                                               :value="item.code"/>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="发病日期：" prop="illStartDate">
                                <el-date-picker
                                        type="datetime"
                                        v-model="sheetData.illStartDate"
                                        format="YYYY-MM-DD HH:mm:ss"
                                        value-format="YYYY-MM-DD HH:mm:ss"/>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="诊断日期：" prop="illFirstDate">
                                <el-date-picker
                                        type="datetime"
                                        v-model="sheetData.illFirstDate"
                                        format="YYYY-MM-DD HH:mm:ss"
                                        value-format="YYYY-MM-DD HH:mm:ss"/>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="死亡日期：">
                                <el-date-picker
                                        type="datetime"
                                        v-model="sheetData.deathDate"
                                        format="YYYY-MM-DD HH:mm:ss"
                                        value-format="YYYY-MM-DD HH:mm:ss"/>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-alert title="传染病"
                                      show-icon
                                      type="warning"
                                      :closable="false"/>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="甲类：" prop="contagion">
                                <el-select v-model="sheetData.contagion"
                                           @change="changesInInfectiousDiseases('contagion')">
                                    <el-option v-for="item in classA"
                                               :label="item.name"
                                               :value="item.code"/>
                                </el-select>
                            </el-form-item>
                        </el-col>

                        <el-col :span="8">
                            <el-form-item label="乙类：" prop="contagion2">
                                <el-select v-model="sheetData.contagion2"
                                           @change="changesInInfectiousDiseases('contagion2')">
                                    <el-option v-for="item in classB"
                                               :label="item.name"
                                               :value="item.code"/>
                                </el-select>
                            </el-form-item>
                        </el-col>

                        <el-col :span="8">
                            <el-form-item label="丙类：" prop="contagion3">
                                <el-select v-model="sheetData.contagion3"
                                           @change="changesInInfectiousDiseases('contagion3')">
                                    <el-option v-for="item in classC"
                                               :label="item.name"
                                               :value="item.code"/>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-alert title="其他法定管理以及重点临测传染病"
                                      show-icon
                                      type="warning"
                                      :closable="false"/>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="其他" prop="contagionQt">
                                <el-select v-model="sheetData.contagionQt"
                                           @change="changesInInfectiousDiseases('contagionQt')">
                                    <el-option v-for="item in otherInfections"
                                               :label="item.name"
                                               :value="item.code"/>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-alert title="梅毒检测结果"
                                      show-icon
                                      type="warning"
                                      :closable="false"/>
                        </el-col>
                        <el-col :span="24">
                            <el-alert title="成人/患儿生母填写"
                                      show-icon
                                      type="info"
                                      :closable="false"/>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="TRUST/RPR:">
                                <el-select v-model="sheetData.mtrust">
                                    <el-option v-for="item in testing"
                                               :label="item.name"
                                               :value="item.code"/>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="滴度：:">
                                <el-select v-model="sheetData.mrpr">
                                    <el-option v-for="item in other"
                                               :label="item.name"
                                               :value="item.code"/>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="TPPA/TPHA:">
                                <el-select v-model="sheetData.mtppa">
                                    <el-option v-for="item in testing"
                                               :label="item.name"
                                               :value="item.code"/>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="其他:">
                                <el-select v-model="sheetData.mother">
                                    <el-option v-for="item in testOther"
                                               :label="item.name"
                                               :value="item.code"/>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-alert title="患儿填写"
                                      show-icon
                                      type="info"
                                      :closable="false"/>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="TRUST/RPR:">
                                <el-select v-model="sheetData.ctrust">
                                    <el-option v-for="item in testing"
                                               :label="item.name"
                                               :value="item.code"/>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="滴度：:">
                                <el-select v-model="sheetData.crpr">
                                    <el-option v-for="item in other"
                                               :label="item.name"
                                               :value="item.code"/>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="TPPA/TPHA:">
                                <el-select v-model="sheetData.ctppa">
                                    <el-option v-for="item in testing"
                                               :label="item.name"
                                               :value="item.code"/>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="19S-IgM-TPHA:">
                                <el-select v-model="sheetData.ctpha">
                                    <el-option v-for="item in testing"
                                               :label="item.name"
                                               :value="item.code"/>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="订正病名：" prop="contagionText">
                                <el-input v-model="sheetData.contagionText"/>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="诊断依据：" prop="disDiagType">
                                <el-select v-model="sheetData.disDiagType">
                                    <el-option v-for="item in diagnosticBasis"
                                               :label="item.name"
                                               :value="item.code"/>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="报告单位：">
                                长沙泰和医院
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="退卡原因：" prop="cardRet">
                                <el-input v-model="sheetData.cardRet"/>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="报告医生：">
                                {{ userData.name }}
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="联系电话：" prop="reportTel">
                                <el-input v-model="sheetData.reportTel"/>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="报告日期：" prop="reportDate">
                                {{ sheetData.reportDate }}
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="填卡日期：" prop="inputDt">
                                {{ sheetData.inputDt }}
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="备注：" prop="remark" style="width: 80%">
                                <el-input v-model="sheetData.remark"/>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="既往史：" prop="anamnesis">
                                <el-select v-model="sheetData.anamnesis">
                                    <el-option v-for="item in pastHistory"
                                               :label="item.name"
                                               :value="item.code"/>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="本次就诊：" prop="visitType">
                                <el-select v-model="sheetData.visitType">
                                    <el-option v-for="item in visitType"
                                               :label="item.name"
                                               :value="item.code"/>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-alert title="病毒性肝炎专报"
                                      show-icon
                                      type="warning"
                                      :closable="false"/>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="临床分型：" prop="gyLcfx">
                                <el-select v-model="sheetData.gyLcfx">
                                    <el-option v-for="item in gyLcfx"
                                               :label="item.name"
                                               :value="item.code"/>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="病原分型：" prop="gyByfx">
                                <el-select v-model="sheetData.gyByfx">
                                    <el-option v-for="item in gyByfx"
                                               :label="item.name"
                                               :value="item.code"/>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <!--     第一行       -->
                        <el-col :span="6">
                            <el-form-item label="ALT：" prop="valt">
                                <el-input v-model="sheetData.valt"/>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="总胆红质：" prop="vzdhz">
                                <el-input v-model="sheetData.vzdhz"/>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="抗-HAVIgM：" prop="vkhavigm">
                                <el-input v-model="sheetData.vkhavigm"/>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="HAV-RNA：" prop="vhavRna">
                                <el-input v-model="sheetData.vhavRna"/>
                            </el-form-item>
                        </el-col>
                        <!--     第2行       -->
                        <el-col :span="6">
                            <el-form-item label="HbsAg：" prop="vhbsag">
                                <el-input v-model="sheetData.vhbsag"/>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="抗-HBs：" prop="vkhbs">
                                <el-input v-model="sheetData.vkhbs"/>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="HBeAg：" prop="vhbeag">
                                <el-input v-model="sheetData.vhbeag"/>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="抗-HBe：" prop="vkhbe">
                                <el-input v-model="sheetData.vkhbe"/>
                            </el-form-item>
                        </el-col>
                        <!--     第3行       -->
                        <el-col :span="6">
                            <el-form-item label="抗-HBc：" prop="vkhbc">
                                <el-input v-model="sheetData.vkhbc"/>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="抗-HBcIgM：" prop="vkhbcigm">
                                <el-input v-model="sheetData.vkhbcigm"/>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="HBV-DNA：" prop="vhbvDna">
                                <el-input v-model="sheetData.vhbvDna"/>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="抗-HCV：" prop="vkhcv">
                                <el-input v-model="sheetData.vkhcv"/>
                            </el-form-item>
                        </el-col>
                        <!--     第4行       -->
                        <el-col :span="6">
                            <el-form-item label="HCV-RNA：" prop="vhcvrna">
                                <el-input v-model="sheetData.vhcvrna"/>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="抗-HDVIgM：" prop="vkhdvigm">
                                <el-input v-model="sheetData.vkhdvigm"/>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="抗HDV：" prop="vkhdv">
                                <el-input v-model="sheetData.vkhdv"/>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="HDV-RNA：" prop="vhdvRna">
                                <el-input v-model="sheetData.vhdvRna"/>
                            </el-form-item>
                        </el-col>
                        <!--     第5行       -->
                        <el-col :span="6">
                            <el-form-item label="抗-HEVIgM：" prop="vkhevigm">
                                <el-input v-model="sheetData.vkhevigm"/>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="抗HEV：" prop="vkhev">
                                <el-input v-model="sheetData.vkhev"/>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="HEV-RNA：" prop="vhevrna">
                                <el-input v-model="sheetData.vhevrna"/>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="订正病名：" prop="gyDzbm">
                                <el-input v-model="sheetData.gyDzbm"/>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
            </el-scrollbar>
        </div>
    </div>
</template>

<script setup name='DialogDiseases'>
import {getAllDictionary, getSheetInfo} from "@/api/case-front-sheet";
import {operations, sexes} from "@/data";
import {autopsies, haveOrNot, yesOrNo} from "@/views/hospitalization/case-front-sheet/common";
import {
    cardReportingType, classA, classB, classC, diagnosticBasis, getClassName, gyByfx, gyLcfx,
    medicalRecordClassification1, medicalRecordClassification2,
    occupation, other, otherInfections, pastHistory, testing, testOther,
    thePatientBelongsTo, visitType
} from "@/data/infectious-diseases-data";
import {getWindowSize} from "@/utils/window-size";
import store from "@/store";
import {getServerDateApi} from "@/api/public-api";
import {stringIsBlank} from "@/utils/blank-utils";
import {confirmCrb, getContagiousDiseaseByPatNo, updateCrb} from "@/api/zhu-yuan-yi-sheng/infectious-diseases";
import {xcMessage} from "@/utils/xiaochan-element-plus";

const props = defineProps({
    patNo: {
        type: String,
        default: '0406942'
    },
    times: {
        type: Number,
        default: 1
    },
    ward: {
        type: String,
        default: '8000001'
    }
})

const emits = defineEmits(['confirm'])

let userData = store.state.user.info
let sheetData = $ref({})
let dics = $ref({})
let patInfo = $ref({})
let infectiousDiseasesList = $ref([])

const setDefault = async () => {
    let date = await getServerDateApi()
    sheetData.name = patInfo.name
    sheetData.socialNo = patInfo.socialNo
    sheetData.sex = patInfo.sex
    sheetData.birthDate = patInfo.birthDate
    sheetData.age = patInfo.age
    sheetData.corporation = patInfo.unitName
    sheetData.relationTel = patInfo.phone
    sheetData.occupationCode = patInfo.occupation
    sheetData.currentDistrict = patInfo.livePlaceCombo
    sheetData.reportDate = date
    sheetData.inputDt = date
    sheetData.patientId = props.patNo
    sheetData.times = props.times
    sheetData.ageUnit = '岁'
    sheetData.reportDept = props.ward
    sheetData.reportHospital = '长沙泰和医院'
}

const changesInInfectiousDiseases = (val) => {
    let temp = ['contagion', 'contagion2', 'contagion3', 'contagionQt']
    temp.forEach(item => {
        if (item !== val) {
            sheetData[item] = null
        }
    })
}

const contagionFunc = (rule, value, callback) => {
    if (stringIsBlank(sheetData.contagion)
        && stringIsBlank(sheetData.contagion2)
        && stringIsBlank(sheetData.contagion3)
        && stringIsBlank(sheetData.contagionQt)) {
        callback(new Error('至少选择一项'));
    } else {
        callback()
    }
}

const vaccinateFunc = (rule, value, callback) => {
    if (stringIsBlank(sheetData.vaccinate) && stringIsBlank(sheetData.vaccinate2)) {
        callback(new Error('至少选择一项'));
    } else {
        callback()
    }
}

const rules = reactive({
    name: [{required: true, message: '该项不能为空', trigger: 'blur'}],
    illType: [{required: true, message: '该项不能为空', trigger: 'blur'}],
    sex: [{required: true, message: '该项不能为空', trigger: 'blur'}],
    birthDate: [{required: true, message: '该项不能为空', trigger: 'blur'}],
    relationTel: [{required: true, message: '该项不能为空', trigger: 'blur'}],
    scopeFlag: [{required: true, message: '该项不能为空', trigger: 'blur'}],
    occupationCode: [{required: true, message: '该项不能为空', trigger: 'blur'}],
    currentStreet: [{required: true, message: '该项不能为空', trigger: 'blur'}],
    vaccinate: [{validator: vaccinateFunc, message: '该项不能为空', trigger: 'blur'}],
    vaccinate2: [{validator: vaccinateFunc, message: '该项不能为空', trigger: 'blur'}],
    illStartDate: [{required: true, message: '该项不能为空', trigger: 'blur'}],
    illFirstDate: [{required: true, message: '该项不能为空', trigger: 'blur'}],
    contagion: [{message: '至少选择一项', validator: contagionFunc, trigger: 'blur'}],
    contagion2: [{message: '至少选择一项', validator: contagionFunc, trigger: 'blur'}],
    contagion3: [{message: '至少选择一项', validator: contagionFunc, trigger: 'blur'}],
    contagionQt: [{message: '至少选择一项', validator: contagionFunc, trigger: 'blur'}],
})

const rowClick = ({row}) => {
    sheetData = row
}

const newlyAddedClick = async () => {
    sheetData = {}
    await setDefault()
}

const query = async () => {
    infectiousDiseasesList = await getContagiousDiseaseByPatNo(props.patNo)
}

onMounted(async () => {
    await query()
    patInfo = await getSheetInfo({
        bah: props.patNo,
        times: props.times,
        inOutFlag: 1
    })
    await setDefault()
    dics = await getAllDictionary()
    dics.getOperations = operations
    dics.getYesOrNo = yesOrNo
    dics.getHaveOrNot = haveOrNot
    dics.getAutopsies = autopsies
})

const formRef = ref(null)

const confirm = () => {
    formRef.value.validate(async (valid) => {
        if (valid) {
            if (stringIsBlank(sheetData.cardNo)) {
                await confirmCrb(sheetData)
            } else {
                await updateCrb(sheetData)
            }
            await query()
        } else {
            xcMessage.error('有必填项不能为空。')
        }
    })
}

defineExpose({
    confirm
})

</script>

<style scoped lang="scss">

</style>
