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

export function handleForm() {
    const formRef = ref();
    const v_num = async (rule: RuleObject, value: number) => {
        return validateCommon(value, '请输入协议价');
    };
    const formState: UnwrapRef<FormState> = reactive({
        price: 0,
    })
    const rules = {
        price: [
            { required: true, message: '请输入协议价', trigger: 'blur', type: 'number', validator: v_num },
        ],
    }
    return { rules, formState, formRef }
}

export function usePrice(formState: UnwrapRef<FormState>) {
    function increasePirce() {
        formState.price++
    }
    function decreasePrice() {
        if (formState.price) {
            formState.price--
        }
    }
    return { increasePirce, decreasePrice }
}

export function useCheckd() {
    const checked = ref<string>();
    const selected = ref<BargainList>()
    function checkGroupChange(checkedValue: string[]) {
        checked.value = checkedValue[checkedValue.length - 1];
    }
    function checkboxChange(item: BargainList) {
        selected.value = item;
    }
    return { checked, selected, checkGroupChange, checkboxChange }
}

// 计算盈亏
export function useProfit(item: BargainList) {
    const { buyorsell, holderprice, agreeunit, holderqty } = item
    // let result = '--'
    // if(buyorsell === BuyOrSell.buy) {

    // } else {

    // }
}