<template>
  <a-modal
    :title="modalTitle"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    class="ant-modal2"
    @cancel="handleCancel"
  >
    <a-form :form="form">

      <a-form-item v-show="false">
        <a-input v-decorator="['id']" type="hidden"/>
      </a-form-item>

      <row-list :col="2">
        <row-item>

          <a-form-item
            label="采购明细总额"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              disabled="disabled"
              v-model="record.money"/>
          </a-form-item>
        </row-item>
        <row-item>

          <a-form-item
            label="币种"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              disabled="disabled"
              v-model="record.moneyTypeName"/>
          </a-form-item>
        </row-item>
        <row-item>

          <a-form-item
            label="确认采购总额"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input-number
              style="width: 100%"
              :min="0"
              :step="0.01"
              :formatter="BaseTool.Amount.formatter"
              :parser="BaseTool.Amount.parser"
              v-decorator="['money', {rules: [{required: true, message: '采购总价不能为空'}]}]"/>
          </a-form-item>
        </row-item>
        <row-item v-if="tempStoreFlag">

          <a-form-item
            label="临时仓储位置"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-select
              v-decorator="['tempStore', { rules: [{required: true, message: '临时仓储位置不能为空'}]}]"
              placeholder="请选择">
              <a-select-option
                v-for="(label,value) in tempStoreMap"
                :key="value"
                :label="label"
                :value="parseInt(value)">{{ label }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </row-item>
      </row-list>
    </a-form>
    <template slot="footer">
      <a-button :loading="confirmLoading" type="primary" @click="save()">确认提交</a-button>
    </template>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { purchaseOrderMoneyConfirm } from '@/api/purchase/purchase-order'

export default {
  name: 'BasePurchaseOrderMoneyConfirm',
  components: { },
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false,
      moneyMap: {},
      tempStoreMap: {},
      tempStoreFlag: false

    }
  },
  props: {},
  created () {
    // 下拉框map
    this.moneyMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.MONEY_TYPE)
    this.tempStoreMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.PURCHASE_STORE_PLACE)
  },
  methods: {
    base (record) {
      this.visible = true
      const { form: { setFieldsValue } } = this
      this.modalTitle = '采购确认'
      this.record = {
        ...record
      }
      this.record.money = this.BaseTool.Amount.formatter(this.record.money)
      this.record.moneyTypeName = this.moneyMap[this.record.moneyType]
      this.tempStoreFlag = this.record.reportType >= this.DictCache.VALUE.REPORT_RANK.REPORT_RANK_FGS
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'id',
          'money'
        ])))
      })
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        purchaseOrderMoneyConfirm(values)
          .then(() => {
            this.handleCancel(values)
          }).catch(() => {
            this.confirmLoading = false
          })
      })
    },
    handleCancel (values) {
      this.visible = false
      this.confirmLoading = false
      this.form.resetFields()
      this.$emit('ok', values)
    }

  }
}
</script>
