
<!-- 新增仪器信息 -->
<template>
  <div class="resource">
      <el-dialog @close="closeDialog" :visible.sync="dialogTableVisible" width='70%' style="margin-top: 6hv">
     <div slot="title" class="titleBox">
        <i :class="formType === 0? 'el-icon-document':'el-icon-edit-outline'" style="font-size: 20px"></i>
        <!-- <img style="width: 25px; height: 25px" src='../../../../assets/img/imgScreen/more.png' /> -->
        <span style="margin-left:10px;font-size:18px">{{formTypeDsc}}</span>
        </div>
        <!-- 表单 -->
      <el-form ref="formData" label-width="85px" size="mini" :model="formData" :rules="ruleValidate">
        <el-row>
          <!-- <el-col :span="8" >
            <el-form-item label="仪器编号" prop="eqpNo">
              <el-input v-model="formData.eqpNo" style="width:100%" :disabled="true"></el-input>
            </el-form-item>
          </el-col> -->
          <el-col :span="8" >
            <el-form-item label="仪器名称" prop="eqpName">
              <el-input v-model="formData.eqpName" style="width:100%"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8" >
            <el-form-item label="仪器类型" prop="eqpTypeCode">
              <el-select  style="width:100%" v-model="formData.eqpTypeCode" clearable placeholder="请选择">
                  <el-option
                  v-for="(item,index) in eqpTypeNameList"
                  :key="index"
                  :label="item.baseName"
                  :value="item.baseCode">
                  </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="8" >
            <el-form-item label="仪器类型编号" style="white-space:nowrap" prop="eqpTypeCode" :rules="[{required: true, message: '不能为空', trigger: 'blur'}]">
              <el-input v-model="formData.eqpTypeCode" style="width:100%"></el-input>
            </el-form-item>
          </el-col> -->
           <el-col :span="8" >
            <el-form-item label="用户编号" prop="eqpUseNo">
              <el-input v-model="formData.eqpUseNo" style="width:100%"></el-input>
            </el-form-item>
          </el-col>
         </el-row>
         <el-row>
          <el-col :span="8" >
            <el-form-item label="规格型号" prop="specModel">
              <el-input v-model="formData.specModel" style="width:100%"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8" >
            <el-form-item label="出厂编号" prop="mfcNumber">
              <el-input v-model="formData.mfcNumber" style="width:100%"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8" >
            <el-form-item label="采集IP" prop="collIp">
              <el-input v-model="formData.collIp" style="width:100%"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8" >
            <el-form-item label="采集串口" prop="collCom">
              <el-input v-model="formData.collCom" style="width:100%"></el-input>
            </el-form-item>
          </el-col>
           <el-col :span="8" >
            <el-form-item label="采集类型" prop="collTypeCode">
                <el-select v-model="formData.collTypeCode" clearable placeholder="请选择" style="width:100%">
                    <el-option
                    v-for="(item, index) in collTypeNameList"
                    :key="index"
                    :label="item.baseName"
                    :value="item.baseCode">
                    </el-option>
                </el-select>
            </el-form-item>
        </el-col>
          <el-col :span="8" >
            <el-form-item label="制造厂商" prop="manufacturer">
              <el-input v-model="formData.manufacturer" style="width:100%"></el-input>
            </el-form-item>
          </el-col>
         </el-row>
        <el-row>
          <el-col :span="8" >
            <el-form-item label="仪器状态" prop="minzu">
              <el-select v-model="formData.stateFlag" style="width:100%" clearable placeholder="请选择">
                  <el-option
                  v-for="(item, index) in stateFlagList"
                  :key="index"
                  :label="item.stateFlagName"
                  :value="item.id">
                  </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8" >
            <el-form-item label="下次核查日期" style="white-space:nowrap" prop="nextChkDate">
              <el-date-picker v-model="formData.nextChkDate" style="width:100%" type="date" placeholder="选择日期" format="yyyy-MM-dd" value-format="yyyy-MM-dd"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8" >
            <el-form-item label="使用岗位" prop="usePost">
              <el-select v-model="formData.usePost" style="width:100%" clearable placeholder="请选择">
                  <el-option
                  v-for="(item, index) in usePostList"
                  :key="index"
                  :label="item.postName"
                  :value="item.postName">
                  </el-option>
              </el-select>
              <!-- <el-input type="text" style="width:100%" maxlength="55" v-model="formData.usePost"></el-input> -->
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8" >
            <el-form-item label="公司名称" prop="companyNo">
              <el-select v-model="formData.companyNo" filterable style="width:100%" collapse-tags @change="selectDepartment(formData.companyNo,1)" size="small">
                <el-option v-for="item in companyNameType" :key="item.orgCode" :label="item.orgName" :value="item.orgCode">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8" >
            <el-form-item prop="deptNo" label="部门名称">
              <el-select v-model="formData.deptNo" filterable clearable style="width:100%" @change="selectClass(formData.deptNo,1)" collapse-tags size="small" >
                <el-option v-for="item in sectionNameType" :key="item.orgCode" :label="item.orgName" :value="item.orgCode">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="班组名称" prop="sectionNo">
             <el-select v-model="formData.sectionNo" clearable  style="width:100%" filterable collapse-tags size="small">
                <el-option v-for="item in sectionClass" :key="item.orgCode" :label="item.orgName" :value="item.orgCode">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
         </el-row>
        <el-row>
          <el-col :span="8" >
            <el-form-item label="维护合同编号" style="white-space:nowrap" prop="mtnContractno">
              <el-input v-model="formData.mtnContractno" style="width:100%"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8" >
            <el-form-item label="合同到期日期" style="white-space:nowrap" prop="mtnCtrctEndDate">
              <el-date-picker v-model="formData.mtnCtrctEndDate" style="width:100%" type="date" placeholder="选择日期" format="yyyy-MM-dd" value-format="yyyy-MM-dd"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8" >
            <el-form-item label="检定周期值" style="white-space:nowrap" prop="vrfCycValue">
              <el-input v-model="formData.vrfCycValue" style="width:100%"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8" >
            <el-form-item label="检定周期单位" style="white-space:nowrap" prop="vrfCycUnit">
              <el-select v-model="formData.vrfCycUnit" style="width:100%" clearable placeholder="请选择">
                <el-option
                v-for="(item, index) in vrfCycUnitList"
                :key="index"
                :label="item.vrfCycUnitName"
                :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8" >
            <el-form-item label="下次检定日期" style="white-space:nowrap" prop="nextVrfDate">
              <el-date-picker v-model="formData.nextVrfDate" style="width:100%" type="date" placeholder="选择日期" format="yyyy-MM-dd" value-format="yyyy-MM-dd"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8" >
            <el-form-item label="核查周期值" style="white-space:nowrap" prop="chkCycValue">
              <el-input v-model="formData.chkCycValue" style="width:100%"></el-input>
            </el-form-item>
          </el-col>
          </el-row>
        <el-row>
          <el-col :span="8" >
            <el-form-item label="核查周期单位" style="white-space:nowrap" prop="chkCycUnit">
              <el-select v-model="formData.chkCycUnit" style="width:100%" clearable placeholder="请选择">
                <el-option
                v-for="(item, index) in chkCycUnitList"
                :key="index"
                :label="item.chkCycUnitName"
                :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8" >
            <el-form-item label="仪器描述" prop="eqpDesc">
              <el-input v-model="formData.eqpDesc" style="width:100%"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8" >
            <el-form-item label="负责人" prop="headMan" >
              <el-input size="mini" style="width: 100%;" v-model="formData.headMan"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8" >
            <el-form-item label="接收日期" style="white-space:nowrap" prop="acpDate">
              <el-date-picker v-model="formData.acpDate" style="width:100%" type="date" placeholder="选择日期" format="yyyy-MM-dd" value-format="yyyy-MM-dd"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8" >
            <el-form-item label="启用日期" style="white-space:nowrap" prop="startDate">
              <el-date-picker v-model="formData.startDate" style="width:100%" type="date" placeholder="选择日期" format="yyyy-MM-dd" value-format="yyyy-MM-dd"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8" >
            <el-form-item label="检定状态" prop="vafFlag" >
              <el-input size="mini" style="width: 100%;" v-model="formData.vafFlag"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="文件上传" prop="memo1">
              <el-input
                v-model="formData.memo1"
                size="small"
                readonly
                disabled
                style="width: 192px; display: inline-block"
              ></el-input>
              <el-upload
                class="upload"
                action=""
                v-if="formData.memo1 - 0 === 0"
                style="width: 40px; display: inline-block"
                :multiple="false"
                :show-file-list="false"
                accept=""
                :http-request="httpRequest"
              >
                <el-button
                  type="goon"
                  size="mini"
                  icon="el-icon-upload2"
                ></el-button>
              </el-upload>
              <el-button
                v-else
                type="primary"
                size="mini"
                class="el-icon-circle-close"
                @click="dropFile"
              ></el-button>
            </el-form-item>
          </el-col>
          <el-col :span="13" >
            <el-form-item label="备注" prop="memo" >
              <el-input size="mini" style="width: 100%;" v-model="formData.memo"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
          <span slot="footer" class="dialog-footer">
       <el-button type="primary" icon="el-icon-check" size="mini" @click="formType === 0 ? addData('formData'):modifyData('formData')">确定</el-button>
        <el-button size="mini" type="primary" icon="el-icon-remove-outline" @click="cancel">取消</el-button>
      </span>
 </el-dialog>
  </div>
