<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 style="margin-left: 8px" 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-form-item>

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

          <a-form-item
            label="入库仓库"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-select
              v-decorator="['warehouseId', {initialValue: 1, rules: [{required: true, message: '入库仓库不能为空'}]}]"
              placeholder="请选择">
              <a-select-option
                v-for="item in warehouses"
                :key="item.id"
                :label="item.name"
                :value="item.id">{{ item.name }}
              </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-select
              v-decorator="['enterReason', {initialValue: 1, rules: [{required: true, message: '入库原因不能为空'}]}]"
              placeholder="请选择">
              <a-select-option
                v-for="(label,value) in reasonMap"
                :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-textarea
              v-decorator="['remark']" >
            </a-textarea>

          </a-form-item>
        </row-item>
      </row-list>
    </a-form>
    <title-divider title="明细" width="80px"></title-divider>
    <purchase-order-list ref="orderList" :viewFlag="true"/>
  </a-card>
</template>

<script>
import pick from 'lodash.pick'
import { updatePurchaseOrderInfo } from '@/api/purchase/purchase-order'
import PurchaseOrderList from '@/views/purchase/purchase-order/PurchaseOrderList'
import { queryStore } from '@/api/store/store'

export default {
  name: 'BasePurchaseOrderInbound',
  components: { PurchaseOrderList },
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false,
      // 下拉框map
      countryMap: {},
      dispatchTypeMap: {},
      storePlaceMap: {},
      warehouses: [],
      moneyType: {},
      reasonMap: {}

    }
  },
  props: {},
  created () {
    // 下拉框map
    this.dispatchTypeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.DISPATCH_TYPE)
    this.countryMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SYS_USER_COUNTRY)
    this.storePlaceMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.PURCHASE_STORE_PLACE)
    this.moneyType = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.MONEY_TYPE)
    this.reasonMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.IN_STORE_FORM_TYPE)
  },
  methods: {
    base (record) {
      this.visible = true
      const modal = this.$refs.orderList
      modal.base(record)
      const { form: { setFieldsValue } } = this
      this.modalTitle = '填报入库细信息'
      queryStore({ filter: 3, useProject: record.projectId}).then(res => {
        this.warehouses = res.data
        // 日期处理
        this.$nextTick(() => {
          setFieldsValue(Object.assign(pick(record, [
            'id',
            'warehouseId',
            'warehouseName',
            'enterReason',
            'delFlag'
          ])))
        })
      })
    },
    save (value) {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        updatePurchaseOrderInfo(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>
