<template>
  <!-- 新增期货账户 -->
  <a-modal class="commonModal add-futures"
           title="新增期货账户"
           v-model:visible="visible"
           @cancel="cancel"
           centered
           :maskClosable="false"
           width="890px">
    <template #footer>
      <a-button key="submit"
                type="primary"
                :loading="loading"
                @click="cancel">取消
      </a-button>
      <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="fcid">
            <a-select class="inlineFormSelect"
                      style="width: 200px"
                      v-model:value="formState.fcid"
                      placeholder="请选择期货公司">
              <a-select-option :value="item.fcid"
                               :key="item.fcid"
                               v-for="item in companyList">{{item.fcname}}
              </a-select-option>
            </a-select>
          </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="accountid">
            <a-input class="dialogInput"
                     style="width: 200px"
                     v-model:value="formState.accountid"
                     placeholder="请输入期货登录账号" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="交易密码"
                       name="password">
            <a-input-password class="dialogInput"
                              style="width: 200px"
                              autocomplete="new-password"
                              v-model:value="formState.password"
                              placeholder="请输入交易密码" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="指定交易用户"
                       name="">
            <span class="white">{{getUserName()}}</span>
          </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, watchEffect } from 'vue';
import { queryResultLoadingAndInfo, requestResultLoadingAndInfo } from '@/common/methods/request/resultInfo';
import { hedgeOutMainReq } from '@/services/proto/accountinfo';
import { ErmcpBizGroupReq, HedgeOutMainConfigReq } from '@/services/proto/accountinfo/interface';
import { ErmcpFuturesCompany } from '@/services/go/ermcp/account/interface';
import { QueryFuturesCompany } from '@/services/go/ermcp/account';
import { getUserName } from '@/services/bus/user';
import { validateAction } from '@/common/setup/form';
import { handleAddOrModifyFuturesForm } from '../setup';
import { AddFuturesFormState } from '../interface';
import { getLongTypeLoginID } from '@/services/bus/login';
import { LongType } from '@/services/socket/login/interface';
import { getUserId } from '@/services/bus/account';

export default defineComponent({
    name: 'account_info_futures_btn_add',
    components: {},
    setup(props, context) {
        // 控制关闭弹窗
        const { visible, cancel } = closeModal('account_info_futures_btn_add');
        const loading = ref<boolean>(false);
        const companyList = ref<ErmcpFuturesCompany[]>([]);
        const { rules, formState, formRef } = handleAddOrModifyFuturesForm();
        watchEffect(() => {
            if (visible.value) {
                queryResultLoadingAndInfo(QueryFuturesCompany, loading).then((res) => {
                    companyList.value = res;
                });
            }
        });

        function submit() {
            validateAction<AddFuturesFormState>(formRef, formState).then((res) => {
                let reqParam: HedgeOutMainConfigReq = {
                    marketid: 15101, // uint64 内部市场ID（HedgeOutMainConfig）
                    hedgeaccountcode: res.accountid, // string 对冲账号ID（HedgeOutMainConfig）
                    hedgeaccountpwd: res.password, // string 对冲账号密码（HedgeOutMainConfig）
                    limitnumber: 0, // uint64 挂单笔数限额默认0（HedgeOutMainConfig）
                    status: 1, // int32 渠道账号状态 1-可买入可卖出 2-可卖出不可买入默认1（HedgeOutMainConfig）
                    accountname: res.accountname, // string 账户名称accountname1（Taaccount）
                    // accountid: Number(res.accountid), // uint64 资金账户ID（Taaccount）
                    taaccounttype: 1, // int32 账号类型 - 1:外部账号 2:内部账号 3:内部做市自营账号 4:内部做市接单账号（Taaccount）
                    currencyid: 1, // int32 货币ID默认为1（Taaccount）
                    outthreshold: 0, // double 出金阈值默认为0（Taaccount）
                    ismain: 1, // int32 是否母账号 0:不是母账户 1:是母账户（Taaccount）
                    relateduserid: getUserId(), // uint64 关联用户（Taaccount）
                    maxsubaccouts: 0, // uint64 最大子账户数[最大99999][外部母账户用 默认0（TAACCOUNTCONFIG）
                    fcid: res.fcid as number, // uint64 期货公司
                    creatorid: Number(getLongTypeLoginID()), // uint64 创建人
                    modifierid: Number(getLongTypeLoginID()), // uint64 修改人
                    areauserid: getUserId(), // uint64 机构用户ID
                };
                // 期货账户主账户
                requestResultLoadingAndInfo(hedgeOutMainReq, reqParam, loading, ['新增账户成功', '新增账户失败:']).then(() => {
                    cancel();
                    context.emit('refresh');
                });
            });
        }

        return {
            formState,
            rules,
            formRef,
            visible,
            cancel,
            companyList,
            getUserName,
            submit,
            loading,
        };
    },
});
</script>

<style lang="less">
.add-futures {
}
</style
>;
