<template>
  <el-dialog title="编辑设备信息"
             :visible.sync="dialogvisible"
             width="75%"
             :before-close="handleCloseEdit">
    <el-form size="mini"
             :model="testlistform"
             :rules="rulestestlistform"
             label-width="130px"
             ref="testlistform">
      <el-row :gutter="20"
              class="donorsaddformcss">
        <el-col :span="8">
          <el-form-item label="设备编码"
                        prop="Code"
                        label-width="120px">
            <el-input v-model="testlistform.Code"
                      placeholder="请输入设备编码"
                      :disabled="true"
                      style="width:100%"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="设备名称"
                        prop="Name"
                        label-width="120px">
            <el-input v-model="testlistform.Name"
                      placeholder="请输入设备名称"
                      style="width:100%"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="型号"
                        prop="Model"
                        label-width="120px">
            <el-input v-model="testlistform.Model"
                      placeholder="请输入型号"
                      style="width:100%"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="品牌名称"
                        prop="Brand"
                        label-width="120px">
            <el-input v-model="testlistform.Brand"
                      placeholder="请输入品牌名称"
                      style="width:100%"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="设备状态"
                        prop="State"
                        label-width="120px">
            <el-select v-model="testlistform.State"
                       placeholder="请选择设备状态"
                       style="width:100%">
              <el-option v-for="item in statelist"
                         :key="item.Id"
                         :label="item.stateName"
                         :value="item.Id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="校准时间"
                        label-width="120px"
                        prop="CalibrationTime">
            <el-date-picker v-model="testlistform.CalibrationTime"
                            type="datetime"
                            style="width:100%"
                            placeholder="请选择校准时间">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="责任人"
                        label-width="120px">
            <el-input v-model="testlistform.Responsible"
                      placeholder="请输入责任人">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="实验室位置"
                        prop="RoomType"
                        label-width="120px">
            <el-select v-model="testlistform.Location"
                       placeholder="请选择实验室类型"
                       style="width:100%">
              <el-option v-for="item in typeList"
                         :key="item.Id"
                         :label="item.RoomName"
                         :value="item.RoomName"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注信息"
                        label-width="120px">
            <el-input v-model="testlistform.Remarks"
                      type="textarea"
                      :rows=3
                      placeholder="请输入备注信息"
                      style="width:100%"></el-input>
          </el-form-item>
        </el-col>

      </el-row>
    </el-form>
    <span slot="footer">
      <el-button size="mini"
                 type="primary"
                 @click="savedata">保存</el-button>
      <el-button size="mini"
                 @click="handleCloseEdit">关闭</el-button>
    </span>
  </el-dialog>
</template>

