<template>
    <PageLayer>
        <template #header class="hd-cl">
            <el-input v-model="keyCode" placeholder="请输入证件号/门诊号/住院号/体检号" style="width: 320px;">
                <template #prepend>关键字</template>
            </el-input>
            <el-button type="primary" icon="Search" @click="queryCmr" style="margin-left: 10px">查询慢病信息
            </el-button>
            <el-button type="primary" icon="Search" @click="queryBrInfo" style="margin-left: 10px">获取基本信息
            </el-button>
            <el-button type="primary" icon="Plus" @click="addForm(ruleFormRef)" style="margin-left: 10px">新增
            </el-button>
            <el-button type="primary" icon="Edit" @click="editForm(ruleFormRef)" style="margin-left: 10px">编辑
            </el-button>
            <el-button type="primary" icon="Select" @click="submitForm(ruleFormRef)" style="margin-left: 10px">保存
            </el-button>
            <el-button type="primary" icon="Refresh" @click="resetForm(ruleFormRef)" style="margin-left: 10px">重置
            </el-button>
        </template>
        <template #main>
            <span style="text-align: center;">
                <h2>长沙泰和医院慢病患者信息首次登记表</h2>
            </span>
            <div class="demo-collapse">
                <el-form ref="ruleFormRef" :model="ruleForm" :rules="rulesAll" label-width="160px" class="demo-ruleForm"
                    :size="formSize" status-icon :disabled="isEditForm">
                    <el-input type="hidden" v-model="ruleForm.pId" />
                    <el-collapse v-model="activeName">
                        <el-collapse-item title="基本资料" name="1" disabled>
                            <el-row>
                                <el-col :span="6">
                                    <el-form-item label="门诊号" prop="hisMzNo">
                                        <el-input v-model="ruleForm.hisMzNo" maxlength="20" show-word-limit
                                            placeholder="请填写门诊号" />
                                    </el-form-item>
                                </el-col>
                                <el-col :span="6">
                                    <el-form-item label="住院号" prop="hisZyNo">
                                        <el-input v-model="ruleForm.hisZyNo" maxlength="20" show-word-limit
                                            placeholder="请填写住院号" />
                                    </el-form-item>
                                </el-col>
                                <el-col :span="6">
                                    <el-form-item label="病案号" prop="hisBaNo">
                                        <el-input v-model="ruleForm.hisBaNo" maxlength="12" show-word-limit
                                            placeholder="请填写病案号" />
                                    </el-form-item>
                                </el-col>
                                <el-col :span="6">
                                    <el-form-item label="体检号" prop="hisTjNo">
                                        <el-input v-model="ruleForm.hisTjNo" maxlength="20" show-word-limit
                                            placeholder="请填写体检号" />
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="6">
                                    <el-form-item label="用户名称" prop="pName">
                                        <el-input v-model="ruleForm.pName" minlength="1" maxlength="20" show-word-limit
                                            placeholder="请填写用户名称" />
                                    </el-form-item>
                                </el-col>
                                <el-col :span="6">
                                    <el-form-item label="性别" prop="sex">
                                        <el-select v-model="ruleForm.sex" placeholder="请选择性别" clearable style="width: 100%">
                                            <el-option v-for="item in dics.getSexCode" :key="item.code" :label="item.name"
                                                :value="item.code" />
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="6">
                                    <el-form-item label="年龄" prop="age">
                                        <el-input v-model="ruleForm.age" minlength="1" maxlength="6" show-word-limit
                                            placeholder="请填写年龄" />
                                    </el-form-item>
                                </el-col>
                                <el-col :span="6">
                                    <el-form-item label="出生日期" prop="birthDate">
                                        <el-date-picker v-model="ruleForm.birthDate" type="date" label="出生日期"
                                            placeholder="请选择出生日期" style="width: 100%" />
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="8">
                                    <el-form-item label="证件号" prop="socialNo">
                                        <el-input v-model="ruleForm.socialNo" minlength="1" maxlength="20" show-word-limit
                                            placeholder="请填写证件号" />
                                    </el-form-item>
                                </el-col>
                                <el-col :span="8">
                                    <el-form-item label="证件类型" prop="certificateType">
                                        <el-select v-model="ruleForm.certificateType" placeholder="请选择证件类型" clearable
                                            style="width: 100%">
                                            <el-option v-for="item in dics.getPsnCertType" :key="item.code"
                                                :label="item.name" :value="item.code" />
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                                <!-- <el-col :span="6">
                                    <el-form-item label="卡号" prop="cardNo">
                                        <el-input v-model="ruleForm.cardNo" maxlength="20" show-word-limit
                                            placeholder="请填写卡号" />
                                    </el-form-item>
                                </el-col> -->
                                <el-col :span="8">
                                    <el-form-item label="病人来源" prop="pType">
                                        <el-select v-model="ruleForm.pType" placeholder="请选择病人来源" clearable
                                            style="width: 100%">
                                            <el-option v-for="item in dics.getAdmissWay" :key="item.code" :label="item.name"
                                                :value="item.code" />
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="6">
                                    <el-form-item label="本人电话" prop="relTel">
                                        <el-input v-model="ruleForm.relTel" maxlength="20" show-word-limit
                                            placeholder="请填写本人电话" />
                                    </el-form-item>
                                </el-col>
                                <!-- <el-col :span="6">
                                    <el-form-item label="联系电话2" prop="relTel2">
                                        <el-input v-model="ruleForm.relTel2" maxlength="20" show-word-limit
                                            placeholder="请填写联系电话2" />
                                    </el-form-item>
                                </el-col> -->
                                <el-col :span="6">
                                    <el-form-item label="联系人或家属电话" prop="relNameTel">
                                        <el-input v-model="ruleForm.relNameTel" maxlength="20" show-word-limit
                                            placeholder="请填写联系人或家属电话" />
                                    </el-form-item>
                                </el-col>
                                <el-col :span="6">
                                    <el-form-item label="联系人姓名" prop="relName">
                                        <el-input v-model="ruleForm.relName" maxlength="30" show-word-limit
                                            placeholder="请填写联系人姓名" />
                                    </el-form-item>
                                </el-col>
                                <el-col :span="6">
                                    <el-form-item label="联系人关系" prop="relCode">
                                        <el-select v-model="ruleForm.relCode" placeholder="请选择联系人关系" style="width: 100%">
                                            <el-option v-for="item in dics.getRelations" :key="item.code" :label="item.name"
                                                :value="item.code" />
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <!-- <el-row>
                                <el-col :span="6">
                                    <el-form-item label="联系人关系" prop="relCode">
                                        <el-select v-model="ruleForm.relCode" placeholder="请选择联系人关系" style="width: 100%">
                                            <el-option label="" value="0" hidden />
                                            <el-option label="本人" value="1" />
                                            <el-option label="父子" value="2" />
                                            <el-option label="母子" value="3" />
                                            <el-option label="其他" value="4" />
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="联系人地址" prop="relStreet">
                                        <el-input v-model="ruleForm.relStreet" maxlength="30" show-word-limit
                                            placeholder="请填写联系人地址" />
                                    </el-form-item>
                                </el-col>
                                <el-col :span="6">
                                    <el-form-item label="联系人邮编" prop="relZip">
                                        <el-input v-model="ruleForm.relZip" maxlength="6" show-word-limit
                                            placeholder="请填写联系人邮编" />
                                    </el-form-item>
                                </el-col>
                            </el-row> -->
                            <el-row>
                                <el-col :span="18">
                                    <el-form-item label="慢病类型" prop="chronicDiseaseTypeArr">
                                        <el-select v-model="ruleForm.chronicDiseaseTypeArr" multiple
                                            :multiple-limit="chronicDiseaseNo" collapse-tags collapse-tags-tooltip
                                            :max-collapse-tags="5" placeholder="请选择慢病类型" clearable style="width: 100%">
                                            <el-option v-for="item in dics.getCrmClass" :key="item.code" :label="item.name"
                                                :value="item.code" />
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="6">
                                    <el-form-item label="确诊时间" prop="lastDate">
                                        <el-date-picker v-model="ruleForm.lastDate" type="date" label="确诊时间"
                                            value-format="YYYY-MM-DD" placeholder="请选择确诊时间" style="width: 100%" />
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="24">
                                    <el-form-item label="患者重点随访监测事项" prop="pComment">
                                        <el-input v-model="ruleForm.pComment" maxlength="50" show-word-limit
                                            placeholder="请填写患者重点随访监测事项" />
                                    </el-form-item>
                                </el-col>
                            </el-row>
                        </el-collapse-item>
                        <!-- <el-collapse-item title="患者信息" name="3">
                            <el-row>
                                <el-col :span="6">
                                    <el-form-item label="婚姻状况" prop="marryCode">
                                        <el-select v-model="ruleForm.marryCode" placeholder="请选择婚姻状况" style="width: 100%">
                                            <el-option label="" value="" hidden />
                                            <el-option label="未婚" value="1" />
                                            <el-option label="已婚" value="2" />
                                            <el-option label="离异" value="3" />
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="6">
                                    <el-form-item label="国籍" prop="countryCode">
                                        <el-select v-model="ruleForm.countryCode" placeholder="请选择国籍" style="width: 100%">
                                            <el-option label="" value="" hidden />
                                            <el-option label="湖南" value="1" />
                                            <el-option label="长沙" value="2" />
                                            <el-option label="开福区" value="3" />
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="6">
                                    <el-form-item label="民族" prop="nationCode">
                                        <el-select v-model="ruleForm.nationCode" placeholder="请选择民族" style="width: 100%">
                                            <el-option label="" value="" hidden />
                                            <el-option label="汉族" value="1" />
                                            <el-option label="少数民族" value="2" />
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="6">
                                    <el-form-item label="职业" prop="occupationCode">
                                        <el-select v-model="ruleForm.occupationCode" placeholder="请选择职业"
                                            style="width: 100%">
                                            <el-option label="" value="" hidden />
                                            <el-option label="工人" value="1" />
                                            <el-option label="农民" value="2" />
                                            <el-option label="其他" value="3" />
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="6">
                                    <el-form-item label="行业" prop="vocationCode">
                                        <el-select v-model="ruleForm.vocationCode" placeholder="请选择行业" style="width: 100%">
                                            <el-option label="农业" value="1" />
                                            <el-option label="林业" value="2" />
                                            <el-option label="畜牧业" value="3" />
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="6">
                                    <el-form-item label="所属企业" prop="entId">
                                        <el-input v-model="ruleForm.entId" maxlength="12" show-word-limit
                                            placeholder="请填写所属企业" />
                                    </el-form-item>
                                </el-col>
                                <el-col :span="6">
                                    <el-form-item label="区县编码" prop="districtCode">
                                        <el-input v-model="ruleForm.districtCode" maxlength="6" show-word-limit
                                            placeholder="请填写区县编码" />
                                    </el-form-item>
                                </el-col>
                                <el-col :span="6">
                                    <el-form-item label="邮编" prop="eMail">
                                        <el-input v-model="ruleForm.eMail" maxlength="20" show-word-limit
                                            placeholder="请填写区县邮编" />
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="6">
                                    <el-form-item label="家庭电话" prop="homeTel">
                                        <el-input v-model="ruleForm.homeTel" maxlength="20" show-word-limit
                                            placeholder="请填写家庭电话" />
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="家庭住址" prop="homeStreet">
                                        <el-input v-model="ruleForm.homeStreet" maxlength="30" show-word-limit
                                            placeholder="请填写家庭住址" />
                                    </el-form-item>
                                </el-col>
                                <el-col :span="6">
                                    <el-form-item label="家庭邮编" prop="homeZip">
                                        <el-input v-model="ruleForm.homeZip" maxlength="6" show-word-limit
                                            placeholder="请填写家庭邮编" />
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="6">
                                    <el-form-item label="单位电话" prop="empTel">
                                        <el-input v-model="ruleForm.empTel" maxlength="20" show-word-limit
                                            placeholder="请填写单位电话" />
                                    </el-form-item>
                                </el-col>
                                <el-col :span="6">
                                    <el-form-item label="单位名称" prop="empName">
                                        <el-input v-model="ruleForm.empName" maxlength="30" show-word-limit
                                            placeholder="请填写单位名称" />
                                    </el-form-item>
                                </el-col>
                                <el-col :span="6">
                                    <el-form-item label="单位住址" prop="empStreet">
                                        <el-input v-model="ruleForm.empStreet" maxlength="30" show-word-limit
                                            placeholder="请填写单位住址" />
                                    </el-form-item>
                                </el-col>
                                <el-col :span="6">
                                    <el-form-item label="单位邮编" prop="empZip">
                                        <el-input v-model="ruleForm.empZip" maxlength="6" show-word-limit
                                            placeholder="请填写单位邮编" />
                                    </el-form-item>
                                </el-col>
                            </el-row>
                        </el-collapse-item> -->
                        <el-collapse-item title="其他信息" name="2" disabled>
                            <!-- <el-row>
                                <el-col :span="6">
                                    <el-form-item label="最近一次医疗方式" prop="lastType">
                                        <el-select v-model="ruleForm.lastType" placeholder="请选择医疗方式" style="width: 100%">
                                            <el-option label="门诊" value="1" />
                                            <el-option label="住院" value="2" />
                                            <el-option label="体检" value="3" />
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="6">
                                    <el-form-item label="最近一次医疗时间" prop="lastDate">
                                        <el-date-picker v-model="ruleForm.lastDate" type="datetime" label="医疗时间"
                                            placeholder="请选择医疗时间" style="width: 100%" />
                                    </el-form-item>
                                </el-col>
                                <el-col :span="6">
                                    <el-form-item label="等级描述" prop="importComment">
                                        <el-input v-model="ruleForm.importComment" maxlength="30" show-word-limit
                                            placeholder="请填写病人等级描述" />
                                    </el-form-item>
                                </el-col>
                            </el-row> -->
                            <!-- <el-row>
                                <el-col :span="6">
                                    <el-form-item label="潜在问题" prop="quLevel">
                                        <el-select v-model="ruleForm.quLevel" placeholder="请选择潜在问题" style="width: 100%">
                                            <el-option label="初级问题" value="1" />
                                            <el-option label="高级问题" value="2" />
                                            <el-option label="特级问题" value="3" />
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="6">
                                    <el-form-item label="潜在问题说明" prop="quComment">
                                        <el-input v-model="ruleForm.quComment" maxlength="30" show-word-limit
                                            placeholder="请填写潜在问题说明" />
                                    </el-form-item>
                                </el-col>
                            </el-row> -->
                            <el-row>
                                <el-col :span="6">
                                    <el-form-item label="病人等级" prop="importLevel">
                                        <el-select v-model="ruleForm.importLevel" placeholder="请选择病人等级" clearable
                                            style="width: 100%">
                                            <el-option label="未住院患者" value="1" />
                                            <el-option label="住院一次患者" value="2" />
                                            <el-option label="住院两次及以上" value="3" />
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="6">
                                    <el-form-item label="省" prop="provinceCode">
                                        <el-select v-model="ruleForm.provinceCode" placeholder="请选择省" style="width: 100%"
                                            filterable @change="provinceCharge" clearable>
                                            <el-option v-for="item in provinceData" :key="item.code" :label="item.name"
                                                :value="item.code" />
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="6">
                                    <el-form-item label="市" prop="cityCode">
                                        <el-select v-model="ruleForm.cityCode" placeholder="请选择市" style="width: 100%"
                                            filterable @change="cityCharge" clearable>
                                            <el-option v-for="item in cityData" :key="item.code" :label="item.name"
                                                :value="item.code" />
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="6">
                                    <el-form-item label="区" prop="areaCode">
                                        <el-select v-model="ruleForm.areaCode" placeholder="请选择区" style="width: 100%"
                                            filterable clearable>
                                            <el-option v-for="item in countyData" :key="item.code" :label="item.name"
                                                :value="item.code" />
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="24">
                                    <el-form-item label="详细地址" prop="detailAdress">
                                        <el-input v-model="ruleForm.detailAdress" maxlength="50" show-word-limit
                                            placeholder="请填写详细地址" />
                                    </el-form-item>
                                </el-col>
                            </el-row>
                        </el-collapse-item>
                        <el-collapse-item title="体征信息" name="3" disabled>
                            <el-row>
                                <el-col :span="6">
                                    <el-form-item label="身高" prop="height">
                                        <el-input v-model="ruleForm.height" type="number" placeholder="请填写身高">
                                            <template #append>cm</template>
                                        </el-input>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="6">
                                    <el-form-item label="体重" prop="weight">
                                        <el-input v-model="ruleForm.weight" type="number" placeholder="请填写体重">
                                            <template #append>kg</template>
                                        </el-input>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="6">
                                    <el-form-item label="体温" prop="temperature">
                                        <el-input v-model="ruleForm.temperature" type="number" placeholder="请填写体温">
                                            <template #append>°C</template>
                                        </el-input>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="6">
                                    <el-form-item label="心率" prop="heartRate">
                                        <el-input v-model="ruleForm.heartRate" placeholder="请填写心率"
                                            oninput="value=value.replace(/^(0+)|[^\d]+/g,'')">
                                            <template #append>次</template>
                                        </el-input>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="6">
                                    <el-form-item label="收缩压" prop="bloodPressureHigh">
                                        <el-input v-model="ruleForm.bloodPressureHigh" placeholder="请填写收缩压"
                                            oninput="value=value.replace(/^(0+)|[^\d]+/g,'')">
                                            <template #append>mmHg</template>
                                        </el-input>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="6">
                                    <el-form-item label="舒张压" prop="bloodPressureLow">
                                        <el-input v-model="ruleForm.bloodPressureLow" placeholder="请填写舒张压"
                                            oninput="value=value.replace(/^(0+)|[^\d]+/g,'')">
                                            <template #append>mmHg</template>
                                        </el-input>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="6">
                                    <el-form-item label="血糖" prop="bloodSugar">
                                        <el-input v-model="ruleForm.bloodSugar" type="number" placeholder="请填写血糖">
                                            <template #append>mmol/L</template>
                                        </el-input>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="6">
                                    <el-form-item label="血氧" prop="bloodOxygen">
                                        <el-input v-model="ruleForm.bloodOxygen" placeholder="请填写血氧">
                                            <template #append>%</template>
                                        </el-input>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                        </el-collapse-item>
                        <el-collapse-item title="创建信息" name="4" disabled>
                            <el-row>
                                <el-col :span="6">
                                    <el-form-item label="主管医生" prop="referPhysician">
                                        <el-select-v2 v-model="ruleForm.referPhysician" clearable remote style="width: 100%"
                                            filterable :options="empList" :remote-method="empMethod" placeholder="请选择主管医生">
                                            <template #default="{ item }">
                                                <span style="margin-right: 8px">{{ item.label }}</span>
                                                <span style="color: var(--el-text-color-secondary); font-size: 13px">
                                                    {{ item.value }}
                                                </span>
                                            </template>
                                        </el-select-v2>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="6">
                                    <el-form-item label="建档医生" prop="creatId">
                                        <div v-if="ruleForm.creatId !== '99999'">
                                            <el-select-v2 disabled v-model="ruleForm.creatId" clearable remote
                                                style="width: 100%" filterable :options="empList" :remote-method="empMethod"
                                                placeholder="请选择建档医生">
                                                <template #default="{ item }">
                                                    <span style="margin-right: 8px">{{ item.label }}</span>
                                                    <span style="color: var(--el-text-color-secondary); font-size: 13px">
                                                        {{ item.value }}
                                                    </span>
                                                </template>
                                            </el-select-v2>
                                        </div>
                                        <div v-else>
                                            <el-select-v2 v-model="ruleForm.creatId" clearable remote style="width: 100%"
                                                filterable :options="empList" :remote-method="empMethod"
                                                placeholder="请选择建档医生">
                                                <template #default="{ item }">
                                                    <span style="margin-right: 8px">{{ item.label }}</span>
                                                    <span style="color: var(--el-text-color-secondary); font-size: 13px">
                                                        {{ item.value }}
                                                    </span>
                                                </template>
                                            </el-select-v2>
                                        </div>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="6">
                                    <el-form-item label="建卡时间" prop="createDate">
                                        <el-input :value="ruleForm.createDate" readonly placeholder="默认当前时间为建卡时间" />
                                    </el-form-item>
                                </el-col>
                                <el-col :span="6">
                                    <el-form-item label="下次随访时间" prop="visitDate">
                                        <el-date-picker v-model="ruleForm.visitDate" type="date" label="随访时间"
                                            value-format="YYYY-MM-DD" placeholder="请选择下次随访时间" style="width: 100%" />
                                    </el-form-item>
                                </el-col>
                                <!-- <el-col :span="6">
                                    <el-form-item label="最近一次随访次数" prop="visitTimes">
                                        <el-input v-model="ruleForm.visitTimes" maxlength="10" show-word-limit
                                            placeholder="请填写最近一次随访次数" />
                                    </el-form-item>
                                </el-col>
                                <el-col :span="6">
                                    <el-form-item label="最近一次随访时间" prop="visitDate">
                                        <el-date-picker v-model="ruleForm.visitDate" type="datetime" label="随访时间"
                                            placeholder="请选择最近一次随访时间" style="width: 100%" />
                                    </el-form-item>
                                </el-col> -->
                            </el-row>
                        </el-collapse-item>
                    </el-collapse>
                </el-form>
            </div>
        </template>
    </PageLayer>
