import { ErmcpLoginUser, ErmcpLoginUserEx } from "@/services/go/ermcp/account/interface";
import { reactive, ref, UnwrapRef } from "vue";
import { AddFuturesFormState, BusinessFormState, ResetPasswordFormState, TraderFormState } from './interface';


/**
 * 表单
 * @returns
 */
export function handleBusinessForm() {
    function initFormData() {
        return {
            logincode: '',
            userid: undefined,
            accountname: '',
            loginname: '',
            password: '',
            mobile: '',
            roleids: [],
            logintaaccounts: [],
        }
    }
    const formRef = ref();
    const formState: UnwrapRef<BusinessFormState> = reactive(initFormData())
    const rules = {
        logincode: [{ required: true, message: '请输入登录账号', trigger: 'blur' }],
        accountname: [{ required: true, message: '请输入账户名称', trigger: 'blur' }],
        loginname: [{ required: true, message: '请输入账户名称', trigger: 'blur' }],
        password: [{ required: true, message: '请输入登录密码', trigger: 'blur' }],
        mobile: [{ required: true, message: '请输入手机号码', trigger: 'blur' }],
        userid: [{ required: true, message: '请输入账户权限' }],
        roleids: [{ required: true, message: '请选择账号角色', trigger: 'change', type: 'array', }],
        logintaaccounts: [{ required: true, message: '请选择期货账户', trigger: 'change', type: 'array', }],
    }
    return { rules, formState, formRef, initFormData }
}

/**
 * 获取所属用户
 * @returns
 */
export function handleRoleName() {
    // 所属用户
    const rolename = ref<string>('');
    function getRoleName(tableList: ErmcpLoginUserEx[], selectedData: ErmcpLoginUser) {
        let flag = false;
        for (const el of tableList) {
            for (const item of el.userlist) {
                if (item.loginid === selectedData.loginid) {
                    rolename.value = el.rolename;
                    flag = true;
                    break
                }
            }
            if (flag) break
        }
    }
    return { rolename, getRoleName }
}

/**
 * 处理密码表单
 * @returns
 */
export function handlePasswordForm() {
    const formRef = ref();
    const formState: UnwrapRef<ResetPasswordFormState> = reactive({
        accountname: '',
        password: '',
        oldPassword: '',
        comfirePassword: '',
    })
    const validatePassword = () => {
        const { comfirePassword, password } = formState
        if (comfirePassword && comfirePassword === password) {
            return Promise.resolve();
        } else {
            return Promise.reject('确认新密码错误');
        }
    }
    const rules = {
        password: [{ required: true, message: '请输入账户密码', trigger: 'blur' }],
        oldPassword: [{ required: true, message: '请输入原密码', trigger: 'blur' }],
        comfirePassword: [{ required: true, validator: validatePassword, trigger: 'blur' }],

    }
    return { rules, formState, formRef }
}

/**
 *
 * @returns 交易用户
 */
export function handleTradeForm() {
    const formRef = ref();
    const formState: UnwrapRef<TraderFormState> = reactive({
        rolename: '',
        mobile: '',
    })
    const rules = {
        rolename: [{ required: true, message: '请输入用户名称', trigger: 'blur' }],
        mobile: [{ required: true, message: '请输入手机号码', trigger: 'blur' }],
    }
    return { rules, formState, formRef }
}

/**
 * 新增、修改期货账户
 * @returns
 */
export function handleAddOrModifyFuturesForm() {
    const formRef = ref();
    function initFormData() {
        return {
            accountid: '',
            accountname: '',
            password: '',
            fcid: undefined,
        }
    }
    const formState: UnwrapRef<AddFuturesFormState> = reactive(initFormData())
    const rules = {
        accountname: [{ required: true, message: '请输入账户名称', trigger: 'blur' }],
        password: [{ required: true, message: '请输入交易密码', trigger: 'blur' }],
        accountid: [{ required: true, message: '请输入期货登录账号', trigger: 'blur' }],
        fcid: [{ required: true, message: '请选择期货公司' }],
    }
    return { rules, formState, formRef, initFormData }
}

// 处理账号角色
export function handleRoles() {
    // 账户角色
    const roleTypeList = [
        { key: 22, value: '业务员' },
        { key: 23, value: '跟单员' },
    ];
    const selectedRole = ref<number[]>([]);
    function getSelectedRole(val: string) {
        //角色类型(逗号隔开,如22,23), 22:业务员 23:跟单员 24:交易员
        selectedRole.value = val.split(',').map((e) => +e);
    }
    return { roleTypeList, selectedRole, getSelectedRole }
}