import {
    defineStore
} from 'pinia';
import {
    ref
} from 'vue'
import {
    createRequest
} from '../utils/request';
import similarityJobs from '@/utils/similarity_Job.js';
import {
    UUID
} from "@/lib/uuid-min.js";

// 简历完成度计算
function getResumeCompletionPercentage(resume) {
    const requiredFields = [
        'name',
        'age',
        'sex',
        'birthDate',
        'education',
        'politicalAffiliation',
        'phone',
        'salaryMin',
        'salaryMax',
        'area',
        'status',
        'jobTitleId',
        'jobTitle',
    ];

    const totalFields = requiredFields.length;
    let filledFields = requiredFields.filter((field) => {
        const value = resume[field];
        return value !== null && value !== '' && !(Array.isArray(value) && value.length === 0);
    }).length;

    return ((filledFields / totalFields) * 100).toFixed(0) + '%';
}

const useUserStore = defineStore("user", () => {
    // 定义状态
    const hasLogin = ref(false)
    // const openId = ref('')
    const userInfo = ref({});
    const role = ref({});
    const token = ref('')
    const resume = ref({})
    const Completion = ref('0%')
    const seesionId = ref(uni.getStorageSync('seesionId') || '')

    const login = (value) => {
        hasLogin.value = true;
        userInfo.value = value;
        openId.value = value.wxOpenId;
        token.value = value.token
        uni.setStorage({
            key: 'token',
            data: value.token
        });
    }

    const logOut = () => {
        hasLogin.value = false;
        token.value = ''
        resume.value = {}
        userInfo.value = {}
        role.value = {}
        uni.clearStorageSync('userInfo')
        uni.clearStorageSync('token')
        uni.redirectTo({
            url: '/pages/login/login',
        });
    }

    const getUserInfo = () => {
        return new Promise((reslove, reject) => {
            createRequest('/getInfo', {}, 'get').then((userInfo) => {
                setUserInfo(userInfo);
                reslove(userInfo)
            });
        })
    }

    const getUserResume = () => {
        return new Promise((reslove, reject) => {
            createRequest('/app/user/resume', {}, 'get').then((resume) => {
                Completion.value = getResumeCompletionPercentage(resume.data)
                similarityJobs.setUserInfo(resume.data)
                setUserInfo(resume);
                reslove(resume)
            });
        })
    }

    const loginSetToken = async (value) => {
        token.value = value
        uni.setStorageSync('token', value);
        // 获取用户信息
        return getUserResume()
    }

    const setUserInfo = (values) => {
        userInfo.value = values.data;
        // role.value = values.role;
        hasLogin.value = true;
    }


    const tokenlogin = (token) => {
        createRequest('/app/login', {
            token
        }).then((resData) => {
            onsole.log(resData)
        })
    }

    const initSeesionId = () => {
        const seesionIdVal = UUID.generate()
        uni.setStorageSync('seesionId', seesionIdVal);
        seesionId.value = seesionIdVal
    }

    // 导入
    return {
        hasLogin,
        userInfo,
        token,
        resume,
        login,
        logOut,
        loginSetToken,
        getUserResume,
        initSeesionId,
        seesionId,
        Completion
    }
})

export default useUserStore;