</template>
  
<script  lang="ts" setup name="ChronicDiseaseRegister">
import { reactive, ref, onMounted, nextTick } from 'vue'
import store from '@/store'
import { clone } from '@/utils/clone'
import PageLayer from '../../../layout/PageLayer.vue'
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage } from 'element-plus'
import { getDatetime } from '../../../utils/date'
import { getCrmDictionary, saveCrmPatientMi, selectCrmPatientMiByCode, selectBrInfoByCode } from "@/api/chronic-disease/chronic-disease"
import { employeeList } from "@/api/zhu-yuan-yi-sheng/pat-info-query"
import { provinceList, cityList, countyList, cityFilter, countyFilter } from "@/data/area.js"

const activeName = ref(['1', '2', '3', '4', '5', '6', '7'])
const userCode = store.state.user.info.code
const userName = store.state.user.info.name
let dics: any = ref({})
const empList = ref([])
const provinceData = ref([{ code: '', name: '' }])
const cityData = ref([{ code: '', name: '' }])
const countyData = ref([{ code: '', name: '' }])

onMounted(async () => {
    await nextTick(async () => {
        dics.value = await getCrmDictionary()
        provinceData.value = provinceList
        cityData.value = cityList
        countyData.value = countyList
        empMethod('')
        ruleForm.value.creatId = userCode
    })

})

