import {ApiResponse, DictItem} from "@/types";
import flyio from "../flyio";
import {
    EnrollRule,
    VoluntaryRecord,
    VoluntaryResult,
    SelectedUniversityMajor
} from "@/types/voluntary";
// import {sleep} from "@/uni_modules/uv-ui-tools/libs/function";

export function getRenderRules(um: SelectedUniversityMajor) {
    // return new Promise(async (resolve, reject) => {
    //     await sleep(300)
    //     const mock: EnrollRule[] = [{
    //         category: '文化素质',
    //         content: '语数物300分',
    //         details: [{
    //             defaultValue: 80,
    //             enumInputType: 'Score',
    //             enumRuleCategory: 'Enroll',
    //             fieldName: '语',
    //             label: '语文',
    //             options: ['100'],
    //             readonly: false,
    //         }, {
    //             defaultValue: 80,
    //             enumInputType: 'Score',
    //             enumRuleCategory: 'Enroll',
    //             fieldName: '数',
    //             label: '数学',
    //             options: ['100'],
    //             readonly: false,
    //         }, {
    //             defaultValue: 80,
    //             enumInputType: 'Score',
    //             enumRuleCategory: 'Enroll',
    //             fieldName: '物',
    //             label: '物理',
    //             options: ['100'],
    //             readonly: false,
    //         }]
    //     }, {
    //         category: '职业技能',
    //         content: '职业技能300分',
    //         details: [{
    //             defaultValue: 80,
    //             enumInputType: 'Score',
    //             enumRuleCategory: 'Enroll',
    //             fieldName: '技',
    //             label: '技能',
    //             options: ['200'],
    //             readonly: false,
    //         }, {
    //             defaultValue: 80,
    //             enumInputType: 'Score',
    //             enumRuleCategory: 'Enroll',
    //             fieldName: '职',
    //             label: '职业适应',
    //             options: ['100'],
    //             readonly: false,
    //         }]
    //     }]
    //     resolve({code: 200, data: mock})
    // })
    // 普通高中，EnrollRuleItem.readonly=true 取学考成绩
    const {universityId, majorId} = um
    return flyio.post('/voluntary/getRenderRules', {universityId, majorId}) as Promise<ApiResponse<EnrollRule[]>>
}

export function getSkillRules(um: SelectedUniversityMajor) {
    // return new Promise(async (resolve, reject) => {
    //     await sleep(300)
    //     const mock: EnrollRule[] = [{
    //         category: '文化素质',
    //         content: '语数物300分',
    //         details: [{
    //             defaultValue: 67,
    //             enumInputType: 'Score',
    //             enumRuleCategory: 'Enroll',
    //             fieldName: '语',
    //             label: '语文',
    //             options: ['100'],
    //             readonly: false,
    //         }, {
    //             defaultValue: 78,
    //             enumInputType: 'Score',
    //             enumRuleCategory: 'Enroll',
    //             fieldName: '数',
    //             label: '数学',
    //             options: ['100'],
    //             readonly: false,
    //         }, {
    //             defaultValue: 90,
    //             enumInputType: 'Score',
    //             enumRuleCategory: 'Enroll',
    //             fieldName: '物',
    //             label: '物理',
    //             options: ['100'],
    //             readonly: false,
    //         }]
    //     }, {
    //         category: '职业技能',
    //         content: '职业技能300分',
    //         details: []
    //     }]
    //     resolve({code: 200, data: mock})
    // })
    // EnrollRule 需要返回文化+职业，但职业EnrollRule.details = []不需要返回字段规则
    const {universityId, majorId} = um
    return flyio.post('/voluntary/getSkillRules', {universityId, majorId}) as Promise<ApiResponse<EnrollRule[]>>
}

export function postRenderRules(target: SelectedUniversityMajor, model: Record<string, any>) {
    // return new Promise(async (resolve, reject) => {
    //     await sleep(300)
    //     const mock: Partial<VoluntaryResult> = {
    //         ...target,
    //         "enumPickType": "Danger",
    //         "enrollRate": 66,
    //         "enrollRateText": '风险极高',
    //         "histories": [{
    //             "year": '2025',
    //             "score": null, // 最低分
    //             "plan": 5, // 计划人数
    //             "enroll": 5, // 录取人数
    //             "diff": -12, // 负表示低于录取分；正数表示高于录取分
    //             "ruleContent": '完整的说明', // 招录规则
    //             "application": 3.3, // 报名人数比值
    //             "admission": 1 // 计划人数比值
    //         }, {
    //             "year": '2024',
    //             "score": null, // 最低分
    //             "plan": 5, // 计划人数
    //             "enroll": 5, // 录取人数
    //             "diff": -12, // 负表示低于录取分；正数表示高于录取分
    //             "ruleContent": '完整的说明', // 招录规则
    //             "application": 3.3, // 报名人数比值
    //             "admission": 1 // 计划人数比值
    //         }, {
    //             "year": '2023',
    //             "score": null, // 最低分
    //             "plan": 5, // 计划人数
    //             "enroll": 5, // 录取人数
    //             "diff": -12, // 负表示低于录取分；正数表示高于录取分
    //             "ruleContent": '完整的说明', // 招录规则
    //             "application": 3.3, // 报名人数比值
    //             "admission": 1 // 计划人数比值
    //         }],
    //         "skill": {
    //             year: '2025',
    //             cultureScore: 233,
    //             cultureRule: '语数物300分',
    //             enrollScore: 496,
    //             skillScore: 263, // 反向测技能分
    //             diff: -44 // 负数表示低于skillScore，正数高于
    //         }
    //     }
    //     resolve({code: 200, data: mock})
    // })
    const {universityId, majorId} = target
    return flyio.post('/voluntary/postRenderRules', {
        target: {universityId, majorId},
        model
    }) as Promise<ApiResponse<VoluntaryResult>>
}

