<template>
  <a-card :bordered="false" v-show="visible" class="card" :title="modalTitle">
    <a-row :gutter="48" slot="extra">
      <a-col :md="48" :sm="48">
        <span class="table-page-search-submitButtons" style="float: right">
          <a-button type="primary" @click="save()">保存</a-button>
          <a-button style="margin-left: 8px" @click="handleCancel()">返回</a-button>
        </span>
      </a-col>
    </a-row>
    <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="['dispatchOrderId']" type="hidden"/>
      </a-form-item>

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

          <a-form-item
            label="装箱信息"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              v-decorator="['inventoryLocation', {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-dispatch-container-list ref="list"/>
  </a-card>
</template>

<script>
import pick from 'lodash.pick'
import { addPurchaseDispatchContainer, updatePurchaseDispatchContainer } from '@/api/purchase/purchase-dispatch-container'
import PurchaseDispatchContainerList from '@/views/purchase/purchase-dispatch-container-list/PurchaseDispatchContainerList'

export default {
  name: 'BasePurchaseDispatchContainer',
  components: { PurchaseDispatchContainerList },
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false,
      addFlag: false
      // 下拉框map
    }
  },
  props: {},
  created () {
    // 下拉框map
  },
  methods: {
    base (record, dispatchOrderId) {
      this.BaseTool.ListForm.clearOneList(this)
      this.visible = true
      const modal = this.$refs.list
      const { form: { setFieldsValue } } = this
      this.$store.dispatch('setPurchaseDispatchId', dispatchOrderId)
      // 如果是空标识添加
      if (this.BaseTool.Object.isBlank(record)) {
        this.modalTitle = '添加装箱信息'
        this.addFlag = true
        this.$store.dispatch('setAddFlag', true)
        this.$nextTick(() => {
          setFieldsValue({
            dispatchOrderId: dispatchOrderId
          })
        })
        modal.base('-1')
        return
      }
      this.addFlag = false
      this.modalTitle = '编辑装箱信息'
      this.$store.dispatch('setAddFlag', false)
      modal.base(record.id)
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'id',
          'billNo',
          'purchaseApplyId',
          'purchasePlanId',
          'demandPlanId',
          'purchaseOrderId',
          'dispatchOrderId',
          'companyId',
          'companyName',
          'projectId',
          'projectName',
          'inventoryLocation',
          'quantity',
          'weight',
          'volume',
          'status',
          'remark',
          'delFlag'
        ])))
      })
    },
    save (value) {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        // 日期处理
        values.submitType = value
        this.BaseTool.ListForm.setOneListValues(this, values)
        if (this.BaseTool.String.isBlank(values.id)) {
          addPurchaseDispatchContainer(values)
            .then(() => {
              this.handleCancel(values)
            }).catch(() => {
              this.confirmLoading = false
            })
        } else {
          updatePurchaseDispatchContainer(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>