const empMethod = async (val: string) => {
    await employeeList(val).then((res: any) => {
        empList.value = res
    })
}

interface RuleForm {
    pId: string
    pName: string
    sex: string
    birthDate: string
    hisMzNo: string
    hisZyNo: string
    hisBaNo: string
    hisTjNo: string
    socialNo: string
    cardNo: string
    pType: string
    entId: string
    marryCode: string
    countryCode: string
    nationCode: string
    occupationCode: string
    vocationCode: string
    districtCode: string
    eMail: string
    homeTel: string
    homeStreet: string
    homeZip: string
    empTel: string
    empName: string
    empStreet: string
    empZip: string
    relTel: string
    relTel2: string
    relNameTel: string
    relName: string
    relCode: string
    relStreet: string
    relZip: string
    lastType: string
    lastDate: string
    pComment: string
    importLevel: string
    importComment: string
    quLevel: string
    quComment: string
    certificateType: string
    chronicDiseaseType: string
    chronicDiseaseTypeArr: string[]
    createDate: string
    creatId: string
    userName: string
    provinceCode: string
    cityCode: string
    detailAdress: string
    referPhysician: string
    referPhysicianName: string
    visitTimes: number
    visitDate: string
    age: string
    areaCode: string
    height: string
    weight: string
    temperature: string
    bloodPressureLow: string
    bloodPressureHigh: string
    heartRate: string
    bloodSugar: string
    bloodOxygen: string
}

