<template>
  <!-- 交收登记-->
  <a-modal class="commonModal paddingDialog settlement"
           :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">{{ formatValue(selectedRow.qty + selectedRow.enumdicname) }}</span>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="合同暂定价">
              <span class="white">{{ formatValue(selectedRow.price) }}</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.pricedqty + " " + selectedRow.enumdicname }}</span>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="已交收量">
              <span class="white">{{ selectedRow.reckonrealqty + " " + selectedRow.enumdicname }}</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-col :span="12">
            <a-form-item label="保证金">
              <span class="white">{{ selectedRow.margin + " 元" }}</span>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="其他费用">
              <span
                    class="white">{{ selectedRow.reckonotheramount + " 元" }}</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-checkbox-group class="commonCheckboxGroup">
          <a-row>
            <a-col :span="12">
              <a-form-item class="relative">
                <template #label>
                  <a-checkbox value="交收量"
                              v-model:checked="checkedObj.qty"
                              @change="()=>checkedObj.qty = !checkedObj.qty">交收量</a-checkbox>
                </template>
                <a-form-item class="relative"
                             v-if="checkedObj.qty"
                             name="ReckonRealQty">
                  <a-input-number v-model:value="formState.ReckonRealQty"
                                  class="dialogInput"
                                  style="width: 200px"
                                  :min="0"
                                  placeholder="请输入交收量"
                                  suffix="单位" />
                  <div class="tip mt10">
                    <span>待交收量：{{selectedRow.unsureqty}}{{selectedRow.enumdicname}}</span><a
                       class="blue fr"
                       @click="formState.ReckonRealQty=selectedRow.unsureqty">全部登记</a>
                  </div>
                </a-form-item>
                <a-form-item class="relative"
                             v-else>
                  <a-input-number disabled
                                  class="dialogInput"
                                  style="width: 200px"
                                  :min="0"
                                  placeholder="请输入交收量"
                                  suffix="单位" />
                  <div class="tip mt10"><span class="fl">未定价量：--{{selectedRow.enumdicname}}</span><a
                       class="blue fr">全部登记</a>
                  </div>
                </a-form-item>
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item class="relative">
                <template #label>
                  <a-checkbox value="其他费用"
                              v-model:checked="checkedObj.amount"
                              @change="()=>checkedObj.amount = !checkedObj.amount">其他费用</a-checkbox>
                </template>
                <a-form-item v-if="checkedObj.amount"
                             name="ReckonOtherAmount">
                  <a-input-number v-model:value="formState.ReckonOtherAmount"
                                  class="dialogInput"
                                  style="width: 200px"
                                  :min="0"
                                  placeholder="请输入其他费用"
                                  suffix="元" />
                </a-form-item>
                <a-form-item v-else>
                  <a-input-number readonly
                                  class="dialogInput"
                                  style="width: 200px"
                                  :min="0"
                                  placeholder="请输入其他费用"
                                  suffix="元" />
                </a-form-item>
              </a-form-item>
            </a-col>
            <a-col :span="12"
                   class="mt10">
              <a-form-item class="relative">
                <template #label>
                  <a-checkbox value="调整保证金"
                              v-model:checked="checkedObj.margin"
                              @change="()=>checkedObj.margin = !checkedObj.margin">调整保证金
                  </a-checkbox>
                </template>
                <a-form-item class="relative"
                             v-if="checkedObj.margin">
                  <a-radio-group class="commonRadioGroup"
                                 v-model:value="checkedObj.adjustMargin">
                    <a-row style="width: 200px">
                      <a-col :span="12">
                        <a-radio :value="1">追加</a-radio>
                      </a-col>
                      <a-col :span="12">
                        <a-radio :value="2">退还</a-radio>
                      </a-col>
                    </a-row>
                  </a-radio-group>
                  <div class="tip mt10">
                    <a-form-item name="AddMargin"
                                 v-if="checkedObj.adjustMargin === 1">
                      <a-input-number v-model:value="formState.AddMargin"
                                      class="dialogInput"
                                      style="width: 200px;"
                                      :min="0"
                                      placeholder="请输入调整保证金"
                                      suffix="元" />
                    </a-form-item>
                    <a-form-item name="DecMargin"
                                 v-else>
                      <a-input-number v-model:value="formState.DecMargin"
                                      class="dialogInput"
                                      style="width: 200px;"
                                      :min="0"
                                      placeholder="请输入调整保证金"
                                      suffix="元" />
                    </a-form-item>
                  </div>
                </a-form-item>
                <a-form-item class="relative"
                             v-else>
                  <a-radio-group class="commonRadioGroup"
                                 disabled>
                    <a-row style="width: 200px">
                      <a-col :span="12">
                        <a-radio :value="1">追加</a-radio>
                      </a-col>
                      <a-col :span="12">
                        <a-radio :value="2">退还</a-radio>
                      </a-col>
                    </a-row>
                  </a-radio-group>
                  <div class="tip mt10">
                    <a-input-number disabled
                                    class="dialogInput"
                                    style="width: 200px;"
                                    :min="0"
                                    placeholder="请输入调整保证金"
                                    suffix="元" />
                  </div>
                </a-form-item>
              </a-form-item>
            </a-col>
            <a-col :span="12"
                   class="mt10">
              <a-form-item class="relative">
                <template #label>
                  <a-checkbox value="调整金额"
                              v-model:checked="checkedObj.justAmount"
                              @change="()=>checkedObj.justAmount = !checkedObj.justAmount">调整金额
                  </a-checkbox>
                </template>
                <a-form-item class="relative"
                             v-if="checkedObj.justAmount">
                  <a-radio-group class="commonRadioGroup"
                                 v-model:value="checkedObj.adjustAmount">
                    <a-row style="width: 200px">
                      <a-col :span="12">
                        <a-radio :value="1"
                                 @focus="()=>{formState.ReckonAdjustAmount  ?formState.ReckonAdjustAmount= Math.abs(formState.ReckonAdjustAmount):0}">
                          增加</a-radio>
                      </a-col>
                      <a-col :span="12">
                        <a-radio :value="2"
                                 @focus="()=>{formState.ReckonAdjustAmount  ?formState.ReckonAdjustAmount= -1* Math.abs(formState.ReckonAdjustAmount):0}">
                          减少</a-radio>
                      </a-col>
                    </a-row>
                  </a-radio-group>
                  <div class="tip mt10">
                    <a-form-item name="ReckonAdjustAmount">
                      <a-input-number v-model:value="formState.ReckonAdjustAmount"
                                      @change="formState.ReckonAdjustAmount= Math.abs(formState.ReckonAdjustAmount)"
                                      class="dialogInput"
                                      style="width: 200px;"
                                      :min="0"
                                      placeholder="请输入调整金额"
                                      suffix="元"
                                      v-if="checkedObj.adjustAmount===1" />
                      <a-input-number v-model:value="formState.ReckonAdjustAmount"
                                      @change="formState.ReckonAdjustAmount= -1*Math.abs(formState.ReckonAdjustAmount)"
                                      class="dialogInput"
                                      :min="0"
                                      style="width: 200px;"
                                      placeholder="请输入调整金额"
                                      suffix="元"
                                      v-else />
                    </a-form-item>
                  </div>
                </a-form-item>
                <a-form-item class="relative"
                             v-else>
                  <a-radio-group class="commonRadioGroup"
                                 disabled>
                    <a-row style="width: 200px">
                      <a-col :span="12">
                        <a-radio :value="1">增加</a-radio>
                      </a-col>
                      <a-col :span="12">
                        <a-radio :value="2">减少</a-radio>
                      </a-col>
                    </a-row>
                  </a-radio-group>
                  <div class="tip mt10">
                    <a-input-number disabled
                                    class="dialogInput"
                                    style="width: 200px;"
                                    :min="0"
                                    placeholder="请输入调整金额"
                                    suffix="元" />
                  </div>
                </a-form-item>
              </a-form-item>
            </a-col>
            <a-col :span="24">
              <a-form-item label="备注"
                           class="relative mt20 pl40">
                <a-input class="dialogInput"
                         style="width: 600px"
                         placeholder="请输入备注信息"
                         v-model:value="formState.Remark" />
              </a-form-item>
            </a-col>
          </a-row>
        </a-checkbox-group>
        <a-form-item name="IsChecked"></a-form-item>
      </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 { settlementReq } from '@/views/business/purchase/components/setup';
