<!-- 新增资格认证弹窗 -->
<template>
  <div class="resource">
      <el-dialog @close="closeDialog" :visible.sync="dialogTableVisible">
     <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="70px" size="mini" :model="formData">
        <el-row>
         <!-- <el-col :span="8">
          <el-form-item label="职工编号" prop="empNo">
            <el-input v-model="formData.empNo" :disabled="true"></el-input>
          </el-form-item>
        </el-col> -->
         <!-- <el-col :span="8">
          <el-form-item label="职工姓名" prop="empName">
            <el-input v-model="formData.empName" :disabled="true"></el-input>
          </el-form-item>
        </el-col> -->
         <!-- <el-col :span="8">
          <el-form-item label="资格证编号" prop="certId" style="white-space:nowrap">
            <el-input v-model="formData.certId" :disabled="true"></el-input>
          </el-form-item>
        </el-col> -->
        <!-- <el-col :span="8">
          <el-form-item label="资格证编号" prop="certifyNo" style="white-space:nowrap">
            <el-input v-model="formData.certifyNo" :readonly="true"></el-input>
          </el-form-item>
        </el-col> -->
        <el-col :span="8">
          <el-form-item label="资格证名称" prop="certifyName" style="white-space:nowrap">
            <el-input v-model="formData.certifyName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
            <el-form-item label="资格证类型" prop="certifyType" style="white-space:nowrap">
                <el-input v-model="formData.certifyType"></el-input>
            </el-form-item>
        </el-col>
        </el-row>
        <el-row>
        <el-col :span="8">
          <el-form-item label="资格证等级" prop="certifyLevel" style="white-space:nowrap">
            <el-input v-model="formData.certifyLevel"></el-input>
          </el-form-item>
        </el-col>
         <el-col :span="8">
          <el-form-item label="发证机构" prop="issueOrg">
            <el-input v-model="formData.issueOrg"></el-input>
          </el-form-item>
        </el-col>
         <el-col :span="8">
          <el-form-item label="发证日期" prop="issueDate">
            <el-date-picker v-model="formData.issueDate" style="width:100%" type="date" placeholder="选择日期" format="yyyy-MM-dd" value-format="yyyy-MM-dd"></el-date-picker>
          </el-form-item>
        </el-col>
         </el-row>
        <el-row>
        <el-col :span="8">
          <el-form-item label="有效期起" prop="strDate">
            <el-date-picker v-model="formData.strDate" 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="endDate">
            <el-date-picker v-model="formData.endDate" 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="unlimited">
               <el-select v-model="formData.unlimited" clearable placeholder="请选择">
                    <el-option
                    v-for="(item,index) in unlimitedList"
                    :key="index"
                    :label="item.unlimited"
                    :value="item.id">
                    </el-option>
               </el-select>
          </el-form-item>
        </el-col>
        </el-row>
        <el-row>
         <el-col :span="24">
          <el-form-item style="width: 100%;" label="备注" prop="memo">
            <el-input size="mini" 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():modifyData()">确定</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: ['showFlag3', 'Params', 'Params3'],
  data () {
    return {
        dialogTableVisible: false,
        formTypeDsc: '修改资格认证信息',
        formType: 0,
        seen: true,
        seenNo: true,
        beInvalid: false,
        empNos: [],
        empNames: [],
        formData : {
            empNo: '',
            empName: '',
            certId: '',
            certifyName: '',
            certifyType: '',
            certifyLevel: '',
            issueOrg: '',
            issueDate: '',
            strDate: '',
            endDate: '',
            memo: '',
        },
         unlimitedList:[
          {unlimited: '是',id: '1'},
          {unlimited: '否',id: '0'},
        ]
    }
  },
    methods: {
      cancel () {
        this.formData = {
                empNo: '',
                empName: '',
                certId: '',
                certifyName: '',
                certifyType: '',
                certifyLevel: '',
                issueOrg: '',
                issueDate: '',
                strDate: '',
                endDate: '',
                memo: ''
        };
        this.dialogTableVisible = false;
        this.$emit('refresh')
      },
      clearList(){
      this.empNos = []
      this.empNames = []
    },
      async addData(){
        this.formData.createMan = userName;
        this.formData.createNo = userId;
        let flag = false;
        for(let i = 0;i < this.empNos.length;i++){
          this.formData.empNo = this.empNos[i];
          this.formData.empName = this.empNames[i];
          const cfs = await this.axios.post('pass/baseManagement/v1/limslrcempcerts/', this.formData, { individualType: 'json' });
          if(cfs.succeed){
            flag = true;
          }else{
            flag = false;
            this.$message.error(res.message)
          }
        }
        if(flag){
          this.dialogTableVisible = false;
          this.$message.success('操作成功')
          this.$emit('refresh')
        }
        this.clearList();
      },
    //  addData () {
    //     this.$refs['formData'].validate((valid) => {
    //           if (valid) {
    //              this.formData.createMan = userName;
    //              this.formData.createNo = userId;
    //              this.axios.post('pass/baseManagement/v1/limslrcempcerts/', 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 () {
        this.$refs['formData'].validate((valid) => {
              if (valid) {
              for (var i = 0; i< this.formData.length; i++) {
              this.formData.updateMan = userName;
              this.formData.updateNo = userId;
              }
              this.axios.put('pass/baseManagement/v1/limslrcempcerts/', 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)
                })
              }
           })
      },
      closeDialog () {
         this.formData = {
              empNo: '',
              empName: '',
              certId: '',
              certifyName: '',
              certifyType: '',
              certifyLevel: '',
              issueOrg: '',
              issueDate: '',
              strDate: '',
              endDate: '',
              memo: ''
          };
         this.seen = true;
         this.seenNo = true;
         this.beInvalid = false;
     },
      },
   watch: {
     showFlag3 (arg) {
        this.dialogTableVisible = true;
        if (this.Params3 !== null) {
           this.formData = Object.assign({},this.Params3)
           this.formType = 1;
           this.formTypeDsc = '修改资格认证信息';
           this.beInvalid = true;
           this.seen = false;
        } else {
          for(let i = 0;i < this.Params.length; i++){
            this.empNos.push(this.Params[i].empNo);
            this.empNames.push(this.Params[i].empName);
          }
           this.formType = 0;
           this.seenNo = false;
          //  this.formData.empNo = this.Params.empNo;
          //  this.formData.certId = this.Params.certId;
          //  this.formData.empName = this.Params.empName;
           this.formTypeDsc = '新增资格认证信息';
        }
      }
    }
}
</script>
<style lang="less">
.tb-row {
    margin-bottom: 2px !important;
}
</style>