const keyCode = ref('')
const formSize = ref('default')
const isEditForm = ref(false)
const chronicDiseaseNo = ref(20)
let now = getDatetime()
const ruleFormRef = ref<FormInstance>()
const ruleForm = ref<RuleForm>({
    pId: '',
    pName: '',
    sex: '',
    birthDate: '',
    hisMzNo: '',
    hisZyNo: '',
    hisBaNo: '',
    hisTjNo: '',
    socialNo: '',
    cardNo: '',
    pType: '',
    entId: '',
    marryCode: '',
    countryCode: '',
    nationCode: '',
    occupationCode: '',
    vocationCode: '',
    districtCode: '',
    eMail: '',
    homeTel: '',
    homeStreet: '',
    homeZip: '',
    empTel: '',
    empName: '',
    empStreet: '',
    empZip: '',
    relTel: '',
    relTel2: '',
    relNameTel: '',
    relName: '',
    relCode: '',
    relStreet: '',
    relZip: '',
    lastType: '',
    lastDate: '',
    pComment: '',
    importLevel: '',
    importComment: '',
    quLevel: '',
    quComment: '',
    certificateType: '',
    chronicDiseaseType: '',
    chronicDiseaseTypeArr: [],
    createDate: now,
    creatId: '',
    userName: '',
    provinceCode: '430000',
    cityCode: '430100',
    detailAdress: '',
    referPhysician: '',
    referPhysicianName: '',
    visitTimes: 0,
    visitDate: '',
    age: '',
    areaCode: '',
    height: '',
    weight: '',
    temperature: '',
    bloodPressureLow: '',
    bloodPressureHigh: '',
    heartRate: '',
    bloodSugar: '',
    bloodOxygen: '',
})

