<template>
  <a-modal
    :title="modalTitle"
    :width="1200"
    :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-input v-decorator="['type']" type="hidden"/>
        <a-input v-decorator="['goodsId']" type="hidden"/>
        <a-input v-decorator="['purchasePlanId']" type="hidden"/>
        <a-input v-decorator="['tempFlag']" 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-search
              enter-button="选择"
              v-decorator="['no', {rules: [{required: true, message: '编号不能为空'}]}]"
              @search="handleSelect"
            />
          </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-select
              disabled="disabled"
              v-decorator="['unit', {rules: [{required: true, message: '单位不能为空'}]}]"
              placeholder="请选择">
              <a-select-option
                v-for="(label,value) in unitMap"
                :key="value"
                :label="label"
                :value="parseInt(value)">{{ label }}
              </a-select-option>
            </a-select>
          </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
              v-decorator="['brand']"/>
          </a-form-item>
        </row-item>
        <row-item>

          <a-form-item
            label="适用车型"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              v-decorator="['suitableModel']"/>
          </a-form-item>
        </row-item>
        <row-item>

          <a-form-item
            label="产地"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-select
              v-decorator="['producePlace']"
              placeholder="请选择">
              <a-select-option
                v-for="(label,value) in areaMap"
                :key="value"
                :label="label"
                :value="parseInt(value)">{{ label }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </row-item>
        <row-item>

          <a-form-item
            label="车架号"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              v-decorator="['vin']"/>
          </a-form-item>
        </row-item>
        <row-item>

          <a-form-item
            label="发动机号"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              v-decorator="['engineNum']"/>
          </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>
    <purchase-demand-plan-list-select-modal ref="selectModal" @selected="handleSelected"></purchase-demand-plan-list-select-modal>
    <template slot="footer">
      <a-button :loading="confirmLoading" type="primary" @click="save()">保存</a-button>
    </template>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import PurchaseDemandPlanListSelectModal from '@/views/purchase/purchase-demand-plan/list-modules/PurchaseDemandPlanListSelectModal'

export default {
  name: 'BasePurchaseList',
  components: { PurchaseDemandPlanListSelectModal },
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      demandPlanId: '-1',
      visible: false,
      addFlag: false,
      // 下拉框map
      unitMap: {},
      areaMap: {},
      statusMap: {}

    }
  },
  props: {},
  created () {
    // 下拉框map
    this.unitMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SBINFO_UNIT)
    this.areaMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.PRODUCER_AREA)
  },
  methods: {
    base (record) {
      this.visible = true
      const { form: { setFieldsValue } } = this
      // 如果是空标识添加
      if (this.BaseTool.Object.isBlank(record)) {
        this.addFlag = true
        this.modalTitle = '添加'
        const id = this.BaseTool.String.uuid()
        // 日期处理
        this.$nextTick(() => {
          setFieldsValue({
            id: id
          })
        })
        return
      }
      this.modalTitle = '编辑'
      this.addFlag = false
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'id',
          'demandPlanId',
          'purchasePlanId',
          'goodsId',
          'no',
          'type',
          'name',
          'specs',
          'unit',
          'price',
          'type',
          'quantity',
          'totalMoney',
          'taxRate',
          'taxMoney',
          'brand',
          'suitableModel',
          'vin',
          'engineNum',
          'discount',
          'producePlace',
          'weight',
          'volume',
          'dimension',
          'contractId',
          'remark'
        ])))
      })
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        // 日期处理
        if (this.addFlag) {
          this.BaseTool.ListForm.pushOneListAdd(this, values)
        } else {
          this.BaseTool.ListForm.pushOneListUpdate(this, values)
        }
        this.handleCancel(values)
      })
    },
    handleCancel (values) {
      this.visible = false
      this.confirmLoading = false
      this.form.resetFields()
      this.$emit('ok', values)
    },
    handleSelect () {
      this.$refs.selectModal.base(null, { demandPlanId: this.$store.getters.demandPlanId })
    },
    handleSelected (record, keys, rows) {
      const [ row ] = rows
      const { form: { setFieldsValue } } = this
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign({
          'goodsId': row.goodsId,
          'no': row.no,
          'name': row.name,
          'specs': row.specs,
          'type': row.type,
          'unit': row.unit,
          'price': row.price
        }))
      })
    },
    handleTotalMoney (value) {
      const { form: { setFieldsValue, getFieldValue } } = this
      const price = getFieldValue('price')
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign({
          'totalMoney': value * price
        }))
      })
    }

  }

}
</script>
