import type { BasicTableProps } from '@/components/VxeTable';
import { api_accountPeriodAdd, api_accountPeriodChangeStatus, api_accountPeriodDelete, api_accountPeriodEdit, api_accountPeriodList, api_batchEditTmpReceive } from '@/api/accountPeriod/accountPeriod';
import { AccountPeriodDto, AccountPeriodParam, BatchEditTmpReceive } from '@/api/accountPeriod/accountPeriodModel';
import { useDropDownData } from '@/hooks/useDropDownData';
import { useEleFormValidate } from '@/hooks/useEleFormValidate';
import { useQueryList } from '@/hooks/useQueryList';
import { AccountPeriodType } from '@/type/BaseEnum';
import { cloneDeep } from '@/utils/cloneDeep';
import { resCodeSuccess, useReturnData } from '@/utils/useReturnData';
import { ref, reactive, onMounted, unref } from 'vue';

const { resData } = useReturnData()
/**
 * 账期列表数据处理
 * @returns 
 */
function useAccountPeriodList(accountPeriodOptions: BasicTableProps) {
    const { storeDropDownData, customertList, accountPeriodTypeList } = useDropDownData()
    const accountPeriodListState = reactive({
        loading: false,
        deleteFlagList: [
            { value: false, label: '启用' },
            { value: true, label: '停用' },
        ]
    })
    const accountPeriodListParams = reactive<AccountPeriodParam>({
        customerId: undefined,
        accountPeriod: undefined,
        deleteFlag: undefined,
        pageNum: 1,
        pageSize: 10,
        total: 10,
    })
    const accountPeriodFormRef = ref()
    const accountPeriodEditRef = ref<{ show: Fn }>()
    const accountPeriodLimitEditRef = ref<{ show: Fn }>()
    // 添加
    const addBtn = () => {
        unref(accountPeriodEditRef)?.show(AccountPeriodType.ADD)
    }
    const batchModification = () => {
        unref(accountPeriodLimitEditRef)?.show()
    }
    // 初始化
    const initList = async () => {
        try {
            accountPeriodListState.loading = true
            accountPeriodOptions.loading = true
            const res = await api_accountPeriodList(listParams)
            if (resCodeSuccess(res)) {
                accountPeriodOptions.data = res.data.records
                accountPeriodListParams.total = res.data.total
            }
        } finally {
            accountPeriodListState.loading = false
            accountPeriodOptions.loading = false
        }
    }
    // 重置
    const { resetForm } = useEleFormValidate()
    const resetParams = () => {
        resetForm(accountPeriodFormRef.value)
    }
    // 初始化列表、分页数据
    const { sizeChange, currentChange, searchBtn, resetBtn, listParams } = useQueryList<AccountPeriodParam>(initList, resetParams, accountPeriodListParams)
     // 操作类型
     const hanleChangeAction = async (data: Recordable) => {
        const row = cloneDeep(data)
        delete row._X_ROW_KEY
        const types = row.types;
        switch (types) {
            // 编辑
            case AccountPeriodType.EDIT:
                unref(accountPeriodEditRef)?.show(AccountPeriodType.EDIT, row)
                break;
            // 删除
            case AccountPeriodType.DELETE:
                deleteAccountPeriod(row.id)
                break;
            // 查看
            case AccountPeriodType.VIEW:
                break;
            // 是否禁用
            case AccountPeriodType.FEESET_ACTIVE:
                activeAccountPeriod(row.id, !row.deleteFlag)
                break
        }
    }
    // 新增或编辑
    const hanleSaveAccountPeriod = async (params: AccountPeriodDto, setLoading: Fn, onclose: Fn) => {
        try {
            if(params.id) {
                const res = await api_accountPeriodEdit(params)
                const addData = resData(res, '新增成功')
                if (addData == 500) return
                initList()
                onclose()
            } else {
                const res = await api_accountPeriodAdd(params)
                const editData = resData(res, '编辑成功')
                if (editData == 500) return
                initList()
                onclose()
            }
        } finally {
            setLoading(false)
        }
    }
    // 删除
    const deleteAccountPeriod = async (id: number) => {
        const res = await api_accountPeriodDelete({ id })
        const deleteData = resData(res, '删除成功')
        if (deleteData !== 500) initList()
    }
    // 启用/停用
    const activeAccountPeriod = async (id: number, deleteFlag: boolean) => {
        const res = await api_accountPeriodChangeStatus({ id, deleteFlag })
        const activeData = resData(res, '操作成功')
        if (activeData !== 500) initList()
    }
    // 批量修改额度
    const hanleAccountPeriodLimit = async (params: BatchEditTmpReceive, setLoading: Fn, onclose: Fn) => {
        try {
            const res = await api_batchEditTmpReceive(params)
            const activeData = resData(res, '操作成功')
            if (activeData !== 500) {
                onclose()
                initList()
            }
        } finally {
            setLoading(false)
        }
    }
    
    onMounted(() => {
        storeDropDownData.addCustomertList()
        storeDropDownData.addAccountPeriodTypeList()
    })

    return {
        accountPeriodListState,
        accountPeriodListParams,
        accountPeriodFormRef,
        accountPeriodEditRef,
        accountPeriodLimitEditRef,
        customertList,
        accountPeriodTypeList,
        sizeChange,
        currentChange, 
        searchBtn, 
        resetBtn, 
        addBtn,
        batchModification,
        hanleChangeAction,
        hanleSaveAccountPeriod,
        hanleAccountPeriodLimit
    }
}

export {
    useAccountPeriodList
}