<!-- 外国嘉宾 -->
<template>
    <div>
        <van-nav-bar fixed :border="false">
            <template #left>
                <van-icon name="arrow-left" color="#3D3D3D" @click="onClickLeft" />
            </template>
            <template #title>
                <p class="navTitle" v-text="lang[langType].signUp"></p>
            </template>
        </van-nav-bar>
        <div class="form">
            <van-form ref="form" @submit="onSubmit" label-width="8em">
                <!-- 国家/地区 -->
                <van-field size="large" input-align="right" clickable readonly
                    @click="countryRegionList = lang[langType].countryRegionList, showCountryRegion = true"
                    right-icon="arrow"
                    :rules="[{ required: true, trigger: 'onBlur', message: lang[langType].countryRegionContent }]"
                    required v-model="formData.countryRegionName" :placeholder="lang[langType].countryRegionContent">
                    <template #label>
                        <span :class="langType === 'zh' ? 'label' : ''" v-html="lang[langType].countryRegion"></span>
                    </template>
                </van-field>
                <van-popup v-model:show="showCountryRegion" destroy-on-close round position="bottom">
                    <van-picker :confirm-button-text="lang[langType].confirm" :columns="countryRegionList"
                        :cancel-button-text="lang[langType].cancel" @confirm="countryRegionConfirm"
                        :title="lang[langType].countryRegion" @cancel="showCountryRegion = false">
                        <template #columns-top>
                            <van-search @update:model-value="input" v-model="searchConuntry" :placeholder="lang[langType].searchText" />
                        </template>
                    </van-picker>
                </van-popup>

                <!-- 姓 -->
                <van-field size="large" input-align="right"
                    :rules="[{ required: true, trigger: 'onBlur', message: lang[langType].lastNameContent }]" required
                    v-model="formData.lastName" :placeholder="lang[langType].lastNameContent">
                    <template #label>
                        <span :class="langType === 'zh' ? 'label' : ''" v-html="lang[langType].lastName"></span>
                    </template>
                </van-field>

                <!-- 名 -->
                <van-field size="large" input-align="right"
                    :rules="[{ required: true, trigger: 'onBlur', message: lang[langType].firstNameContent }]" required
                    v-model="formData.firstName" :placeholder="lang[langType].firstNameContent">
                    <template #label>
                        <span :class="langType === 'zh' ? 'label' : ''" v-html="lang[langType].firstName"></span>
                    </template>
                </van-field>

                <!-- 性别 -->
                <van-field size="large" input-align="right"
                    :rules="[{ required: true, trigger: 'onBlur', message: lang[langType].genderContent }]"
                    v-model="formData.gender" required :placeholder="lang[langType].genderContent">
                    <template #label>
                        <span :class="langType === 'zh' ? 'label' : ''" v-html="lang[langType].gender"></span>
                    </template>
                </van-field>

                <!-- 生日 -->
                <!-- <van-field size="large" input-align="right"
                    :rules="[{ required: true, trigger: 'onBlur', message: lang[langType].birthdayContent }]" clickable
                    right-icon="calendar-o" readonly @click="showBirthday = true" v-model="formData.birthday" required
                    :placeholder="lang[langType].birthdayContent">
                    <template #label>
                        <span :class="langType === 'zh' ? 'label' : ''" v-html="lang[langType].birthday"></span>
                    </template>
                </van-field> -->
                <van-popup v-model:show="showBirthday" destroy-on-close round position="bottom">
                    <van-date-picker :confirm-button-text="lang[langType].confirm"
                        :cancel-button-text="lang[langType].cancel" @confirm="packerConfirm"
                        :title="lang[langType].birthday" @cancel="showBirthday = false" :min-date="new Date(1920, 0, 1)"
                        :max-date="new Date()" />
                </van-popup>

                <!-- 照片 -->
                <van-field size="large" input-align="right" :placeholder="lang[langType].photoContent" required
                    :rules="[{ required: true, trigger: 'onChange', message: lang[langType].photoContent }]"
                    v-model="formData.photoUrl">
                    <template #label>
                        <span :class="langType === 'zh' ? 'label' : ''" v-html="lang[langType].photo"></span>
                    </template>
                    <template #input>
                        <van-uploader :preview-full-image="false" accept="image/*" v-model="photo"
                            :before-delete="beforeDelete" :max-count="1" :max-size="524288000" @oversize="filesize"
                            :after-read="beforeRead" result-type="file">
                        </van-uploader>
                        <p class="upFileSubtitle" v-text="lang[langType].photoSubContent"> </p>
                    </template>
                </van-field>

                <!-- 手机号 -->
                <van-field size="large" input-align="right"
                    :rules="[{ required: true, trigger: 'onBlur', message: lang[langType].contactContent }]"
                    v-model="formData.contact" type="number" required :placeholder="lang[langType].contactContent">
                    <template #label>
                        <span :class="langType === 'zh' ? 'label' : ''" v-html="lang[langType].contact"></span>
                    </template>
                </van-field>

                <!-- 邮箱 -->
                <van-field size="large" input-align="right"
                    :rules="[{ required: true, trigger: 'onBlur', message: lang[langType].emailContent }]" required
                    v-model="formData.email" :placeholder="lang[langType].emailContent">
                    <template #label>
                        <span :class="langType === 'zh' ? 'label' : ''" v-html="lang[langType].email"></span>
                    </template>
                </van-field>

                <!-- 紧急联系人 -->
                <van-field size="large" input-align="right" v-model="formData.emergencyContact"
                    :rules="[{ required: true, trigger: 'onBlur', message: lang[langType].emergencyContactContent }]"
                    required :placeholder="lang[langType].emergencyContactContent">
                    <template #label>
                        <span :class="langType === 'zh' ? 'label' : ''" v-html="lang[langType].emergencyContact"></span>
                    </template>
                </van-field>

                <!-- 紧急联系人电话 -->
                <van-field size="large" input-align="right" v-model="formData.emergencyContactPhone" required
                    type="number"
                    :rules="[{ required: true, trigger: 'onBlur', message: lang[langType].emergencyContactPhoneContent }]"
                    :placeholder="lang[langType].emergencyContactPhoneContent">
                    <template #label>
                        <span :class="langType === 'zh' ? 'label' : ''"
                            v-html="lang[langType].emergencyContactPhone"></span>
                    </template>
                </van-field>

                <!-- 证件类型 -->
                <van-field size="large" label-align="top" v-model="formData.cardType" clickable
                    :rules="[{ required: true, trigger: 'onChange', message: lang[langType].conferenceTypeContent }]"
                    :placeholder="lang[langType].conferenceTypeContent" required>
                    <template #label>
                        <span :class="langType === 'zh' ? 'label' : ''" v-html="lang[langType].conferenceType"></span>
                    </template>
                    <template #input>
                        <van-radio-group checked-color="#3F5AC8" style="width: 100%;" v-model="formData.cardType"
                            :direction="langType == 'zh' ? 'horizontal' : 'vertical'">
                            <template v-for="item in cardTypeList" :key="item.value">
                                <van-radio v-if="item.value !== '身份证'"
                                    :style="langType == 'zh' ? 'width: 50%;margin: 10px 0 0 0;' : 'margin: 10px 0 0 0;'"
                                    :name="item.value">{{ item.text
                                    }}</van-radio>
                            </template>
                        </van-radio-group>
                    </template>
                </van-field>

                <!-- 证件 -->
                <van-field size="large" input-align="right"
                    :rules="[{ required: true, trigger: 'onBlur', message: lang[langType].passportContent }]"
                    v-model="formData.passport" required :placeholder="lang[langType].passportContent">
                    <template #label>
                        <span :class="langType === 'zh' ? 'label' : ''" v-html="lang[langType].passport"></span>
                    </template>
                </van-field>

                <!-- 宗教 -->
                <!-- <van-field size="large" input-align="right"
                    :rules="[{ required: true, trigger: 'onBlur', message: lang[langType].beliefContent }]"
                    v-model="formData.belief" required :placeholder="lang[langType].beliefContent">
                    <template #label>
                        <span :class="langType === 'zh' ? 'label' : ''" v-html="lang[langType].belief"></span>
                    </template>
                </van-field> -->

                <!-- 饮食要求 -->
                <van-field size="large" input-align="right" v-model="formData.catering" rows="1" autosize
                    :rules="[{ required: true, trigger: 'onBlur', message: lang[langType].cateringContent }]"
                    type="textarea" required :placeholder="lang[langType].cateringContent">
                    <template #label>
                        <span :class="langType === 'zh' ? 'label' : ''" v-html="lang[langType].catering"></span>
                    </template>
                </van-field>

                <div style="background-color: #F7F7F7;height: 10px;"></div>

                <!-- 工作单位 -->
                <van-field size="large" input-align="right" v-model="formData.workUnit"
                    :rules="[{ required: true, trigger: 'onBlur', message: lang[langType].companyContent }]" required
                    :placeholder="lang[langType].companyContent">
                    <template #label>
                        <span :class="langType === 'zh' ? 'label' : ''" v-html="lang[langType].company"></span>
                    </template>
                </van-field>

                <!-- 单位类型 -->
                <van-field size="large" input-align="right" v-model="formData.unitTypeName" clickable
                    :rules="[{ required: true, trigger: 'onChange', message: lang[langType].unitTypeContent }]"
                    right-icon="calendar-o" readonly :placeholder="lang[langType].unitTypeContent"
                    @click="showUnitType = true" required>
                    <template #label>
                        <span :class="langType === 'zh' ? 'label' : ''" v-html="lang[langType].unitType"></span>
                    </template>
                </van-field>
                <van-popup v-model:show="showUnitType" destroy-on-close round position="bottom">
                    <van-picker :confirm-button-text="lang[langType].confirm"
                        :cancel-button-text="lang[langType].cancel" :columns="unitTypeList"
                        @cancel="showUnitType = false" @confirm="onUnitTypeConfirm" />
                </van-popup>

                <!-- 职务 -->
                <van-field size="large" input-align="right" v-model="formData.career"
                    :rules="[{ required: true, trigger: 'onBlur', message: lang[langType].careerContent }]" required
                    :placeholder="lang[langType].careerContent">
                    <template #label>
                        <span :class="langType === 'zh' ? 'label' : ''" v-html="lang[langType].career"></span>
                    </template>
                </van-field>

                <!-- 职级 -->
                <van-field size="large" label-align="top" input-align="right" v-model="formData.level"
                    :rules="[{ required: true, trigger: 'onBlur', message: lang[langType].levelContent }]" required
                    :placeholder="lang[langType].levelContent">
                    <template #label>
                        <span :style="langType === 'zh' ? 'text-align-last: justify;' : ''"
                            :class="langType === 'zh' ? 'label' : ''" v-html="lang[langType].level"></span>
                    </template>
                    <template #input>
                        <van-radio-group checked-color="#3F5AC8" style="width: 100%;" v-model="formData.level"
                            :direction="langType == 'zh' ? 'horizontal' : 'vertical'">
                            <van-radio style="flex: 1;margin-top: 10px " :name="item.value"
                                v-for="item in lang[langType].careerList" :key="item.value">{{
                                    item.text
                                }}</van-radio>
                        </van-radio-group>
                    </template>
                </van-field>

                <!-- 传真 -->
                <van-field size="large" input-align="right" v-model="formData.fax"
                    :placeholder="lang[langType].faxContent">
                    <template #label>
                        <span :class="langType === 'zh' ? 'label' : ''" v-html="lang[langType].fax"></span>
                    </template>
                </van-field>

                <!-- 办公地址 -->
                <van-field size="large" input-align="right" v-model="formData.officeAddress" rows="1" autosize
                    type="textarea" :placeholder="lang[langType].officeAddressContent">
                    <template #label>
                        <span :class="langType === 'zh' ? 'label' : ''" v-html="lang[langType].officeAddress"></span>
                    </template>
                </van-field>

                <div style="background-color: #F7F7F7;height: 10px;"></div>
                <!-- 接受采访意向  -->
                <van-field size="large" v-model="formData.interview" required label-align="top"
                    :rules="[{ required: true, trigger: 'onBlur', message: lang[langType].interviewContent }]"
                    :placeholder="lang[langType].interviewContent">
                    <template #label>
                        <span :class="langType === 'zh' ? 'label' : ''" v-html="lang[langType].interview"></span>
                    </template>
                    <template #input>
                        <van-radio-group checked-color="#3F5AC8" style="justify-content: flex-end;width: 100%;"
                            v-model="formData.interview" direction="horizontal">
                            <van-radio style="flex: 1;margin-top: 10px " name="是">{{ lang[langType].confirm
                            }}</van-radio>
                            <van-radio style="flex: 1;margin-top: 10px " name="否">{{ lang[langType].cancel
                            }}</van-radio>
                        </van-radio-group>
                    </template>
                </van-field>

                <div style="background-color: #F7F7F7;height: 10px;"></div>

                <!-- 是否参加开幕式 active0  -->
                <van-field size="large" label-align="top" v-model="formData.active1" required
                    :rules="[{ required: true, trigger: 'onBlur', message: lang[langType].active0Content }]"
                    :placeholder="lang[langType].active0Content">
                    <template #label>
                        <span :class="langType === 'zh' ? 'label' : ''" v-html="lang[langType].active0"></span>
                    </template>
                    <template #input>
                        <van-radio-group checked-color="#3F5AC8" style="width: 100%;" v-model="formData.active1"
                            direction="horizontal">
                            <van-radio style="flex: 1;margin-top: 10px " name="是">{{ lang[langType].confirm
                                }}</van-radio>
                            <van-radio style="flex: 1;margin-top: 10px " name="否">{{ lang[langType].cancel
                                }}</van-radio>
                        </van-radio-group>
                    </template>
                </van-field>

                <!-- 选择分论坛  -->
                <van-field size="large" label-align="top" required
                    :rules="[{ required: true, trigger: 'onBlur', message: lang[langType].activeContent }]"
                    :placeholder="lang[langType].activeContent">
                    <template #label>
                        <span :class="langType === 'zh' ? 'label' : ''" v-html="lang[langType].active"></span>
                    </template>
                    <template #input>
                        <van-checkbox-group checked-color="#3F5AC8" style="width: 100%;padding-left: 16px;"
                            v-model="formData.active2">
                            <van-checkbox style="flex: 1;margin-top: 10px " name="共享数字经济发展机遇" shape="square">
                                {{ lang[langType].active1 }}
                            </van-checkbox>
                            <van-checkbox style="flex: 1;margin-top: 10px " name="共筑网络安全防线" shape="square">
                                {{ lang[langType].active2 }}
                            </van-checkbox>
                            <van-checkbox style="flex: 1;margin-top: 10px " name="共商人工智能发展治理" shape="square">
                                {{ lang[langType].active3 }}
                            </van-checkbox>
                        </van-checkbox-group>
                    </template>
                </van-field>

                <!-- 拟参加的活动  -->
                <van-field size="large" label-align="top" v-model="formData.activitiesAttended" required
                    :rules="[{ required: true, trigger: 'onBlur', message: lang[langType].activitiesAttendedContent }]"
                    :placeholder="lang[langType].activitiesAttendedContent">
                    <template #label>
                        <span :class="langType === 'zh' ? 'label' : ''"
                            v-html="lang[langType].activitiesAttended"></span>
                    </template>
                    <template #input>
                        <div>
                            <van-checkbox-group :border="false" ref="checkbox" checked-color="#3F5AC8"
                                v-model="activitiesAttended" @change="isGroup">
                                <van-cell clickable :title="lang[langType].active4" @click="checkboxRefs[0].toggle()">
                                    <template #icon>
                                        <van-checkbox :ref="el => checkboxRefs[0] = el" style="margin-right: 5px;"
                                            name="active4" @click.stop shape="square" />
                                    </template>
                                </van-cell>
                                <van-cell clickable :title="lang[langType].active5" @click="checkboxRefs[1].toggle()">
                                    <template #icon>
                                        <van-checkbox :ref="el => checkboxRefs[1] = el" style="margin-right: 5px;"
                                            name="active5" @click.stop shape="square" />
                                    </template>
                                </van-cell>
                                <van-cell clickable :title="lang[langType].active6" @click="checkboxRefs[2].toggle()">
                                    <template #icon>
                                        <van-checkbox :ref="el => checkboxRefs[2] = el" style="margin-right: 5px;"
                                            name="active6" @click.stop shape="square" />
                                    </template>
                                </van-cell>
                            </van-checkbox-group>
                            <van-cell clickable :title="lang[langType].active7" @click="selectunde = !selectunde">
                                <template #icon>
                                    <van-checkbox checked-color="#3F5AC8" @change="isUndefined" v-model="selectunde"
                                        style="margin-right: 5px;" name="undefined" @click.stop shape="square" />
                                </template>
                            </van-cell>
                        </div>

                    </template>
                </van-field>

                <div style="background-color: #F7F7F7;height: 10px;"></div>

                <!--  抵达西安交通车次 -->
                <van-field size="large" input-align="right" v-model="formData.reachTrainNumber"
                    :placeholder="lang[langType].reachTrainNumberContent">
                    <template #label>
                        <span :class="langType === 'zh' ? 'label' : ''" v-html="lang[langType].reachTrainNumber"></span>
                    </template>
                </van-field>

                <!--  抵达西安时间 -->
                <van-field size="large" input-align="right" v-model="formData.reachTime" clickable
                    right-icon="calendar-o" readonly @click="showReachTime = true"
                    :placeholder="lang[langType].reachTimeContent">
                    <template #label>
                        <span :class="langType === 'zh' ? 'label' : ''" v-html="lang[langType].reachTime"></span>
                    </template>
                </van-field>
                <van-popup v-model:show="showReachTime" destroy-on-close round position="bottom">
                    <van-picker :confirm-button-text="lang[langType].confirm"
                        :cancel-button-text="lang[langType].cancel" :title="lang[langType].reachTime"
                        :columns="dateTimeColumns" @confirm="reachTimeConfirm" @cancel="showReachTime = false" />
                </van-popup>

                <!--  离开车次 -->
                <van-field size="large" input-align="right" v-model="formData.leavingTrainNumber"
                    :placeholder="lang[langType].leavingTrainNumberContent">
                    <template #label>
                        <span :class="langType === 'zh' ? 'label' : ''"
                            v-html="lang[langType].leavingTrainNumber"></span>
                    </template>
                </van-field>

                <!--  离开时间 -->
                <van-field size="large" input-align="right" v-model="formData.leavingTime" clickable
                    right-icon="calendar-o" readonly @click="showLeavingTime = true"
                    :placeholder="lang[langType].leavingTimeContent">
                    <template #label>
                        <span :class="langType === 'zh' ? 'label' : ''" v-html="lang[langType].leavingTime"></span>
                    </template>
                </van-field>
                <van-popup v-model:show="showLeavingTime" destroy-on-close round position="bottom">
                    <van-picker :confirm-button-text="lang[langType].confirm"
                        :cancel-button-text="lang[langType].cancel" :title="lang[langType].leavingTime"
                        :columns="dateTimeColumns" @confirm="leavingTimeConfirm" @cancel="showLeavingTime = false" />
                </van-popup>

                <!--  离店时间 -->
                <van-field size="large" input-align="right" v-model="formData.checkOutTime" clickable
                    right-icon="calendar-o" readonly @click="showCheckOutTime = true"
                    :placeholder="lang[langType].checkOutTimeContent">
                    <template #label>
                        <span :class="langType === 'zh' ? 'label' : ''" v-html="lang[langType].checkOutTime"></span>
                    </template>
                </van-field>
                <van-popup v-model:show="showCheckOutTime" destroy-on-close round position="bottom">
                    <van-picker :confirm-button-text="lang[langType].confirm"
                        :cancel-button-text="lang[langType].cancel" :title="lang[langType].checkOutTime"
                        :columns="dateTimeColumns" @confirm="checkOutTimeConfirm" @cancel="showCheckOutTime = false" />
                </van-popup>

                <van-field size="large" input-align="right" v-model="formData.message" rows="3" autosize type="textarea"
                    :placeholder="lang[langType].ortherNotesContent">
                    <template #label>
                        <span :class="langType === 'zh' ? 'label' : ''" v-html="lang[langType].ortherNotes"></span>
                    </template>
                </van-field>
                <br />
                <van-button
                    style="background: linear-gradient(270deg, #1D19BC 0%, #1C4CD5 100%);margin: 0 auto;width: calc(100% - 40px);"
                    block type="primary" native-type="submit">
                    {{ lang[langType].submit }}
                </van-button>
            </van-form>
        </div>
    </div>
