<template>
    <AppLayout title="AI+就业服务程序">
        <view class="tab-container">
            <view class="uni-margin-wrap">
                <swiper
                    class="swiper"
                    :current="tabCurrent"
                    :circular="false"
                    :indicator-dots="false"
                    :autoplay="false"
                    :duration="500"
                >
                    <swiper-item @touchmove.stop="false">
                        <view class="login-content">
                            <image class="logo" src="@/static/logo.png"></image>
                            <view class="logo-title">就业</view>
                        </view>
                        <view class="btns">
                            <button class="wxlogin" @click="loginTest">内测登录</button>
                            <view class="wxaddress">{{ config.appInfo.areaName }}公共就业和人才服务中心</view>
                        </view>
                    </swiper-item>
                <swiper-item @touchmove.stop="false">
                <view class="content-one">
                    <view>
                        <view class="content-title">
                            <view class="title-lf">
                                <view class="lf-h1">请您完善求职名片</view>
                                <view class="lf-text">个人信息仅用于推送优质内容</view>
                            </view>
                            <view class="title-ri">
                                <text style="color: #256bfa">1</text>
                                <text>/2</text>
                            </view>
                        </view>
                        <view class="content-input" :class="{ 'input-error': nameError }">
                            <view class="input-titile">姓名</view>
                            <input 
                                class="input-con2" 
                                v-model="fromValue.name" 
                                maxlength="18" 
                                placeholder="请输入姓名"
                                @input="validateName"
                            />
                            <view v-if="nameError" class="error-message">{{ nameError }}</view>
                        </view>
                        <view class="content-sex" :class="{ 'input-error': sexError }">
                            <view class="sex-titile">性别</view>
                            <view class="sext-ri">
                                <view
                                    class="sext-box"
                                    :class="{ 'sext-boxactive': fromValue.sex === 0 }"
                                    @click="changeSex(0)"
                                >
                                    男
                                </view>
                                <view
                                    class="sext-box"
                                    :class="{ 'sext-boxactive': fromValue.sex === 1 }"
                                    @click="changeSex(1)"
                                >
                                    女
                                </view>
                            </view>
                        </view>
                        <view v-if="sexError" class="error-message">{{ sexError }}</view>
                        <view class="content-input" :class="{ 'input-error': ageError }">
                            <view class="input-titile">年龄</view>
                            <input 
                                class="input-con2" 
                                v-model="fromValue.age" 
                                maxlength="3" 
                                placeholder="请输入年龄"
                                @input="validateAge"
                            />
                            <view v-if="ageError" class="error-message">{{ ageError }}</view>
                        </view>
                        <view class="content-input" :class="{ 'input-error': experienceError }" @click="changeExperience">
                            <view class="input-titile">工作经验</view>
                            <input
                                class="input-con"
                                v-model="state.workExperience"
                                disabled
                                placeholder="请选择您的工作经验"
                            />
                            <view v-if="experienceError" class="error-message">{{ experienceError }}</view>
                        </view>
                        <view class="content-input" @click="changeEducation">
                            <view class="input-titile">学历</view>
                            <input class="input-con" v-model="state.educationText" disabled placeholder="本科" />
                        </view>
                        <view class="content-input" :class="{ 'input-error': idCardError }">
                            <view class="input-titile">身份证</view>
                            <input 
                                class="input-con2" 
                                v-model="fromValue.idCard" 
                                maxlength="18" 
                                placeholder="请输入身份证号码" 
                                @input="validateIdCard"
                            />
                            <view v-if="idCardError" class="error-message">{{ idCardError }}</view>
                            <view v-if="fromValue.idCard && !idCardError" class="success-message">✓ 身份证格式正确</view>
                        </view>
                        </view>
                            <view class="next-btn" @tap="nextStep">下一步</view>
                        </view>
                </swiper-item>
                <swiper-item @touchmove.stop="false">
                <view class="content-one">
                    <view>
                        <view class="content-title">
                            <view class="title-lf">
                                <view class="lf-h1">请您完善求职名片</view>
                                <view class="lf-text">个人信息仅用于推送优质内容</view>
                            </view>
                            <view class="title-ri">
                                <text style="color: #256bfa">2</text>
                                <text>/2</text>
                            </view>
                        </view>
                        <view class="content-input" @click="changeArea">
                            <view class="input-titile">求职区域</view>
                            <input
                                class="input-con"
                                v-model="state.areaText"
                                disabled
                                placeholder="请选择您的求职区域"
                            />
                        </view>
                        <view class="content-input" @click="changeJobs">
                            <view class="input-titile">求职岗位</view>
                            <input
                                class="input-con"
                                disabled
                                v-if="!state.jobsText.length"
                                placeholder="请选择您的求职岗位"
                            />
                            <view class="input-nx" @click="changeJobs" v-else>
                                <view class="nx-item" v-for="item in state.jobsText">{{ item }}</view>
                            </view>
                        </view>
                        <view class="content-input" @click="changeSkillLevel">
                            <view class="input-titile">技能等级</view>
                            <input
                                class="input-con"
                                v-model="state.skillLevelText"
                                disabled
                                placeholder="请选择您的技能等级"
                            />
                        </view>
                        <view class="content-input" @click="changeSkills">
                            <view class="input-titile">技能名称</view>
                            <input
                                class="input-con"
                                disabled
                                v-if="!state.skillsText.length"
                                placeholder="请选择您的技能名称"
                            />
                            <view class="input-nx" @click="changeSkills" v-else>
                                <view class="nx-item" v-for="(item, index) in state.skillsText" :key="index">{{ item }}</view>
                            </view>
                        </view>
                        <view class="content-input" @click="changeSalay">
                            <view class="input-titile">期望薪资</view>
                            <input
                                class="input-con"
                                v-model="state.salayText"
                                disabled
                                placeholder="请选择您的期望薪资"
                            />
                        </view>
                    </view>
                        <view class="next-btn" @tap="complete">开启求职之旅</view>
                        </view>
                    </swiper-item>
                </swiper>
            </view>
        </view>
        <SelectJobs ref="selectJobsModel"></SelectJobs>
        <SelectPopup ref="selectPopupRef"></SelectPopup>
    </AppLayout>
