<template>
    <nav-bar :navbar-data="navbarData"></nav-bar>
    <view class="cardDetail" :style="{ marginTop: 'calc('+ height +'px + 18rpx)', marginBottom: '20rpx' }">
        <view class="top">
            <van-cell-group>
                <view class="row">
                    <label>用户名</label>
                    <view class="row-right">
                        <input type="text" :value="patName" placeholder="请输入用户名" @input="onChangePatName">
                    </view>
                </view>
                <view class="row">
                    <label>证件号</label>
                    <view class="row-right">
                        <input type="text" :value="simpleCardNo" placeholder="请输入证件号" disabled>
                    </view>
                </view>
                <view class="row">
                    <label>证件类型</label>
                    <view class="row-right">
                        <input type="text" :value="cardTpye" placeholder="请输入证件类型" disabled>
                    </view>
                </view>
                <view class="row">
                    <label>手机号</label>
                    <view class="row-right">
                        <input type="text" :value="mobile" placeholder="请输入手机号" @input="onChangeMobile">
                    </view>
                </view>
                <view class="row">
                    <label>社保卡</label>
                    <view class="row-right">
                        <input type="text" :value="socialCard" placeholder="请输入社保卡" disabled>
                    </view>
                </view>
                <!-- <van-field value="{{ patName }}" label="用户名" bind:change="onChangePatName" bindblur="onBlurPatName" />
			<van-field value="{{ simpleCardNo }}" label="证件号" disabled />
			<van-field value="{{ healthCode }}" label="就诊卡" disabled />
			<van-field value="{{ cardTpye }}" label="证件类型" disabled />
			<van-field value="{{ mobile }}" label="手机号" bind:change="onChangeMobile" bindblur="onBlurMobile" />
			<van-field value="{{ socialCard }}" label="社保卡" disabled /> -->
            </van-cell-group>
        </view>
        <view class="selectL">
            <!-- 性别 -->
            <van-cell title="性别" is-link>
				<template v-slot:value>
					<picker mode="selector" :range="sexList" range-key="label" @change="onSelectSex"><text>{{ genderVal.label ? genderVal.label : cardDetail.sexDesc }}</text></picker>
				</template>
            </van-cell>

            <!-- 出生日期 -->
            <van-cell title="出生日期" is-link>
                <template v-slot:value>
                    <uni-datetime-picker type="date" :border="false" :date-icon="false" :clear-icon="false" height="24px" v-model="currentDate" :start="minDate" :end="maxDate" @change="confirmDate" />
                </template>
            </van-cell>

            <!-- 国籍 -->
            <van-cell title="国籍" is-link>
                <template v-slot:value>
                    <picker mode="selector" :range="citizenshipList" range-key="label" @change="ongetCitizenshipData">
                        <text>{{ citizenshipVal.text ? citizenshipVal.text : cardDetail.countryDesc ? cardDetail.countryDesc : '请选择' }}</text>
                    </picker>
                </template>
            </van-cell>

            <!-- 民族 -->
            <van-cell title="民族" is-link>
                <template v-slot:value>
                    <picker mode="selector" :range="nationList" range-key="label" @change="ongetNationData"><text>{{ nationValue.label ? nationValue.label : cardDetail.nationDesc }}</text></picker>
                </template>
            </van-cell>

            <common class="addr" :provinceDesc="nativeProvinceDesc" :cityDesc="nativeCityDesc" :defaultIndexs="nativeDefaultIndexs" status='2'
                addressData='籍贯' @onSelectproCitie='onSelectproCitieList' :defaultProvince="defaultProvince"
                :defaultCity="defaultCity" />
            <common class="addr" :provinceDesc="birthProvinceDesc" :cityDesc="birthCityDesc" :areaDesc="birthAreaDesc" id="bornID"
                status='4' addressData='出生地' @onBornCitie='onBornCitieList' :defaultProvince="defaultProvince"
                :defaultCity="defaultCity" :defaultArea="defaultArea" />
            <common class="addr" :provinceDesc="regProvinceDesc" :cityDesc="regCityDesc" :areaDesc="regAreaDesc"
                :villageDesc="regVillageDesc" id="hukouAddressID" status="5" addressData='户口地址'
                @hukouAddressLists='hukouAddressClick' :defaultProvince="defaultProvince" :defaultCity="defaultCity"
                :defaultArea="defaultArea" />

            <!-- 户口详细地址 -->
            <view class="row">
                <label>户口详细地址</label>
                <view class="row-right">
                    <input type="text" :value="regAddress" placeholder="请输入" @input="onChangeRegAddress" />
                </view>
            </view>

            <!-- 现住址 -->
            <common class="addr" :provinceDesc="provinceDesc" :cityDesc="cityDesc" :areaDesc="areaDesc" :villageDesc="villageDesc" 
                id="componentId" status="1" addressData='现住址' @twoLevelCommentBtn='twoLevelCommentBtnClick'
                :defaultProvince="defaultProvince" :defaultCity="defaultCity" :defaultArea="defaultArea" />
            <!-- 现住详细地址 -->
            <view class="row">
                <label>现住详细地址</label>
                <view class="row-right">
                    <input type="text" :value="address" placeholder="请输入" @input="onChangeAddress" />
                </view>
            </view>

            <!-- 婚否 -->
            <van-cell title="婚否" is-link>
                <template v-slot:value>
                    <picker mode="selector" :range="maritalList" range-key="text" @change="ongetMarry"><text>{{ marryVal.text ? marryVal.text : cardDetail.maritalDesc }}</text></picker>
                </template>
            </van-cell>

            <!-- 职业 -->
            <van-cell title="职业" is-link>
                <template v-slot:value>
                    <picker mode="selector" :range="jobsList" range-key="label" @change="ongetJobs">
                        <text>{{ jobsVal.text ? jobsVal.text : cardDetail.occupationDesc }}</text>
                    </picker>
                </template>
            </van-cell>

            <!-- 工作单位名称 -->
            <view class="row">
                <label>工作单位名称</label>
                <view class="row-right">
                    <input type="text" :value="jobsVal.text == '无业人员' ? '-' : companyData" placeholder="请输入" @input="onChangecompanyData" disabled="{{ jobsVal.text == '无业人员' ? true : false}}" />
                </view>
            </view>

            <!-- 工作单位电话 -->
            <view class="row">
                <label>工作单位电话</label>
                <view class="row-right">
                    <input type="text" :value="jobsVal.text == '无业人员' ? '-' : companyTelText" placeholder="请输入" @input="onChangeCompanyTel" disabled="{{ jobsVal.text == '无业人员' ? true : false}}" />
                </view>
            </view>
            <!-- <van-cell-group>
			<van-field value="{{jobsVal.text == '无业人员' ? '-' : companyData }}" label="工作单位名称" bind:change="onChangecompanyData" disabled="{{ jobsVal.text == '无业人员' ? true : false}}" bindblur="onBlurcompanyData" />
		</van-cell-group> -->
            <!-- <van-cell-group>
			<van-field value="{{jobsVal.text == '无业人员'  ? '-' : companyTelText }}" label="工作单位电话" bind:change="onChangeCompanyTel" disabled="{{jobsVal.text == '无业人员' ? true : false}}" bindblur="onBlurCompanyTel" />
		</van-cell-group> -->
        </view>

        <!-- <view wx:if="{{ageNum <14 }}" class='child'> -->
        <view class='child'>
            <van-cell-group>
                <!-- 紧急联系人 -->
                <view class="row">
                    <label>紧急联系人</label>
                    <view class="row-right">
                        <input type="text" :value="EContact" placeholder="请输入姓名" @input="onEContact" />
                    </view>
                </view>
                <!-- <van-field value="{{ EContact }}" label="紧急联系人" placeholder="请输入姓名" bind:change="onEContact" bindblur="onBlurEContact" /> -->

                <!-- 证件类型 -->
                <van-cell title="证件类型" is-link>
                    <template v-slot:value>
                        <picker mode="selector" :range="cardTypeList" range-key="label" @change="onEContactCredType">
							<text>{{ EContactCredType.text ? EContactCredType.text : cardDetail.EContactCredTypeDesc ? cardDetail.EContactCredTypeDesc : '请选择'}}</text>
						</picker>
                    </template>
                </van-cell>

                <!-- 证件号 -->
                <view class="row">
                    <label>证件号</label>
                    <view class="row-right">
                        <input type="text" :value="EContactIDNo" placeholder="请输入证件号" @input="onEContactIDNo" />
                    </view>
                </view>
                <!-- <van-field value="{{ EContactIDNo }}" label="证件号" placeholder="请输入证件号" bind:change="onEContactIDNo" /> -->

                <!-- 手机号 -->
                <view class="row">
                    <label>手机号</label>
                    <view class="row-right">
                        <input type="digit" :value="EContactMobile" placeholder="请输入手机号" @input="onEContactMobile" />
                    </view>
                </view>
                <!-- <van-field value="{{ EContactMobile }}" type="digit" label="手机号" placeholder="请输入手机号" bind:change="onEContactMobile" /> -->

                <!-- 关系 -->
                <van-cell title="关系" is-link>
                    <template v-slot:value>
                        <picker mode="selector" :range="relationList" range-key="label" @change="onRelation">
							<text>{{ relation.label ? relation.label : cardDetail.relationDesc ? cardDetail.relationDesc : '请选择' }}</text>
						</picker>
                    </template>
                </van-cell>
            </van-cell-group>

            <!--联系人地址-->
            <common class="addr" :provinceDesc="eContactProvinceDesc" :cityDesc="eContactCityDesc" :areaDesc="eContactAreaDesc"
                :villageDesc="eContactVillageDesc" id="urgentID" status="3" addressData='联系人地址'
                @urgentClick='urgentListClick' :defaultProvince="defaultProvince" :defaultCity="defaultCity"
                :defaultArea="defaultArea" />

            <!--详细地址-->
            <view class="row">
                <label>详细地址</label>
                <view class="row-right">
                    <input type="text" :value="eContactAddress" placeholder="请输入" @input="onChangeEContactAddress" />
                </view>
            </view>
            <!-- <van-cell-group>
			<van-field value="{{ eContactAddress }}" label="详细地址" bind:change="onChangeEContactAddress" bindblur="onBlurEContactAddress" />
		</van-cell-group> -->
        </view>
        <view class="button">
            <view style="float:left;width:50%" class="F5222D">
                <van-button :disabled="delState ? true : false" type="default" @tap="deleteCard">{{ delState ? '删除中...' : '删除' }}</van-button>
            </view>
            <view style="float:left;width:50%">
                <van-button :disabled="saveState ? true : false" type="info" @tap="saveData">{{ saveState ? '保存中...' : '保存' }}</van-button>
            </view>
        </view>
    </view>
    <van-dialog id="van-dialog" ref="van-dialog" :closeable="false" />