</template>
<script setup>
import { defineProps, defineEmits, defineExpose, reactive, ref, computed } from 'vue';
import { showToast, showDialog } from 'vant';
import { editData, uploadImage } from '@/api/2025.js';
import lang from './lang.js';
const props = defineProps(['langType', 'verificationCode', 'subData']);

const emits = defineEmits(['closeComponent']);

const formData = reactive({
    active2: []
})
const cardTypeList = computed(() => lang[props.langType].cardTypeList)
const unitTypeList = computed(() => lang[props.langType].foreignCountryUnitType)
const form = ref(null);
const showBirthday = ref(false)
const showReachTime = ref(false)
const showLeavingTime = ref(false)
const showCheckOutTime = ref(false)
const showUnitType = ref(false)
const selectunde = ref(false)
const showCountryRegion = ref(false)
const activitiesAttended = ref([])
const photo = ref([]);
const checkboxRefs = ref([]);
const checkbox = ref(null);
const searchConuntry = ref('')
const countryRegionList = ref([]);

let t
const input = (val) => {
    if (t) clearTimeout(t);
    t = setTimeout(() => {
        countryRegionList.value = lang[props.langType].countryRegionList.filter(item => {
            // 判断是否包含搜索的关键字，中英西班牙
            if (props.langType === 'zh') return item.text.includes(val)
            else return item.text.toLowerCase().includes(val.toLowerCase())
        })
        clearTimeout(t);
    }, 200);
}