import { SettlementReq } from '@/services/proto/contract/interface';
import { handleForm } from './setup';
import { _closeModal } from '@/common/setup/modal/modal';

export default defineComponent({
    name: 'purchase_pending_settlement',
    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, checkedObj } = handleForm();

        function submit() {
            const wrapEl = unref(formRef);
            wrapEl.validate().then((nameList: any) => {
                delete nameList.IsChecked;
                const param: SettlementReq = {};
                const { ReckonRealQty, ReckonOtherAmount, AddMargin, ReckonAdjustAmount, DecMargin, Remark } = formState;
                // 交收量
                if (ReckonRealQty) {
                    param.ReckonRealQty = ReckonRealQty;
                }
                // 其它费用
                if (ReckonOtherAmount) {
                    param.ReckonOtherAmount = ReckonOtherAmount;
                }
                // 追加保证金
                if (AddMargin) {
                    param.AddMargin = AddMargin;
                }
                // 减少保证金
                if (DecMargin) {
                    param.DecMargin = DecMargin;
                }
                // 调整金额
                if (ReckonAdjustAmount) {
                    param.ReckonAdjustAmount = ReckonAdjustAmount;
                }
                if (Remark) {
                    param.Remark = Remark;
                }
                // const params: SettlementReq = checkedObj.value.adjustMargin === 1?{
                //     ReckonRealQty: formState.ReckonRealQty!, // 交收量
                //     ReckonOtherAmount: formState.ReckonOtherAmount!, // 其它费用
                //     AddMargin: formState.AddMargin!, // 追加保证金
                //     ReckonAdjustAmount: checkedObj.value.adjustAmount === 1 ? formState.ReckonRealQty! : -1*formState.ReckonRealQty!, // 调整金额 +-代表增减
                // }:{
                //     ReckonRealQty: formState.ReckonRealQty!, // 交收量
                //     ReckonOtherAmount: formState.ReckonOtherAmount!, // 其它费用
                //     DecMargin: formState.DecMargin!, // 减少保证金
                //     ReckonAdjustAmount: checkedObj.value.adjustAmount === 1 ? formState.ReckonRealQty! : -1*formState.ReckonRealQty!, // 调整金额 +-代表增减
                // };
                // 发出交收请求
                settlementReq(props.selectedRow.spotcontractid, param, loading)
                    .then(() => {
                        cancel(true);
                    })
                    .catch((err) => {});
            });
        }

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

<style lang="less">
.settlement {
    .has-error .ant-form-explain {
        padding-left: 40px;
    }
}
</style>;
