<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">{{ selectedData.accountname }}</span>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="资金账号" name="">
            <span class="white">{{ selectedData.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 { requestResultLoadingAndInfo } from '@/common/methods/request/resultInfo';
import { _closeModal } from '@/common/setup/modal/modal';
import { geLoginID_number } from '@/services/bus/login';
import { getUserId } from '@/services/bus/user';
import { QueryAccMgrTaAccountInfo } from '@/services/go/ermcp/account';
import { ErmcpTaAccount, ErmcpTaAccountEx } from '@/services/go/ermcp/account/interface';
import { taAccountTransfersXMoneyReq } from '@/services/proto/accountinfo';
import { TaaccountTransfersxmoneyReq } from '@/services/proto/accountinfo/interface';
import { message, Modal } from 'ant-design-vue';
import { defineComponent, PropType, ref, toRaw, watchEffect } from 'vue';

export default defineComponent({
  name: 'account_info_futures_btn_child_credit',
  emits: ['cancel', 'update'],
  components: {},
  props: {
    selectedData: {
      type: Object as PropType<ErmcpTaAccount>,
      default: {},
    },
    firstSelectedData: {
      default: {},
      type: Object as PropType<ErmcpTaAccountEx>,
    },
  },
  setup(props, context) {
    const { visible, cancel } = _closeModal(context);
    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.selectedData?.accountid) {
        const param = {
          userid: getUserId(),
          accountid: props.selectedData.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.selectedData.accountid, // uint64 期货账户ID
              sxmoney: Number(param.value as number), // double 授信金额正为加负为减
              modifierid: Number(geLoginID_number()), // uint64 修改人
              areauserid: Number(props.selectedData.relateduserid), // uint64 机构用户ID
            };
            requestResultLoadingAndInfo(taAccountTransfersXMoneyReq, reqParam, loading, ['账户授信成功', '账户授信失败:']).then(() => {
              cancel(true);
            });
          } 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>