</template>

<script setup>
import SelectJobs from '@/components/selectJobs/selectJobs.vue';
import SelectPopup from '@/components/selectPopup/selectPopup.vue';
import { reactive, inject, watch, ref, onMounted } from 'vue';
import { onLoad, onShow } from '@dcloudio/uni-app';
import useUserStore from '@/stores/useUserStore';
import useDictStore from '@/stores/useDictStore';
const { $api, navTo, config, IdCardValidator } = inject('globalFunction');
const { loginSetToken, getUserResume } = useUserStore();
const { getDictSelectOption, oneDictData } = useDictStore();

// #ifdef H5
const injectedOpenSelectPopup = inject('openSelectPopup', null);
// #endif

// status
const selectJobsModel = ref();
const selectPopupRef = ref();

// 创建本地的 openSelectPopup 函数
const openSelectPopup = (config) => {
    // #ifdef MP-WEIXIN
    if (selectPopupRef.value) {
        selectPopupRef.value.open(config);
    }
    // #endif
    
    // #ifdef H5
    if (injectedOpenSelectPopup) {
        injectedOpenSelectPopup(config);
    }
    // #endif
};
const tabCurrent = ref(1);
const salay = [2, 5, 10, 15, 20, 25, 30, 50, 80, 100];
const state = reactive({
    station: [],
    stationCateLog: 1,
    lfsalay: [2, 5, 10, 15, 20, 25, 30, 50],
    risalay: JSON.parse(JSON.stringify(salay)),
    areaText: '',
    educationText: '',
    workExperience: '',
    salayText: '',
    jobsText: [],
    skillLevelText: '',
    skillsText: [],
});
const fromValue = reactive({
    sex: null,
    education: '4',
    salaryMin: 2000,
    salaryMax: 2000,
    area: 0,
    jobTitleId: '',
    workExperience: '1',
    idCard: '',
    name: '',
    age: '',
    skillLevel: '',
    skills: '',
});

// 输入校验相关
const idCardError = ref('');
const nameError = ref('');
const ageError = ref('');
const sexError = ref('');
const experienceError = ref('');

onLoad((parmas) => {
    getTreeselect();
});

onMounted(() => {});

