<template>
  <div>
    <Modal v-model="showModal" :mask-closable="false" width="700">
      <p slot="header">分包出库</p>
      <div>
        <Form id="batch-edit-form" ref="formObj" :model="formObj" :rules="ruleValidate" :label-width="90">
          <Form-item label="出库人：" prop="outer">
<!--            <InputNumber v-model.number="formObj.outFee" :min="0" :step="1"-->
<!--                         name="outFee" style="width:100%" placeholder="">-->
<!--            </InputNumber>-->
            <Input v-model="formObj.outer" @click.native="_selectStaff" readonly name="outer"></Input>
          </Form-item>
          <Form-item label="出库时间：" prop="outStockTime">
            <div class="block">
              <el-date-picker
                v-model="formObj.outStockTime"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择日期时间">
              </el-date-picker>
            </div>


          </Form-item>
          <Form-item label="备注" prop="remark">
            <Input v-model="formObj.remark" :rows="3" type="textarea"
                   name="remark"
                   placeholder="请输入备注"/>
          </Form-item>
        </Form>
      </div>
      <div slot="footer">
        <ModalFooter ref="footerModal" :footer="footerList" @on-result-change="_footerResult"></ModalFooter>
      </div>
    </Modal>
    <!--选择实验室-->
    <EditLabSelect ref="labModal" @on-result-change="_labResult"></EditLabSelect>
    <UserInfo ref="userModal" @on-result-change="_userData" is-change></UserInfo>
  </div>
</template>
<script>
import Global from '../../../api/config'
import ModalFooter from '../../../components/base/modalFooter'
import UserInfo from '../../../components/user-info-single/assignPerson'
import { meterSample } from '../../../api'
import EditLabSelect from './EditLabSelect'
/**
 * 添加编辑分包信息
 */
const defVal = {
  outer: undefined,
  outerId: undefined,
  outStockTime: undefined,
  remark: undefined
}
export default {
  components: {
    EditLabSelect,
    ModalFooter,
    UserInfo
    // AutoCompletes
  },
  data() {
    return {
      ids: [],
      selectData: [],
      formObj: defVal,
      ruleValidate: {
        // price: [{required: true, message: '分包价不能为空', trigger: 'blur', type: 'number'}],
      },
      showModal: false,
      footerList: [
        { id: '', name: '取消', type: '' },
        { id: '', name: '保存', type: 'primary' }
      ],
      labList: []
    }
  },
  methods: {
    getNowTime() {
      const now = new Date()
      const year = now.getFullYear() // 得到年份
      let month = now.getMonth() // 得到月份
      let date = now.getDate() // 得到日期
      let hour = now.getHours()
      let min = now.getMinutes()
      let second = now.getSeconds()
      month = month + 1
      month = month.toString().padStart(2, '0')
      date = date.toString().padStart(2, '0')
      hour = hour.toString().padStart(2, '0')
      min = min.toString().padStart(2, '0')
      second = second.toString().padStart(2, '0')
      const defaultDate = `${year}-${month}-${date} ${hour}:${min}:${second}`
      // this.$set(this.searchFormField, 'date', defaultDate)
      console.log(now)
      console.log('当前时间', defaultDate)
      console.log('时间', now)
      // this.formObj.outTime = now
      this.formObj.outStockTime = defaultDate
      // this.$set(this.searchFormField, 'date', defaultDate)
    },
    _labResult(data) {
      console.log(data)
    },
    _userData(data, msg, contractTempData) {
      if (msg === 'salesman') {
        // 选择业务员
        this.formObj.outPerson = data.realname
        // this.formObj.detail.salesmanId = data.userId
        // this.formObj.detail.salesmanPhone = data.mobile
      }
    },
    _footerResult(name) {
      switch (name) {
        case '取消':
          this._cancel()
          break
        case '保存':
          this._ok()
          break
      }
    },
    _selectStaff() {
      this.$refs.userModal._open('salesman')
    },
    _hideLoading() {
      this.$refs.footerModal._hideLoading()
    },
    _ok() {
      this._subpackageOutStock({ ids: this.ids, obj: this.formObj })
    },
    _subpackageOutStock: async function(data) {
      const result = await meterSample.subpackageOutStock(data)
      if (result) {
        this.$Message.success('出库成功！')
        this.showModal = false
        this.$emit('on-result-change')
        this._hideLoading()
      }
    },

    _cancel() {
      this.showModal = false
    },
    _open(ids, selectData) {
      this.formObj = {
        outer: undefined,
        outerId: undefined,
        outStockTime: undefined,
        remark: undefined
      }
      this._userInfo()
      this.getNowTime()
      this._hideLoading()
      this.$refs.formObj.resetFields()
      this.showModal = true
      this.ids = ids
      this.selectData = selectData
    },
    _labChange(msg, data) {
      switch (msg) {
        case 'query':
          this.formObj.subcontractor = data.name
          this._getLab(data.name)
          break
      }
    },
    _userInfo() {
      const user = Global.getUserInfo('userInfo')
      this.formObj.outer = user.realname
      this.formObj.outerId = user.id
    },

    _labSel() {
      const voList = []
      for (let i = 0; i < this.selectData.length; i++) {
        voList.push({
          itemName: this.selectData[i].itemName,
          testBasis: this.selectData[i].testBasis
        })
      }
      this.$refs.labModal._open(voList, 'batch-lab-form')
    }
  }
}
</script>
