import { ref, reactive, unref } from 'vue';
import { BaseTableProps, TableActionType } from "@/components/Table/src/types";
import { DialogModel } from "@/type/BastType";
import { EditType, Title } from '@/type/BaseEnum';
import { FormInstance } from 'element-plus';
import { useEleFormValidate } from '@/hooks/useEleFormValidate';
import { ListIitem } from '@/api/base/BaseDataModel';
import { newDate } from '@/utils/dateTimeStr';
import { getCustomerRateversionListApi } from '@/api/customerrate/customerrate';
import { useFilterTypeList } from '@/hooks/useFilterTypeList';

/**
 * 新增或编辑客户汇率
 * 
 * @param dialog 弹窗属性
 * @param onClose 取消弹窗
 * @param onShow 显示弹窗
 * @param dataTable 表格基础数据
 * @param methodsTable 表格操作
 * @param store pinia状态数据
 * @param emit 事件
 */
export default function useAddEditCurrencyRate(dialog: DialogModel, onClose: Function, onShow: Function, dataTable: BaseTableProps, methodsTable: TableActionType, emit, store) {
    // 定义基础数据
    const state = reactive({
        type: EditType.ADD,
        form: {
            titleName: '',
            date: [],
        }
    })
    // 币种列表
    const currencyList = ref<ListIitem[]>()
    // 基础表单的ref
    const addEditCustomerRateFormRef = ref<FormInstance>()
    const ruleFormCustomerRateTableRef = ref<FormInstance>()
    // 校验、重置表单
    const { formValidate, resetForm } = useEleFormValidate();

    //处理币种类型数据 
    const { newSourceLists, filterNewsList } = useFilterTypeList<BaseTableProps>(dataTable, 'currencyCode', currencyList, 'value')

    // 显示操作 
    const show = async (type: string, row?: Recordable) => {
        dialog.title = EditType.ADD === type ? Title.ADD : Title.EDIT
        // 获取币种列表
        currencyList.value = store.getCurrencyList
        // 表格编辑
        onShow()
        if (row && type == EditType.EDIT) {
            const res = await getCustomerRateversionListApi({ rateVersionId: row.rateVersionId })
            const dataArr = res.data
            if (res.code === 200) {
                state.form.titleName = row.titleName
                state.form.date = [newDate(row.startDate), newDate(row.endDate)] as never[]
                dataTable.dataSource = dataArr
            }
            //设置编辑属性
            state.type = type;
        }
    }

    // 取消弹窗
    const hanleOnClose = () => {
        resetForm(addEditCustomerRateFormRef.value)
        // 初始化内容
        // 清空表格数据
        dataTable.dataSource = []
        // 默认新增
        state.type = EditType.ADD
        onClose()
    }

    // 获取的数据
    const confirm = async () => {
        // 基础表单-校验
        const isFormValidate = await formValidate(addEditCustomerRateFormRef.value)
        if (!isFormValidate) return;
        // 动态表格-校验
        const isFormValidate2 = await formValidate(ruleFormCustomerRateTableRef.value)
        if (!isFormValidate2) return;

        // 动态-表格数据
        const data = methodsTable.getDataSource()
        // 整合提交的数据
        const prpos = {
            type: state.type,
            form: state.form,
            list: data
        }
        // console.log(unref(prpos), ":整合提交的数据");
        emit("saveCustomerRate", unref(prpos), hanleOnClose)//事件、取消弹窗
        // 取消弹窗
        // hanleOnClose()
    }


    // 操作行数据
    const hanleAddColumns = (actionType: string, row?: Recordable) => {
        const index = row?.$index
        const data = methodsTable.getDataSource()
        switch (actionType) {
            case EditType.ADD:
                data.push({})
                break;
            // 删除操作
            case EditType.DELETE:
                data.splice(index, 1)
                break;
            default:
                break;
        }
    }

    return {
        hanleAddColumns,
        hanleOnClose,
        confirm,
        show,
        addEditCustomerRateFormRef,
        ruleFormCustomerRateTableRef,
        state,
        newSourceLists,
        filterNewsList
    }
}