<template>
  <a-modal
    :title="modalTitle"
    :width="1200"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel"
  >
    <a-form :form="form">

      <a-form-item v-show="false">
        <a-input v-decorator="['id']" type="hidden"/>
        <a-input v-decorator="['demandPlanId']" type="hidden"/>
        <a-input v-decorator="['tempFlag']" type="hidden"/>
        <a-input v-decorator="['type']" type="hidden"/>
        <a-input v-decorator="['goodsId']" type="hidden"/>
        <a-input v-decorator="['brand']" type="hidden"/>
        <a-input v-decorator="['suitableModel']" type="hidden"/>
        <a-input v-decorator="['vin']" type="hidden"/>
        <a-input v-decorator="['engineNum']" type="hidden"/>
        <a-input v-decorator="['producePlace']" type="hidden"/>
      </a-form-item>

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

          <a-form-item
            label="编号"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              disabled="disabled"
              v-decorator="['no', {rules: [{required: true, message: '名称不能为空'}]}]"/>
          </a-form-item>
        </row-item>
        <row-item>

          <a-form-item
            label="名称"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              disabled="disabled"
              v-decorator="['name', {rules: [{required: true, message: '名称不能为空'}]}]"/>
          </a-form-item>
        </row-item>
        <row-item>

          <a-form-item
            label="规格"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              disabled="disabled"
              v-decorator="['specs']"/>
          </a-form-item>
        </row-item>
        <row-item>

          <a-form-item
            label="单位"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              disabled="disabled"
              v-decorator="['unit', {rules: [{required: true, message: '单位不能为空'}]}]"/>
          </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%"
              disabled="disabled"
              :min="0"
              :step="0.01"
              :formatter="BaseTool.Amount.formatter"
              :parser="BaseTool.Amount.parser"
              v-decorator="['price', {rules: [{required: true, message: '单价不能为空'}]}]"/>
          </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"
              @change="handleTotalMoney"
              v-decorator="['quantity', {rules: [{required: true, message: '数量不能为空'}]}]"/>
          </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="['totalMoney', {rules: [{required: true, message: '总价不能为空'}]}]"/>
          </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="['controlMoney', {rules: [{required: true, message: '控制金额不能为空'}]}]"/>
          </a-form-item>
        </row-item>
        <row-item>

          <a-form-item
            label="合同项目名称"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              disabled="disabled"
              v-decorator="['contractName']"/>
          </a-form-item>
        </row-item>
        <row-item>

          <a-form-item
            label="合同项目数量"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input-number
              disabled="disabled"
              style="width: 100%"
              :min="0"
              :step="0.01"
              :formatter="BaseTool.Amount.formatter"
              :parser="BaseTool.Amount.parser"
              v-decorator="['contractQuantity']"/>
          </a-form-item>
        </row-item>
        <row-item>

          <a-form-item
            label="合同总金额"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input-number
              disabled="disabled"
              style="width: 100%"
              :min="0"
              :step="0.01"
              :formatter="BaseTool.Amount.formatter"
              :parser="BaseTool.Amount.parser"
              v-decorator="['contractMoney']"/>
          </a-form-item>
        </row-item>
        <row-item>

          <a-form-item
            label="合同项目单位"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              disabled="disabled"
              v-decorator="['contractProjectUnit']"/>
          </a-form-item>
        </row-item>
        <row-item>

          <a-form-item
            label="项目单位"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              disabled="disabled"
              v-decorator="['projectUnit']"/>
          </a-form-item>
        </row-item>

        <row-item>

          <a-form-item
            label="备注"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-textarea
              v-decorator="['remark']"/>
          </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'

export default {
  name: 'BasePurchaseDemandPlanListAdd',
  components: {
  },
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false,
      type: null,
      addFlag: false,
      originQuantity: 0,
      originTotalMoney: 0,
      originControlMoney: 0,
      // 下拉框map
      unitMap: {},
      contractProjectUnitMap: {},
      projectUnitMap: {}

    }
  },
  props: {},
  created () {
    // 下拉框map
    this.unitMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SBINFO_UNIT)
    this.contractProjectUnitMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.CONTRACT_PROJECT_UNIT)
    this.projectUnitMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.PROJECT_UNIT)
  },
  methods: {
    base (record) {
      this.originQuantity = record.quantity
      this.originTotalMoney = record.totalMoney
      this.originControlMoney = record.controlMoney
      this.visible = true
      const { form: { setFieldsValue } } = this
      this.modalTitle = '编辑'
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'id',
          'demandPlanId',
          'no',
          'type',
          'name',
          'specs',
          'unit',
          'price',
          'quantity',
          'totalMoney',
          'controlMoney',
          'contractId',
          'contractName',
          'contractQuantity',
          'contractMoney',
          'goodsId',
          'producePlace',
          'engineNum',
          'vin',
          'suitableModel',
          'brand',
          'contractProjectUnit',
          'projectUnit',
          'boughtQuantity',
          'usedMoney',
          'remark',
          'delFlag'
        ])))
      })
    },
    handleTotalMoney (value) {
      const { form: { setFieldsValue, getFieldValue } } = this
      const price = getFieldValue('price')
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign({
          'totalMoney': value * price
        }))
      })
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        values.addQuantity = values.quantity - this.originQuantity
        values.addTotalMoney = values.totalMoney - this.originTotalMoney
        values.addControlMoney = values.controlMoney - this.originControlMoney
        this.BaseTool.ListForm.pushOneListUpdate(this, values)
        this.handleCancel(values)
      })
    },
    handleCancel (values) {
      this.visible = false
      this.confirmLoading = false
      this.form.resetFields()
      this.$emit('ok', values)
    }

  }
}
</script>
