<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-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"
              :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
              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
              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
              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
              v-decorator="['contractProjectUnit']"/>
          </a-form-item>
        </row-item>
        <row-item>

          <a-form-item
            label="项目单位"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              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>
    <spare-part-info-select-modal ref="spareSelectModal" @selected="handleSpareSelected"/>
    <sb-model-select-modal ref="sbModelSelectModal" @selected="handleSbModelSelectd"/>
    <template slot="footer">
      <a-popconfirm @confirm="save()" title="此操作会直接更新数据，不需要通过提交操作，确认保存吗？">
        <a-button :loading="confirmLoading" type="primary">保存</a-button>
      </a-popconfirm>
    </template>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { addPurchaseDemandPlanList, updatePurchaseDemandPlanList } from '@/api/purchase/purchase-demand-plan-list'
import SparePartInfoSelectModal from '@/views/sqarepartmanage/sparepartinfo/modules/SparePartInfoSelectModal'
import SbModelSelectModal from '@/views/sb/model/modules/SbModelSelectModal'

export default {
  name: 'BasePurchaseDemandPlanList',
  components: {
    SparePartInfoSelectModal,
    SbModelSelectModal
  },
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false,
      type: null,
      addFlag: false,
      // 下拉框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.visible = true
      this.type = this.$store.getters.purchaseType
      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',
          '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'
        ])))
      })
    },
    handleSpareSelect () {
      if (this.type < 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
        }))
      })
    },
    handleTotalMoney (value) {
      const { form: { setFieldsValue, getFieldValue } } = this
      const price = getFieldValue('price')
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign({
          'totalMoney': value * price
        }))
      })
    },
    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.ggxhName,
          'unit': row.unit,
          'price': row.initialValue,
          'producePlace': row.cd
        }))
      })
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        // 日期处理
        if (this.BaseTool.String.isBlank(values.id)) {
          addPurchaseDemandPlanList(values)
            .then(() => {
              this.handleCancel(values)
            }).catch(() => {
              this.confirmLoading = false
            })
        } else {
          updatePurchaseDemandPlanList(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>