</template>

<script>
import $http from "@/utils/https";
import navBar from '@/components/navBar/index'
import VanButton from '@/components/button/index'
import VanCellGroup from '@/components/cell-group/index'
import VanCell from '@/components/cell/index'
import VanDialog from '@/components/dialog/index.vue';
import common from '../../common/address'
import Dialog from '@/components/dialog/dialog'

const app = getApp()
export default {
    components: {
        'nav-bar': navBar,
        'van-button': VanButton,
        'van-cell-group': VanCellGroup,
        'van-cell': VanCell,
        'van-dialog': VanDialog,
        'common': common
    },
    data() {
        return {
            height: app.globalData.height,
            navbarData: {
                isGoHome: true,
                homePath: '../../../../pages/tabBar/index/index',
                isGoBack: true,
                title: "就诊人详情"
            },
            dataColumns: [{
                text: '杭州'
            },
            {
                text: '宁波'
            },
            {
                text: '温州'
            },
            ],
            jobsValShow: false,
            url: '',
            saveState: false,
            delState: false,
            idCardContact: true,
            EContactIDShowL: true,
            patName: '',
            /**姓名 */
            mobile: '',
            /**手机号 */
            idCardNo: '',
            simpleCardNo: '',
            /**身份证号 */
            genderVal: {},
            /**性别 */
            citizenshipVal: {}, //国籍
            nationValue: {},
            /**民族 */
            address: '',
            /**地址 */
            socialCard: '',
            /**社保卡 */
            healthCode: '',
            /**证件类型 */
            cardTpye: '',
            /**就诊卡 */
            EContact: '', //联系人
            EContactIDNo: '',
            EContactMobile: '',
            EContactCredType: {},
            relation: {},
            currentDate: new Date().getTime(),
            minDate: new Date(1900, 1, 1).getTime(),
            maxDate: new Date().getTime(),
            formatter(type, value) {
                if (type === 'year') {
                    return `${value}`;
                } else if (type === 'month') {
                    return `${value}`;
                } else if (type === 'day') {
                    return `${value}`;
                }
                return value;
            },
            showTime: false,
            show: false,
            showNation: false,
            showCardType: false,
            showRelation: false,
            showMarry: false,
            sexList: [],
            /**下拉数据 */
            nationList: [],
            citizenshipList: [], //国籍
            cardTypeList: [],
            relationList: [],
            maritalList: [],
            jobsList: [],
            cardDetail: {},
            /**卡详情数据 */
            marryVal: {},
            jobsVal: {},
            patID: '',
            areaInfo: '',
            selectAddress: [],
            hukouAddress: [],
            provinceDesc: '',
            cityDesc: '',
            areaDesc: '',
            villageDesc: '',
            ageNum: '',
            hometownArr: [],
            urgentArr: [],
            bornArr: [],
            companyData: '', //工作单位
            companyTelText: '',
            regAddress: '', //户口详情地址
            eContactAddress: '', //联系人详情地址
            nativeProvinceDesc: '',
            nativeCityDesc: '',
            birthProvinceDesc: '',
            birthCityDesc: '',
            birthAreaDesc: '',
            regProvinceDesc: '',
            regCityDesc: '',
            regAreaDesc: '',
            regVillageDesc: '',
            eContactProvinceDesc: '',
            eContactCityDesc: '',
            eContactAreaDesc: '',
            eContactProvinceDesc: '',
            defaultCity: {},//默认市
            defaultProvince: {},//默认省
            defaultArea: {}, //默认区
        }
    },
    onLoad: function (options) {
        let patID = options.id
        if (patID) {
            this.setData({
                patID: patID
            }, () => {
                this.getcardDetailList()
                // this.getcardDetail();
            })
        }
        if (options && (typeof options == 'object') && options.url) {
            this.setData({
                url: options.url,
                patID: options.patID
            }, () => {
                this.getcardDetailList()
            });
        }
    },
    methods: {
        showToast(title,icon){
            uni.showToast({
                icon: icon ? icon : 'none',
                title: title,
                duration: 2000
            })
        },

        // 现住址
        twoLevelCommentBtnClick: function (e) {
            this.setData({
                selectAddress: e.detail.value
            });
        },

        // 户口地址
        hukouAddressClick: function (e) {
            this.setData({
                hukouAddress: e.detail.value
            });
        },

        // 籍贯
        onSelectproCitieList: function (e) {
            console.log(e)
            this.setData({
                nativeDefaultIndexs: e.detail.indexs,
                hometownArr: e.detail.value
            })
        },

        // 紧急联系人地址
        urgentListClick: function (e) {
            this.setData({
                urgentArr: e.detail.value
            })
        },

        // 出生地
        onBornCitieList: function (e) {
            this.setData({
                bornArr: e.detail.value
            })
        },

        onGetCode: function (e) {
            this.setData({
                areaInfo: e.detail.areaInfo
            })
        },

        /**请求就诊人列表下拉数据
         * **/
        getcardDetailList: function () {
            let data = {
                params: []
            }
            $http.post("urlS", this, {
                code: "01030011",
                data: data,
                success: function (res) {
                    if (+res.errorCode === 0) {
                        if (res.sex.length > 0) {
                            for (var i = 0; i < res.sex.length; i++) {
                                res.sex[i].text = res.sex[i].label
                            }
                            this.setData({
                                sexList: res.sex
                            });
                        }
                        if (res.nation.length > 0) {
                            for (var k = 0; k < res.nation.length; k++) {
                                res.nation[k].text = res.nation[k].label
                            }
                            this.setData({
                                nationList: res.nation
                            });
                        }
                        if (res.country.length > 0) {
                            for (var l = 0; l < res.country.length; l++) {
                                res.country[l].text = res.country[l].label
                            }
                            this.setData({
                                citizenshipList: res.country
                            });
                        }
                        if (res.credType.length > 0) {
                            for (var j = 0; j < res.credType.length; j++) {
                                res.credType[j].text = res.credType[j].label
                            }
                            let a = res.credType.filter((item) => item.label == '居民身份证');
                            this.setData({
                                cardTypeList: res.credType,
                                EContactCredType: a[0]
                            });
                        }
                        if (res.relation.length > 0) {
                            for (var h = 0; h < res.relation.length; h++) {
                                res.relation[h].text = res.relation[h].label
                            }
                            this.setData({
                                relationList: res.relation
                            });
                        }
                        if (res.marital.length > 0) {
                            for (var m = 0; m < res.marital.length; m++) {
                                res.marital[m].text = res.marital[m].label
                            }
                            console.log(res.marital)
                            this.setData({
                                maritalList: res.marital
                            });
                        }
                        if (res.OCCU.length > 0) {
                            for (var q = 0; q < res.OCCU.length; q++) {
                                res.OCCU[q].text = res.OCCU[q].label
                            }
                            this.setData({
                                jobsList: res.OCCU
                            },() => {
                                console.log(this.jobsList)
                            });
                        }
                        this.setData({
                            defaultProvince: res.defalutProvince,
                            defaultCity: res.defalutCity,
                            defaultArea: res.defalutArea
                        }, () => {
                            this.getcardDetail();
                        })
                    } else {
                        this.setData({
                            nationList: [],
                            sexList: []
                        });
                    }
                }
            });
        },

        // 通过身份证号获取年龄大小
        calculateAge: function (certificateNum) {
            if (certificateNum) {
                var myDate = new Date();
                var month = myDate.getMonth() + 1;
                var day = myDate.getDate();
                var age = myDate.getFullYear() - certificateNum.substring(6, 10) - 1;
                if (certificateNum.substring(10, 12) < month || certificateNum.substring(10, 12) == month && certificateNum.substring(12, 14) <= day) {
                    age++;
                }
                this.setData({
                    ageNum: age
                })
            }
        },

        idCardcode: function (idCard) {
            //15位或者18位身份证号码的正则表达式
            var regIdCard = /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/;

            //如果通过该验证，说明身份证格式正确，但准确性还需计算
            if (regIdCard.test(idCard)) {
                if (idCard.length == 18) {
                    var idCardWi = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); //将前17位加权因子保存在数组里
                    var idCardY = new Array(1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2); //这是除以11后，可能产生的11位余数、验证码，也保存成数组
                    var idCardWiSum = 0; //用来保存前17位各自乖以加权因子后的总和
                    for (var i = 0; i < 17; i++) {
                        idCardWiSum += idCard.substring(i, i + 1) * idCardWi[i];
                    }

                    var idCardMod = idCardWiSum % 11; //计算出校验码所在数组的位置
                    var idCardLast = idCard.substring(17); //得到最后一位身份证号码

                    //如果等于2，则说明校验码是10，身份证号码最后一位应该是X
                    if (idCardMod == 2) {
                        if (idCardLast == "X" || idCardLast == "x") {
                            this.setData({
                                EContactIDNo: idCard,
                                idCardContact: true
                            });
                        } else {
                            this.setData({
                                idCardContact: false
                            })
                        }
                    } else {
                        //用计算出的验证码与最后一位身份证号码匹配，如果一致，说明通过，否则是无效的身份证号码
                        if (idCardLast == idCardY[idCardMod]) {
                            this.setData({
                                EContactIDNo: idCard,
                                idCardContact: true
                            });
                        } else {
                            this.setData({
                                idCardContact: false
                            })
                        }
                    }
                }
            } else {
                this.setData({
                    idCardContact: false
                })
            }
        },

        /**请求卡详情数据
         * **/
        getcardDetail: function () {
            let data = {
                params: [{
                    patID: this.patID
                }]
            }
            $http.post("urlS", this, {
                code: "01030002",
                data: data,
                success: function (res) {
                    if (+res.errorCode === 0) {
                        // if(res.result.data.EContactCredTypeDesc){
                        //   this.setData({EContactCredType:{}})
                        // }
                        if (res.result.data.idCardNo.length == 18) {
                            var str = res.result.data.idCardNo.substr(6, 8);
                            var simCard = res.result.data.idCardNo.replace(str, '****')
                        } else {
                            var simCard = ''
                        }
                        this.setData({
                            patName: res.result.data.patName,/**姓名 */
                            mobile: res.result.data.mobile,/**手机号 */
                            idCardNo: res.result.data.idCardNo,
                            simpleCardNo: simCard ? simCard : res.result.data.idCardNo,/**身份证号 */
                            address: res.result.data.address, /**地址 */
                            healthCode: res.result.data.healthCode, /**证件类型 */
                            cardTpye: res.result.data.cardTpyeDesc, /**就诊卡 */
                            selectDate: res.result.data.birthDate,
                            provinceDesc: res.result.data.provinceDesc,
                            cityDesc: res.result.data.cityDesc,
                            areaDesc: res.result.data.areaDesc,
                            villageDesc: res.result.data.villageDesc,
                            EContact: res.result.data.EContact,
                            EContactMobile: res.result.data.EContactMobile,
                            EContactIDNo: res.result.data.EContactIDNo,
                            cardDetail: res.result.data,
                            regAddress: res.result.data.regAddress,
                            companyData: res.result.data.company,
                            companyTelText: res.result.data.companyTel,
                            eContactAddress: res.result.data.eContactAddress,
                            jobsVal: {
                                text: res.result.data.occupationDesc,
                                value: res.result.data.occupationID
                            },
                            EContactCredType: {
                                text: res.result.data.EContactCredTypeDesc,
                                label: res.result.data.EContactCredTypeDesc,
                                value: res.result.data.EContactCredTypeID
                            },
                            nativeProvinceDesc: res.result.data.nativeProvinceDesc,
                            nativeCityDesc: res.result.data.nativeCityDesc,

                            birthProvinceDesc: res.result.data.birthProvinceDesc,
                            birthCityDesc: res.result.data.birthCityDesc,
                            birthAreaDesc: res.result.data.birthAreaDesc,

                            regProvinceDesc: res.result.data.regProvinceDesc,
                            regCityDesc: res.result.data.regCityDesc,
                            regAreaDesc: res.result.data.regAreaDesc,
                            regVillageDesc: res.result.data.regVillageDesc,

                            eContactProvinceDesc: res.result.data.eContactProvinceDesc,
                            eContactCityDesc: res.result.data.eContactCityDesc,
                            eContactAreaDesc: res.result.data.eContactAreaDesc,
                            eContactVillageDesc: res.result.data.eContactVillageDesc
                        });
                        this.idCardcode(res.result.data.EContactIDNo);
                        this.calculateAge(res.result.data.idCardNo);
                        let defaultProvinces = this.defaultProvince.descripts ? this.defaultProvince.descripts : '';
                        let defaultCitys = this.defaultCity.descripts ? this.defaultCity.descripts : '';
                        let defaultProvinceID = this.defaultProvince.id ? this.defaultProvince.id : '';
                        let defaultCityID = this.defaultCity.id ? this.defaultCity.id : '';
                        let defaultAreas = this.defaultArea.descripts ? this.defaultArea.descripts : '';
                        let defaultAreaID = this.defaultArea.id ? this.defaultArea.id : '';
                        // if (res.result.data.provinceDesc) {
                        //     this.selectComponent("#componentId").getProvinceDetail(res.result.data.provinceDesc);
                        // } else {
                        //     this.selectComponent("#componentId").getProvinceDetail(defaultProvinces);
                        // }
                        // if (res.result.data.eContactProvinceDesc) {
                        //     this.selectComponent("#urgentID").getProvinceDetail(res.result.data.eContactProvinceDesc);
                        // } else {
                        //     this.selectComponent("#urgentID").getProvinceDetail(defaultProvinces);
                        // }
                        // if (res.result.data.regProvinceDesc) {
                        //     this.selectComponent("#hukouAddressID").getProvinceDetail(res.result.data.regProvinceDesc);
                        // } else {
                        //     this.selectComponent("#hukouAddressID").getProvinceDetail(defaultProvinces);
                        // }
                        // if (res.result.data.nativeProvinceDesc) {
                        //     this.selectComponent("#proAndeCitie").getProvinceDetail(res.result.data.nativeProvinceDesc);
                        // } else {
                        //     this.selectComponent("#proAndeCitie").getProvinceDetail(defaultProvinces);
                        // }
                        // if (res.result.data.birthProvinceDesc) {
                        //     this.selectComponent("#bornID").getProvinceDetail(res.result.data.birthProvinceDesc);
                        // } else {
                        //     this.selectComponent("#bornID").getProvinceDetail(defaultProvinces);
                        // }

                        // if (res.result.data.cityDesc) {
                        //     this.selectComponent("#componentId").getCityDetail(res.result.data.provinceID, res.result.data.cityDesc);
                        // } else {
                        //     this.selectComponent("#componentId").getCityDetail(defaultProvinceID, defaultCitys);
                        // }
                        // if (res.result.data.eContactCityDesc) {
                        //     this.selectComponent("#urgentID").getCityDetail(res.result.data.eContactProvinceID, res.result.data.eContactCityDesc);
                        // } else {
                        //     this.selectComponent("#urgentID").getCityDetail(defaultProvinceID, defaultCitys);
                        // }
                        // if (res.result.data.regCityDesc) {
                        //     this.selectComponent("#hukouAddressID").getCityDetail(res.result.data.regProvinceID, res.result.data.regCityDesc);
                        // } else {
                        //     this.selectComponent("#hukouAddressID").getCityDetail(defaultProvinceID, defaultCitys);
                        // }
                        // if (res.result.data.nativeCityDesc) {
                        //     this.selectComponent("#proAndeCitie").getCityDetail(res.result.data.nativeProvinceID, res.result.data.nativeCityDesc);
                        // } else {
                        //     this.selectComponent("#proAndeCitie").getCityDetail(defaultProvinceID, defaultCitys);
                        // }
                        // if (res.result.data.birthCityDesc) {
                        //     this.selectComponent("#bornID").getCityDetail(res.result.data.birthProvinceID, res.result.data.birthCityDesc);
                        // } else {
                        //     this.selectComponent("#bornID").getCityDetail(defaultProvinceID, defaultCitys);
                        // }
                        // if (res.result.data.areaDesc) {
                        //     this.selectComponent("#componentId").getAreaDetail(res.result.data.cityID, res.result.data.areaDesc);
                        // } else {
                        //     this.selectComponent("#componentId").getAreaDetail(defaultCityID, defaultAreas);
                        // }
                        // if (res.result.data.eContactAreaDesc) {
                        //     this.selectComponent("#urgentID").getAreaDetail(res.result.data.eContactCityID, res.result.data.eContactAreaDesc)
                        // } else {
                        //     this.selectComponent("#urgentID").getAreaDetail(defaultCityID, defaultAreas)
                        // }
                        // if (res.result.data.regAreaDesc) {
                        //     this.selectComponent("#hukouAddressID").getAreaDetail(res.result.data.regCityID, res.result.data.regAreaDesc);
                        // } else {
                        //     this.selectComponent("#hukouAddressID").getAreaDetail(defaultCityID, defaultAreas);
                        // }
                        // if (res.result.data.birthAreaDesc) {
                        //     this.selectComponent("#bornID").getAreaDetail(res.result.data.birthCityID, res.result.data.birthAreaDesc);
                        // } else {
                        //     this.selectComponent("#bornID").getAreaDetail(defaultCityID, defaultAreas);
                        // }
                        // if (res.result.data.villageDesc) {
                        //     this.selectComponent("#componentId").getStreetDetail(res.result.data.areaID, res.result.data.villageDesc);
                        // } else {
                        //     this.selectComponent("#componentId").getStreetDetail(defaultAreaID, '');
                        // }
                        // if (res.result.data.eContactVillageDesc) {
                        //     this.selectComponent("#urgentID").getStreetDetail(res.result.data.eContactAreaID, res.result.data.eContactVillageDesc)
                        // } else {
                        //     this.selectComponent("#urgentID").getStreetDetail(defaultAreaID, '')
                        // }
                        // if (res.result.data.regVillageDesc) {
                        //     this.selectComponent("#hukouAddressID").getStreetDetail(res.result.data.regAreaID, res.result.data.regVillageDesc);
                        // } else {
                        //     this.selectComponent("#hukouAddressID").getStreetDetail(defaultAreaID, '');
                        // }
                    } else {
                        this.setData({
                            cardDetail: {}
                        });
                    }
                }
            });
        },

        saveData: function () {
            this.setData({
                saveState: true
            });

            if (this.mobile && !(/^1[3456789]\d{9}$/.test(this.mobile.replace(/\s*/g, '')))) {
                this.setData({ saveState: false })
                this.showToast('就诊人手机号码有误','error')
                return
            }
            if (!this.mobile) {
                this.setData({ saveState: false })
                this.showToast('就诊人手机号码不能为空','error')
                return
            }
            // if(!this.EContact){
            //   this.setData({saveState: false })
            //   Toast.fail('紧急联系人不能为空');
            //   return
            // }
            // if(this.ageNum < 18){
            //   if(this.EContactCredType.label == '居民身份证'){
            //    if (!this.idCardContact ) {
            //     this.setData({saveState: false })
            //     Toast.fail('联系人证件号有误');
            //     return
            //   }else if(!this.EContactIDNo){ 
            //     this.setData({saveState: false })
            //     Toast.fail('联系人证件号不能为空');
            //     return
            //   }
            //   }else if(!this.EContactIDNo && this.EContactCredType.label !== '居民身份证'){ 
            //     this.setData({saveState: false })
            //     Toast.fail('联系人证件号不能为空');
            //     return
            //   }
            // }else{
            //   if(this.EContactCredType.label == '居民身份证'){
            //     if(!this.idCardContact){
            //       if( this.EContactIDShow){
            //         this.setData({saveState: false })
            //         Toast.fail('联系人证件有误');
            //         return
            //       }
            //     }

            //   }
            // }
            // if ( this.EContactMobile && !(/^1[3456789]\d{9}$/.test(this.EContactMobile.replace(/\s*/g, '')))) {
            //   this.setData({saveState: false })
            //   Toast.fail('联系人手机号码有误');
            //   return
            // }
            // if( !this.EContactMobile){
            //   this.setData({saveState: false })
            //   Toast.fail('联系人手机号码不能为空');
            //   return
            // }
            // if(!this.relation.label && !this.cardDetail.relationDesc){
            //   this.setData({saveState: false })
            //   Toast.fail('联系人关系不能为空');
            //   return
            // }
            // if(this.selectAddress.length== 0 && !this.cardDetail.provinceID){
            //   this.setData({saveState: false })
            //   Toast.fail('请选择联系人地址');
            //   return
            // }
            // if(!this.eContactAddress){
            //   this.setData({saveState: false })
            //   Toast.fail('联系人详情地址不能为空');
            //   return
            // }
            let data = {
                params: [{
                    address: this.address,
                    birthDate: this.selectDate,
                    idCardNo: this.idCardNo,
                    mobile: this.mobile,
                    nationID: this.nationValue.text ? this.nationValue.value : this.cardDetail.nationID,
                    patName: this.patName,
                    patNo: this.cardDetail.patNo,
                    provinceID: this.selectAddress[0] && this.selectAddress[0][0] ? this.selectAddress[0][0].value : this.cardDetail.provinceID, //现住址
                    cityID: this.selectAddress[1] && this.selectAddress[1][0] ? this.selectAddress[1][0].value : this.cardDetail.cityID,
                    areaID: this.selectAddress[2] && this.selectAddress[2][0] ? this.selectAddress[2][0].value : this.cardDetail.areaID,
                    villageID: this.selectAddress[3] && this.selectAddress[3][0] ? this.selectAddress[3][0].value : this.cardDetail.villageID,
                    sexID: this.genderVal.text ? this.genderVal.value : this.cardDetail.sexID,
                    patID: this.patID,
                    patEContact: this.EContact,
                    patEContactMobile: this.EContactMobile,
                    patEContactCredTypeIDNo: this.EContactIDNo,
                    patEContactCredTypeID: this.EContactCredType.text ? this.EContactCredType.value : this.cardDetail.EContactCredTypeID,
                    relationID: this.relation.text ? this.relation.value : this.cardDetail.relationID,
                    countryID: this.citizenshipVal.text ? this.citizenshipVal.value : this.cardDetail.countryID, //国籍id
                    maritalID: this.marryVal.text ? this.marryVal.value : this.cardDetail.maritalID, //婚否
                    occupationID: this.jobsVal.text ? this.jobsVal.value : this.cardDetail.occupationID, //职业
                    company: this.companyData, //工作单位
                    companyTel: this.companyTelText, //工作单位电话
                    nativeProvinceID: this.hometownArr[0] ? this.hometownArr[0].value : this.cardDetail.nativeProvinceID, //籍贯
                    nativeCityID: this.hometownArr[1] ? this.hometownArr[1].value : this.cardDetail.nativeCityID,
                    nativeAreaID: '',
                    birthProvinceID: this.bornArr[0] ? this.bornArr[0].value : this.cardDetail.birthProvinceID, //出生地
                    birthCityID: this.bornArr[1] ? this.bornArr[1].value : this.cardDetail.birthCityID,
                    birthAreaID: this.bornArr[2] ? this.bornArr[2].value : this.cardDetail.birthAreaID,
                    regProvinceID: this.hukouAddress[0] ? this.hukouAddress[0].value : this.cardDetail.regProvinceID, //户口地址
                    regCityID: this.hukouAddress[1] ? this.hukouAddress[1].value : this.cardDetail.regCityID,
                    regAreaID: this.hukouAddress[2] ? this.hukouAddress[2].value : this.cardDetail.regAreaID,
                    regVillageID: this.hukouAddress[3] ? this.hukouAddress[3].value : this.cardDetail.regVillageID,
                    regAddress: this.regAddress,
                    eContactProvinceID: this.urgentArr[0] ? this.urgentArr[0].value : this.cardDetail.eContactProvinceID, //紧急联系人地址
                    eContactCityID: this.urgentArr[1] ? this.urgentArr[1].value : this.cardDetail.eContactCityID,
                    eContactAreaID: this.urgentArr[2] ? this.urgentArr[2].value : this.cardDetail.eContactAreaID,
                    eContactVillageID: this.urgentArr[3] ? this.urgentArr[3].value : this.cardDetail.eContactVillageID,
                    eContactAddress: this.eContactAddress,
                }]
            }
            $http.post("urlS", this, {
                code: "01030005",
                data: data,
                success: function (res) {
                    if (+res.errorCode === 0) {
                        this.showToast(res.errorMessage, 'success');
                        if (this.url != "") {
                            uni.navigateTo({
                                url: this.url
                            })
                        } else {
                            uni.redirectTo({
                                url: '../addAVisitor/addAVisitor'
                            })
                        }
                        this.setData({
                            saveState: false
                        })
                    } else {
                        this.setData({
                            saveState: false
                        })
                        this.showToast(res.errorMessage, 'error');
                    }
                }
            })
        },

        /**删除卡
         * **/
        deleteCard: function () {
            let userData = this.$cache.get('userData');
            Dialog.confirm({
                context: this,
                title: '确认删除该就诊人吗???',
                message: "删除就诊人后将无法查看与该就诊人相关的消息、问诊及诊疗记录。请谨慎操作哦。"
            })
                .then(() => {
                    this.setData({
                        delState: true
                    });
                    let data = {
                        params: [{
                            patID: this.patID
                        }]
                    }
                    $http.post("urlS", this, {
                        code: "01030006",
                        data: data,
                        success: function (res) {
                            if (+res.errorCode === 0) {
                                app.globalData.isUpdatePatList = true;
                                this.showToast(res.errorMessage, 'success');
                                if (userData && userData.patID === this.patID) {
                                    this.signOut();
                                }
                                if (this.url != "") {
                                    uni.navigateTo({
                                        url: this.url
                                    })
                                } else {
                                    uni.redirectTo({
                                        url: '../addAVisitor/addAVisitor'
                                    })
                                }
                                this.setData({
                                    delState: false
                                })
                            } else {
                                this.setData({
                                    delState: false
                                })
                                this.showToast(res.errorMessage, 'error');
                            }
                        }
                    });
                })
                .catch(() => {
                    // on cancel
                });
        },

        // 退出
        signOut: function () {
            try {
                let promise = app.globalData.tim.logout();
                promise.then((imResponse) => {
                    app.globalData.isLogOut = false;
                    console.log("退出登录成功：", imResponse)
                }).catch(function (imError) {
                    console.log('退出失败');
                });
            } catch (err) {
                console.log("退出失败")
            }
        },

        // 现住地详细地址
        onChangeAddress: function (e) {
            this.setData({
                address: e.detail.value
            })
        },

        // 现居地详细地址
        onChangeRegAddress: function (e) {
            this.setData({
                regAddress: e.detail.value
            })
        },

        // 联系人详细地址
        onChangeEContactAddress: function (e) {
            this.setData({
                eContactAddress: e.detail.value
            })
        },

        onChangecompanyData: function (e) {
            if (this.jobsVal.text == '无业人员') {
                this.setData({
                    companyData: ''
                })
            } else {
                this.setData({
                    companyData: e.detail.value
                })
            }
        },

        onBlurcompanyData: function (e) {
            if (this.jobsVal.text == '无业人员') {
                this.setData({
                    companyData: ''
                })
            } else {
                this.setData({
                    companyData: e.detail.value
                })
            }
        },

        onChangeCompanyTel: function (e) {
            if (this.jobsVal.text == '无业人员') {
                this.setData({
                    companyTelText: ''
                })
            } else {
                this.setData({
                    companyTelText: e.detail.value
                })
            }
        },

        onBlurCompanyTel: function (e) {
            if (this.jobsVal.text == '无业人员') {
                this.setData({
                    companyTelText: ''
                })
            } else {
                this.setData({
                    companyTelText: e.detail.value
                })
            }
        },

        confirmDate: function () {
            var d = new Date(this.currentDate);
            var date = (d.getFullYear()) + "-" + (d.getMonth() + 1) + "-" + (d.getDate());
            this.setData({
                selectDate: date
            })
        },

        // 民族下拉
        ongetNationData: function (e) {
            this.setData({
                nationValue: this.nationList[e.detail.value]
            });
        },

        // 国籍下拉
        ongetCitizenshipData: function (e) {
            this.setData({
                citizenshipVal: this.citizenshipList[e.detail.value] 
            });
        },

        // 性别下拉
        onSelectSex: function (e) {
            this.setData({
                genderVal: this.sexList[e.detail.value]
            });
        },

        // 紧急联系人证件类型
        onEContactCredType: function (e) {
            this.setData({
                EContactCredType: this.cardTypeList[e.detail.value]
            });
        },

        // 与紧急联系人关系
        onRelation: function (e) {
            this.setData({
                relation: this.relationList[e.detail.value]
            });
        },

        // 紧急联系人姓名
        onEContact: function (e) {
            this.setData({
                EContact: e.detail.value
            })
        },

        // 紧急联系人证件号
        onEContactIDNo: function (e) {
            if (this.EContactCredType.label == '居民身份证') {
                if (e.detail) {
                    this.idCardcode(e.detail.value);
                } else {
                    this.setData({
                        EContactIDNo: ''
                    });
                }

            } else {
                this.setData({
                    EContactIDNo: e.detail.value
                });
            }

        },

        // 紧急联系人手机号
        onEContactMobile: function (e) {
            this.setData({
                EContactMobile: e.detail.value
            })
        },

        //婚否
        ongetMarry: function (e) {
            this.setData({
                marryVal: this.maritalList[e.detail.value]
            });
        },

        //职业
        ongetJobs: function (e) {
            const self = this
            const jobsVal = self.jobsList[e.detail.value]
            self.setData({
                jobsVal
            },() => {
                if (jobsVal && jobsVal.label == '无业人员') {
                    self.setData({
                        companyTelText: '',
                        companyData: ''
                    })
                }
            });
        },

        // 就诊人用户名
        onChangePatName: function (e) {
            this.setData({
                patName: e.detail.value
            });
        },
        
        // 就诊人手机号
        onChangeMobile: function (e) {
            this.setData({
                mobile: e.detail.value
            });
        },
    }
}
</script>