<script>
import axios from 'axios'
import uploadajax from '@/assets/js/uploadajax.js'
import InstrumentApi from '@/api/instrument'
import {
  getRoomNumber
} from '@/api/instrumentroom'
export default {
  name: 'instrumentadd',
  props: {
    InstrumentId: {
      default: 0
    }
  },
  data () {
    return {
      fileList: [],
      FileUrl: {},
      uploadFile: {},
      dialogvisible: false,
      formtype: '1',
      disabledbarcode: false,
      typeList: [],
      testlistform: {
        Code: '',
        Name: '',
        Supplier: '',
        SupplierId: '',
        Model: '',
        Brand: '',
        Classification: '',
        State: 1,
        Remarks: '',
        CalibrationDeadlineType: 2,
        CalibrationTime: new Date(),
        CalibrationDeadline: 1,
        HeartbeatTime: new Date(),
        TimeNotification: 0
      },
      Advancetime: 0,
      triggerlist: {},
      TimeNotification: false, // 有效期提醒
      classificationlist: [],
      getsupplierlist: [],
      statelist: [{
        stateName: '正常',
        Id: 1
      }, {
        stateName: '维修',
        Id: 2
      }, {
        stateName: '闲置',
        Id: 3
      }],
      timeType: [{
        stateName: '天',
        Id: 1
      }, {
        stateName: '周',
        Id: 2
      }, {
        stateName: '月',
        Id: 3
      }, {
        stateName: '年',
        Id: 4
      }],
      rulestestlistform: {
        Code: [{
          required: true,
          message: '请输入设备编码',
          trigger: 'blur'
        }],
        Name: [{
          required: true,
          message: '请输入设备名称',
          trigger: 'blur'
        }],
        Model: [{
          required: true,
          message: '请输入型号',
          trigger: 'blur'
        }],
        Brand: [{
          required: true,
          message: '请输入品牌名称',
          trigger: 'blur'
        }],
        State: [{
          required: true,
          message: '请输入设备状态',
          trigger: 'blur'
        }],
        Classification: [{
          required: true,
          message: '请输入设备大类',
          trigger: 'blur'
        }]

      }
    }
  },
  created () {
    //  this.getEntity(this.InstrumentId)
    // this.getclassificationlist()
    this.getroomtypelist()
  },
  methods: {
    // 获取所有实验室类型
    getroomtypelist () {
      let _this = this
      let params = {
      }
      getRoomNumber(params)
        .then(res => {
          _this.typeList = res.info
          // this.searchroomdata()
        })
        .catch(err => {
          console.error(err)
        })
    },
    // 操作规程文件上传
    uploadrequest (option) {
      let _this = this
      axios.post(this.$uploadFile, {})
        .then(function (res) {
          if (res.data && res.data.fid && res.data.fid !== '') {
            option.action = `http://${res.data.url}/${res.data.fid}`
            _this.uploadFile = {
              uid: option.file.uid,
              url: res.data.publicUrl,
              fid: res.data.fid
            }
            uploadajax(option)
          } else {
            _this.$message({
              type: 'warning',
              message: '未上传成功！请刷新界面重新上传！'
            })
          }
        })
        .catch(function (error) {
          console.log(error)
          _this.$message({
            type: 'warning',
            message: '未上传成功！请重新上传！'
          })
        })
    },
    handleRemove (file, fileList) {
      this.testlistform.FileUrl = ''
      this.testlistform.FileName = ''
      this.FileUrl = {}
    },
    handleUploadSuccess (res, file) {
      this.testlistform.FileUrl = `${this.uploadFile.url}/${this.uploadFile.fid}`
      this.testlistform.FileName = file.name
      this.FileUrl = URL.createObjectURL(file.raw)
    },
    savedata () {
      let _this = this
      this.$refs.testlistform.validate(valid => {
        if (valid) {
          InstrumentApi.UpdateInstrument(this.testlistform, {})
            .then(res => {
              this.dialogvisible = false
              _this.$message({
                type: 'success',
                message: '保存成功'
              })
              // 刷新
              // this.$emit('initDatas')
              this.$emit('closeEditDialog')
            })
            .catch(err => {
              // handle error
              console.error(err)
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getEntity (pid) {
      let _this = this
      let query = {
        id: pid
      }
      InstrumentApi.getOneInstrument(query)
        .then(response => {
          _this.testlistform = response.records
          // _this.testlistform.CalibrationTime = new Date(response.CalibrationTime)
          if (_this.testlistform.FileName !== '') {
            let file = {
              name: _this.testlistform.FileName,
              url: _this.testlistform.FileUrl
            }
            _this.fileList.push(file)
          }
        }).catch(err => {
          // handle error
          console.error(err)
        })
    },
    // // 查询action
    // getttriggernow (id) {
    //   gettriggerlist({}, id)
    //     .then(res => {
    //       let _this = this
    //       _this.Advancetime = res.items.Advancetime
    //       // 查询子表 有效期
    //       _this.addTriggerl(_this.testlistform.Id, _this.testlistform.Code, _this.testlistform.TimeNotification, _this.testlistform.Name, _this.Advancetime, _this.testlistform.CalibrationTime, _this.testlistform.CalibrationDeadline, _this.testlistform.CalibrationDeadlineType)
    //     })
    //     .catch(err => {
    //       console.error(err)
    //     })
    // },
    // getclassificationlist () {
    //   // 获取样本单位
    //   let _this = this
    //   let params = {
    //     code: 'InstrumentItem'
    //   }
    //   classificationlist(params)
    //     .then(res => {
    //       _this.classificationlist = res.info
    //     })
    // },
    // // 添加报警
    // addTriggerl (ID, Code, TimeNotification, Name, Advancetime, CalibrationTime, CalibrationDeadline, CalibrationDeadlineType) {
    //   // eslint-disable-next-line no-undef
    //   let _this = this
    //   _this.triggerlist.Aid = '4802'
    //   _this.triggerlist.DId = ID
    //   _this.triggerlist.Advancetime = Advancetime// action  报警提前时间
    //   _this.triggerlist.TimeNotification = TimeNotification // 保质期到期提醒
    //   _this.triggerlist.ProductDate = CalibrationTime// '校准日期',
    //   _this.triggerlist.AccCode = Code // '物品编码',
    //   _this.triggerlist.Name = Name // '名称',
    //   _this.triggerlist.ValidityLong = CalibrationDeadline // '有效时长',
    //   _this.triggerlist.ValidityLongType = CalibrationDeadlineType// '有效时长类型：1天；2周；3月；4年',
    //   _this.triggerlist.Category = 3 // '种类 1、耗材；2：试剂'
    //   if (CalibrationDeadlineType === 1) {
    //     _this.triggerlist.RemindTime = new Date(_this.addDate(CalibrationTime, CalibrationDeadline))
    //   } else if (CalibrationDeadlineType === 2) {
    //     _this.triggerlist.RemindTime = new Date(_this.addDate(CalibrationTime, CalibrationDeadline * 7))
    //   } else if (CalibrationDeadlineType === 3) {
    //     _this.triggerlist.RemindTime = new Date(_this.addDate(CalibrationTime, CalibrationDeadline * 30))
    //   } else if (CalibrationDeadlineType === 4) {
    //     _this.triggerlist.RemindTime = new Date(_this.addDate(CalibrationTime, CalibrationDeadline * 365))
    //   }
    //   addTrigger(_this.triggerlist)
    //     .then(function (response) {

    //     })
    // },
    // 计算日期
    addDate (date, days) {
      if (days === undefined || days === '') {
        days = 1
      }
      var dates = new Date(date)
      dates.setDate(dates.getDate() + days)
      var month = dates.getMonth() + 1
      var day = dates.getDate()
      var mm = "'" + month + "'"
      var dd = "'" + day + "'"

      // 单位数前面加0
      if (mm.length === 3) {
        month = '0' + month
      }
      if (dd.length === 3) {
        day = '0' + day
      }

      var time = dates.getFullYear() + '-' + month + '-' + day
      return time
    },
    // // 获取供应商
    // getSupplier () {
    //   let _this = this
    //   let params = {
    //     customerName: 'Supplier'
    //   }
    //   getsupplierlist(params)
    //     .then(res => {
    //       _this.getsupplierlist = res.info
    //     })
    // },
    // 返回当前页
    handleCloseEdit () {
      // this.$refs['uploader'].clearFiles()
      this.fileList = []
      this.$emit('closeEditDialog')
    }
    // deletedataforDid (val) {
    //   deletetriggerlistfordid(val)
    //     .then(res => {
    //     })
    //     .catch(err => {
    //       // handle error
    //       console.error(err)
    //     })
    // }

  }
  // watch: {
  //   InstrumentId: function (newVal) {
  //     this.getEntity(newVal)
  //   }
  // }

}

</script>

<style lang="scss">
.button {
  padding: 0;
  float: right;
}
</style>
