<template>
  <a-modal
    :title="modalTitle"
    :width="1200"
    :visible="visible"
    class="ant-modal2"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel"
  >
    <a-steps class="steps" :current="currentTab">
      <a-step title="选择采购单" />
      <a-step title="选择发运明细" />
    </a-steps>
    <a-form :form="form" v-show="currentTab === 0">

      <a-form-item v-show="false">
        <a-input v-decorator="['id']" type="hidden"/>
        <a-input v-decorator="['dispatchOrderId']" type="hidden"/>
        <a-input v-decorator="['purchaseOrderId']" type="hidden"/>
      </a-form-item>

      <row-list :col="2">
        <row-item>
          <a-form-item
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
            label="采购单"
          >
            <a-input-search
              :disabled="!addFlag"
              enter-button="选择"
              v-decorator="['purchaseOrderNo', {rules: [{required: true, message: '采购单不能为空'}]}]"
              @search="handleSelect"
            />
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
            label="备注"
          >
            <a-input
              v-decorator="['remark']"/>
          </a-form-item>
        </row-item>
      </row-list>
    </a-form>
    <purchase-dispatch-order-list ref="orderList" v-show="currentTab=== 1"/>
    <purchase-order-select-modal ref="selectModal" @selected="handleSelected"></purchase-order-select-modal>
    <template slot="footer">
      <a-button v-show="currentTab!== 0" :loading="confirmLoading" style="margin-left: 8px" @click="prev()">上一步</a-button>
      <a-button style="margin-left: 8px" :loading="confirmLoading" v-show="currentTab!== 1" type="primary" @click="next()">下一步</a-button>
      <a-button style="margin-left: 8px" v-show="currentTab=== 1" :loading="confirmLoading" type="primary" @click="save()">保存</a-button>
    </template>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { addPurchaseOrderDispatchOrder, updatePurchaseOrderDispatchOrder } from '@/api/purchase/purchase-order-dispatch-order'
import PurchaseDispatchOrderList from '@/views/purchase/purchase-dispatch-order-list/PurchaseDispatchOrderListForChoose'
import PurchaseOrderSelectModal from '@/views/purchase/purchase-order/modules/PurchaseOrderSelectModal'

export default {
  name: 'BasePurchaseDispatchOrder',
  components: { PurchaseDispatchOrderList, PurchaseOrderSelectModal },
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false,
      currentTab: 0,
      addFlag: false
      // 下拉框map

    }
  },
  props: {},
  created () {
  },
  methods: {
    base (record) {
      this.currentTab = 0
      const { form: { setFieldsValue } } = this
      this.BaseTool.ListForm.clearOneList(this)
      this.visible = true
      // 如果是空标识添加
      if (!record) {
        this.modalTitle = '添加发运明细'
        this.addFlag = true
        this.$store.dispatch('setPurchaseOrderId', '-1')
        this.$store.dispatch('setAddFlag', true)
        this.$nextTick(() => {
          this.$refs.orderList.base()
          setFieldsValue({
            dispatchOrderId: this.$store.getters.purchaseDispatchId
          })
        })
        return
      }

      this.addFlag = false
      this.modalTitle = '编辑发运明细'
      this.$store.dispatch('setAddFlag', false)
      this.$store.dispatch('setPurchaseOrderId', record.purchaseOrderId)
      this.$nextTick(() => {
        this.$refs.orderList.base()
        setFieldsValue(Object.assign(pick(record, [
          'id',
          'purchaseOrderId',
          'purchaseOrderNo',
          'dispatchOrderId',
          'remark'
        ])))
      })
    },
    viewList () {
      this.$nextTick(() => {
        this.$refs.orderList.base()
      })
    },
    next () {
      if (this.currentTab === 0) {
        this.save()
      } else {
        if (this.currentTab === 1) {
          this.$refs.orderList.updateNewest()
        }
        this.currentTab += 1
      }
    },
    prev () {
      if (this.currentTab === 1) {
        this.$refs.orderList.updateNewest()
      }
      this.currentTab -= 1
    },
    handleSelected (record, keys, rows) {
      const [ row ] = rows
      const { form: { setFieldsValue } } = this
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue({
          purchaseOrderId: row.id,
          purchaseOrderNo: row.billNo
        })
        this.$store.dispatch('setPurchaseOrderId', row.id)
      })
    },
    handleSelect () {
      const inventoryLocation = this.$store.getters.inventoryLocation
      let companyId = null
      let groupId = null
      if (inventoryLocation === this.DictCache.VALUE.PURCHASE_STORE_PLACE.PURCHASE_STORE_PLACE_CQEG) {
        groupId = this.$store.getters.userInfo.groupId
      } else {
        companyId = this.$store.getters.userInfo.companyId
      }
      console.log('userInfo', this.$store.getters.userInfo)
      console.log('inventoryLocation', this.$store.getters.inventoryLocation)
      this.$refs.selectModal.base(null, { companyId: companyId, groupId: groupId, waitDispatchQuantity: 0, minStatus: this.DictCache.VALUE.PURCHASE_ORDER_STATUS.PURCHASE_ORDER_STATUS_CGQR, maxStatus: this.DictCache.VALUE.PURCHASE_ORDER_STATUS.PURCHASE_ORDER_STATUS_YWJ, dispatchFlag: true })
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        // 日期处理
        if (!values.purchaseOrderId) {
          this.$message.error('请选择采购单')
          return
        }
        this.BaseTool.ListForm.setOneListValues(this, values)
        if (this.addFlag) {
          addPurchaseOrderDispatchOrder(values)
            .then((res) => {
              this.currentTab += 1
              const record = res.data
              this.viewList()
              this.addFlag = false
              // 日期处理
              const { form: { setFieldsValue } } = this
              this.$nextTick(() => {
                setFieldsValue(Object.assign(pick(record, [
                  'id',
                  'purchaseOrderId',
                  'purchaseOrderNo',
                  'dispatchOrderId',
                  'remark'
                ])))
                this.confirmLoading = false
              }).catch(() => {
                this.confirmLoading = false
              })
            })
        } else {
          updatePurchaseOrderDispatchOrder(values)
            .then(() => {
              if (this.currentTab === 1) {
                this.handleCancel({})
                return
              }
              this.currentTab += 1
              this.viewList()
              this.confirmLoading = false
            }).catch(() => {
              this.confirmLoading = false
            })
        }
      })
    },
    handleCancel (values) {
      if (this.currentTab === 1) {
        this.$refs.orderList.updateNewest()
      }
      this.visible = false
      this.confirmLoading = false
      this.form.resetFields()
      this.$emit('ok', values)
    }

  }
}
</script>
