import { ListIitem } from '@/api/base/BaseDataModel';
import { ScheduleSettingDto } from '@/api/scheduleSetting/apiScheduleSettingModel';
import { EditType } from '@/type/BaseEnum';
import { isString } from '@/utils/is';
import { ref, reactive, nextTick } from 'vue';
/**
 * 新增或编辑 
 */
function useAddEditScheduleSetting(onClose: Function, onShow: Function, resetForm: (FormInstance) => void, dialog) {
    // 校验
    const addEditScheduleSettingFormRules = reactive({
        beanName: [
            { required: true, message: '请输入实例名称', trigger: 'change' }
        ],
        methodName: [
            { required: true, message: '请输入方法名称', trigger: 'change' }
        ],
        // methodParams: [
        //     { required: true, message: '请输入参数名称', trigger: 'change' }
        // ],
        cycleType: [
            { required: true, message: '请选择周期', trigger: 'change' }
        ],
    })
    // 表单数据
    const addEditScheduleSettingForm = reactive<ScheduleSettingDto>({
        beanName: '',
        methodName: '',
        methodParams: '',
        cycleType: undefined
    })
    // 列表数据
    const dealData = reactive({
        type: EditType.ADD,//状态
        cycleTypeList: [] as ListIitem[]
    })
    // ref dom
    const addEditScheduleSettingRef = ref()

    // 隐藏
    const onclose = () => {
        // 清空数据
        resetForm(addEditScheduleSettingRef.value)
        for (const key of Object.keys(addEditScheduleSettingForm)) {
            if (isString(addEditScheduleSettingForm[key])) {
                addEditScheduleSettingForm[key] = "";
            } else {
                // 否则设置为undefined
                addEditScheduleSettingForm[key] = undefined;
            }
        }
        onClose()
    }
    // 显示
    const show = (type: string, data: Recordable) => {
        // 标题
        type === EditType.ADD ? dialog.title = "新增设置" : dialog.title = "编辑设置"
        onShow()
        nextTick(() => {
            if (type === EditType.EDIT) {
                // 编辑
                Object.assign(addEditScheduleSettingForm, { ...data })
                dealData.type = EditType.EDIT
                return false
            }
            dealData.type = EditType.ADD
        })
    }

    return {
        addEditScheduleSettingForm,
        addEditScheduleSettingFormRules,
        dealData,
        addEditScheduleSettingRef,
        onclose,
        show
    }
}

export {
    useAddEditScheduleSetting
}