import { getAccountTypeList } from "@/services/bus/account";
import { QueryPermancePlanTmpRsp } from "@/services/go/wrtrade/interface";
import { RuleObject } from "ant-design-vue/lib/form/interface";
import moment from "moment";
import { computed, onBeforeUnmount, reactive, ref, UnwrapRef } from "vue";
import { BlocsListingForm, Rules } from './interface';

/************************ ============= 仓单贸易 贸易圈 挂牌 ================ ****************/

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

// 数量
const useBlocksNumber = (formState: UnwrapRef<BlocsListingForm>) => {
    const numCheck = ref<boolean>(false); // 是否整单
    function increaseNumber() {
        formState.OrderQty++;
    }
    function decreaseNumber() {
        if (formState.OrderQty) {
            formState.OrderQty--;
        }
    }
    return { numCheck, increaseNumber, decreaseNumber };
};

// 交易账号
const useBlocksAccount = (formState: UnwrapRef<BlocsListingForm>) => {
    // 资金账号
    const accountList = getAccountTypeList([2]);
    if (accountList.length) {
        formState.accountid = accountList[0].accountid;
    }
    // 处理内部资金账号
    function handleSelectedAccount() {
        const item = getSelectedAccount()!;
        const list = getAccountTypeList([2]);
        return item.taaccounttype === 2 ? item : list[0];
    }
    function getSelectedAccount() {
        return accountList.find((e) => e.accountid === formState.accountid);
    }
    return { getSelectedAccount, accountList, handleSelectedAccount };
};

// 金额
const useBlocksMoney = (formState: UnwrapRef<BlocsListingForm>) => {
    // 挂牌金额
    function getMoney() {
        let result = 0;
        const { FixedPrice, OrderQty } = formState;
        if (FixedPrice && OrderQty) {
            result = FixedPrice * OrderQty;
        }
        return result ? result.toFixed(2) : '--';
    }
    // 履约保证金
    function getMargin() {
        let result = 0;
        const { FixedPrice, OrderQty, margin } = formState;
        if (FixedPrice && OrderQty && margin) {
            result = (FixedPrice * OrderQty * margin) / 100;
        }
        return result ? result.toFixed(2) : '--';
    }
    return { getMoney, getMargin };
};

// 选择朋友
const useBlocksFriends = (formState: UnwrapRef<BlocsListingForm>) => {
    const friendCheck = ref<boolean>(false); // 是否不限好友
    const showFriend = ref<boolean>(false);
    function chooseFriend(value: number[] | false) {
        if (value) {
            formState.friends = value;
            friendCheck.value = false
        }
        showFriend.value = false;
    }
    function openFriend() {
        showFriend.value = true;
    }
    function getFriendLength() {
        return formState.friends.length;
    }
    computed(() => {
        if (friendCheck) {
            formState.friends = []
        }
    })
    // 是否 不限好友
    function limiteFriends() {
        if (friendCheck.value) {
            formState.friends.length = 0;
        }
    }
    return { friendCheck, showFriend, chooseFriend, openFriend, getFriendLength, limiteFriends };
};

// 履约模板
const useBlocksPermaceTemp = (formState: UnwrapRef<BlocsListingForm>) => {
    const showPermance = ref<boolean>(false);
    function choosePermance(value: QueryPermancePlanTmpRsp | false) {
        if (value) {
            formState.permanceTempName = value.templatename;
            formState.permaceTempId = value.autoid;
        }
        showPermance.value = false;
    }
    function openPermance() {
        showPermance.value = true;
    }
    return { showPermance, choosePermance, openPermance };
};

// 表单
const handleForm = () => {
    const formState: UnwrapRef<BlocsListingForm> = reactive({
        accountid: undefined,
        FixedPrice: 0,
        OrderQty: 0,
        delistingQty: 0,
        margin: 0,
        DeliveryMonth: moment(),
        vidaliteTime: moment(),
        friends: [],
        permanceTempName: '',
        permaceTempId: 0,
    })
    function validateMargin(rule: RuleObject, value: number) {
        if (value) {
            return Promise.resolve();
        } else {
            return Promise.reject('请输入履约保证金');
        }
    }
    const formRef = ref();
    const rules: Rules = {
        FixedPrice: [{ required: true, message: '请输入挂牌价格', trigger: 'blur', type: 'number', }],
        OrderQty: [
            { required: true, message: '请输入挂牌数量', trigger: 'blur', type: 'number' },
            { min: 1, message: '挂牌挂牌数量不能小于1', type: 'number' }
        ],
        delistingQty: [
            { required: true, message: '请输入起摘数量', trigger: 'blur', type: 'number' },
            // { min: 1, message: '起摘数量不能小于1', type: 'number' }
        ],
        margin: [
            // { required: true, message: '请输入履约保证金', trigger: 'blur', type: 'number' },
            { required: true, validator: validateMargin, trigger: 'blur', type: 'number' }
        ],
        accountid: [{ required: true, message: '请输入交易账号' }],
        vidaliteTime: [{ required: true, message: '请选择挂牌有效期' }],
        DeliveryMonth: [{ required: true, message: '请选择交收月' }],
    }
    onBeforeUnmount(() => {
        formRef.value.resetFields();
    });
    return { rules, formState, formRef }
}


export { useBlocksPrice, useBlocksNumber, useBlocksAccount, useBlocksMoney, useBlocksFriends, useBlocksPermaceTemp, handleForm };
