<template>
  <!-- 发票登记-->
  <a-modal class="commonModal paddingDialog invoice" :title="selectedRow.contracttype===1? '采购合同-发票登记': '销售合同-发票登记'" v-model:visible="visible" centered @cancel="cancel" width="890px">
    <template #footer>
      <a-button key="submit" class="cancelBtn" @click="cancel">取消
      </a-button>
      <a-button key="submit" type="primary" :loading="loading" @click="submit">发票登记
      </a-button>
    </template>
    <fieldset class="formFieldSet">
      <legend>合同基本信息</legend>
      <a-form class="inlineForm">
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="合同编号">
              <span class="white">{{ formatValue(selectedRow.contractno) }}</span>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="定价类型">
              <span class="white">{{ getPriceTypeName(selectedRow.pricetype) }}</span>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item :label="'销售方'">
              <span class="white">{{ formatValue(selectedRow.accountname) }}</span>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="现货品种">
              <span class="white">{{ formatValue(selectedRow.deliverygoodsname) }}</span>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </fieldset>
    <fieldset class="formFieldSet">
      <legend>已登记信息</legend>
      <a-form class="inlineForm">
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="贷款总额">
              <span class="white">{{ formatValue(selectedRow.loanamount) + " 元" }}</span>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="保证金">
              <span class="white">{{ formatValue(selectedRow.margin)+ " 元" }}</span>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="其他费用">
              <span class="white">{{ selectedRow.reckonotheramount + " 元"}}</span>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="合计总额">
              <span class="white">{{ selectedRow.totalamount+ " 元" }}</span>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="已支付额 ">
              <span class="white">{{ selectedRow.payamount + " 元"}}</span>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="已收票额">
              <span class="white">{{ selectedRow.invoiceamount + " 元" }}</span>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="预收票额">
              <span class="white">{{ handleNoneValue(selectedRow.preinvoiceamount, ' 元') }}</span>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </fieldset>
    <fieldset class="formFieldSet">
      <legend>本次发票信息</legend>
      <a-form class="inlineForm" ref="formRef" :model="formState" :rules="rules">
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="收票金额" class="relative mb40" name="InvoiceAmount">
              <a-input-number v-model:value="formState.InvoiceAmount" class="dialogInput" style="width: 200px" suffix="元" placeholder="请输入收票金额" />
              <div class="tip mt10">
                <div>应收票额：{{ handleNoneValue(selectedRow.daikaiamount, ' 元') }} </div>
              </div>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="发票附件" class="mb40">
              <UploadImg :visible="visible" @upload="uploadImgAction" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </fieldset>
  </a-modal>
</template>

<script lang="ts">
import { defineComponent, PropType, ref, unref } from 'vue';
import { formatValue } from '@/common/methods';
import { Ermcp3SellBuyContract } from '@/services/go/ermcp/purchase/interface';
import { getPriceTypeName } from '@/common/constants/enumsName';
import { InvoiceReq } from '@/services/proto/contract/interface';
import { invoiceReq } from '@/views/business/purchase/components/setup';
import { handleForm } from './setup';
import { _closeModal } from '@/common/setup/modal/modal';
import UploadImg from '@/common/components/uploadImg/index.vue';
import { getUploadImg } from '@/common/setup/upload';

export default defineComponent({
  name: 'purchase_pending_invoice',
  components: { UploadImg },
  emits: ['cancel', 'update'],
  props: {
    selectedRow: {
      type: Object as PropType<Ermcp3SellBuyContract>,
      default: {},
    },
  },
  setup(props, context) {
    const { visible, cancel } = _closeModal(context);
    const { getFirstImg, uploadImgAction } = getUploadImg();
    const loading = ref<boolean>(false);
    const { rules, formState, formRef } = handleForm();

    // 处理价格小于0的情况，
    function handleNoneValue(value: number | string, suffix = '') {
      return value < 0 ? '--' : value.toString() + suffix
    }

    function submit() {
      const wrapEl = unref(formRef);
      wrapEl.validate().then(() => {
        loading.value = true;
        const params: InvoiceReq = {
          InvoiceAmount: formState.InvoiceAmount!,
        };
        // 发出发票登记
        invoiceReq(props.selectedRow.spotcontractid, params, loading, getFirstImg())
          .then(() => {
            cancel(true);
          })
          .catch((err) => { });
      });
    }

    return {
      visible,
      cancel,
      submit,
      loading,
      formRef,
      formState,
      formatValue,
      rules,
      getPriceTypeName,
      uploadImgAction,
      handleNoneValue,
    };
  },
});
</script>