</template>
<script>
import {
  getCookie
} from '@/utils/util.js';
import {
  cookieUserId,
  cookieUserName
} from '@/config/config.js';
let userName = getCookie(cookieUserName);
let userId = getCookie(cookieUserId);
export default {
  props: ['showFlag', 'Params'],
  data () {
    return {
        dialogTableVisible: false,
        formTypeDsc: '新增仪器信息',
        formType: 0,
        seen: true,
        seenNo: true,
        beInvalid: false,
        formData : {
           eqpNo:'',
           eqpName: '',
           eqpTypeName: '',
           eqpTypeCode: '',
           manufacturer: '',
           specModel: '',
           sectionNo: '',
           mfcNumber: '',
           collIp: '',
           collCom: '',
           eqpUseNo: '',
           eqpDesc: '',
           usePost: '',
           useDept: '',
           mtnContractno: '',
           mtnCtrctEndDate: '',
           vrfCycValue: '',
           vrfCycUnit: '',
           nextVrfDate: '',
           chkCycValue: '',
           chkCycUnit: '',
           nextChkDate: '',
           stateFlag: '',
           headMan: '',
           acpDate: '',
           startDate: '',
           vafFlag: '',
           memo: '',
           memo1: '',
           collTypeCode: ''
        },
        eqpTypeNameList: [],//仪器类型
        usePostList:[],//使用岗位
        sectionClass: [],
        wrkGrpNameList: [], //班组
        companyNameType:[],
        sectionNameType:[],
        collTypeNameList: [],//采购类型
        vrfCycUnitList: [
        { id: '1',  vrfCycUnitName: '天'},
        { id: '2',  vrfCycUnitName: '周'},
        { id: '3',  vrfCycUnitName: '月'},
        { id: '4',  vrfCycUnitName: '季'},
        { id: '5',  vrfCycUnitName: '年'}
        ],//检定周期单位
        chkCycUnitList: [
        { id: '1',  chkCycUnitName: '天'},
        { id: '2',  chkCycUnitName: '周'},
        { id: '3',  chkCycUnitName: '月'},
        { id: '4',  chkCycUnitName: '季'},
        { id: '5',  chkCycUnitName: '年'},
        ],//核查周期单位
        stateFlagList: [
          {id: '1', stateFlagName: '在用'},
          {id: '2', stateFlagName: '故障'},
          {id: '3', stateFlagName: '维修'},
          {id: '4', stateFlagName: '检定'},
          {id: '5', stateFlagName: '封存'},
          {id: '6', stateFlagName: '降级'},
          {id: '7', stateFlagName: '停用'},
          {id: '0', stateFlagName: '报废'},
        ],//仪器状态
        ruleValidate:{
           eqpName: [{
            required: true,
            message: '不能为空',
            trigger: 'change'
          }],
          eqpTypeCode: [{
            required: true,
            message: '不能为空',
            trigger: 'change'
          }],
          eqpUseNo: [{
            required: true,
            message: '不能为空',
            trigger: 'change'
          }],
          companyNo: [{
            required: true,
            message: '不能为空',
            trigger: 'change'
          }],
          deptNo: [{
            required: true,
            message: '不能为空',
            trigger: 'change'
          }],
          memo1: [{
            required: true,
            message: '不能为空',
            trigger: 'change'
          }],
      }
    }
  },
  mounted () {
    this.getEqpTypeNameList ()
    this.getUsePostList ()
    this.axios.post('pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode',{validFlag:1,baseCode:4843}, {individualType: 'json'}).then(res => {
      this.peopleList = res.data;
      this.formData.people =  this.peopleList.length > 0 ?  this.peopleList[0].baseCode : ''
    })
    this.axios.post('pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode',{validFlag:1,baseCode:4836}, {individualType: 'json'}).then((res) => {
      this.wrkGrpNameList = res.data;
    });
    this.axios.post('pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode',{validFlag:1,baseCode:4844}, {individualType: 'json'}).then((res) => {
      this.collTypeNameList = res.data;
    });
    //获取公司
    this.axios.post("pass/baseManagement/v1/sysorgs/querySysOrgCompanyAll",{}, {individualType: 'json'}).then(response => {
      this.companyNameType = response.data
    });
  },
    methods: {
      getCode (code, arr, codeKey, nameKey) {
      if (!code) {
        return '';
      }
      let targetRow = arr.find(item => {
        return item[codeKey] === code;
      });
      // console.log(targetRow, 'targetRow');
      return targetRow ? targetRow[nameKey] : '';
    },
    getEqpTypeNameList () {
       this.axios.post('pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode',{validFlag:1,baseCode:4808}, {individualType: 'json'})
            .then(res => {
              this.eqpTypeNameList= res.data;
            })
    },
    getUsePostList () {
      let obj ={
        'object':{validFlag:1},
        'pageIndex':1,
        'pageSize':99999
      }
       this.axios.post('pass/baseManagement/v1/limsbaseposts/queryLimsBasePostPage/',obj, {individualType: 'json'}).then(res => {
         this.usePostList = res.data.list;
       })
    },
    cancel () {
      this.formData = {
        memo1:''
      }
      this.dialogTableVisible = false;
    },
    // 文件上传
    async httpRequest (e) {
        let _this = this;
        if (!e.file) {
          return false
        }
        let file = new FormData()
        file.append('file', e.file)
        let config = {
          headers: {
            'Content-Type': 'multipart/form-data'
          }
        }
        this.axios.post('pass/baseManagement/v1/file/upload', file, config).then(res =>{
          if (res.code === '0' || res.code === '200') {
            this.$message.success('上传成功')
            this.formData.memo1 = res.data[0].fileName
          } else {
            this.$message.error(res.message)
          }
        }).catch(e => {
          return this.$message.error(e.message)
      })
  },
    // 删除已有文件
    async dropFile () {
      let result = await this.$confirm('确认删除该文件重新上传吗？').catch(() => 'cancel')
      if (result == 'cancel') return
      var data
      try {
        if(this.formData.memo1){
           const data = await this.axios.get('pass/baseManagement/v1/file/delFile/'+ this.formData.memo1, { individualType: 'json' })
          if (data === '1') {
            this.formData.memo1 = ''
            this.$message.success('文件删除成功')
          } else {
            this.$message.error(data.message)
          }
        }else{
          this.formData.memo1 = ''
          this.$message.success('文件删除成功')
        }
      } catch (e) {
      }
    },
    addData (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.formData.createMan = userName;
          this.formData.createNo = userId;
          this.formData.eqpTypeName = this.getCode(this.formData.eqpTypeCode,this.eqpTypeNameList,'baseCode','baseName')
          this.formData.companyName = this.getCode(this.formData.companyNo,this.companyNameType,'orgCode','orgName')
          this.formData.deptName = this.getCode(this.formData.deptNo,this.sectionNameType,'orgCode','orgName')
          this.formData.sectionName = this.getCode(this.formData.sectionNo,this.sectionClass,'orgCode','orgName')
          this.formData.collTypeName = this.getCode(this.formData.collTypeCode,this.collTypeNameList,'baseCode','baseName')
          this.axios.post('pass/baseManagement/v1/limslrceqpinfos/', this.formData, {individualType: 'json'}).then(res => {
            if (res.succeed) {
              this.dialogTableVisible = false;
              this.$message.success('添加成功')
              this.$emit('refresh')
              } else {
                this.$message.error(res.message)
              }
            }).catch(e => {
              this.$message.error(e.message)
            })
        }
       })
      },
      modifyData (formName) {
        this.$refs[formName].validate((valid) => {
        if (valid) {
        this.formData.updateMan = userName;
        this.formData.updateNo = userId;
        this.formData.eqpTypeName = this.getCode(this.formData.eqpTypeCode,this.eqpTypeNameList,'baseCode','baseName')
        this.formData.companyName = this.getCode(this.formData.companyNo,this.companyNameType,'orgCode','orgName')
        this.formData.deptName = this.getCode(this.formData.deptNo,this.sectionNameType,'orgCode','orgName')
        this.formData.sectionName = this.getCode(this.formData.sectionNo,this.sectionClass,'orgCode','orgName')
        this.formData.collTypeName = this.getCode(this.formData.collTypeCode,this.collTypeNameList,'baseCode','baseName')
        this.axios.put('pass/baseManagement/v1/limslrceqpinfos/', this.formData, {individualType: 'json'}).then(res => {
           if (res.succeed) {
            this.dialogTableVisible = false;
            this.$message.success('修改成功')
            this.$emit('refresh')
           } else {
            this.$message.error(res.message)
           }
           }).catch(e => {
            this.$message.error(e.message)
          })
        }
        })
      },
          //根据选中的公司，查询部门名称数据
        selectDepartment(val,index){
          this.sectionNameType = []
          this.companyNameType.find(item =>{
            if(val === item.orgCode){
              this.sectionNameType = item.sysOrgList
              if (index === 1) {
                  this.formData.deptNo = item.sysOrgList.length > 0 ? item.sysOrgList[0].orgCode : ''
                  this.selectClass(this.formData.deptNo, 1)
              } else if (index === 666){
                this.selectClass(this.formData.deptNo)
              }
            }
          })
        },
        //根据选中的部门名称，查询班组名称
        selectClass(val,index){
          this.sectionClass = []
          this.sectionNameType.find(item =>{
            if(val === item.orgCode){
              this.sectionClass = item.sysOrgList
              if (index === 1) {
                  this.formData.sectionNo = item.sysOrgList.length > 0 ? item.sysOrgList[0].orgCode : ''
              }
            }
          })
        },
     closeDialog () {
       this.formData ={
         memo1: ''
       }
       this.seen = true;
       this.seenNo = true;
       this.beInvalid = false;
     },
     },
   watch: {
        showFlag (arg) {
        if (this.Params !== null) {
          this.formData = Object.assign({},this.Params)
          this.formType = 1;
          this.formTypeDsc = '修改仪器信息';
          this.seen = false;
          this.beInvalid = true;
          this.selectDepartment(this.formData.companyNo, 666)
        } else {
          this.formType = 0;
          this.formTypeDsc = '新增仪器信息';
           this.seenNo = false;
        }
        this.dialogTableVisible = true;
      }
        }
}
</script>
<style lang="less">
.tb-row {
    margin-bottom: 2px !important;
}
</style>
