<template>
  <a-modal
    :title="modalTitle"
    :width="640"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel"
  >
    <a-form :form="form">
      <a-form-item
        label="原仓库"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          disabled
          v-model="sourceStoreName" />
      </a-form-item>
      <a-form-item
        label="目的仓库"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          disabled
          style="width:70%"
          v-decorator="['destStoreName', {rules: [{required: true, message: '仓库不能为空'}]}]" />
        <a-button type="primary" @click="handleStoreSelect">选择仓库</a-button>
      </a-form-item>
    </a-form>
    <p style="color: red">注意事项：<br/>
      1：将某个仓库的备件库存全部转移到另外的仓库<br/>
      2：原仓库库存清空为0，<br/>
      3：原仓库为0的备件不迁移<br/>
      4：新仓库已存在的备件直接增加数量<br/>
      5：新仓库已不存在的备件，则新增该备件<br/>
    </p>
    <template slot="footer">
      <a-button :loading="confirmLoading" type="primary" @click="save()">确定</a-button>
    </template>
    <store-select-modal ref="storeSelectModal" @selected="handleStoreSelected"/>
  </a-modal>
</template>

<script>
import { changeStore } from '@/api/store/store'
import StoreSelectModal from '@/views/store/store/modules/StoreSelectModal'
export default {
  name: 'SbModelBomImportForm',
  components: {
    StoreSelectModal
  },
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false,
      sourceStoreId: null,
      sourceStoreName: null,
      destStoreId: null
    }
  },
  methods: {
    base (record) {
      this.visible = true
      this.sourceStoreId = record.id
      this.sourceStoreName = record.name
      this.modalTitle = '仓库迁移'
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        const formData = new FormData()
        formData.append('sourceStoreId', this.sourceStoreId)
        formData.append('destStoreId', this.destStoreId)
        changeStore(formData)
          .then((res) => {
            this.$message.success(res.data)
            this.handleCancel(values)
          }).catch(() => {
            this.confirmLoading = false
          })
      })
    },
    handleCancel (values) {
      this.visible = false
      this.confirmLoading = false
      this.fileList = []
      this.form.resetFields()
      this.storeId = null
      this.$emit('ok', values)
    },
    handleStoreSelect () {
      this.$refs.storeSelectModal.base({}, { filter: 0 })
    },
    handleStoreSelected (record, keys, rows) {
      // 重新选择了仓库，则明细需要全部清空
      this.data = []
      const [ key ] = keys
      const [ row ] = rows
      const { form: { setFieldsValue } } = this
      this.destStoreId = key
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign({
          'destStoreName': row.name
        }))
      })
    }

  }
}
</script>
