import type { BasicTableProps } from '@/components/VxeTable';
import { api_customerFeeApproval, api_customerFeeApprovalRecordList } from '@/api/customerFeeApproval/customerFeeApproval';
import { CustomerFeeApprovalRecordParam, CustomerFeeApprovalStatusDto } from '@/api/customerFeeApproval/customerFeeApprovalModel';
import { useDropDownData } from '@/hooks/useDropDownData';
import { useEleFormValidate } from '@/hooks/useEleFormValidate';
import { useQueryList } from '@/hooks/useQueryList';
import { useFeesetList } from '@/store/list_feeset';
import { usePersonnel } from '@/store/list_personnel';
import { EditType } from '@/type/BaseEnum';
import { cloneDeep } from '@/utils/cloneDeep';
import { formatDateTime } from '@/utils/dateFormate';
import { isArrayLength } from '@/utils/is';
import { resCodeSuccess, useReturnData } from '@/utils/useReturnData';
import { ref, reactive, unref, computed, onMounted } from 'vue';
import { FeeApprovalStatus } from '@/type/BaseBusinessFeeseType';

/**
 * 处理审批记录
 * @returns 
 */
const { resData } = useReturnData()
function useCustomerFeeApproval(customerFeeApprovalOptions: BasicTableProps) {
    const storePersonnel = usePersonnel()
    const storeFeeset = useFeesetList();
    const { storeDropDownData, productList } = useDropDownData()
    const searchFormRef = ref()
    // 审批弹窗 ref
    const customerFeeApprovalRef = ref<{ show: Fn }>()
    const customerFeeApprovalParams = reactive<CustomerFeeApprovalRecordParam>({
        salesmanId: undefined,
        feeId: undefined,
        productId: undefined,
        beginDate: undefined,
        endDate: undefined,
        pageNum: 1,
        pageSize: 10,
        total: 0
    })
    const customerFeeApprovalState = reactive({
        loading: false,
        dateArr: [],
        isEdit: true,
    })

    // 处理时间参数
    const dealBeginDateEndDate = () => {
        const dateArr = customerFeeApprovalState.dateArr
        if (isArrayLength(dateArr)) {
            listParams.beginDate = formatDateTime(dateArr[0])
            listParams.endDate = formatDateTime(dateArr[1])
        } else {
            listParams.beginDate = undefined
            listParams.endDate = undefined
        }
    }
    // 初始化列表数据
    const initList = async () => {
        dealBeginDateEndDate()
        const res = await api_customerFeeApprovalRecordList(listParams)
        if (resCodeSuccess(res)) {
            customerFeeApprovalOptions.data = res.data.records
            customerFeeApprovalParams.total = res.data.total
        }
    }

    const { resetForm } = useEleFormValidate()
    // 重置
    const resetParams = () => {
        customerFeeApprovalState.dateArr = []
        resetForm(searchFormRef.value)
    }
    // 初始化列表、分页数据
    const { sizeChange, currentChange, searchBtn, resetBtn, listParams } = useQueryList<CustomerFeeApprovalRecordParam>(initList, resetParams, customerFeeApprovalParams)
    // 操作
    const handleClickChange = (row) => {
        const data = cloneDeep(row)
        switch (data.types) {
            case EditType.APPROVE:
                customerFeeApprovalState.isEdit = true
                unref(customerFeeApprovalRef)?.show(data)
                break;
            case EditType.VIEW:
                customerFeeApprovalState.isEdit = false
                unref(customerFeeApprovalRef)?.show(data)
                break;
        }
    }
    // 审批
    const confirmApproval = async (params: CustomerFeeApprovalStatusDto, onclose: Fn, setLoading: Fn) => {
        const res = await api_customerFeeApproval(params)
        const codeData = resData(res, '操作成功')
        if (codeData !== 500) {
            setLoading(false)
            onclose() 
            initList()
        }
    }

    onMounted(() => {
        storePersonnel.addGetSalesmanUserList()
        storeDropDownData.addProductList()
        storeFeeset.addCustomerFeeSetNameList()
        storeFeeset.addFeeApprovalStatusList()
    })
    const salesmanUserList = computed(() => storePersonnel.getSalesmanUsesList)
    const customerFeeSetNameList = computed(() => storeFeeset.getCustomerFeeSetNameList);
    const feeApprovalStatusList = computed(() => {
        return storeFeeset.getFeeApprovalStatusList.filter((item) => item.value > FeeApprovalStatus.PENDING_APPROVAL)
    });

    return {
        searchFormRef,
        customerFeeApprovalParams,
        customerFeeApprovalState,
        sizeChange,
        currentChange,
        searchBtn,
        resetBtn,
        handleClickChange,
        customerFeeApprovalRef,
        salesmanUserList,
        customerFeeSetNameList,
        productList,
        feeApprovalStatusList,
        confirmApproval
    }
}

export {
    useCustomerFeeApproval
}