import { validateCommon } from "@/common/setup/validate";
import { RuleObject } from "ant-design-vue/lib/form/interface";
import { onBeforeUnmount, reactive, ref, UnwrapRef } from "vue";
import { FormParam } from "./interface";


const formState: UnwrapRef<FormParam> = reactive({
    accountid: undefined,
    num: 0,
    priceType: 1,
    price: 0,
})

function initForm() {
    formState.num = 0
}
export function handleForm() {
    const v_price = async (rule: RuleObject, value: number) => {
        return validateCommon(value, '请输入限价');
    };
    const formRef = ref();
    const rules = {
        num: [{ required: true, message: '请输入挂牌数量', trigger: 'blur', type: 'number', min: 1 }],
        price: [{ required: true, trigger: 'blur', type: 'number', validator: v_price }],
    }
    onBeforeUnmount(() => {
        initForm()
        formRef.value.resetFields()
    })
    return { rules, formState, formRef }
}

// 价格
export const useBlocksPrice = () => {
    const priceCheck = ref<boolean>(false); // 是否可议价
    function increasePrice() {
        formState.price++;
    }
    function decreasePrice() {
        if (formState.price) {
            formState.price--;
        }
    }
    return { priceCheck, increasePrice, decreasePrice };
};

// 数量
export const useBlocksNumber = () => {
    function increaseNumber() {
        formState.num++;
    }
    function decreaseNumber() {
        if (formState.num) {
            formState.num--;
        }
    }
    return { increaseNumber, decreaseNumber };
};