function changeSex(sex) {
    fromValue.sex = sex;
    // 选择后清除性别错误
    sexError.value = '';
}

// 姓名实时校验（中文2-18或英文2-30）
function validateName() {
    const name = (fromValue.name || '').trim();
    if (!name) {
        nameError.value = '请输入姓名';
        return;
    }
    const cn = /^[\u4e00-\u9fa5·]{2,18}$/;
    const en = /^[A-Za-z\s]{2,30}$/;
    nameError.value = cn.test(name) || en.test(name) ? '' : '姓名格式不正确';
}

// 年龄实时校验（16-65的整数）
function validateAge() {
    const ageStr = String(fromValue.age || '').trim();
    if (!ageStr) {
        ageError.value = '请输入年龄';
        return;
    }
    const num = Number(ageStr);
    if (!/^\d{1,3}$/.test(ageStr) || Number.isNaN(num)) {
        ageError.value = '年龄必须为数字';
        return;
    }
    ageError.value = num >= 16 && num <= 65 ? '' : '年龄需在16-65之间';
}

// 身份证实时校验
function validateIdCard() {
    const idCard = (fromValue.idCard || '').trim();
    
    // 如果为空，清除错误信息
    if (!idCard) {
        idCardError.value = '';
        return;
    }
    
    // 使用身份证校验器进行校验
    const result = IdCardValidator.validate(idCard);
    
    if (result.valid) {
        idCardError.value = '';
    } else {
        idCardError.value = result.message;
    }
}
function changeExperience() {
    openSelectPopup({
        title: '工作经验',
        maskClick: true,
        data: [oneDictData('experience')],
        success: (_, [value]) => {
            fromValue.workExperience = value.value;
            state.workExperience = value.label;
            // 选择后清除工作经验错误
            experienceError.value = '';
        },
        change(_, [value]) {
            // this.setColunm(1, [123, 123]);
            console.log(this);
        },
    });
}

function changeEducation() {
    openSelectPopup({
        title: '学历',
        maskClick: true,
        data: [oneDictData('education')],
        success: (_, [value]) => {
            fromValue.area = value.value;
            state.educationText = value.label;
        },
    });
}

function changeArea() {
    openSelectPopup({
        title: '区域',
        maskClick: true,
        data: [oneDictData('area')],
        success: (_, [value]) => {
            fromValue.area = value.value;
            state.areaText = config.appInfo.areaName + '-' + value.label;
        },
    });
}

function changeSalay() {
    let leftIndex = 0;
    openSelectPopup({
        title: '薪资',
        maskClick: true,
        data: [state.lfsalay, state.risalay],
        unit: 'k',
        success: (_, [min, max]) => {
            fromValue.salaryMin = min.value * 1000;
            fromValue.salaryMax = max.value * 1000;
            state.salayText = `${fromValue.salaryMin}-${fromValue.salaryMax}`;
        },
        change(e) {
            const salayData = e.detail.value;
            if (leftIndex !== salayData[0]) {
                const copyri = JSON.parse(JSON.stringify(salay));
                const [lf, ri] = e.detail.value;
                const risalay = copyri.slice(lf, copyri.length);
                this.setColunm(1, risalay);
                leftIndex = salayData[0];
            }
        },
    });
}

function changeJobs() {
    selectJobsModel.value?.open({
        title: '添加岗位',
        success: (ids, labels) => {
            fromValue.jobTitleId = ids;
            state.jobsText = labels.split(',');
        },
    });
}

// 技能等级选择
function changeSkillLevel() {
    const skillLevels = [
        { label: '初级', value: '1' },
        { label: '中级', value: '2' },
        { label: '高级', value: '3' }
    ];
    
    openSelectPopup({
        title: '技能等级',
        maskClick: true,
        data: [skillLevels],
        success: (_, [value]) => {
            fromValue.skillLevel = value.value;
            state.skillLevelText = value.label;
        },
    });
}

