<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">{{ 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="元"
                              :min="0"
                              placeholder="请输入收票金额" />
              <div class="tip">
                <div>应开票额：{{ selectedRow.daikaiamount + " 元" }}
                  {{ handleCurrencyname(selectedRow.currencyname) }}</div>
                <div><a class="blue fr">全部登记</a></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 { Ermcp3SellBuyContract } from '@/services/go/ermcp/purchase/interface';
import { getPriceTypeName } from '@/common/constants/enumsName';
import { formatValue } from '@/common/methods';
import { handleForm } from '@/views/business/purchase/components/invoice/setup';
import { InvoiceReq } from '@/services/proto/contract/interface';
import { invoiceReq } from '@/views/business/purchase/components/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: 'sell_pending_invoice',
    emits: ['cancel', 'update'],
    components: { UploadImg },
    props: {
        selectedRow: {
            type: Object as PropType<Ermcp3SellBuyContract>,
            default: {},
        },
    },
    setup(props, context) {
        const { visible, cancel } = _closeModal(context);
        const loading = ref<boolean>(false);
        const { rules, formState, formRef } = handleForm();
        const { getFirstImg, uploadImgAction } = getUploadImg();
        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) => {});
            });
        }
        function handleCurrencyname(currencyname: string) {
            return currencyname === '人民币' ? '' : currencyname;
        }
        return {
            visible,
            cancel,
            submit,
            loading,
            formRef,
            formState,
            formatValue,
            rules,
            getPriceTypeName,
            handleCurrencyname,
            uploadImgAction,
        };
    },
});
</script>

<style lang="less">
</style>;
