<template>
  <div>
    <Modal v-model="showEditModal" v-drag :mask-closable="false" :width="500" class="zIndex-1200">
      <p slot="header">{{modalTitle}}</p>
      <div>
        <Form id="edit-form" ref="formObj" :model="formObj" :rules="ruleValidate" :label-width="100" inline>
          <Form-item label="库房号" prop="storehouse" style="width: 100%">
            <Input v-model="formObj.storehouse" name="storehouse" placeholder="请输入库房号" style="width: 95%;"/>
            <span @click="_storageModel()" style="font-size: 20px;color: #00b5ec">+</span>
          </Form-item>
          <Form-item label="架位号" prop="shelfCode" style="width: 100%">
            <Input v-model="formObj.shelfCode" name="shelfCode" placeholder="请输入架位号"/>
          </Form-item>
          <Form-item label="入库人" prop="inStocker" style="width: 100%">
            <Input v-model="formObj.inStocker" @click.native="_selectStaff" name="inStocker" readonly/>
          </Form-item>
          <Form-item prop="inStockerId" style="width: 100%;display: none">
            <Input v-model="formObj.inStockerId" @click.native="_selectStaff" name="inStockerId" readonly/>
          </Form-item>
          <Form-item label="入库时间：" prop="instockTime" style="width: 100%;">
            <Date-picker v-model="formObj.instockTime" @on-change="_ctimeChange" type="date" split-panels style="width:100%;"
                         placeholder="请选择入库时间" name="instockTime"></Date-picker>
          </Form-item>
          <Form-item label="留存有效期：" prop="keepLimitTime" style="width: 100%;">
            <Date-picker v-model="formObj.keepLimitTime" @on-change="_keepLimitTimeChange" type="date" split-panels style="width:100%;"
                         placeholder="请选择入库时间" name="keepLimitTime"></Date-picker>
          </Form-item>
        </Form>
      </div>
      <div slot="footer">
        <ModalFooter ref="footerModal" @on-result-change="_footerResult" :footer="footerList"></ModalFooter>
      </div>
    </Modal>
    <UserInfo ref="userModal" @on-result-change="_userData" is-change></UserInfo>
    <storageSelect ref="storageModal" @on-result-change="_storageData"></storageSelect>
  </div>
</template>
<script>
/**
 * 添加编辑
 */
import ModalFooter from '../../components/base/modalFooter'
import { soilSample } from '../../api'
import Global from '../../api/config'
import UserInfo from '../../components/user-info-single/assignPerson'
import storageSelect from './storageSelect'

export default {
  components: {
    ModalFooter,
    UserInfo,
    storageSelect
  },
  data() {
    return {
      formId: '',
      id: '',
      lengthLimitList: [
        { key: 'stdNum', title: '标准号' },
        { key: 'enName', title: '英文名称' },
        { key: 'belongUnit', title: '归口单位' },
        { key: 'publishUnit', title: '发布单位' }
      ],
      modalTitle: '入库',
      formObj: {
        name: '',
        warehouse: '',
        type: '',
        remark: '',
        inStocker: '',
        inStockerId: ''
      },
      options: [
        {
          name: '国家标准'
        },
        {
          name: '地方标准'
        },
        {
          name: '行业标准'
        },
        {
          name: '企业标准'
        }
      ],
      typeData: [
        {
          name: '待检',
          value: 0
        },
        {
          name: '备样',
          value: 1
        },
        {
          name: '备用',
          value: 2
        }
      ],
      ruleValidate: {
        warehouse: [
          { required: true, message: '库房号不能为空', trigger: 'blur' }
        ]
      },
      showEditModal: false,
      classifyList: [
        { value: 0, name: '判定依据' },
        { value: 1, name: '检测依据' },
        { value: 2, name: '其他' }
      ],
      typeList: [
        { value: 0, name: '国家标准' },
        { value: 1, name: '地方标准' },
        { value: 2, name: '行业标准' },
        { value: 3, name: '企业标准' }
      ],
      statusList: [
        { value: 0, name: '现行' },
        { value: 1, name: '即将实施' },
        { value: 2, name: '部分被代替' },
        { value: 3, name: '被代替' },
        { value: 4, name: '作废' }
      ],
      footerList: [
        { id: '', name: '取消', type: '' },
        { id: '', name: '确定', type: 'primary' }
      ]
    }
  },
  methods: {
    _storageModel() {
      this.$refs.storageModal._open()
    },
    _storageData(data) {
      this.$forceUpdate()
      this.formObj.storehouse = data.warehouse
      this.formObj.shelfCode = data.name
    },
    _getUser() {
      const user = Global.getUserInfo()
      this.formObj.inStocker = user.realname
      this.formObj.inStockerId = user.id
      console.log(user)
    },
    _selectStaff() {
      this.$refs.userModal._open()
    },
    _userData(data, msg, contractTempData) {
      this.formObj.inStocker = data.realname
      this.formObj.inStockerId = data.id
      console.log(data, msg)
    },
    _ctimeChange(data) {
      this.formObj.instockTime = data
    },
    _keepLimitTimeChange(data) {
      this.formObj.keepLimitTime = data
    },
    /** *modal-footer */
    _footerResult(name) {
      switch (name) {
        case '取消':
          this._cancel()
          break
        case '确定':
          this._ok()
          break
      }
    },
    _hideLoading() {
      this.$refs.footerModal._hideLoading()
    },
    _resultChange(msg) {
      this.showEditModal = false
      this.$Message.success(msg)
      this.$emit('on-result-change')
      this._hideLoading()
    },
    _ok() {
      this.$refs.formObj.validate(valid => {
        if (valid) {
          const data = this.$serialize('edit-form')
          console.log(data)
          data.ids = this.id
          this._instockSubmit(data)
        } else {
          this.$Message.error('表单验证失败!')
          this._hideLoading()
        }
      })
    },
    _instockSubmit: async function(data) {
      const result = await soilSample.inStorageSubmit(data)
      if (result) {
        this._resultChange('成功入库')
      }
    },
    _save: async function(data) {
      const result = await soilSample.aloneWarehouseLocationAdd(data)
      if (result) {
        this._resultChange('添加成功!')
      }
    },
    _edit: async function(data) {
      const result = await soilSample.aloneWarehouseLocationEdit(data)
      if (result) {
        this._resultChange('编辑成功!')
      }
    },
    _cancel() {
      this._hideLoading()
      this.showEditModal = false
    },
    _open(ids) {
      this.formId = this.$randomCode()
      this._hideLoading()
      this.$refs.formObj.resetFields()
      this.id = ids.join(',')
      this._getUser()
      this.showEditModal = true
    },
    _registerAdd() {
      this.formId = this.$randomCode()
      this._hideLoading()
      this.$refs.formObj.resetFields()
      this.modalTitle = '添加'
      this.formObj.id = ''
      this.formObj.type = 3
      this.showEditModal = true
    }
  }
}
</script>
