<template>
  <!-- 款项登记-->
  <a-modal class="commonModal paddingDialog funds"
           :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="selectedRow.contracttype===1? '采购方': '销售方'">
              <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 }}</span>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="保证金">
              <span class="white">{{ selectedRow.margin }}</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 }}</span>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="合计总额">
              <span class="white">{{ formatValue(selectedRow.pricedavg) }}</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 }}</span>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="预计款额">
              <span class="white">{{ selectedRow.prepayamount }}</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">10000000.00元</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="元"
                              type="number"
                              placeholder="请输入登记金额"
                              v-model:value="formState.Amount" />
            </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, reactive, ref, unref, watch, watchEffect } from 'vue';
import { closeModal } from '@/common/setup/modal/index';
import { formatValue, initData } from '@/common/methods';
import { Ermcp3SellBuyContract } from '@/services/go/ermcp/purchase/interface';
import { getPriceTypeName } from '@/views/business/purchase/setup';
import { operationContractReq } from '@/services/proto/contract';
import { message } from 'ant-design-vue';
import { objectToUint8Array } from '@/utils/objHandle';
import { FundsReq, InvoiceReq } from '@/services/proto/contract/interface';
import { fundsReq, invoiceReq } from '@/views/business/purchase/components/setup';
import { handleForm } from './setup';
import { QueryMiddleGoodsChange } from '@/services/go/ermcp/goodsInfo';

export default defineComponent({
    name: 'purchase_pending_funds',
    components: {},
    props: {
        selectedRow: {
            type: Object as PropType<Ermcp3SellBuyContract>,
            default: {},
        },
    },
    setup(props, context) {
        const { visible, cancel } = closeModal('purchase_pending_funds');
        const loading = ref<boolean>(false);
        const { rules, formState, formRef } = handleForm();
        // watch(visible, (newValue, oldValue) => { //直接监听
        //   if(newValue){
        //     initData(()=>{
        //       QueryMiddleGoodsChange(props.selectedRow.deliverygoodsid).then(res=>{
        //         debugger
        //       })
        //     })
        //   }
        // });
        watchEffect(()=>{
          if(visible.value){
            initData(()=>{
              QueryMiddleGoodsChange(props.selectedRow.deliverygoodsid).then(res=>{
                
              })
            })
          }
        })
        function submit() {
            console.log(props.selectedRow)
            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();
                        context.emit('refresh');
                    })
            });
        }

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

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