<template>
  <!-- 现货参考价-修改-->
  <a-modal class="add-custom custom-detail" title="修改现货参考价" v-model:visible="visible" centered :maskClosable="false" @cancel="cancel" width="890px">
    <a-form class="inlineForm" ref="formRef" :model="formState" :rules="rules">
      <fieldset class="formFieldSet">
        <legend>现货市价信息</legend>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="现货品种">
              <span class="white">{{ formatValue(selectedRow.deliverygoodsname) }}</span>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="商品">
              <span class="white">{{ formatValue(selectedRow.wrstandardname) }}</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.brandname) }}</span>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="结算币种" name="CurrencyID">
              <a-select class="inlineFormSelect" style="width: 200px" placeholder="请选择结算币种" v-model:value="formState.CurrencyID">
                <a-select-option v-for="option in currencyList" :key="option.enumitemname" :value="option.enumitemname">{{ option.enumdicname }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="24">
            <a-form-item label="现货价格" name="SpotGoodsPrice">
              <a-input-number class="dialogInput" style="width: 200px" v-model:value="formState.SpotGoodsPrice" :min="0" placeholder="请输入现货价格" />
            </a-form-item>
          </a-col>
        </a-row>
      </fieldset>
    </a-form>
    <template #footer>
      <a-button key="submit" type="primary" :loading="loading" @click="cancel">取消</a-button>
      <a-button key="submit" type="primary" :loading="loading" @click="submit">提交</a-button>
    </template>
  </a-modal>
</template>

<script lang="ts">
import { defineComponent, PropType, ref, reactive, UnwrapRef } from 'vue';
import { formatValue } from '@/common/methods';
import { Ermcp3SpotGoodsPrice } from '@/services/go/ermcp/qhj/interface';
import { ErmcpSpotGoodsPriceReq } from '@/services/proto/delivery/interface';
import { _closeModal } from '@/common/setup/modal/modal';
import { getPayCurrencyTypeEnumList } from '@/common/constants/enumsList';
import { validateAction } from '@/common/setup/form';
import { requestResultLoadingAndInfo } from '@/common/methods/request/resultInfo';
import { ermcpSpotGoodsPrice } from '@/services/proto/delivery';
import { geLoginID_number } from '@/services/bus/login';
import moment from 'moment';

export default defineComponent({
  name: 'spotmarket_price_modify',
  emits: ['cancel', 'update'],
  props: {
    selectedRow: {
      type: Object as PropType<Ermcp3SpotGoodsPrice>,
      default: {},
    },
  },
  setup(props, context) {
    const { visible, cancel } = _closeModal(context);
    const loading = ref<boolean>(false);
    const formRef = ref();

    // 选择币种列表
    const currencyList = getPayCurrencyTypeEnumList();

    const { deliverygoodsid, wrstandardid, spotgoodsbrandid, tradedate, currencyid, spotgoodsprice, operateid } = props.selectedRow;
    const formState: UnwrapRef<ErmcpSpotGoodsPriceReq> = reactive({
      DeliveryGoodsID: deliverygoodsid,
      WRStandardID: wrstandardid,
      SpotGoodsBrandID: spotgoodsbrandid,
      CurrencyID: currencyid,
      SpotGoodsPrice: spotgoodsprice,
      TradeDate: moment(new Date()).format('YYYYMMDD'),
      OperateSrc: 2,
      OperateID: geLoginID_number()!,
      OperateType: 2,
    });

    // 表单验证规则
    const rules = {
      CurrencyID: [{ required: true, message: '请选择结算币种', trigger: 'change', type: 'number' }],
      SpotGoodsPrice: [{ required: true, message: '请输入现货价格', trigger: 'blur', type: 'number' }],
    };

    // 提交
    function submit() {
      validateAction<ErmcpSpotGoodsPriceReq>(formRef, formState).then((res) => {
        requestResultLoadingAndInfo(ermcpSpotGoodsPrice, res, loading, ['修改现货参考价成功', '修改现货参考价失败']).then(() => {
          cancel(true);
        });
      });
    }

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