import { ListIitem } from '@/api/base/BaseDataModel';
import { FeeBasicDataDto, PartitionList } from '@/api/customerfee/CustomerFeeSetModel';
import { SubmitFeeItemModel } from '@/api/feeitem/FeeItemModle';
import { BaseTableProps } from '@/components/Table/src/types';
import { BasicTableProps, VxeGridInstance, VxeTableInstance } from '@/components/VxeTable';
import { BaseFeesetAction } from '@/type/BaseFeesetAction';
import { cloneDeep } from '@/utils/cloneDeep';
import { formatDateTime } from '@/utils/dateFormate';
import { isString } from '@/utils/is';
import { ref, reactive, computed, Ref, unref } from 'vue';

/**
 * 成本费用设置-Ref
 */
function useAddEditProviderFeeSetRef() {
    // 基础数据
    const ruleFormFeeBasicDataRef = ref()
    // 一票多件ref
    const mpsdataRef = ref<{ show: (feeBasicData: Partial<FeeBasicDataDto>) => void }>()
    // 费用设置ref
    const ruleFormProviderFeesetTableRef = ref<VxeGridInstance>()
    // 费用设置的明细ref
    const ruleFormProviderFeesetDetailsTableRef = ref()
    // 添加费用类型
    const addEditProviderFeeSetRef = ref<{ show: (type: string, row?: SubmitFeeItemModel) => void }>()
    // 公式计算 ref
    const formulaModleRef = ref<{ show: (unitTypes?: ListIitem[], row?: Recordable) => void }>()
    //分区管理 ref属性
    const basicZoningRef = ref<{ show: (portInfos?: ListIitem[], partitionList?: Recordable[], actionType?: number) => void }>()
    // 分区导出模版
    const zoningExportTemplateRef = ref<{ show: (data: PartitionList[]) => void }>()
    // 分区模版加载
    const zoningLoadTemplateRef = ref<{ show: () => void }>()
    //特殊参数 ref属性
    const specialParameterRef = ref<{ show: (list: Recordable[]) => void }>()
    // 快速导入费用
    const exportExpenseRef = ref<{ show: () => void }>()
    // 费用设置 vxe-table ref
    const xFeesetSetingTableRef = ref();
    // 费用明细 vxe-table ref
    const xFeesetSetingDetailTableRef = ref<VxeTableInstance>()

    return {
        ruleFormFeeBasicDataRef,
        mpsdataRef,
        ruleFormProviderFeesetTableRef,
        ruleFormProviderFeesetDetailsTableRef,
        formulaModleRef,
        basicZoningRef,
        zoningExportTemplateRef,
        zoningLoadTemplateRef,
        specialParameterRef,
        exportExpenseRef,
        xFeesetSetingTableRef,
        xFeesetSetingDetailTableRef
    }
}
/**
 * 成本费用-费用类型实时过滤-computed
 */
function useRealTimeFeeItemListComputed(setingTable: BasicTableProps, feeItemList: Ref) {
    // 过滤费用类型
    const addFilterFeeItemList = computed(() => {
        const feeItemId = setingTable.data?.map(i => i.feeItemId).filter(j => Number(j)) || []
        const FeeItemList = feeItemList.value.filter(item => !feeItemId.includes(item.id))
        return FeeItemList
    })

    // 合并当前费用类型以及过滤使用过的费用类型
    const filterOnFeeItemList = (feeItemId: number) => {
        const cloneFeeItemList = cloneDeep(feeItemList.value) as Recordable[]
        // 过滤已选中的费用类型信息
        const filterData = cloneFeeItemList.filter(item => item.id === feeItemId)
        // 合并没有选中的费用
        let concatFees = [...filterData, ...unref(addFilterFeeItemList)]
        return concatFees
    }

    // 过滤费用明细-费用类型
    const oldFeeItemList = computed(() => {
        const feeItemId = setingTable.data?.map(item => item.feeItemId).filter(item2 => Number(item2)) || []
        const FeeItemList = feeItemList.value.filter(item => feeItemId.includes(item.id))
        return FeeItemList
    })

    return {
        addFilterFeeItemList,
        filterOnFeeItemList,
        oldFeeItemList
    }
}

/**
 * 判断费用设置主列表操作类型
 * @param baseProviderFeeSet 
 * @returns 
 */
function useBaseFeesetAction<T extends Recordable>(baseProviderFeeSet: T) {
    // 查看
    const actionsTypeView = computed(() => {
        return baseProviderFeeSet.type === BaseFeesetAction.VIEW
    })
    // 编辑
    const actionsTypeEdit = computed(() => {
        return baseProviderFeeSet.type === BaseFeesetAction.EDIT
    })
    // 版本/查看
    const actionsTypeVersionView = computed(() => {
        return baseProviderFeeSet.type === BaseFeesetAction.VIEW || baseProviderFeeSet.type === BaseFeesetAction.VERSION
    })

    return {
        actionsTypeView,
        actionsTypeEdit,
        actionsTypeVersionView
    }
}

// 自定义校验
function useDefineValidate(originData, changeData) {
    // 自定义时间校验
    const validateBeginDate = (rule: Recordable, value: any, callback: any) => {
        if (!value) return callback(new Error('请选择时间'))
        // 新增、编辑跳过
        if (originData.type === BaseFeesetAction.EDIT || originData.type === BaseFeesetAction.ADD) return callback();
        const beginDate = isString(changeData.beginDate) ? changeData.beginDate : formatDateTime(changeData.beginDate).split(" ")[0]
        if (originData.editState?.beginDate === beginDate) {
            return callback(new Error('不能与原始时间相同'))
        }
        return callback()
    }
    // 校验数据
    const validateData = reactive({
        beginDate: [{ required: true, validator: validateBeginDate, trigger: 'change' }],
    })

    return { validateData }
}

export {
    useAddEditProviderFeeSetRef,
    useRealTimeFeeItemListComputed,
    useBaseFeesetAction,
    useDefineValidate
}