<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="['storeOrigin']" type="hidden"/>
        <a-input v-decorator="['type']" type="hidden"/>
        <a-input v-decorator="['goodsId']" type="hidden"/>
        <a-input v-decorator="['purchaseApplyId']" 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="handleSpareSelect"
            />
          </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
              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%"
              :min="0"
              :step="0.01"
              disabled="disabled"
              :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"
              @change="handleTotalMoney"
              :formatter="BaseTool.Amount.formatter"
              :parser="BaseTool.Amount.parser"
              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>
    <spare-part-info-select-modal ref="spareSelectModal" @selected="handleSpareSelected"/>
    <sb-model-select-modal ref="sbModelSelectModal" @selected="handleSbModelSelectd"/>
    <template slot="footer">
      <a-button :loading="confirmLoading" type="primary" @click="save()">保存</a-button>
    </template>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import SparePartInfoSelectModal from '@/views/sqarepartmanage/sparepartinfo/modules/SparePartInfoSelectModal'
import SbModelSelectModal from '@/views/sb/model/modules/SbModelSelectModal'

export default {
  name: 'BasePurchaseList',
  components: {
    SparePartInfoSelectModal,
    SbModelSelectModal },
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false,
      addFlag: false,
      // 下拉框map
      unitMap: {},
      areaMap: {},
      // 下拉框map
      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.addFlag = false
      this.modalTitle = '编辑'
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'id',
          'demandPlanId',
          'purchasePlanId',
          'purchaseApplyId',
          'purchaseOrderId',
          'no',
          'type',
          'name',
          'specs',
          'unit',
          'price',
          'integratedPrice',
          'storeOrigin',
          'quantity',
          'totalMoney',
          'taxRate',
          'taxMoney',
          'brand',
          'suitableModel',
          'vin',
          'engineNum',
          'discount',
          'boughtQuantity',
          'arrivedQuantity',
          'notArriveQuantity',
          'usedMoney',
          'dispatchedQuantity',
          'notDispatchQuantity',
          'producePlace',
          'weight',
          'volume',
          'dimension',
          'supplierId',
          'supplierName',
          'supplyPrice',
          'supplyTotalMoney',
          'contractId',
          'remark',
          'status',
          'delFlag'
        ])))
      })
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        // 日期处理
        if (this.addFlag) {
          values.storeOrigin = true
          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, { purchasePlanId: this.$store.getters.purchasePlanId })
    },
    handleSpareSelect () {
      if (this.$store.getters.purchaseType < this.DictCache.VALUE.PURCHASE_TYPE.PURCHASE_TYPE_SBL) {
        this.$refs.sbModelSelectModal.base()
      } else {
        this.$refs.spareSelectModal.base({}, { typeName: this.DictCache.getLabelByValue(this.DictCache.TYPE.PURCHASE_TYPE, this.$store.getters.purchaseType) })
      }
    },
    handleSbModelSelectd (keys, rows) {
      const [ row ] = rows
      const { form: { setFieldsValue } } = this
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign({
          'goodsId': row.id,
          'no': row.nameModel,
          'name': row.name,
          'type': row.typeId,
          'specs': row.model,
          'unit': row.unit,
          'price': row.value
        }))
      })
    },
    // handleSelected (record, keys, rows) {
    //   const [ row ] = rows
    //   const { form: { setFieldsValue } } = this
    //   // 日期处理
    //   this.$nextTick(() => {
    //     setFieldsValue(Object.assign(pick(row, [
    //       'goodsId',
    //       'no',
    //       'type',
    //       'name',
    //       'specs',
    //       'unit',
    //       'price',
    //       'brand',
    //       'suitableModel',
    //       'vin',
    //       'engineNum',
    //       'producePlace'
    //     ])))
    //   })
    // },
    handleSpareSelected (record, keys, rows) {
      const [ row ] = rows
      const { form: { setFieldsValue } } = this
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign({
          'goodsId': row.id,
          'no': row.no,
          'name': row.name,
          'type': row.typeId,
          'specs': row.ggxh,
          'unit': row.unit,
          'price': row.initialValue,
          'producePlace': row.cd
        }))
      })
    },
    handleTotalMoney (value) {
      const { form: { setFieldsValue, getFieldValue } } = this
      const price = getFieldValue('price')
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign({
          'totalMoney': Math.floor(value * price * 100) / 100
        }))
      })
    }

  }
}
</script>
