<template>
    <div class="examination">
     <el-dialog @close="closeDialog" :visible.sync="dialogTableVisible" customClass="customWidth_certifi">
        <div slot="title" class="titleBox" style="display: flex;">
        <i :class="tableType === 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">{{tableTypeDsc}}</span>
        </div>
        <el-form :model="formData" label-width="110px" :rules="monitorForm.monitorRules" ref="monitorForm" >
         <table class="common-table-style4">
          <tr>
             <td>
               <el-form-item prop="tmpNo" label="模板编号">
               <el-input v-model="tmpNo" size="small" readonly disabled ></el-input>
               </el-form-item>
             </td>
             <td>
               <el-form-item prop="tmpName" label="模板名称">
               <el-input v-model="tmpName" size="small" readonly disabled ></el-input>
               </el-form-item>
             </td>
           </tr>
           <tr>
             <td>
               <el-form-item prop="testPlanName" label="计划名称">
               <el-input v-model="formData.testPlanName" size="small" ></el-input>
               </el-form-item>
             </td>
             <td>
               <el-form-item prop="startTime" label="开始时间">
               <el-date-picker v-model="formData.startTime" size="small" type="date" placeholder="选择日期" ></el-date-picker>
               </el-form-item>
             </td>
           </tr>
            <tr>
             <td>
               <el-form-item prop="matClassNameTypeIndex" label="物料类型">
               <el-select v-model="matClassNameTypeIndex" filterable collapse-tags size="small" >
                  <el-option v-for="(item,index) in matClassNameType" :key="index" :label="item.matClassName"
                    :value="index">
                  </el-option>
                </el-select>
               </el-form-item>
             </td>
             <td>
               <el-form-item prop="endTime" label="结束时间">
               <el-date-picker v-model="formData.endTime" size="small" type="date" placeholder="选择日期" ></el-date-picker>
               </el-form-item>
             </td>
           </tr>
            <tr>
             <td>
               <el-form-item prop="matNameTypeIndex" label="物料名称">
               <el-select v-model="matNameTypeIndex" filterable collapse-tags size="small" >
                  <el-option v-for="(item,index) in matNameType" :key="index" :label="item.matName"
                    :value="index">
                  </el-option>
                </el-select>
               </el-form-item>
             </td>
             <td>
               <el-form-item prop="durTime" label="间隔（小时）">
               <el-input v-model="durTime" size="small" ></el-input>
               </el-form-item>
             </td>
           </tr>
            <tr>
             <td>
               <el-form-item prop="pointNameTypeIndex" label="取样地点">
               <el-select v-model="pointNameTypeIndex" filterable collapse-tags size="small" >
                  <el-option v-for="(item,index) in pointNameType" :key="index" :label="item.pointName"
                    :value="index">
                  </el-option>
                </el-select>
               </el-form-item>
             </td>
             <td>
               <el-form-item prop="testLimit" label="分析时限（小时）">
               <el-input v-model="testLimit" size="small" ></el-input>
               </el-form-item>
             </td>
           </tr>
            <tr>
             <td>
               <el-form-item prop="companyNameTypeIndex" label="采样单位">
               <el-select v-model="companyNameTypeIndex" filterable collapse-tags size="small" >
                  <el-option v-for="(item,index) in companyNameType" :key="index" :label="item.companyName"
                    :value="index">
                  </el-option>
                </el-select>
               </el-form-item>
             </td>
             <td>
               <el-form-item prop="nextTime" label="下次执行时间">
               <el-date-picker v-model="nextTime" size="small" type="date" placeholder="选择日期" ></el-date-picker>
               </el-form-item>
             </td>
           </tr>
            <tr>
             <td>
               <el-form-item prop="memo" label="备注">
               <el-input v-model="memo" size="small" style="width: 100%"></el-input>
               </el-form-item>
             </td>
             <td style=" text-align:left">
               <el-form-item prop="autoLogin">
               <el-checkbox v-model="autoLogin">是否自动登录</el-checkbox>
               </el-form-item>
             </td>
           </tr>
        </table>
        </el-form>
      <span slot="footer" class="dialog-footer" v-if="tableType === 0">
        <el-button type="goon" size="mini" icon="el-icon-check" @click="addData()">下一步</el-button>
        <!-- <el-button size="mini" type="goon" icon="el-icon-remove-outline" @click="cancel">取消</el-button> -->
      </span>
       <span slot="footer" class="dialog-footer" v-if="tableType === 1">
        <el-button type="goon" size="mini" icon="el-icon-check" @click="tableType === 0 ? addData():modifyData()">确定</el-button>
        <el-button size="mini" type="goon" 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: ['showFlag2', 'Params', 'Params2'],
    data () {
       var endTimeVerify = (rule, value, callback) => {
	        if(this.formData.endTime){
	          if(Date.parse(this.formData.endTime) > Date.parse(this.formData.startTime)){
	            callback();
	          }else{
	            callback(new Error('结束时间不得早于开始时间！'));
	          }
	        }else{
	          callback();
          }
       };
        return {
            dialogTableVisible: false,
            tableTypeDsc: '新增测试计划信息',
            tableType: 0,
            seen: true,
            seenNo: true,
            seenRule: true,
            beInvalid: false,
            tmpNo: '',
            tmpName: '',
            durTime: '',
            testLimit: '',
            nextTime: '',
            memo: '',
            formData: {
              startTime: '',
              endTime: '',
              testPlanName: '',
            },
            testPlan: {},
            matClassNameTypeIndex: 0,//物料类型
            companyNameTypeIndex: 0,
            matNameTypeIndex: 0,//物料名称
            pointNameTypeIndex: 0,//取样地点
            companyNameType: [
              {companyNo:'1',companyName:'公司名称1'},
              {companyNo:'2',companyName:'公司名称2'}
            ],
            matClassNameType: [],
            matNameType: [],
            pointNameType: [],
            autoLogin: false,
            monitorForm: {
            tableData: [],
            monitorRules: {
              startTime: [{ required: true, message: '该项不能为空', trigger: ['blur', 'change'] }],
              endTime: [{ required: true, message: '该项不能为空', trigger: ['blur', 'change'] },{ required: true, validator: endTimeVerify, trigger: 'blur' }],
              testPlanName:[{ required: true, message: '该项不能为空', trigger: ['blur', 'change'] }],
             }
            }
        }
    },
    mounted () {
      // this.getCompanyNameType ()
      this.getMatClassNameType ()
      this.getMatNameType ()
      this.getPointNameType ()
    },
    methods: {
      cancel () {
        let _this = this;
        _this.monitorForm.tableData = []
        _this.dialogTableVisible = false
        _this.tmpNo = '',
        _this.tmpName = '',
        _this.formData.testPlanName = '',
        _this.formData.startTime = '',
        _this.formData.endTime = '',
        _this.durTime = '',
        _this.testLimit = '',
        _this.nextTime = '',
        _this.memo = '',
        _this.matClassNameTypeIndex = 0,//物料类型
        _this.companyNameTypeIndex = 0,
        _this.matNameTypeIndex = 0,//物料名称
        _this.pointNameTypeIndex = 0,//取样地点
        _this.$emit('refresh')
      },
      addData () {
        let _this = this;
         _this.$refs['monitorForm'].validate((valid) => {
          if (valid) {
        _this.dialogTableVisible = false;
        for (var i = 0; i < _this.monitorForm.tableData.length; i++) {
        _this.monitorForm.tableData[i].createMan = userName;
        _this.monitorForm.tableData[i].createNo = userId;
        _this.monitorForm.tableData[i].tmpNo = _this.tmpNo
        _this.monitorForm.tableData[i].tmpName = _this.tmpName
        _this.monitorForm.tableData[i].testPlanName = _this.formData.testPlanName
        _this.monitorForm.tableData[i].startTime = _this.formData.startTime
        _this.monitorForm.tableData[i].endTime = _this.formData.endTime
        _this.monitorForm.tableData[i].durTime = _this.durTime
        _this.monitorForm.tableData[i].testLimit = _this.testLimit
        _this.monitorForm.tableData[i].nextTime = _this.nextTime
        _this.monitorForm.tableData[i].memo = _this.memo
        if (_this.autoLogin === true) {
           _this.monitorForm.tableData[i].autoLogin = 1
        } else {
           _this.monitorForm.tableData[i].autoLogin = 0
        }
        _this.monitorForm.tableData[i].companyNo = _this.companyNameType[_this.companyNameTypeIndex].companyNo;
        _this.monitorForm.tableData[i].companyName = _this.companyNameType[_this.companyNameTypeIndex].companyName;
        _this.monitorForm.tableData[i].matClassNo = _this.matClassNameType[_this.matClassNameTypeIndex].matClassNo;
        _this.monitorForm.tableData[i].matClassName = _this.matClassNameType[_this.matClassNameTypeIndex].matClassName;
        _this.monitorForm.tableData[i].matNo = _this.matNameType[_this.matNameTypeIndex].matNo;
        _this.monitorForm.tableData[i].matName = _this.matNameType[_this.matNameTypeIndex].matName;
        _this.monitorForm.tableData[i].pointNo = _this.pointNameType[_this.pointNameTypeIndex].pointNo;
        _this.monitorForm.tableData[i].pointName = _this.pointNameType[_this.pointNameTypeIndex].pointName;
        }
        _this.testPlan = _this.monitorForm.tableData[0]
        _this.axios.post('pass/baseManagement/v1/limsstdtestplans/', _this.monitorForm.tableData[0], {individualType: 'json'}).then(res => {
           if (res.succeed) {
              _this.$emit('refresh', res.data)
              this.$message.success('测试计划添加成功！')
            // this.$message.success('添加成功')
           } else {
            this.$message.error(res.message)
           }
           }).catch(e => {
            this.$message.error(e.message)
          })
          }
         })
      },
       modifyData () {
        let _this = this;
         _this.$refs['monitorForm'].validate((valid) => {
          if (valid) {
        _this.dialogTableVisible = false;
         for (var i = 0; i < _this.monitorForm.tableData.length; i++) {
       _this.monitorForm.tableData[i].createMan = userName;
        _this.monitorForm.tableData[i].createNo = userId;
        _this.monitorForm.tableData[i].tmpNo = _this.tmpNo
        _this.monitorForm.tableData[i].tmpName = _this.tmpName
        _this.monitorForm.tableData[i].testPlanName = _this.formData.testPlanName
        _this.monitorForm.tableData[i].startTime = _this.formData.startTime
        _this.monitorForm.tableData[i].endTime = _this.formData.endTime
        _this.monitorForm.tableData[i].durTime = _this.durTime
        _this.monitorForm.tableData[i].testLimit = _this.testLimit
        _this.monitorForm.tableData[i].nextTime = _this.nextTime
        _this.monitorForm.tableData[i].memo = _this.memo
        if (_this.autoLogin === true) {
           _this.monitorForm.tableData[i].autoLogin = 1
        } else {
           _this.monitorForm.tableData[i].autoLogin = 0
        }
        _this.monitorForm.tableData[i].companyNo = _this.companyNameType[_this.companyNameTypeIndex].companyNo;
        _this.monitorForm.tableData[i].companyName = _this.companyNameType[_this.companyNameTypeIndex].companyName;
        _this.monitorForm.tableData[i].matClassNo = _this.matClassNameType[_this.matClassNameTypeIndex].matClassNo;
        _this.monitorForm.tableData[i].matClassName = _this.matClassNameType[_this.matClassNameTypeIndex].matClassName;
        _this.monitorForm.tableData[i].matNo = _this.matNameType[_this.matNameTypeIndex].matNo;
        _this.monitorForm.tableData[i].matName = _this.matNameType[_this.matNameTypeIndex].matName;
        _this.monitorForm.tableData[i].pointNo = _this.pointNameType[_this.pointNameTypeIndex].pointNo;
        _this.monitorForm.tableData[i].pointName = _this.pointNameType[_this.pointNameTypeIndex].pointName;
        }
        _this.axios.put('pass/baseManagement/v1/limsstdtestplans/', _this.monitorForm.tableData[0], {individualType: 'json'}).then(res => {
           if (res.succeed) {
            this.$message.success('修改成功')
            _this.$emit('refresh')
           } else {
            this.$message.error(res.message)
           }
           }).catch(e => {
            this.$message.error(e.message)
          })
          }
         })
      },
      renderHeader (h) {
         return (
             <div style="cursor: pointer ;line-height:20px" onClick={() => this.addRow()}>
             <i class="el-icon-plus" style="font-size:20px; color:rgb(18, 52, 243)"></i>
             </div>
         )
      },
      deleteRow (index) {
        this.monitorForm.tableData.splice(index, 1);
      },
      addRow () {
      let list = {}
      let _this = this;
      if (_this.tableType === 0) {
         _this.monitorForm.tableData.push(list)
      } else if (_this.tableType === 1) {
          return ''
        }
     },
     getCompanyNameType () {
         this.axios.get('pass/baseManagement/v1/limsbaseinfos/'+ '?validFlag=1')
            .then(res => {
              this.companyNameType = res.data.list;
            })
      },
      getMatClassNameType () {
         this.axios.get('pass/baseManagement/v1/limsbasematerialsclasss/'+ '?validFlag=1')
            .then(res => {
              this.matClassNameType = res.data.list;
            })
      },
      getMatNameType () {
         this.axios.get('pass/baseManagement/v1/limsbasematerialss/'+ '?validFlag=1')
            .then(res => {
              this.matNameType = res.data.list;
            })
      },
      getPointNameType () {
         this.axios.get('pass/baseManagement/v1/limsbasepoints/'+ '?validFlag=1')
            .then(res => {
              this.pointNameType = res.data.list;
            })
      },
      closeDialog () {
       let _this = this;
       _this.monitorForm.tableData = [];
       _this.seen = true;
       _this.seenNo = true;
       _this.seenRule = true;
       _this.beInvalid = false;
       _this.tmpNo = '',
       _this.tmpName = '',
       _this.formData.testPlanName = '',
       _this.formData.startTime = '',
       _this.formData.endTime = '',
       _this.durTime = '',
       _this.testLimit = '',
       _this.nextTime = '',
       _this.memo = '',
       _this.matClassNameTypeIndex = 0,//物料类型
       _this.companyNameTypeIndex = 0,
       _this.matNameTypeIndex = 0,//物料名称
       _this.pointNameTypeIndex = 0,//取样地点
       _this.$emit('refresh')
     },
      tableHeaderCellStyle () {
      return 'background-color: rgb(249,249,249) !important;color: rgb(73, 72, 72);border-bottom: 1px solid #DCDFE7;border-top:3px solid #3ac1ea;'
      }
    },
    watch: {
      showFlag2 (arg) {
        let _this = this;
        _this.dialogTableVisible = true;
        if (_this.Params2 !== null) {
          _this.tableType = 1;
          _this.tableTypeDsc = '修改测试计划信息';
          _this.seen = false;
          _this.seenRule = false;
          _this.beInvalid = true;
          _this.monitorForm.tableData.push(_this.Params2);
          _this.tmpNo = _this.Params2.tmpNo;
          _this.tmpName = _this.Params2.tmpName;
          _this.tmpName = _this.Params2.strRowB;
          _this.formData.testPlanName = _this.Params2.testPlanName;
          _this.formData.startTime = _this.Params2.startTime;
          _this.formData.endTime = _this.Params2.endTime;
          _this.durTime = _this.Params2.durTime;
          _this.memo = _this.Params2.memo;
          _this.testLimit = _this.Params2.testLimit;
          _this.nextTime = _this.Params2.nextTime;
          for (var i = 0; i < _this.companyNameType.length; i++) {
           if (_this.companyNameType[i].companyName === _this.Params2.companyName) {
              _this.companyNameTypeIndex = i;
           }
          }
           for (var i = 0; i < _this.matClassNameType.length; i++) {
           if (_this.matClassNameType[i].matClassName === _this.Params2.matClassName) {
              _this.matClassNameTypeIndex = i;
           }
          }
           for (var i = 0; i < _this.matNameType.length; i++) {
           if (_this.matNameType[i].matName === _this.Params2.matName) {
              _this.matNameTypeIndex = i;
           }
          }
           for (var i = 0; i < _this.pointNameType.length; i++) {
           if (_this.pointNameType[i].pointName === _this.Params2.pointName) {
              _this.pointNameTypeIndex = i;
           }
          }
          if (_this.Params2.autoLogin === 1) {
            _this.autoLogin = true
          } else {
            _this.autoLogin = false
          }
        } else {
          _this.tableType = 0;
          _this.monitorForm.tableData.push({});
          _this.seenNo = false;
          _this.tmpNo = _this.Params.tmpNo;
          _this.tmpName = _this.Params.tmpName;
          _this.tableTypeDsc = '新增测试计划信息';
        }
      }
    }
}
</script>
<style lang="less">
.customWidth_certifi{
  width: 700px !important
}
</style>