const rulesAll = reactive<FormRules<RuleForm>>({
    pName: [
        { required: true, message: '请填写用户名称', trigger: 'blur' },
        { min: 1, max: 20, message: '不超过20个字', trigger: 'blur' },
    ],
    sex: [
        { required: true, message: '请选择性别', trigger: 'change' },
    ],
    age: [
        { required: true, message: '请填写年龄', trigger: 'blur' },
    ],
    socialNo: [
        { required: true, message: '请填写证件号', trigger: 'blur' },
    ],
    certificateType: [
        { required: true, message: '请选择证件类型', trigger: 'change' },
    ],
    pType: [
        { required: true, message: '请选择病人来源', trigger: 'change' },
    ],
    relTel: [
        { required: true, message: '请填写本人电话', trigger: 'blur' },
    ],
    chronicDiseaseTypeArr: [
        { required: true, message: '请选择慢病类型', trigger: 'change' },
    ],
    lastDate: [
        { required: true, message: '请选择确诊时间', trigger: 'change' },
    ],
    importLevel: [
        { required: true, message: '请选择病人等级', trigger: 'change' },
    ],
    provinceCode: [
        { required: true, message: '请选择省', trigger: 'change' },
    ],
    cityCode: [
        { required: true, message: '请选择市', trigger: 'change' },
    ],
    areaCode: [
        { required: true, message: '请选择区', trigger: 'change' },
    ],
    referPhysician: [
        { required: true, message: '请选择主管医生', trigger: 'change' },
    ],
    creatId: [
        { required: true, message: '请选择建档医生', trigger: 'change' },
    ],
    createDate: [
        { required: true, message: '请选择建卡时间', trigger: 'change' },
    ],
    visitDate: [
        { required: true, message: '请选择下次随访时间', trigger: 'change' },
    ],
    height: [
        { max: 10, message: '不超过10个字符', trigger: 'blur' },
    ],
    weight: [
        { max: 10, message: '不超过10个字符', trigger: 'blur' },
    ],
    temperature: [
        { max: 10, message: '不超过10个字符', trigger: 'blur' },
    ],
    bloodSugar: [
        { max: 10, message: '不超过10个字符', trigger: 'blur' },
    ],
    heartRate: [
        { max: 10, message: '不超过10个字符', trigger: 'blur' },
    ],
    bloodPressureLow: [
        { max: 10, message: '不超过10个字符', trigger: 'blur' },
    ],
    bloodPressureHigh: [
        { max: 10, message: '不超过10个字符', trigger: 'blur' },
    ],
})