export function postSkillRules(target: SelectedUniversityMajor, model: Record<string, any>) {
    // return new Promise(async (resolve, reject) => {
    //     await sleep(300)
    //     const mock: Partial<VoluntaryResult> = {
    //         ...target,
    //         "enumPickType": "Danger",
    //         "enrollRate": 66,
    //         "enrollRateText": '风险极高',
    //         "skill": {
    //             year: '2025',
    //             cultureScore: 233,
    //             cultureRule: '语数物300分',
    //             enrollScore: 496,
    //             skillScore: 263, // 反向测技能分
    //             diff: -44 // 负数表示低于skillScore，正数高于
    //         }
    //     }
    //     resolve({code: 200, data: mock})
    // })
    // VoluntaryResult.histories 不需要返回
    const {universityId, majorId} = target
    return flyio.post('/voluntary/postSkillRules', {
        target: {universityId, majorId},
        model
    }) as Promise<ApiResponse<VoluntaryResult>>
}

export function getVoluntaryList() {
    // return new Promise(async (resolve, reject) => {
    //     await sleep(300)
    //     const mock: VoluntaryRecord[] = [{
    //         id: 1,
    //         universityLogo: "https://mingxuejingbang.oss-cn-beijing.aliyuncs.com/ie/universityLog/e6027a8c35f7426592f161c47f336fe7.jpg",
    //         universityId: '1',
    //         universityName: "长沙民政职业技术学院",
    //         majors: [{
    //             majorId: "1",
    //             majorName: "随便起名1",
    //             majorAncestors: "",
    //             majorGroup: "专业组一"
    //         }, {
    //             majorId: "2",
    //             majorName: "随便起名2",
    //             majorAncestors: "",
    //             majorGroup: "专业组一"
    //         }, {
    //             majorId: "3",
    //             majorName: "随便起名3",
    //             majorAncestors: "",
    //             majorGroup: "专业组一"
    //         }]
    //     }, {
    //         id: 2,
    //         universityLogo: "https://mingxuejingbang.oss-cn-beijing.aliyuncs.com/ie/universityLog/ed33a51d6ffa43ff89d93337f4a4369e.jpg",
    //         universityId: '2',
    //         universityName: "湖南信息职业技术学院",
    //         majors: [{
    //             majorId: "1",
    //             majorName: "随便起名1",
    //             majorAncestors: "",
    //             majorGroup: "专业组一"
    //         }, {
    //             majorId: "2",
    //             majorName: "随便起名2",
    //             majorAncestors: "",
    //             majorGroup: "专业组一"
    //         }, {
    //             majorId: "3",
    //             majorName: "随便起名3",
    //             majorAncestors: "",
    //             majorGroup: "专业组一"
    //         }]
    //     }, {
    //         id: 3,
    //         universityLogo: "https://mingxuejingbang.oss-cn-beijing.aliyuncs.com/ie/universityLog/83386e105f03474b91d7c1824e81fa0c.jpg",
    //         universityId: '3',
    //         universityName: "湖南工业职业技术学院",
    //         majors: [{
    //             majorId: "1",
    //             majorName: "随便起名1",
    //             majorAncestors: "",
    //             majorGroup: "专业组一"
    //         }, {
    //             majorId: "2",
    //             majorName: "随便起名2",
    //             majorAncestors: "",
    //             majorGroup: "专业组一"
    //         }, {
    //             majorId: "3",
    //             majorName: "随便起名3",
    //             majorAncestors: "",
    //             majorGroup: "专业组一"
    //         }]
    //     }]
    //     resolve({code: 200, data: mock})
    // })
    // 此接口不分页，全部返回
    return flyio.get('/voluntary/getVoluntaryList') as Promise<ApiResponse<VoluntaryRecord[]>>
}

export function addVoluntary(data: Partial<SelectedUniversityMajor>) {
    // 注意合并，数量限制（1个院校6个？确认一下），
    const {universityId, majorId} = data
    return flyio.post('/voluntary/addVoluntary', {universityId, majorId})
}

export function removeVoluntaryByUniversity(universityId: string) {
    // 删除院校下的所有 // SelectedUniversityMajor.universityId
    return flyio.post('/voluntary/removeVoluntaryByUniversity/' + universityId)
}

export function removeVoluntaryByMajor(majorId: string) {
    // 删除指定专业，注意合并 // SelectedUniversityMajor.majorId
    return flyio.post('/voluntary/removeVoluntaryByMajor/' + majorId)
}

export function sortVoluntaryByMajor(universityId: string, majorIdList: string[]) {
    // 按传入专业id顺序，保存专业排序优先级
    return flyio.post('/voluntary/sortVoluntaryByMajor', {universityId, majorIdList})
}

export function sortVoluntaryByUniversity(universityIdList: string[]) {
    // 按传入院校id顺序，保存排序优先级
    return flyio.post('/voluntary/sortVoluntaryByUniversity', {universityIdList})
}
