<!-- 新增教育经历弹窗 -->
<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="75px" 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="eduId"  style="white-space:nowrap">
            <el-input v-model="formData.eduId" :disabled="true"></el-input>
          </el-form-item>
        </el-col> -->
         <el-col :span="8">
          <el-form-item label="学校" prop="school">
            <el-input v-model="formData.school"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="学历" prop="eduDegree">
            <el-input v-model="formData.eduDegree"></el-input>
          </el-form-item>
        </el-col>
         </el-row>
        <el-row>
        <el-col :span="8">
            <el-form-item label="专业" prop="major">
                <el-input v-model="formData.major"></el-input>
            </el-form-item>
        </el-col>
         <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-row>
        <el-row>
          <el-col :span="8">
          <el-form-item label="附件1">
             <el-input v-model="formData.attachment01.oldFileName" size="small" readonly disabled style="width: 70%; float:left; margin-right: 5px"></el-input>
               <el-upload
                class="upload"
                action=""
                :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-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="附件2">
             <el-input v-model="formData.attachment02.oldFileName" size="small" readonly disabled style="width: 70%; float:left; margin-right: 5px"></el-input>
               <el-upload
                class="upload"
                action=""
                :multiple="false"
                :show-file-list="false"
                accept=""
                :http-request="httpRequest2">
               <el-button type="goon" size="mini" icon="el-icon-upload2"></el-button>
               </el-upload>
          </el-form-item>
        </el-col>
         <el-col :span="8">
          <el-form-item label="附件3">
               <el-input v-model="formData.attachment03.oldFileName" size="small" readonly disabled style="width: 70%; float:left; margin-right: 5px"></el-input>
               <el-upload
                class="upload"
                action=""
                :multiple="false"
                :show-file-list="false"
                accept=""
                :http-request="httpRequest3">
               <el-button type="goon" size="mini" icon="el-icon-upload2"></el-button>
               </el-upload>
          </el-form-item>
        </el-col>
        </el-row>
        <el-row>
        <el-col :span="8">
          <el-form-item label="学习情况" prop="eduDesc">
            <el-input v-model="formData.eduDesc"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="16">
           <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: ['showFlag4', 'Params', 'Params4'],
  data () {
    return {
        dialogTableVisible: false,
        formTypeDsc: '修改教育经历信息',
        formType: 0,
        seen: true,
        seenNo: true,
        beInvalid: false,
        empNos: [],
        empNames: [],
        formData : {
          empNo: '',
          empName: '',
          eduDegree: '',
          school: '',
          major: '',
          eduId: '',
          eduDesc: '',
          strDate: '',
          endDate: '',
          attachment01: {oldFileName: '', fileName: ''},
          attachment02: {oldFileName: '', fileName: ''},
          attachment03: {oldFileName: '', fileName: ''},
          memo: ''
       },
       fileUpload: {},
       fileUpload2: {},
       fileUpload3: {}
    }
  },
     methods: {
      cancel () {
        this.formData = {
            empNo: '',
            empName: '',
            eduDegree: '',
            school: '',
            eduId: '',
            major: '',
            eduDesc: '',
            strDate: '',
            endDate: '',
            attachment01: {oldFileName: '', fileName: ''},
            attachment02: {oldFileName: '', fileName: ''},
            attachment03: {oldFileName: '', fileName: ''},
            memo: ''
        };
        this.fileUpload = {}
        this.fileUpload2 = {}
        this.fileUpload3 = {}
        this.dialogTableVisible = false;
      },
      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/limslrcempedus/', this.formData, { individualType: 'json' });
          if(cfs.succeed){
            flag = true;
          }else{
            flag = false;
            this.$message.error(res.message)
          }
        }
        if(flag){
          JSON.stringify(this.fileUpload)=='{}'||JSON.stringify(this.fileUpload2)=='{}'||JSON.stringify(this.fileUpload3)=='{}'? '' : this.modifyFile(res.data)
          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/limslrcempedus/', this.formData, {individualType: 'json'}).then(res => {
      //              if (res.succeed) {
      //               JSON.stringify(this.fileUpload)=='{}'||JSON.stringify(this.fileUpload2)=='{}'||JSON.stringify(this.fileUpload3)=='{}'? '' : this.modifyFile(res.data)
      //               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) {
           this.formData.updateMan = userName;
           this.formData.updateNo = userId;
           this.axios.put('pass/baseManagement/v1/limslrcempedus/', this.formData, {individualType: 'json'}).then(res => {
              if (res.succeed) {
               JSON.stringify(this.fileUpload)=='{}'||JSON.stringify(this.fileUpload2)=='{}'||JSON.stringify(this.fileUpload3)=='{}'? '' : this.modifyFile(res.data)
               this.dialogTableVisible = false;
               this.$message.success('修改成功')
               this.$emit('refresh')
              } else {
               this.$message.error(res.message)
              }
              }).catch(e => {
               this.$message.error(e.message)
             })
           }
        })
      },
      httpRequest: function (e) {
        this.fileUpload = this.getFileUpload(e.file, this.formData.attachment01)
//         this.fileUpload = this.getFileUpload(e.file)
//         this.formData.attachment01.oldFileName = this.fileUpload.oldFileName
//         this.formData.attachment01.fileName = this.fileUpload.fileName
      },
      httpRequest2: function (e) {
        this.fileUpload = this.getFileUpload(e.file, this.formData.attachment02)
//          this.fileUpload2 = this.getFileUpload(e.file)
//          this.formData.attachment02.oldFileName = this.fileUpload2.oldFileName
//          this.formData.attachment02.fileName = this.fileUpload2.fileName
       },
      httpRequest3: function (e) {
        this.fileUpload = this.getFileUpload(e.file, this.formData.attachment03)
//          this.fileUpload3 = this.getFileUpload(e.file)
//          this.formData.attachment03.oldFileName = this.fileUpload3.oldFileName
//          this.formData.attachment02.fileName = this.fileUpload2.fileName
       },
      getFileUpload (fileUpload, attachment) {
        if (!fileUpload) {
           return false
        } 
         let file = new FormData ()
         file.append('file', fileUpload)
          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') {
                attachment.oldFileName = res.data[0].oldFileName
                attachment.fileName = res.data[0].fileName
                // return res.data[0]
            } else {
               this.$message.error(res.message)
            }
            }).catch(e => {
               this.$message.error(e.message)
          })
      },
      modifyFile (rowId) {
       let obj = {
         list:[
           this.fileUpload.filePk,
           this.fileUpload2.filePk,
           this.fileUpload3.filePk,
         ],
         object:{
           objId: rowId,
           objName: "LIMS_LRC_EMP_EDU",
         }
       }
        this.axios.post('pass/baseManagement/v1/limslrcfilesaves/updateListLrcFileSave', obj, {individualType: 'json'}).then(res => {
           if (res.succeed) {
            this.$message.success('上传成功')
           }
        })
     },
      closeDialog () {
        this.formData = {
            empNo: '',
            empName: '',
            eduDegree: '',
            school: '',
            major: '',
            eduId: '',
            eduDesc: '',
            strDate: '',
            endDate: '',
            attachment01: {oldFileName: '', fileName: ''},
            attachment02: {oldFileName: '', fileName: ''},
            attachment03: {oldFileName: '', fileName: ''},
            memo: ''
        };
        this.fileUpload = {}
        this.fileUpload2 = {}
        this.fileUpload3 = {}
        this.$emit('refresh')
     },
     },
   watch: {
    showFlag4 (arg) {
        this.dialogTableVisible = true;
        if (this.Params4 !== null) {
          this.formData = Object.assign({},this.Params4)
          this.formType = 1;
          this.formTypeDsc = '修改教育经历信息';
        } 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.eduId = this.Params.eduId;
          // this.formData.empName = this.Params.empName;
          this.formTypeDsc = '新增教育经历信息';
        }
      }
   }
}
</script>
<style lang="less">
.tb-row {
    margin-bottom: 2px !important;
}
</style>