const isQueryBrInfo = ref(true)
const queryCmr = async () => {
    let code = keyCode.value
    if (!code) {
        ElMessage({
            type: "info",
            message: '查询关键字不能为空',
            duration: 2500,
            showClose: true,
        });
        return
    }
    ruleForm.value = {} as RuleForm
    isEditForm.value = true
    isQueryBrInfo.value = false
    try {
        await selectCrmPatientMiByCode(code).then((res: any) => {
            console.log(res)
            ruleForm.value = res
        })
    } catch (error) {
        console.log(error)
    }

}

const brInfoData = ref({})
const queryBrInfo = async () => {
    let code = keyCode.value
    if (ruleForm.value.pId) {
        isQueryBrInfo.value = true
    }
    if (!code) {
        ElMessage({
            type: "info",
            message: '查询关键字不能为空',
            duration: 2500,
            showClose: true,
        });
        return
    }
    if (isQueryBrInfo.value) {
        ElMessage({
            type: "info",
            message: '请预先查询是否存在慢病信息或者已经存在慢病信息！',
            duration: 2500,
            showClose: true,
        });
        return
    }
    if (isEditForm.value === true) {
        isEditForm.value = false
    }
    try {
        await selectBrInfoByCode(code).then((res: any) => {
            if (!res.mi) {
                ElMessage({
                    type: "warning",
                    message: '未找到病人基本信息',
                    duration: 2500,
                    showClose: true,
                });
                return
            }
            brInfoData.value = res.mi
            if (brInfoData.value) {
                ruleForm.value = clone(brInfoData.value)
                ruleForm.value.createDate = now
                ruleForm.value.creatId = userCode
                ruleForm.value.visitTimes = 0
            }
        })
    } catch (error) {
        console.log(error)
    }
}

const addForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
    if (isEditForm.value === true) {
        isEditForm.value = false
    }
    keyCode.value = ''
    ruleForm.value.creatId = userCode
}

const editForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return

    if (ruleForm.value.creatId !== '99999' && ruleForm.value.creatId !== userCode) {
        ElMessage({
            type: "info",
            message: '您没有权限修改他人登记的病人信息！',
            duration: 2500,
            showClose: true,
        });
        return
    }

    if (isEditForm.value === true) {
        isEditForm.value = false
    }
}

const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            ruleForm.value.chronicDiseaseType = ruleForm.value.chronicDiseaseTypeArr.toString()
            saveCrmPatientMi(ruleForm.value).then((res: any) => {
                if (res.cg) {
                    ElMessage({
                        type: "success",
                        message: res.cg,
                        duration: 2500,
                        showClose: true,
                    });

                    if (isEditForm.value === false) {
                        isEditForm.value = true
                    }
                }
            });

        } else {
            ElMessage({
                type: "info",
                message: '未完成必填项或者填写项不符合,请检查',
                duration: 2500,
                showClose: true,
            });
        }
    })
}

const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
    if (isEditForm.value === true) {
        isEditForm.value = false
    }
    keyCode.value = ''
    ruleForm.value.creatId = userCode
}

const provinceCharge = async () => {
    let province = ruleForm.value.provinceCode
    cityData.value = await cityFilter(province)
}

const cityCharge = async () => {
    let city = ruleForm.value.cityCode
    countyData.value = await countyFilter(city)
}

</script>