<template>
  <el-dialog title="编辑人员信息"
             :visible.sync="dialogvisible"
             width="75%"
             :before-close="handleCloseEdit">
    <el-form size="mini"
             :model="testlistform"
             :rules="rulestestlistform"
             label-width="100px"
             ref="testlistform">
      <el-row :gutter="20"
              class="donorsaddformcss">

        <el-col :span="8">
          <el-form-item label="人员姓名"
                        prop="PersonnelName"
                        label-width="120px">
            <el-input v-model="testlistform.PersonnelName"
                      placeholder="请输入人员姓名"
                      style="width:100%"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="人员编号"
                        prop="PersonneCode"
                        label-width="120px">
            <el-input v-model="testlistform.PersonneCode"
                      placeholder="请输入人员编号"
                      style="width:100%"></el-input>
          </el-form-item>
        </el-col>

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

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

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

        <el-col :span="8">
          <el-form-item label="联系地址"
                        prop="Address"
                        label-width="120px">
            <el-input v-model="testlistform.Address"
                      placeholder="请输入联系地址"></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 PersonnelApi from '@/api/personnel'

export default {
  name: 'personneledit',
  props: {
    PersonnelId: {
      default: 0
    }
  },
  data () {
    return {
      fileList: [],
      FileUrl: {},
      uploadFile: {},
      dialogvisible: false,
      formtype: '1',
      disabledbarcode: false,
      testlistform: {
      },
      Advancetime: 0,
      triggerlist: {},
      TimeNotification: false, // 有效期提醒
      classificationlist: [],
      getsupplierlist: [],
      rulestestlistform: {
        PersonnelName: [{
          required: true,
          message: '请输入人员姓名',
          trigger: 'blur'
        }],
        PersonneCode: [{
          required: true,
          message: '请输入人员编号',
          trigger: 'blur'
        }],
        Telephone: [{
          required: true,
          message: '请输入电话',
          trigger: 'blur'
        }],
        Mailbox: [{
          required: true,
          message: '请输入邮箱',
          trigger: 'blur'
        }],
        CardId: [{
          required: true,
          message: '请输入身份证号',
          trigger: 'blur'
        }],
        Address: [{
          required: true,
          message: '请输入联系地址',
          trigger: 'blur'
        }]
      }
    }
  },
  created () {
  },
  methods: {
    // 操作规程文件上传
    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
      PersonnelApi.UpdatePersonnel(this.testlistform, {})
        .then(res => {
          // savedataedit(_this.testlistform.Id, _this.testlistform)
          // _this.$axios.put('/personnel/saveeditinstument/' + _this.testlistform.Id, _this.testlistform)
          // .then(res => {
          // response
          // if (res.info.code === 0) {
          // if (_this.TimeNotification) {
          //   // _this.getttriggernow(4802)
          // }
          // _this.$message({
          //   type: 'success',
          //   message: res.info.message
          // })
          // window.history.go(-1)
          // } else {
          // _this.$message({
          //   type: 'warning',
          //   message: res.info.message
          // })
          // }
          this.dialogvisible = false
          this.fileList = []
          // 刷新
          // this.$emit('initDatas')
          this.$emit('closeEditDialog')
        })
        .catch(err => {
          // handle error
          console.error(err)
        })

    },
    getEntity (pid) {
      let _this = this
      let query = {
        id: pid
      }
      PersonnelApi.getOnePersonnel(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: 'PersonnelItem'
    //   }
    //   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: {
  //   PersonnelId: function (newVal) {
  //     this.getEntity(newVal)
  //   }
  // }

}

</script>

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