<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" style="width: 95%;" placeholder="请输入库房号"/>
            <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="outStocker" style="width: 100%">
            <Input v-model="formObj.outStocker" @click.native="_selectStaff" name="outStocker" readonly/>
          </Form-item>
          <Form-item prop="outStockerId" style="width: 100%;display: none">
            <Input v-model="formObj.outStockerId" @click.native="_selectStaff" name="outStockerId" readonly/>
          </Form-item>
          <Form-item label="出库接收方" prop="outStockReceiver" style="width: 100%">
            <Input v-model="formObj.outStockReceiver" @click.native="_selectoutStockReceiver" name="outStockReceiver" readonly/>
          </Form-item>
          <Form-item prop="outStockReceiverId" style="width: 100%;display: none">
            <Input v-model="formObj.outStockReceiverId" @click.native="_selectoutStockReceiver" name="outStockReceiverId" readonly/>
          </Form-item>
          <Form-item label="出库时间：" prop="outStock" style="width: 100%;">
            <Date-picker v-model="formObj.outStock" @on-change="_ctimeChange" type="date" split-panels style="width:100%;"
                         placeholder="请选择出库时间" name="outStock"></Date-picker>
          </Form-item>
<!--          <Form-item label="留存有效期：" prop="keepLimitTime">-->
<!--            <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: '',
      userType: '',
      lengthLimitList: [
        { key: 'stdNum', title: '标准号' },
        { key: 'enName', title: '英文名称' },
        { key: 'belongUnit', title: '归口单位' },
        { key: 'publishUnit', title: '发布单位' }
      ],
      modalTitle: '出库',
      formObj: {
        name: '',
        warehouse: '',
        type: '',
        remark: '',
        inStocker: '',
        inStockerId: '',
        outStockReceiver: '',
        outStockReceiverId: ''
      },
      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.outStocker = user.realname
      this.formObj.outStockerId = user.id
      this.formObj.outStockReceiver = user.realname
      this.formObj.outStockReceiverId = user.id
      console.log(user)
    },
    _selectStaff() {
      this.userType = ''
      this.$refs.userModal._open()
    },
    _selectoutStockReceiver() {
      this.userType = 'outStockReceiver'
      this.$refs.userModal._open()
    },
    _userData(data, msg, contractTempData) {
      this.$forceUpdate()
      if (this.userType === 'outStockReceiver') {
        this.formObj.outStockReceiver = data.realname
        this.formObj.outStockReceiverId = data.id
      } else {
        this.formObj.outStocker = data.realname
        this.formObj.outStockerId = data.id
      }
      console.log(data, msg)
    },
    _ctimeChange(data) {
      this.formObj.outStock = 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
          data.entrustCode = '1564980847'
          this._outstockSubmit(data)
        } else {
          this.$Message.error('表单验证失败!')
          this._hideLoading()
        }
      })
    },
    _outstockSubmit: async function(data) {
      const result = await soilSample.outStorageSubmit(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>