// 技能名称选择
function changeSkills() {
    const skills = [
        // 前端开发
        { label: 'HTML', value: 'html' },
        { label: 'CSS', value: 'css' },
        { label: 'JavaScript', value: 'javascript' },
        { label: 'TypeScript', value: 'typescript' },
        { label: 'React', value: 'react' },
        { label: 'Vue', value: 'vue' },
        { label: 'Angular', value: 'angular' },
        { label: 'jQuery', value: 'jquery' },
        { label: 'Bootstrap', value: 'bootstrap' },
        { label: 'Sass/Less', value: 'sass' },
        { label: 'Webpack', value: 'webpack' },
        { label: 'Vite', value: 'vite' },
        
        // 后端开发
        { label: 'Java', value: 'java' },
        { label: 'Python', value: 'python' },
        { label: 'Node.js', value: 'nodejs' },
        { label: 'PHP', value: 'php' },
        { label: 'C#', value: 'csharp' },
        { label: 'Go', value: 'go' },
        { label: 'Ruby', value: 'ruby' },
        { label: 'Spring Boot', value: 'springboot' },
        { label: 'Django', value: 'django' },
        { label: 'Express', value: 'express' },
        { label: 'Laravel', value: 'laravel' },
        
        // 数据库
        { label: 'MySQL', value: 'mysql' },
        { label: 'PostgreSQL', value: 'postgresql' },
        { label: 'MongoDB', value: 'mongodb' },
        { label: 'Redis', value: 'redis' },
        { label: 'Oracle', value: 'oracle' },
        { label: 'SQL Server', value: 'sqlserver' },
        
        // 移动开发
        { label: 'React Native', value: 'reactnative' },
        { label: 'Flutter', value: 'flutter' },
        { label: 'iOS开发', value: 'ios' },
        { label: 'Android开发', value: 'android' },
        { label: '微信小程序', value: 'miniprogram' },
        { label: 'uni-app', value: 'uniapp' },
        
        // 云计算与运维
        { label: 'Docker', value: 'docker' },
        { label: 'Kubernetes', value: 'kubernetes' },
        { label: 'AWS', value: 'aws' },
        { label: '阿里云', value: 'aliyun' },
        { label: 'Linux', value: 'linux' },
        { label: 'Nginx', value: 'nginx' },
        
        // 设计工具
        { label: 'Photoshop', value: 'photoshop' },
        { label: 'Figma', value: 'figma' },
        { label: 'Sketch', value: 'sketch' },
        { label: 'Adobe XD', value: 'adobexd' },
        
        // 其他技能
        { label: 'Git', value: 'git' },
        { label: 'Jenkins', value: 'jenkins' },
        { label: 'Jira', value: 'jira' },
        { label: '项目管理', value: 'projectmanagement' },
        { label: '数据分析', value: 'dataanalysis' },
        { label: '人工智能', value: 'ai' },
        { label: '机器学习', value: 'machinelearning' }
    ];
    
    // 获取当前已选中的技能
    const currentSelectedValues = fromValue.skills ? fromValue.skills.split(',') : [];
    
    openSelectPopup({
        title: '技能名称',
        maskClick: true,
        data: [skills],
        multiSelect: true,
        rowLabel: 'label',
        rowKey: 'value',
        defaultValues: currentSelectedValues,
        success: (selectedValues, selectedItems) => {
            const selectedSkills = selectedItems.map(item => item.value);
            const selectedLabels = selectedItems.map(item => item.label);
            fromValue.skills = selectedSkills.join(',');
            state.skillsText = selectedLabels;
        },
    });
}

function nextStep() {
    // 统一必填与格式校验
    validateName();
    validateAge();
    validateIdCard();

    if (fromValue.sex !== 0 && fromValue.sex !== 1) {
        sexError.value = '请选择性别';
    }

    // 工作经验校验
    if (!state.workExperience) {
        experienceError.value = '请选择您的工作经验';
    } else {
        experienceError.value = '';
    }

    // 学历校验
    if (!state.educationText) {
        $api.msg('请选择您的学历');
        return;
    }

    // 检查所有错误状态
    if (nameError.value) return;
    if (sexError.value) return;
    if (ageError.value) return;
    if (experienceError.value) return;
    if (idCardError.value || !fromValue.idCard) {
        if (!fromValue.idCard) $api.msg('请输入身份证号码');
        return;
    }

    const result = IdCardValidator.validate(fromValue.idCard);
    if (!result.valid) {
        $api.msg(result.message);
        return;
    }
    tabCurrent.value += 1;
}

