import {
    CkProjectDetailRes,
    SaveOrUpdateCkGroupParams,
    SaveOrUpdateCkItemParams, SaveOrUpdateCkProjectParams, SaveOrUpdateCkStandardParams
} from "@/types/views/unitManage/police/assessmentCriteria.ts";
import {deleteDataModal, submitSimpleFormModal} from "@/components/tsx/ModalPro.tsx";
import api from "@/axios";
import {message} from "ant-design-vue";
import {dictSelectNodes} from "@/config/dict.ts";

export const saveOrUpdateCkProject = (params: SaveOrUpdateCkProjectParams = {
    name: '',
    totalScore: 100,
    remark: '',
    type: 'party_government'
}, callback: Function) => {
    submitSimpleFormModal<SaveOrUpdateCkProjectParams>({
        title: params.snowFlakeId ? `编辑【${params.name}】` : '添加考核项目',
        formParams: params,
        formOptions: {
            name: {
                type: 'input',
                label: '考核项目名称',
                required: true
            },
            type: {
                type: 'select',
                label: '考核类型',
                required: true,
                options: dictSelectNodes('EnterprisesUnitType')
            },
            totalScore: {
                type: 'inputNumber',
                label: '总分',
                required: true,
                componentsProps: {
                    precision: 0
                }
            },
            remark: {
                type: 'inputTextArea',
                label: '备注'
            }
        },
        submit: async (params) => {
            const resp = await api.post('/assessmentCriteria/saveOrUpdateCkProject', params)
            message.success(resp.message)
            callback && callback()
        }
    })
}

export const deleteCkProjectById = (name: string, ckProjectId: string, callback: Function) => {
    deleteDataModal(name, async () => {
        const resp = await api.delete('/assessmentCriteria/deleteCkProjectById', {
            ckProjectId
        })
        message.success(resp.message)
        callback && callback();
    })
}

export const ckProjectDetail = async (ckProjectId: string): Promise<CkProjectDetailRes[]> => {
    const {data} = await api.get<CkProjectDetailRes[]>('/assessmentCriteria/ckProjectDetail', {ckProjectId})

    const groupRowSpan: Record<string, { firstIndex: number, count: number }> = {}
    const itemRowSpan: Record<string, { firstIndex: number, count: number }> = {}

    data.forEach((item, index) => {
        //如果第一次没有值
        if (item.ckGroupId) {
            if (!groupRowSpan[item.ckGroupId]) {
                groupRowSpan[item.ckGroupId] = {count: 1, firstIndex: index}
            } else {
                groupRowSpan[item.ckGroupId].count++;
                data[index].groupRowSpan = 0
            }
        }

        if (item.ckItemId) {
            if (!itemRowSpan[item.ckItemId]) {
                itemRowSpan[item.ckItemId] = {count: 1, firstIndex: index}
            } else {
                itemRowSpan[item.ckItemId].count++;
                data[index].itemRowSpan = 0
            }
        }
    })

    Object.values(groupRowSpan).forEach(({count, firstIndex}) => {
        data[firstIndex].groupRowSpan = count;
    })

    Object.values(itemRowSpan).forEach(({count, firstIndex}) => {
        data[firstIndex].itemRowSpan = count;
    })

    return data
}

export const saveOrUpdateCkGroup = (params: SaveOrUpdateCkGroupParams, callback: Function) => {
    submitSimpleFormModal<SaveOrUpdateCkGroupParams>({
        title: params.snowFlakeId ? `编辑【${params.name}】` : '添加考核分组',
        formParams: params,
        formOptions: {
            name: {
                type: 'input',
                label: '分组名字',
                required: true
            },
            totalScore: {
                type: 'inputNumber',
                label: '分组总分',
                required: true,
                componentsProps: {
                    precision: 0
                }
            },
            remark: {
                type: 'inputTextArea',
                label: '备注'
            }
        },
        submit: async (params) => {
            const resp = await api.post('/assessmentCriteria/saveOrUpdateCkGroup', params)
            message.success(resp.message)
            callback && callback()
        }
    })
}

export const saveOrUpdateCkItem = (params: SaveOrUpdateCkItemParams, callback: Function) => {
    submitSimpleFormModal<SaveOrUpdateCkItemParams>({
        title: params.snowFlakeId ? `编辑【${params.name}】` : '添加考核项',
        formParams: params,
        formOptions: {
            name: {
                type: 'input',
                label: '考核项',
                required: true
            },
            type: {
                type: 'radioGroup',
                label: '控件类型',
                required: true,
                options: dictSelectNodes('SelectType')
            },
            remark: {
                type: 'inputTextArea',
                label: '备注'
            }
        },
        submit: async (params) => {
            const resp = await api.post('/assessmentCriteria/saveOrUpdateCkItem', params)
            message.success(resp.message)
            callback && callback()
        }
    })
}

export const saveOrUpdateCkStandard = (params: SaveOrUpdateCkStandardParams, callback: Function) => {
    submitSimpleFormModal<SaveOrUpdateCkStandardParams>({
        title: params.snowFlakeId ? `编辑【${params.name}】` : '添加考核标准',
        formParams: params,
        formOptions: {
            name: {
                type: 'input',
                label: '标准',
                required: true
            },
            deductionPoints: {
                type: 'inputNumber',
                label: '扣分值',
                required: true,
                componentsProps: {
                    precision: 1
                }
            }
        },
        submit: async (params) => {
            const resp = await api.post('/assessmentCriteria/saveOrUpdateCkStandard', params)
            message.success(resp.message)
            callback && callback()
        }
    })
}


