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

// TODO 处理：新增或编辑汇率版本
export default function useAddEditBillRate(dialog: DialogModel, onClose: Function, onShow: Function, methodsTable: TableActionType, emit, data: BaseTableProps, store) {
    //获取全局属性
    const { global } = useInstance()
    // 定义基础数据
    const state = reactive({
        type: EditType.ADD,
        form: {
            titleName: '',
            date: [],
        },
        rules: {
            titleName: [{ required: true, message: "请输入名称", trigger: "blur" }],
            date: [{ required: true, message: "请选择时间", trigger: "blur" }],
        }
    })
    // 币种列表
    const currencyList = ref<ListIitem[]>()
    // 基础表单的ref
    const addbillrateFormRef = ref<FormInstance>()
    const ruleFormBillMainTableRef = ref<FormInstance>()

    // 校验、重置表单
    const { formValidate, resetForm } = useEleFormValidate();

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

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

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

        // 动态-表格数据
        const data = methodsTable.getDataSource()
        // 整合提交的数据
        const prpos = {
            type: state.type,
            form: state.form,
            list: data
        }
        // console.log(unref(prpos), ":整合提交的数据");
        emit("saveBillRate", 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;
        }
        // console.log(row, ":行数据", data, ":表格数据");
    }

    //当期日期以后的的时间(暂时失效，待处理)
    const pickerOptions = {
        disabledDate(time) {
            return time.getTime() < Date.now() - 8.64e7;
        }
    };

    //  禁用当日之前的时间
    const disabledDate = (time: Date) => {
        // 一天的时间戳
        const daysTime = 24 * 60 * 60 * 1000
        return time.getTime() < Date.now() - daysTime;
    }

    return {
        show,
        confirm,
        hanleOnClose,
        hanleAddColumns,
        disabledDate,
        pickerOptions,
        addbillrateFormRef,
        ruleFormBillMainTableRef,
        currencyList,
        state
    }
}