// 获取职位
function getTreeselect() {
    $api.createRequest('/app/common/jobTitle/treeselect', {}, 'GET').then((resData) => {
        state.station = resData.data;
    });
}

// 登录
function loginTest() {
    // uni.share({
    //     provider: 'weixin',
    //     scene: 'WXSceneSession',
    //     type: 2,
    //     imageUrl: 'https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/uni@2x.png',
    //     success: function (res) {
    //         console.log('success:' + JSON.stringify(res));
    //     },
    //     fail: function (err) {
    //         console.log('fail:' + JSON.stringify(err));
    //     },
    // });
    const params = {
        username: 'test',
        password: 'test',
    };
    $api.createRequest('/app/login', params, 'post').then((resData) => {
        $api.msg('模拟帐号密码测试登录成功');
        loginSetToken(resData.token).then((resume) => {
            if (resume.data.jobTitleId) {
                // 设置推荐列表，每次退出登录都需要更新
                useUserStore().initSeesionId();
                uni.reLaunch({
                    url: '/pages/index/index',
                });
            } else {
                nextStep();
            }
        });
    });
}

function complete() {
    const result = IdCardValidator.validate(fromValue.idCard);
    if (result.valid) {
        // 构建 experiencesList 数组
        const experiencesList = [];
        if (fromValue.skills && fromValue.skillLevel) {
            const skillsArray = fromValue.skills.split(',');
            skillsArray.forEach(skill => {
                if (skill.trim()) {
                    experiencesList.push({
                        name: skill.trim(),
                        levels: fromValue.skillLevel
                    });
                }
            });
        }
        
        // 构建符合要求的请求数据（experiencesList 与 appUser 同级）
        const requestData = {
            appUser: {
                name: fromValue.name,
                isCompanyUser: 1,
                age: fromValue.age,
                sex: fromValue.sex,
                workExperience: fromValue.workExperience,
                education: fromValue.education,
                idCard: fromValue.idCard,
                area: fromValue.area,
                jobTitleId: fromValue.jobTitleId,
                salaryMin: fromValue.salaryMin,
                salaryMax: fromValue.salaryMax
            },
            experiencesList: experiencesList
        };
        
        $api.createRequest('/registerUser', requestData, 'post').then(async (resData) => {
            $api.msg('完成');
            
            // 如果接口返回了token，需要重新保存token
            if (resData.token) {
                try {
                    await loginSetToken(resData.token);
                    console.log('Token已更新:', resData.token);
                } catch (error) {
                    console.error('更新Token失败:', error);
                }
            }
            
            // 保存成功后，重新获取用户信息并更新缓存
            try {
                await getUserResume();
                console.log('用户信息已更新到缓存');
            } catch (error) {
                console.error('获取用户信息失败:', error);
                // 即使获取用户信息失败，也不影响页面跳转
            }
            
            // 跳转到首页
            uni.reLaunch({
                url: '/pages/index/index',
            });
        });
    } else {
        $api.msg('身份证校验失败');
        console.log('验证失败:', result.message);
    }
}
</script>

<style lang="stylus" scoped>
.tab-container
    height: 100%
    width: 100%
    display: flex
    align-items: center
    justify-content: center
    flex-direction: row
.uni-margin-wrap
    width: 100%
    height: 100%
.swiper
    width: 100%
    height: 100%
.swiper-item
    display: block;
    width: 100%
    height: 100%
.input-nx
    position: relative
    border-bottom: 2rpx solid #EBEBEB
    padding-bottom: 30rpx
    display: flex
    flex-wrap: wrap
    .nx-item
        padding: 16rpx 24rpx
        width: fit-content
        border-radius: 20rpx
        border: 2rpx solid #E8EAEE
        background-color: #f8f9fa
        margin-right: 16rpx
        margin-top: 16rpx
        font-size: 28rpx
        color: #333333
        transition: all 0.2s ease
        
        &:hover
            background-color: #e9ecef
            border-color: #256bfa
            color: #256bfa
