<template>
  <a-modal
    :title="modalTitle"
    :width="1200"
    :visible="visible"
    :confirmLoading="confirmLoading"
    class="ant-modal2"
    @cancel="handleCancel"
  >
    <title-divider title="基本信息" width="90px"></title-divider>
    <a-form :form="form">

      <a-form-item v-show="false">
        <a-input v-decorator="['id']" type="hidden"/>
        <a-input v-decorator="['purchaseOrderId']" type="hidden"/>
        <a-input v-decorator="['contractId']" type="hidden"/>
        <a-input v-decorator="['goodsId']" 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="['name', {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
              v-decorator="['model', {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
              type="number"
              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
              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
              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
              type="number"
              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-textarea
              v-decorator="['remark']"/>
          </a-form-item>
        </row-item>
      </row-list>
    </a-form>
    <title-divider title="清单" width="90px"></title-divider>
    <purchase-contract-list ref="contractList"></purchase-contract-list>
    <purchase-order-list-select-modal ref="selectModal" @selected="handleSelected"></purchase-order-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 PurchaseContractList from '../PurchaseContractList'
import PurchaseOrderListSelectModal from '../list-modules/PurchaseOrderListSelectModal'

export default {
  name: 'BasePurchaseOrderContractMainList',
  components: { PurchaseContractList, PurchaseOrderListSelectModal },
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      id: '-1',
      form: this.$form.createForm(this),
      visible: false,
      // 下拉框map
      unitMap: {}

    }
  },
  props: {},
  created () {
    // 下拉框map
    this.unitMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.PROJECT_UNIT)
  },
  methods: {
    base (record, contractId) {
      this.BaseTool.ListForm.clearOneList(this)
      this.BaseTool.ListForm.clearTwoList(this)
      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,
            purchaseOrderId: this.$store.getters.purchaseOrderId,
            contractId: contractId
          })
          this.BaseTool.ListForm.initTwoList(this, id)
          const modal = this.$refs.contractList
          modal.base(id, true)
        })
        return
      }
      this.addFlag = false
      this.modalTitle = '编辑合同主清单'
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'id',
          'demandPlanId',
          'purchasePlanId',
          'purchaseApplyId',
          'purchaseOrderId',
          'contractId',
          'tempFlag',
          'name',
          'model',
          'machineNo',
          'engineModel',
          'engineNo',
          'quantity',
          'unit',
          'price',
          'totalMoney',
          'remark',
          'delFlag'
        ])))
        this.BaseTool.ListForm.initTwoList(this, record.id)
        const modal = this.$refs.contractList
        modal.base(record.id, false)
      })
    },
    handleSelect () {
      this.$refs.selectModal.base(null, {
        purchaseOrderId: this.$store.getters.purchaseOrderId,
        minStatus: this.DictCache.VALUE.PURCHASE_LIST_STATUS.PURCHASE_LIST_STATUS_ORDER,
        goodsIdNot: '-1'
      })
    },
    handleSelected (record, keys, rows) {
      const [row] = rows
      const { form: { setFieldsValue } } = this
      this.id = row.id
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue({
          name: row.name,
          model: row.specs,
          quantity: row.quantity,
          goodsId: row.goodsId,
          unit: row.unit,
          price: row.price,
          totalMoney: row.totalMoney
        })
      })
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        this.BaseTool.ListForm.setTwoListValues(this, values, values.id)
        // 替换成原来的id 方便后端改状态
        values.id = this.id
        // 日期处理
        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)
    }
  }
}
</script>
