import { ListIitem } from '@/api/base/BaseDataModel';
import { api_ScheduleSettingAdd, api_ScheduleSettingDelete, api_ScheduleSettingEdit, api_ScheduleSettingList, api_ScheduleSettingStatus } from '@/api/scheduleSetting/apiScheduleSetting';
import { ScheduleSettingDto, ScheduleSettingParam, ScheduleSettingStatus } from '@/api/scheduleSetting/apiScheduleSettingModel';
import { useDropDownData } from '@/hooks/useDropDownData';
import { EditType } from '@/type/BaseEnum';
import { BaseFeesetAction } from '@/type/BaseFeesetAction';
import { ref, reactive, onMounted } from 'vue';
import { Refresh, Edit, Delete } from "@element-plus/icons-vue";
import { cloneDeep } from '@/utils/cloneDeep';
import { useReturnData } from '@/utils/useReturnData';
import { useErrorConfirm } from '@/utils/useErrorConfirm';
import { useQueryList } from '@/hooks/useQueryList';

const { report } = useErrorConfirm()
/**
 * 参数设置
 */
function useScheduleSetting() {
    const { storeDropDownData } = useDropDownData()
    const { resData } = useReturnData()
    //定义列表查询的参数
    const listParm = reactive<ScheduleSettingParam>({
        id: undefined,
        beanName: '',
        methodName: '',
        methodParams: '',
        jobStatus: undefined,
        pageNum: 1,
        pageSize: 10,
        total: 0
    })
    // 列表数据
    const scheduleSettingList = reactive({
        mainList: [{}] as Recordable[],
        stateList: [] as ListIitem[],//状态
        cycleTypeList: [] as ListIitem[],//周期
        loading: false,//加载
    })
    const addEditScheduleSettingRef = ref<{ show: (type: string, data?: Recordable) => void }>()
    // TODO 权限还未修改正取
    const actionOptions = [
        {
            value: BaseFeesetAction.EDIT,//状态
            label: "编辑",//文本
            type: "primary",//按钮样式
            icon: Edit,//按钮图标
            permissions: ['sys:schedulesetting:add']
        },
        {
            value: BaseFeesetAction.FEESET_ACTIVE,
            label: "运行",
            labelStop: "停止",
            type: "success",
            icon: Refresh,
            permissions: ['sys:country:changeJobStatus']
        },
        {
            value: BaseFeesetAction.DELETE,
            label: "删除",
            type: "danger",
            icon: Delete,
            permissions: ['sys:schedulesetting:delete']
        }
    ]
    // 操作类型
    const hanleChangeAction = async (data: Recordable) => {
        const row = cloneDeep(data)
        const types = row.types;
        // 费用操作列表
        console.log(types, ":费用操作类型", row);
        switch (types) {
            // 产品费率-编辑
            case BaseFeesetAction.EDIT:
                addEditBtn(EditType.EDIT, row)
                break;
            case BaseFeesetAction.FEESET_ACTIVE:
                // 改变状态
                const statusParams: ScheduleSettingStatus = {
                    id: row.id,
                    jobStatus: row.jobStatus === 1 ? 0 : 1
                }
                const statusRes = await api_ScheduleSettingStatus(statusParams)
                const statusCode = resData(statusRes)
                if (statusCode !== 500) getScheduleSettingList()
                break;
            // 产品费率-删除
            case BaseFeesetAction.DELETE:
                const deleteRes = await api_ScheduleSettingDelete({ id: row.id })
                const deleteCode = resData(deleteRes)
                if (deleteCode !== 500) getScheduleSettingList()
                break;
            default:
                break;
        }
    }

    //获取任务参数列表
    const getScheduleSettingList = async () => {
        scheduleSettingList.loading = true
        let res = await api_ScheduleSettingList(listParams)
        if (res && res.code == 200) {
            scheduleSettingList.mainList = res.data.records
            listParm.total = res.data.total;
        }
        scheduleSettingList.loading = false
    }
    // 查找周期名称
    const dealcycleType = (val: string | number) => {
        const findData = scheduleSettingList.cycleTypeList.find(item => item.value === val)
        return findData?.label
    }

    // 重置
    const resetParams = () => {
        listParm.beanName = '';
        listParm.methodName = '';
        listParm.methodParams = '';
        listParm.jobStatus = undefined;
    }
    // 初始化列表、分页数据
    const { sizeChange, currentChange, searchBtn, resetBtn, listParams } = useQueryList<ScheduleSettingParam>(getScheduleSettingList, resetParams, listParm)

    // 添加
    const addEditBtn = (type: string, data?: Recordable) => addEditScheduleSettingRef.value?.show(type, data)
    const hanleSaveScheduleSetting = async (params: ScheduleSettingDto, type: string, onclose: Function) => {
        try {
            // 新增
            if (type === EditType.ADD) {
                const addRes = await api_ScheduleSettingAdd(params)
                const codeData = resData(addRes)
                if (codeData !== 500) onclose()
            } else {
                // 编辑
                const editRes = await api_ScheduleSettingEdit(params)
                const codeData = resData(editRes)
                if (codeData !== 500) onclose()
            }
        } catch (error) {
            report(error)
        } finally {
            // 刷新列表
            getScheduleSettingList();
        }
    }

    onMounted(async () => {
        scheduleSettingList.cycleTypeList = await storeDropDownData.addCycleTypeList()//周期类型
        scheduleSettingList.stateList = await storeDropDownData.addJobStatusList()
    })

    return {
        listParm,
        scheduleSettingList,
        addEditScheduleSettingRef,
        actionOptions,
        sizeChange,
        currentChange,
        searchBtn,
        resetBtn,
        addEditBtn,
        hanleChangeAction,
        hanleSaveScheduleSetting,
        dealcycleType
    }
}

export {
    useScheduleSetting
}