<template>
  <!-- 授信-->
  <a-modal class="add-custom custom-detail"
           title="授信"
           v-model:visible="visible"
           centered
           :maskClosable="false"
           @cancel="cancel"
           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">
      <a-row :gutter="24">
        <a-col :span="12">
          <a-form-item label="账户名称"
                       name="userid">
            <span class="white">{{secondeData.accountname}}</span>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="资金账号"
                       name="">
            <span class="white">{{secondeData.accountid}}</span>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="授信总额"
                       name="">
            <span class="white"> {{mortgagecredit}}</span>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="子账户当前余额"
                       name="">
            <span class="white">{{currentbalance}}</span>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="授信金额"
                       class="relative"
                       name="">
            <a-input class="dialogInput"
                     style="width: 200px"
                     v-model:value="money"
                     placeholder="请输入授信金额"></a-input>
            <div class="tip">正数为增加，负数为减少</div>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-modal>
</template>

<script lang="ts">
import { defineComponent, PropType, ref, toRaw, watchEffect } from 'vue';
import { closeModal } from '@/common/setup/modal/index';
import { ErmcpLoginUser, ErmcpTaAccount, ErmcpTaAccountEx } from '@/services/go/ermcp/account/interface';
import { message, Modal } from 'ant-design-vue';
import { requestResultLoadingAndInfo } from '@/common/methods/request/resultInfo';
import { LoginaccountOperateReq, TaaccountTransfersxmoneyReq } from '@/services/proto/accountinfo/interface';
import { loginAccountOperate, taAccountTransfersXMoneyReq } from '@/services/proto/accountinfo';
import { getLongTypeLoginID } from '@/services/bus/login';
import { QueryAccMgrTaAccountInfo } from '@/services/go/ermcp/account';
import { getUserId } from '@/services/bus/account';

export default defineComponent({
    name: 'account_info_futures_btn_child_credit',
    components: {},
    props: {
        selectedData: {
            type: Object as PropType<ErmcpTaAccount>,
            default: {},
        },
        secondeData: {
            default: {},
            type: Object as PropType<ErmcpTaAccount>,
        },
    },
    setup(props, context) {
        const { visible, cancel } = closeModal('account_info_futures_btn_child_credit');
        const loading = ref<boolean>(false);
        // 当前余额
        const currentbalance = ref<number>(0);
        // 授信总额
        const mortgagecredit = ref<number>(0);
        const money = ref<number | null>(null);
        watchEffect(() => {
            if (visible.value && props.secondeData?.accountid) {
                const param = {
                    userid: getUserId(),
                    accountid: props.secondeData.accountid,
                };
                QueryAccMgrTaAccountInfo(param).then((res) => {
                    console.log(res);
                    if (res.length) {
                        currentbalance.value = res[0].currentbalance;
                        mortgagecredit.value = res[0].mortgagecredit;
                    }
                });
            }
        });

        function submit() {
            Modal.confirm({
                title: '是否确认授信账户',
                okText: '确认授信',
                cancelText: '取消',
                onOk() {
                    const param = toRaw(money);
                    if (param) {
                        let reqParam: TaaccountTransfersxmoneyReq = {
                            accountid: props.secondeData.accountid, // uint64 期货账户ID
                            sxmoney: Number(param.value as number), // double 授信金额正为加负为减
                            modifierid: Number(getLongTypeLoginID()), // uint64 修改人
                            areauserid: Number(props.secondeData.relateduserid), // uint64 机构用户ID
                        };
                        requestResultLoadingAndInfo(taAccountTransfersXMoneyReq, reqParam, loading, ['账户授信成功', '账户授信失败:']).then(() => {
                            cancel();
                            context.emit('refresh');
                        });
                    } else {
                        message.error('请输入授信金额');
                    }
                },
                onCancel() {},
            });
        }

        return {
            visible,
            cancel,
            maskClosableFlag: false,
            submit,
            loading,
            currentbalance,
            mortgagecredit,
            money,
        };
    },
});
</script>

<style lang="less">
.custom-detail {
    .ant-form.inlineForm {
        margin-top: 20px;
    }
}
</style>;
