<!-- 新增培训记录弹窗 -->
<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="courseName">
            <el-input v-model="formData.courseName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="培训类型" prop="trnType">
            <el-input v-model="formData.trnType"></el-input>
          </el-form-item>
        </el-col>
         </el-row>
        <el-row>
        <!-- <el-col :span="8">
          <el-form-item label="培训记录编号" prop="traId" style="white-space:nowrap">
            <el-input v-model="formData.traId" :disabled="true"></el-input>
          </el-form-item>
        </el-col> -->
        <el-col :span="8">
            <el-form-item label="培训形式" prop="trnMode">
                <el-input v-model="formData.trnMode"></el-input>
            </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="培训机构" prop="trnOrg">
            <el-input v-model="formData.trnOrg"></el-input>
          </el-form-item>
        </el-col>
         <el-col :span="8">
          <el-form-item label="培训目的" prop="trnPurpose">
            <el-input v-model="formData.trnPurpose"></el-input>
          </el-form-item>
        </el-col>
         </el-row>
        <el-row>
         <el-col :span="8">
          <el-form-item label="培训内容" prop="trnConten">
            <el-input v-model="formData.trnConten"></el-input>
          </el-form-item>
        </el-col>
         <el-col :span="8">
          <el-form-item label="培训开始时间" prop="strDate" style="white-space:nowrap">
            <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" style="white-space:nowrap">
            <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="培训费用" prop="trnCost">
            <el-input v-model="formData.trnCost"></el-input>
          </el-form-item>
        </el-col>
         <el-col :span="8">
          <el-form-item label="公司支付费用" prop="paidByComp" style="white-space:nowrap">
            <el-input v-model="formData.paidByComp"></el-input>
          </el-form-item>
        </el-col>
          <el-col :span="8">
          <el-form-item label="个人支付费用" prop="paidBySelf" style="white-space:nowrap">
            <el-input v-model="formData.paidBySelf"></el-input>
          </el-form-item>
        </el-col>
         </el-row>
         <el-row>
         <el-col :span="8">
          <el-form-item label="培训成绩" prop="trnResult">
            <el-input v-model="formData.trnResult"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
            <el-form-item label="是否通过" prop="passed">
                <el-select v-model="formData.passed" filterable clearable placeholder="请选择">
                    <el-option
                    v-for="(item, index ) in passedList"
                    :key="index"
                    :label="item.passedName"
                    :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
        </el-col>
         <el-col :span="8">
          <el-form-item label="导师评语" prop="tchComments">
            <el-input v-model="formData.tchComments"></el-input>
          </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="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: ['showFlag5', 'Params', 'Params5'],
  data () {
    return {
         dialogTableVisible: false,
         formTypeDsc: '修改培训记录信息',
         formType: 0,
         seen: true,
         seenNo: true,
         beInvalid: false,
         empNos: [],
         empNames: [],
         formData : {
             empNo: '',
             courseName: '',
             trnType: '',
             traId: '',
             trnOrg: '',
             trnConten: '',
             trnCost: '',
             strDate: '',
             endDate: '',
             paidByComp: '',
             paidBySelf: '',
             trnResult: '',
             passed: '',
             tchComments: '',
             attachment01: {oldFileName: '', fileName: ''},
             attachment02: {oldFileName: '', fileName: ''},
             attachment03: {oldFileName: '', fileName: ''},
             memo: '',
         },
         passedList: [
            {id: '0', passedName: '否'},
            {id: '1', passedName: '是'}
         ],
        fileUpload: {},
        fileUpload2: {},
        fileUpload3: {}
    }
  },
  mounted () {
  },
  methods: {
      cancel () {
        this.formData = {
                empNo: '',
                empName: '',
                courseName: '',
                trnType: '',
                traId: '',
                trnOrg: '',
                trnConten: '',
                trnCost: '',
                strDate: '',
                endDate: '',
                paidByComp: '',
                paidBySelf: '',
                trnResult: '',
                passed: '',
                tchComments: '',
                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/limslrcemptrainings/', 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/limslrcemptrainings/', 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)
    //            })
    //            console.log("lalla", this.formData)
    //        }
    //     })
    //   },
      modifyData () {
        this.$refs['formData'].validate((valid) => {
        if (valid) {
           this.formData.updateMan = userName;
           this.formData.updateNo = userId;
           this.axios.put('pass/baseManagement/v1/limslrcemptrainings/', 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_TRAINING",
         }
       }
        this.axios.post('pass/baseManagement/v1/limslrcfilesaves/updateListLrcFileSave', obj, {individualType: 'json'}).then(res => {
           if (res.succeed) {
            this.$message.success('上传成功')
           }
        })
     },
       closeDialog () {
          this.formData = {
            empNo: '',
            empName: '',
            courseName: '',
            trnType: '',
            traId: '',
            trnOrg: '',
            trnConten: '',
            trnCost: '',
            strDate: '',
            endDate: '',
            paidByComp: '',
            paidBySelf: '',
            trnResult: '',
            passed: '',
            tchComments: '',
            attachment01: {oldFileName: '', fileName: ''},
            attachment02: {oldFileName: '', fileName: ''},
            attachment03: {oldFileName: '', fileName: ''},
            memo: ''  
        }
        this.fileUpload = {}
        this.fileUpload2 = {}
        this.fileUpload3 = {}
     },
     },
   watch: {
     showFlag5 (arg) {
        this.dialogTableVisible = true;
        if (this.Params5 !== null) {
          this.formData = Object.assign({},this.Params5)
          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.traId = this.Params.traId;
          // this.formData.empName = this.Params.empName;
          this.formTypeDsc = '新增培训记录信息';
        }
      }
   }
}
</script>
<style lang="less">
.tb-row {
    margin-bottom: 2px !important;
}
</style>
