import { ListIitem } from '@/api/base/BaseDataModel';
import { ProductRateRatioDto } from '@/api/productrateratio/apiProductRateRatioModel';
import { EditType } from '@/type/BaseEnum';
import { cloneDeep } from '@/utils/cloneDeep';
import { dealEmptyObject } from '@/utils/dealData';
import { isArrayLength } from '@/utils/is';
import { FormInstance } from 'element-plus';
import { ref, reactive, computed, watch, unref, nextTick } from 'vue';
/**
 * 新增或编辑产品费率
 */
function useAddEditProductRateRatio(onClose: Function, onShow: Function, dialog: Recordable, props: Recordable, resetForm: (FormInstance) => void) {
    // 校验
    const addEditProductRateRatioFormRules = reactive({
        productId: [
            { required: true, message: '请选择产品名称', trigger: 'change' }
        ],
        feeItemId: [
            { required: true, message: '请选择费用类型', trigger: 'change' }
        ],
        cycleType: [
            { required: true, message: '请选择周期', trigger: 'change' }
        ],
        productIdList: [
            { required: true, message: '请选择共享产品', trigger: 'change' }
        ],
        providerIdList: [
            { required: true, message: '请选择服务商', trigger: 'change' }
        ]
    })
    // 表单数据
    const addEditProductRateRatioForm = reactive<ProductRateRatioDto>({
        id: undefined,
        productId: undefined,
        productName: undefined,
        feeItemId: undefined,
        feeItemName: undefined,
        cycleType: undefined,
        productIdList: [],
        providerIdList: [],
        productNameList: [],
        providerNameList: [],
        rateRatioState: 0,
        rateRatioStateName: '生效',
    })
    // 处理过滤产品列表数据
    const dealData = reactive({
        type: EditType.ADD,//状态
        stateList: [{ label: '生效', value: 0 }, { label: '停用', value: 1 }],
        feeItemLists: [] as ListIitem[],//费用类型
        productList: [] as ListIitem[],
        dealProductList: [] as ListIitem[],//产品名称的列表
        dealProductIdList: [] as ListIitem[],//共享产品的列表
    })
    // ref dom
    const addEditProductRateRatioRef = ref()
    // 产品类型
    const productList = computed(() => props.productList)
    // 费用类型
    const feeItemLists = computed(() => props.feeItemLists)
    // 产品名称
    const getProductName = (value: string | number) => {
        const findObj = dealData.productList.find(fi => fi.value === value)
        addEditProductRateRatioForm.productName = findObj?.label
        // 更新共享产品
        dealData.dealProductIdList = dealData.productList.filter(ft => ft.value !== value)
    }
    // 费用科目名称
    const getFeeItemName = (value: string | number) => {
        const findObj = dealData.feeItemLists.find(fi => fi.value === value)
        addEditProductRateRatioForm.feeItemName = findObj?.label
    }
    // 状态名称
    const changeRateRatioStateName = (value: string | number) => {
        const findObj = dealData.stateList.find(fi => fi.value === value)
        addEditProductRateRatioForm.rateRatioStateName = findObj?.label
    }
    // 共享产品名称
    const changeProductNameList = (valArr: string[], selectList: ListIitem[]) => {
        const filteredData = selectList.filter(obj => valArr.includes(String(obj.value)));
        addEditProductRateRatioForm.productNameList = filteredData.map((item) => item.label)
        // 更新产品名称
        dealData.dealProductList = dealData.productList.filter(ft => !valArr.includes(String(ft.value)))
    }
    // 成本共享服务商名称
    const changeProviderNameList = (valArr: string[], selectList: ListIitem[]) => {
        const filteredData = selectList.filter(obj => valArr.includes(String(obj.value)));
        addEditProductRateRatioForm.providerNameList = filteredData.map((item) => item.label)
    }

    // 显示
    const show = async (type: string, data?: Recordable) => {
        // 标题
        type === EditType.ADD ? dialog.title = "新增产品费率" : dialog.title = "编辑产品费率"
        if (type === EditType.EDIT) {
            // 编辑
            Object.assign(addEditProductRateRatioForm, { ...data, types: null })
            // 过滤对应的产品列表
            dealData.dealProductList = dealData.productList.filter(ft => !addEditProductRateRatioForm.productIdList?.includes(String(ft.value)))
            dealData.dealProductIdList = dealData.productList.filter(ft => ft.value !== addEditProductRateRatioForm.productId)
            dealData.type = EditType.EDIT
        } else {
            dealData.type = EditType.ADD
        }
        onShow()
    }

    // 隐藏
    const onclose = () => {
        const newObj = dealEmptyObject(addEditProductRateRatioForm)
        Object.assign(addEditProductRateRatioForm, newObj)
        addEditProductRateRatioForm.feeItemId = undefined
        addEditProductRateRatioForm.rateRatioState = 0
        nextTick(() => {
            // 清空数据
            resetForm(addEditProductRateRatioRef.value)
        })
        onClose()
    }

    watch(() => productList.value, () => {
        // 初始化产品列表数据
        const cloneProductList = cloneDeep(unref(productList))
        dealData.productList = cloneProductList
        dealData.dealProductList = cloneProductList
        dealData.dealProductIdList = cloneProductList
    })
    watch(() => feeItemLists.value, (val: Recordable[]) => {
        // 初始化费用类型
        if (isArrayLength(val)) dealData.feeItemLists = val.map(item => ({ label: item.itemName, value: item.id }))
    })

    return {
        addEditProductRateRatioFormRules,
        addEditProductRateRatioForm,
        addEditProductRateRatioRef,
        dealData,
        getProductName,
        getFeeItemName,
        changeProductNameList,
        changeProviderNameList,
        changeRateRatioStateName,
        show,
        onclose
    }
}

export {
    useAddEditProductRateRatio
}