import { RuleItem } from '@/common/setup/warehouse_receipt_trade/interface';
import { getGoodsById, getQuoteDayInfoByCode } from '@/services/bus/goods';
import { getRules } from '@/services/bus/rules';
import { RuleObject } from "ant-design-vue/lib/form/interface";

// 验证 挂牌数量
/**
 *
 * @param delistingQty 起摘数量
 * @param 挂牌数量对应的key
 * @param minivalue 最小单位
 * @param maxValue 最大数量（可选)
 * @returns
 */
export const useVerifyListingNum = <T extends object, k extends keyof T>(formState: T, key: k, minivalue: number, maxValue?: number) => {
    const v_num = async (rule: RuleObject, value: number) => {
        if (value) {
            if (formState[key] && value < (formState[key] as unknown as number)) {
                return Promise.reject(`挂牌数量不能小于起摘数量`)
            }
            if (value < minivalue) {
                return Promise.reject(`挂牌数量不能小于${minivalue}`)
            }
            if (maxValue && value > maxValue) {
                return Promise.reject(`挂牌数量不能大于${maxValue}`)
            }
            if (value % minivalue) {
                return Promise.reject(`挂牌数量不是${minivalue}的倍数`)
            }

            return Promise.resolve();

        } else {
            return Promise.reject('请输入挂牌数量')
        }
    };
    return { v_num }
}


/**
 * 验证 挂牌基差
 * @param formState 表单状态
 * @param key 商品ID对应的key
 * @returns
 */
export const useVerifyListingBasis = <T, k extends keyof T>(formState: T, key: k) => {
    const v_basis = async (rule: RuleObject, value: number) => {
        if (formState[key]) {
            if (value) {
                const goodsCode = getGoodsById(formState[key] as unknown as number)?.goodscode as string;
                const quote = getQuoteDayInfoByCode(goodsCode);
                if (quote) {
                    const { basisdownratio, basisupratio } = getRules()[0];
                    // 最小值 = 基差下限百分比 * 昨日收盘价 * -1
                    const minValue = (basisdownratio * quote.presettle * -1).toFixed(2);
                    // 最大值 = 基差上限百分比 * 昨日收盘价
                    const maxValue = (basisupratio * quote.presettle).toFixed(2);

                    if (value < Number(minValue)) {
                        return Promise.reject(`基差不能小于${minValue}`);
                    }
                    if (value > Number(maxValue)) {
                        return Promise.reject(`基差不能大于${maxValue}`);
                    }
                    return Promise.resolve();
                }
                return Promise.reject('商品合约不存在');
            } else {
                return Promise.reject('请输入基差');
            }
        } else {
            return Promise.reject('请选择商品合约');
        }
    };
    return { v_basis }
}

/**
 * 验证 起摘数量
 * @param minValue 最小变动值
 */
export const useVerifyBeginDelistingNum = (minValue: number) => {
    const v_beginDelistingNum = async (rule: RuleObject, value: number) => {
        if (value) {
            if (minValue) {
                if (value % minValue !== 0) {
                    return Promise.reject(`起摘数量不是${minValue}的倍数`);
                }
            }
            return Promise.resolve();
        } else {
            return Promise.reject('请输入起摘数量');
        }
    }
    const v_beginDelistingRules: RuleItem[] = [{ required: true, validator: v_beginDelistingNum, trigger: 'blur', type: 'number', }]
    return { v_beginDelistingNum, v_beginDelistingRules }
}