const onClickLeft = () => {
    emits('closeComponent');
};

const isGroup = (val) => {
    if (val.length > 0) {
        formData.activitiesAttended = val.join(',')
        selectunde.value = false
    }
}
const isUndefined = (val) => {
    if (val) {
        activitiesAttended.value = []
        formData.activitiesAttended = 'undefined'
    }
}
/**
 * 将subData数据转换为formData
 */
const initFormData = () => {
    if (!props.subData) return;
    const relationship = {
        lastName: 'lastName', // 姓
        firstName: 'firstName', // 名
        countryRegion: 'countryRegion', // 国家/地区
        religion: 'belief', // 宗教
        level: 'level', // 职级
        gender: 'gender', // 性别
        birthDate: 'birthday', // 出生日期
        photo: 'photoUrl', // 照片
        phone: 'contact', // 手机号
        email: 'email', // 邮箱
        emergencyContact: 'emergencyContact', // 紧急联系人
        emergencyContactPhone: 'emergencyContactPhone', // 紧急联系人电话
        cardType: 'cardType', // 证件类型
        idCard: 'passport', // 证件号
        dietaryRequirements: 'catering', // 饮食要求
        workUnit: 'workUnit', // 工作单位
        unitType: 'unitType', // 单位类型
        position: 'career', // 职务
        fax: 'fax', // fax
        interviewIntention: 'interview', // 接受采访意向
        officeAddress: 'officeAddress', // 办公地址
        arrivalNumberXian: 'reachTrainNumber', // 抵达西安交通车次
        arrivalTimeXian: 'reachTime', // 抵达西安时间
        departureNumber: 'leavingTrainNumber', // 离开车次
        departureTime: 'leavingTime', // 离开时间
        checkOutTime: 'checkOutTime', // 离店时间
        remarks: 'message', // 备注
        active1: 'active1', // 共筑网络安全防线
        active2: 'active2', // 共商人工智能发展治理
        active3: 'active3', // 共筑网络安全防线
    }
    const subData = JSON.parse(JSON.stringify(props.subData))
    const keys = Object.keys(subData || {});
    subData.active4 === '是' && activitiesAttended.value.push('active4');
    subData.active5 === '是' && activitiesAttended.value.push('active5');
    subData.active6 === '是' && activitiesAttended.value.push('active6');
    if (!formData.active2) {
        formData.active2 = []
    }
    for (let i = 0; i < keys.length; i++) {
        const key = keys[i];
        if (key === 'photo') {
            photo.value.push({
                url: subData[key],
            })
        }
        if (key === 'unitType') {
            try {
                formData.unitTypeName = lang[props.langType].foreignCountryUnitType.filter(item => item.value === subData[key])[0].text;
            } catch (err) {
                formData.unitTypeName = '';
            }
        }
        if (key === 'active7') {
            formData.active2.push('共享数字经济发展机遇')
            continue
        }
        if (key === 'active2') {
            formData.active2.push('共筑网络安全防线')
            continue
        }
        if (key === 'active3') {
            formData.active2.push('共商人工智能发展治理')
            continue
        }
        formData[relationship[key]] = subData[key];
    }
}
initFormData()
const reset = () => {
    form.value.resetValidation();
}
const createTextDate = values => values.slice(0, 3).join('-') + ' ' + values.slice(3).join(':');
const packerConfirm = (values) => {
    showBirthday.value = false;
    formData.birthday = createTextDate(values.selectedValues);
}
const leavingTimeConfirm = ({ selectedValues }) => {
    showLeavingTime.value = false;
    formData.leavingTime = createTextDate(selectedValues);
}
const reachTimeConfirm = ({ selectedValues }) => {
    showReachTime.value = false;
    formData.reachTime = createTextDate(selectedValues);
}
const checkOutTimeConfirm = ({ selectedValues }) => {
    showCheckOutTime.value = false;
    formData.checkOutTime = createTextDate(selectedValues);
}
const onUnitTypeConfirm = (values) => {
    showUnitType.value = false;
    formData.unitType = values.selectedOptions[0].value;
    formData.unitTypeName = values.selectedOptions[0].text;
}
const countryRegionConfirm = (values) => {
    showCountryRegion.value = false;
    formData.countryRegionName = values.selectedOptions[0].text;
    formData.countryRegion = values.selectedOptions[0].value;
}

