<template>
  <!-- 新增交易员 -->
  <a-modal class="commonModal add-traders"
           title="新增交易员"
           v-model:visible="visible"
           @cancel="close"
           centered
           :loading="loading"
           :maskClosable="false"
           width="890px">
    <template #footer>
      <a-button key="submit"
                type="primary"
                :loading="loading"
                @click="submit">完成</a-button>
    </template>
    <a-form class="inlineForm"
            ref="formRef"
            :model="formState"
            :rules="rules">
      <a-row :gutter="24">
        <a-col :span="12">
          <a-form-item label="所属用户"
                       name="">
            <span class="white">{{selectedData.rolename}}</span>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="账户名称"
                       name="accountname">
            <a-input class="dialogInput"
                     style="width: 200px"
                     v-model:value="formState.accountname"
                     placeholder="请输入账户名称" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="登录账号"
                       name="logincode">
            <a-input class="dialogInput"
                     style="width: 200px"
                     v-model:value="formState.logincode"
                     placeholder="请输入登录账号" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="登录密码"
                       name="password">
            <a-input-password class="dialogInput"
                              style="width: 200px"
                              v-model:value="formState.password"
                              placeholder="请输入登录密码" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="手机号码"
                       name="mobile">
            <a-input class="dialogInput"
                     v-model:value="formState.mobile"
                     style="width: 200px"
                     placeholder="请输入手机号码" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="授权期货账户"
                       class="checkboxGroupItem"
                       name="logintaaccounts">
            <a-checkbox-group class="commonCheckboxGroup"
                              v-model:value="formState.logintaaccounts">
              <a-row>
                <a-col :span="12"
                       v-for="(item, index) in filerAccount()"
                       :key="index"
                       :class="[index > 1 ? 'mt10' : '']">
                  <a-checkbox :value="item.accountid"
                              v-if="item.tradestatus === 1">{{item.accountname}}/{{item.accountid}}
                  </a-checkbox>
                </a-col>
              </a-row>
            </a-checkbox-group>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-modal>
</template>

<script lang="ts">
import { closeModal } from '@/common/setup/modal/index';
import { defineComponent, ref, PropType } from 'vue';
import { ErmcpLoginUserEx, ErmcpTaAccount } from '@/services/go/ermcp/account/interface';
import { requestResultLoadingAndInfo } from '@/common/methods/request/resultInfo';
import { handleBusinessForm } from '../setup';
import { validateAction } from '@/common/setup/form';
import { BusinessFormState } from '../interface';
import { loginAccountOperate } from '@/services/proto/accountinfo';
import { LoginaccountOperateReq, LoginTaaccount } from '@/services/proto/accountinfo/interface';

export default defineComponent({
    name: 'account_info_trade_child_btn_add',
    components: {},
    props: {
        selectedData: {
            default: {},
            type: Object as PropType<ErmcpLoginUserEx>,
        },
        accountList: {
            default: [],
            type: Array as PropType<ErmcpTaAccount[]>,
        },
    },
    setup(props, context) {
        const loading = ref<boolean>(false);
        // 控制关闭弹窗
        const { visible, cancel } = closeModal('account_info_trade_child_btn_add');
        const { rules, formState, formRef } = handleBusinessForm();
        function close() {
            formRef.value.resetFields();
            cancel();
        }
        function filerAccount() {
            return props.accountList.filter((e) => e.relateduserid === props.selectedData.roleid);
        }
        function submit() {
            validateAction<BusinessFormState>(formRef, formState).then((res) => {
                const reqParam: LoginaccountOperateReq = {
                    operatetype: 10, // uint32 操作类型-1:新增 2:修改 3:新增管理员 4:修改管理员 5:锁定 6:解锁 7:注销 8:恢复 9:重置密码 10:新增登录帐号 11:停用用户 12：恢复用户 13:修改用户信息
                    logincode: res.logincode, // string 登录账号
                    accountname: res.accountname, // string 账户名称
                    password: res.password, // string 登录密码（明文）
                    mobile: res.mobile, // string 手机号码（明文）
                    roleids: [props.selectedData.roleid], // uint64 账号角色
                    userid: props.selectedData.roleid,
                    logintaaccounts: res.logintaaccounts.map((res) => {
                        const taAccount: LoginTaaccount = {
                            accountid: res,
                        };
                        return taAccount;
                    }), // LoginTaaccount 期货账户（勾选交易员必填）
                };
                requestResultLoadingAndInfo(loginAccountOperate, reqParam, loading, ['新增交易员成功', '新增交易员失败:']).then(() => {
                    close();
                    context.emit('refresh');
                });
            });
        }
        return {
            formState,
            rules,
            formRef,
            visible,
            close,
            submit,
            loading,
            filerAccount,
        };
    },
});
</script>

<style lang="less">
.add-traders {
    .checkboxGroupItem {
        .ant-row {
            .ant-col.ant-col-12 {
                white-space: nowrap;
                min-width: 356px;
            }
        }
    }
}
</style
>;

