<template>
  <!-- 款项登记-->
  <a-modal class="commonModal paddingDialog funds"
           title="款项登记"
           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-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="暂定价货款总额">
              <span
                    class="white">{{ selectedRow.pricetype === 3 ? formatValue(selectedRow.loanamount) : 0 }}</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">{{ selectedRow.pricedamount + " 元"}}</span>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="调整金额">
              <span class="white">{{ selectedRow.reckonadjustamount + " 元"}}</span>
            </a-form-item>
          </a-col>
        </a-row>

        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="货款总额">
              <span
                    class="white">{{ selectedRow.pricedamount + selectedRow.reckonadjustamount }}{{handleCurrencyname(selectedRow.currencyname) + " 元"}}</span>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="保证金">
              <span
                    class="white">{{ selectedRow.margin }}{{handleCurrencyname(selectedRow.currencyname) + " 元"}}</span>
            </a-form-item>
          </a-col>
        </a-row>

        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="其他费用">
              <span
                    class="white">{{ selectedRow.reckonotheramount }}{{handleCurrencyname(selectedRow.currencyname)  + " 元"}}</span>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="合计总额">
              <span
                    class="white">{{ formatValue(selectedRow.totalamount) }}{{handleCurrencyname(selectedRow.currencyname)  + " 元"}}</span>
            </a-form-item>
          </a-col>
        </a-row>

        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="已收付额">
              <span
                    class="white">{{ selectedRow.payamount }}{{handleCurrencyname(selectedRow.currencyname)  + " 元"}}</span>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="预收款额">
              <span
                    class="white">{{ selectedRow.prepayamount }}{{handleCurrencyname(selectedRow.currencyname)  + " 元"}}</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="应收款额"
                         name="unpayamount">
              <span class="white">{{selectedRow.unpayamount}}</span>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="款项类型">
              <a-radio-group class="commonRadioGroup"
                             v-model:value="formState.payType">
                <a-radio :value="1">收款通知</a-radio>
                <a-radio :value="2">退款申请</a-radio>
              </a-radio-group>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="登记金额"
                         name="Amount">
              <a-input-number class="dialogInput"
                              style="width: 200px"
                              suffix="元"
                              :min="0"
                              type="number"
                              placeholder="请输入登记金额"
                              v-model:value="formState.Amount" />
              {{handleCurrencyname(selectedRow.currencyname)}}
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="备注"
                         name="PricedQty"
                         class="relative">
              <a-input class="dialogInput"
                       style="width: 608px"
                       placeholder="请输入备注信息"
                       v-model:value="formState.Remark" />
            </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 { fundsReq } from '@/views/business/purchase/components/setup';
import { handleForm } from './setup';
import { _closeModal } from '@/common/setup/modal/modal';

export default defineComponent({
    name: 'sell_pending_funds',
    components: {},
    emits: ['cancel', 'update'],
    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();

        function submit() {
            const wrapEl = unref(formRef);
            wrapEl.validate().then(() => {
                loading.value = true;
                const params: any = {};
                if (formState.payType === 1) {
                    params.PayAmount = Number(formState.Amount);
                } else {
                    params.DeductAmount = Number(formState.Amount);
                }
                // 发出发票登记
                fundsReq(props.selectedRow.spotcontractid, params, loading, formState.Remark).then(() => {
                    cancel(true);
                });
            });
        }
        function handleCurrencyname(currencyname: string) {
            return currencyname === '人民币' ? '' : currencyname;
        }
        return {
            visible,
            cancel,
            submit,
            loading,
            formState,
            formatValue,
            formRef,
            rules,
            getPriceTypeName,
            handleCurrencyname,
        };
    },
});
</script>

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