// 移除技能标签的箭头样式，因为技能标签不需要箭头指示
.container
    // background: linear-gradient(#4778EC, #002979);
    width: 100%;
    height: calc(100vh - var(--window-top) - var(--status-bar-height) - var(--window-bottom));
    position: fixed;
    background: url('@/static/icon/background2.png') 0 0 no-repeat;
    background-size: 100% 728rpx;
    display: flex;
    flex-direction: column
    .container-hader
        height: 88rpx;
        text-align: center;
        line-height: 88rpx;
        color: #000000;
        font-weight: bold
        font-size: 32rpx

.login-content
    position: absolute;
    left: 50%;
    top: 40%;
    transform: translate(-50%, -50%);
    display: flex;
    align-items: flex-end;
    flex-wrap: nowrap;
    .logo
        width: 266rpx;
        height: 182rpx;
    .logo-title
        font-size: 88rpx;
        color: #22c984;
        width: 180rpx;
.btns
    position: absolute;
    top: 70%;
    left: 50%;
    transform: translate(-50%, 0)
    .wxlogin
        width: 562rpx;
        height: 140rpx;
        border-radius: 70rpx;
        background-color: #13C57C;
        color: #FFFFFF;
        text-align: center;
        line-height: 140rpx;
        font-size: 70rpx;
    .wxaddress
        color: #BBBBBB;
        margin-top: 70rpx;
        text-align: center;
.content-one
    padding: 60rpx 28rpx;
    display: flex;
    flex-direction: column;
    justify-content: space-between
    height: calc(100% - 120rpx)
    .content-title
        display: flex
        justify-content: space-between;
        align-items: center
        margin-bottom: 70rpx
        .title-lf
            font-size: 44rpx;
            color: #000000;
            font-weight: 600;
            .lf-text
                font-weight: 400;
                font-size: 28rpx;
                color: #6C7282;
        .title-ri
            font-size: 36rpx;
            color: #000000;
            font-weight: 600;
    .content-input
        margin-bottom: 52rpx
        .input-titile
            font-weight: 400;
            font-size: 28rpx;
            color: #6A6A6A;
        .input-con2
            font-weight: 400;
            font-size: 32rpx;
            color: #333333;
            line-height: 80rpx;
            height: 80rpx;
            border-bottom: 2rpx solid #EBEBEB
        .input-con
            font-weight: 400;
            font-size: 32rpx;
            color: #333333;
            line-height: 80rpx;
            height: 80rpx;
            border-bottom: 2rpx solid #EBEBEB
            position: relative;
        .input-con::before
            position: absolute;
            right: 20rpx;
            top: calc(50% - 2rpx);
            content: '';
            width: 4rpx;
            height: 18rpx;
            border-radius: 2rpx
            background: #697279;
            transform: translate(0, -50%) rotate(-45deg) ;
        .input-con::after
            position: absolute;
            right: 20rpx;
            top: 50%;
            content: '';
            width: 4rpx;
            height: 18rpx;
            border-radius: 2rpx
            background: #697279;
            transform: rotate(45deg)
    .error-message
        color: #ff4757;
        font-size: 24rpx;
        margin-top: 10rpx;
        line-height: 1.4;
    .success-message
        color: #2ed573;
        font-size: 24rpx;
        margin-top: 10rpx;
        line-height: 1.4;
    .input-error
        .input-con2
            border-bottom-color: #ff4757;
    .content-sex
        height: 110rpx;
        display: flex
        justify-content: space-between;
        align-items: flex-start;
        border-bottom: 2rpx solid #EBEBEB
        margin-bottom: 52rpx
        .sex-titile
            line-height: 80rpx;
        .sext-ri
            display: flex
            align-items: center;
            .sext-box
                height: 76rpx;
                width: 152rpx;
                text-align: center;
                line-height: 80rpx;
                border-radius: 12rpx 12rpx 12rpx 12rpx
                border: 2rpx solid #E8EAEE;
                margin-left: 28rpx
                font-weight: 400;
                font-size: 28rpx;
            .sext-boxactive
                color: #256BFA
                background: rgba(37,107,250,0.1);
                border: 2rpx solid #256BFA;
    .next-btn
        width: 100%;
        height: 90rpx;
        background: #256BFA;
        border-radius: 12rpx 12rpx 12rpx 12rpx;
        font-weight: 500;
        font-size: 32rpx;
        color: #FFFFFF;
        text-align: center;
        line-height: 90rpx
</style>