// 上传
const beforeDelete = (index) => {
    photo.value.splice(index, 1);
    formData.photoUrl = '';
    return false;
}
const filesize = () => showToast(lang[props.langType].fileSize);
const beforeRead = (file) => {
    if (!file) showToast(lang[props.langType].photoContent);
    uploadImage(file).then(res => {
        showToast(lang[props.langType].upFileSusscess);
        formData.photoUrl = res
        photo.value = [
            {
                url: res,
                file: file.file,
            }
        ]
    }).catch((res) => {
        showToast(res.msg || lang[props.langType].upFileError);
    })
    return false;
};


let arrivalTime = []
const dateTimeColumns = computed(() => {
    const dateList = lang[props.langType].dateList;
    const years = [{ text: dateList[0], value: '', disabled: true }, ...Array.from({ length: 10 }, (_, i) => {
        const t = 2020 + i + ''
        return {
            text: t,
            value: t
        }
    })];
    const months = [{ text: dateList[1], value: '', disabled: true }, ...Array.from({ length: 12 }, (_, i) => {
        const m = (i + 1).toString().padStart(2, '0')
        return {
            text: m,
            value: m
        }
    })];

    // 动态计算当前选中年月对应的天数
    const daysInMonth = new Date(
        parseInt(arrivalTime[0] || '0'),
        parseInt(arrivalTime[1] || '0'),
        0
    ).getDate();
    const days = [{ text: dateList[2], value: '', disabled: true }, ...Array.from({ length: daysInMonth }, (_, i) => {
        const d = (i + 1).toString().padStart(2, '0')
        return {
            text: d,
            value: d
        }
    })];

    const hours = [{ text: dateList[3], value: '', disabled: true }, ...Array.from({ length: 24 }, (_, i) => {
        const h = i.toString().padStart(2, '0')
        return {
            text: h,
            value: h
        }
    })];
    const minutes = [{ text: dateList[4], value: '', disabled: true }, ...Array.from({ length: 60 }, (_, i) => {
        const m = i.toString().padStart(2, '0')
        return {
            text: m,
            value: m
        }
    })];
    return [years, months, days, hours, minutes];
});


