<template>
  <!-- 新增期货子账户 -->
  <a-modal class="commonModal add-futures-son"
           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="userinfotype">
            <span class="white">{{selectedData.mainAcc.accountname}}/{{selectedData.mainAcc.hedgeaccountcode}}</span>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="账户名称"
                       name="accountname">
            <a-input class="dialogInput"
                     v-model:value="formState.accountname"
                     style="width: 200px"
                     placeholder="请输入账户名称" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="期货保证金"
                       name="trademargintmpid">
            <a-select class="inlineFormSelect"
                      style="width: 200px"
                      v-model:value="formState.trademargintmpid"
                      placeholder="请选择保证金模板">
              <a-select-option v-for="item in marginList"
                               :key="item.tradeconfigtmpid"
                               :value="item.tradeconfigtmpid">
                {{item.tradeconfigtmpname}}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="期货手续费"
                       name="tradefeetmpid">
            <a-select class="inlineFormSelect"
                      style="width: 200px"
                      v-model:value="formState.tradefeetmpid"
                      placeholder="请选择期货手续费">
              <a-select-option v-for="item in feeList"
                               :key="item.tradeconfigtmpid"
                               :value="item.tradeconfigtmpid">
                {{item.tradeconfigtmpname}}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="指定交易用户"
                       name="traderUserId">
            <a-select class="inlineFormSelect"
                      style="width: 200px"
                      v-model:value="formState.traderUserId"
                      placeholder="请选择交易用户">
              <a-select-option v-for="item in userList"
                               :key="item.roleid"
                               :value="item.roleid">
                {{item.rolename}}
              </a-select-option>
            </a-select>
            <a-button @click="addTrader"
                      class="operBtn">新增</a-button>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
    <AddTrader @refresh="updateTrader" />
  </a-modal>
</template>

<script lang="ts">
import { closeModal, openModal } from '@/common/setup/modal/index';
import { defineComponent, PropType, ref, watchEffect } from 'vue';
import { requestResultLoadingAndInfo } from '@/common/methods/request/resultInfo';
import {ErmcpBizGroupReq, HedgeOutMainConfigReq} from '@/services/proto/accountinfo/interface';
import { validateAction } from '@/common/setup/form';
import { FormState } from './interface';
import { handleBusinessForm, handleTradeTemplate } from './setup';
import { hedgeOutMainReq } from '@/services/proto/accountinfo';
import { ErmcpLoginUserEx, ErmcpTaAccountEx } from '@/services/go/ermcp/account/interface';
import { handlerManagerList } from '@/common/setup/user';
import {getLongTypeLoginID} from "@/services/bus/login";
import {getUserId} from "@/services/bus/account";
import AddTrader from '../add-traders/index.vue'
import { ErmcpTradeConfigTMP } from '@/services/go/ermcp/business-review/interface';

export default defineComponent({
    name: 'account_info_futures_btn_add',
    components: {
      AddTrader,
    },
    props: {
        selectedData: {
            type: Object as PropType<ErmcpTaAccountEx>,
            default: {},
        },
        userList: {
            type: Array as PropType<ErmcpLoginUserEx[]>,
            default: [],
        },
        marginList: {
            type: Array as PropType<ErmcpTradeConfigTMP[]>,
            default: [],
        },
        feeList: {
            type: Array as PropType<ErmcpTradeConfigTMP[]>,
            default: [],
        },
    },
    setup(props, context) {
        // 控制关闭弹窗
        const { visible, cancel } = closeModal('account_info_futures_btn_child_add');
        // 新增交易用户
        const {openAction: addTrader} = openModal('account_info_trade_btn_add') 
        const loading = ref<boolean>(false);
        const { rules, formState, formRef } = handleBusinessForm();
        function updateTrader() {
          context.emit('updateTrader')
        }
        function submit() {
            validateAction<FormState>(formRef, formState).then((res) => {
                let reqParam: HedgeOutMainConfigReq = {
                    marketid: 15101, // uint64 内部市场ID（HedgeOutMainConfig）
                    limitnumber: 0, // uint64 挂单笔数限额默认0（HedgeOutMainConfig）
                    status: 1, // int32 渠道账号状态 1-可买入可卖出 2-可卖出不可买入默认1（HedgeOutMainConfig）
                    accountname: res.accountname, // string 账户名称accountname1（Taaccount）
                    taaccounttype: 1, // int32 账号类型 - 1:外部账号 2:内部账号 3:内部做市自营账号 4:内部做市接单账号（Taaccount）
                    currencyid: 1, // int32 货币ID默认为1（Taaccount）
                    outthreshold: 0, // double 出金阈值默认为0（Taaccount）
                    ismain: 0, // int32 是否母账号 0:不是母账户 1:是母账户（Taaccount）
                    parentaccountid: props.selectedData.mainAcc.accountid, // uint64 所属根账号（Taaccount）
                    relateduserid: res.traderUserId, // uint64 关联用户（Taaccount）
                    fromaccountid: props.selectedData.mainAcc.accountid, // uint64 所属上级账户（Taaccount）
                    trademargintmpid: res.trademargintmpid, // uint64 保证金模板id（TAACCOUNTCONFIG）
                    tradefeetmpid: res.tradefeetmpid, // uint64 手续费模板ID（TAACCOUNTCONFIG）
                    maxsubaccouts: 0, // uint64 最大子账户数[最大99999][外部母账户用 默认0（TAACCOUNTCONFIG）
                    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,
            submit,
            addTrader,
            updateTrader,
            loading,
        };
    },
});
</script>

<style lang="less">
.add-futures-son {
    .inlineForm {
        .operBtn {
            position: absolute;
            top: -5px;
            margin-bottom: 0;
            margin-top: 0;
        }
    }
}
</style
>;