<style lang="scss">
@use '../../common/style/card.scss' as *;

.addr { 
    position: relative;
    z-index: 999;
}
.row input:disabled {
    color: #666;
}
.cardDetail {
    padding: 15rpx;
    padding-bottom: 100rpx;
}

.cardDetail .van-cell {
    /* justify-content: space-between; */
    position: relative;
}

.cardDetail .van-cell .van-cell__title {
    flex: none;
    width: 200rpx;
    max-width: initial !important;
    min-width: initial !important;
}

.cardDetail .van-cell .van-cell__value {
    flex: none;
    width: 67%;
    text-align: left;
    color: #333;
    display: block;
}

/* .cardDetail .van-cell .van-cell__right-icon-wrap {
  width: 
} */
.selectL {
    margin-top: 20rpx;
}

.cardDetail .button {
    width: 100%;
    text-align: center;
    position: fixed;
    z-index: 10;
    bottom: 0rpx;
    left: 0rpx;
}

.cardDetail .button button {
    width: 100%;
    height: 100rpx;
    font-size: 30rpx;
}

.cardDetail .child {
    margin-top: 20rpx;
    padding-bottom: 100rpx;
}

/* .cardDetail .selectL .nationValue .van-popup--safe{
  height: 70%;
} */
.selectL .van-cell__value {
    color: #666;
}

.child .van-cell__value {
    color: #666;
}

.cardDetail .field-index--van-field {
    z-index: 0;
}

.cardDetail .nationValue {
    position: fixed;
    bottom: 15%;
    width: 100%;
    height: 530rpx;
    z-index: 3;
    left: 0rpx;
}

.cardDetail .time {
    position: fixed;
    z-index: 3;
    width: 100%;
    bottom: 0rpx;
    left: 0rpx;
}

.cardDetail .sexPopup {
    position: fixed;
    z-index: 3;
    width: 100%;
    bottom: 0rpx;
    left: 0rpx;
}

.cardDetail .F5222D button {
    color: #F5222D;
}

.cardDetail .popup {
    left: 0 !important;
}

.cardDetail .van-picker-column__item--selected {
    font-weight: 700;
    background-color: #f9f9f9;
}</style>