<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="cancel"
                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="24">
          <a-form-item label="指定交易用户"
                       name="traderUserId">
            <span class="white">{{getRoleName()}}</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, PropType } from 'vue';
import { validateAction } from '@/common/setup/form';
import { FormState } from '@/views/information/account_info/compoments/add-child-futures/interface';
import { HedgeOutMainConfigReq } from '@/services/proto/accountinfo/interface';
import { getLongTypeLoginID } from '@/services/bus/login';
import { getUserId } from '@/services/bus/account';
import { requestResultLoadingAndInfo } from '@/common/methods/request/resultInfo';
import { hedgeOutMainReq } from '@/services/proto/accountinfo';
import { handleBusinessForm } from '../add-child-futures/setup';
import { ErmcpLoginUserEx, ErmcpTaAccount, ErmcpTaAccountEx } from '@/services/go/ermcp/account/interface';
import { mergeTwoObj } from '@/utils/objHandle';
import { ErmcpTradeConfigTMP } from '@/services/go/ermcp/business-review/interface';

export default defineComponent({
    name: 'account_info_futures_btn_child_modify',
    components: {},
    props: {
        selectedData: {
            default: {},
            type: Object as PropType<ErmcpTaAccountEx>,
        },
        secondeData: {
            default: {},
            type: Object as PropType<ErmcpTaAccount>,
        },
        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_modify');

        const loading = ref<boolean>(false);
        const { rules, formState, formRef } = handleBusinessForm();
        watchEffect(() => {
            if (visible.value) {
                // 默认第一个 服务只放回一个，这里是个坑
                const { secondeData } = props;
                formState.traderUserId = secondeData.relateduserid;
                mergeTwoObj(formState, secondeData);
            }
        });
        function getRoleName() {
            const item = props.userList.find((e) => e.roleid === props.secondeData.relateduserid);
            return item ? item.rolename : '--';
        }
        // 增加accountid 就是修改
        function submit() {
            validateAction<FormState>(formRef, formState).then((res) => {
                let reqParam: HedgeOutMainConfigReq = {
                    accountid: props.secondeData.accountid,
                    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,
            getRoleName,
            loading,
        };
    },
});
</script>

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