let loading = false
const onSubmit = () => {
    if (loading) return
    loading = true
    const active = {
        active2: '否',
        active3: '否',
        active4: '否',
        active5: '否',
        active6: '否',
        active7: '否'
    }
    if (!selectunde.value) {
        activitiesAttended.value.forEach((item) => {
            active[item] = '是'
        })
    }
    if (formData.active2.includes('共享数字经济发展机遇')) {
        active.active7 = '是'
    }
    if (formData.active2.includes('共筑网络安全防线')) {
        active.active2 = '是'
    }
    if (formData.active2.includes('共商人工智能发展治理')) {
        active.active3 = '是'
    }
    editData({
        ...active,
        active1: formData.active1 || '否',
        id: props.subData.id, // 人员id
        code: props.verificationCode, // 邀请码
        lastName: formData.lastName, // 姓
        firstName: formData.firstName, // 名
        gender: formData.gender, // 性别
        birthDate: formData.birthday, // 出生日期
        photo: formData.photoUrl, // 照片
        phone: formData.contact, // 手机号
        countryRegion: formData.countryRegion, // 国家/地区
        email: formData.email, // 邮箱
        religion: formData.belief, // 宗教
        emergencyContact: formData.emergencyContact, // 紧急联系人
        emergencyContactPhone: formData.emergencyContactPhone, // 紧急联系人电话
        cardType: formData.cardType, // 证件类型
        idCard: formData.passport, // 证件号
        dietaryRequirements: formData.catering, // 饮食要求
        workUnit: formData.workUnit, // 工作单位
        unitType: formData.unitType, // 单位类型
        position: formData.career, // 职务
        level: formData.level, // 职级
        fax: formData.fax, // fax
        interviewIntention: formData.interview, // 接受采访意向
        officeAddress: formData.officeAddress, // 办公地址
        arrivalNumberXian: formData.reachTrainNumber, // 抵达西安交通车次
        arrivalTimeXian: formData.reachTime, // 抵达西安时间
        departureNumber: formData.leavingTrainNumber, // 离开车次
        departureTime: formData.leavingTime, // 离开时间
        checkOutTime: formData.checkOutTime, // 离店时间
        remarks: formData.message, // 备注
        userType: '国外嘉宾'
    }).then((res) => {
        if (!isNaN(res.code) && res.code !== 0) {
            showToast(res.msg || lang[props.langType].networkError);
            return;
        }
        // 清空表单内容 from
        // Object.keys(from).forEach((key) => {
        //     from[key] = '';
        // });
        loading = false
        showDialog({
            message: lang[props.langType].upRegistration,
        })
    }).catch((res) => {
        loading = false
        showToast(res.msg || lang[props.langType].networkError);
    });
}
defineExpose({ reset })
</script>
<style lang="scss"></style>