<!-- 新增人员合同信息表弹窗 -->
<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>
        <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="contId">
            <el-input v-model="formData.contId" :disabled="true"></el-input>
          </el-form-item>
        </el-col> -->
        <el-col :span="8">
          <el-form-item label="合同名称" prop="contractTitle">
            <el-input v-model="formData.contractTitle"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
            <el-form-item label="合同类型" prop="contractType">
                <el-input v-model="formData.contractType"></el-input>
            </el-form-item>
        </el-col>
        </el-row>
        <el-row>
         <el-col :span="8">
          <el-form-item label="档案编号" prop="fileNumber">
            <el-input v-model="formData.fileNumber"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
            <el-form-item label="是否试用" prop="onTrial">
                <el-select v-model="formData.onTrial" filterable clearable placeholder="请选择">
                    <el-option
                    v-for="(item, index) in onTrialList"
                    :key="index"
                    :label="item.onTrialName"
                    :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="unlimited">
                <el-select v-model="formData.unlimited" filterable clearable placeholder="请选择">
                    <el-option
                    v-for="(item, index) in unlimitedList"
                    :key="index"
                    :label="item.unlimitedName"
                    :value="item.id">
                    </el-option>
                </el-select>
            </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="试用期至" prop="trialEndDate">
            <!-- <el-input v-model="formData.trialEndDate" type="date" placeholder="选择日期" format="yyyy 年 MM 月 dd 日" value-format="yyyy-MM-dd"></el-input> -->
            <el-date-picker v-model="formData.trialEndDate" 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="signDate">
            <el-date-picker v-model="formData.signDate" 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="附件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="开始日期" 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="附件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-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: ['showFlag2', 'Params', 'Params2'],
  data () {
    return {
        dialogTableVisible: false,
        formTypeDsc: '修改人员合同信息',
        formType: 0,
        seen: true,
        seenNo: true,
        beInvalid: false,
        empNos:[],
        empNames:[],
        contId:[],
        formData: {
            empNo: '',
            empName: '',
            contId: '',
            contractTitle: '',
            contractType: '',
            onTrial: '',
            unlimited: '',
            fileNumber: '',
            trialEndDate: '',
            signDate: '',
            strDate: '',
            endDate: '',
            attachment01: {oldFileName: '', fileName: ''},
            attachment02: {oldFileName: '', fileName: ''},
            attachment03: {oldFileName: '', fileName: ''},
            memo: ''                       
         },
        onTrialList: [
            {id: '1', onTrialName: '是'},
            {id: '0', onTrialName: '否'}
        ],
        unlimitedList: [
            {id: '1', unlimitedName: '是'},
            {id: '0', unlimitedName: '否'}
        ],
        fileUpload: {},
        fileUpload2: {},
        fileUpload3: {}
    }
  },
   mounted () {},
   methods: {
     cancel () {
        this.formData = {
                empNo: '',
                empName: '',
                contId: '',
                contractTitle: '',
                contractType: '',
                onTrial: '',
                unlimited: '',
                fileNumber: '',
                trialEndDate: '',
                signDate: '',
                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/limslrcempconts/', 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();
      },
      // async addData () {
      //   this.$refs['formData'].validate((valid) => {
      //     if (valid) {
      //       this.formData.createMan = userName;
      //       this.formData.createNo = userId;
      //       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/limslrcempconts/', this.formData, { individualType: 'json' });
      //         if (cfs.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)
      //               }
      //       }
      //         }
      //      })
      //    },
       modifyData () {
        this.$refs['formData'].validate((valid) => {
          if (valid) {
             this.formData.updateMan = userName;
             this.formData.updateNo = userId;
             this.axios.put('pass/baseManagement/v1/limslrcempconts/', this.formData, {individualType: 'json'}).then(res => {
                if (res.succeed) {
                 JSON.stringify(this.fileUpload)=='{}'||JSON.stringify(this.ufileUpload2)=='{}'||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.formData.attachment01.oldFileName = this.fileUpload.oldFileName
        // this.formData.attachment01.fileName = this.fileUpload.fileName
      },
      httpRequest2: function (e) {
         this.fileUpload2 = this.getFileUpload(e.file, this.formData.attachment02)
        //  this.formData.attachment02.oldFileName = this.fileUpload2.oldFileName
        //  this.formData.attachment02.fileName = this.fileUpload2.fileName
       },
      httpRequest3: function (e) {
         this.fileUpload3 = this.getFileUpload(e.file, this.formData.attachment03)
        //  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_CONT",
         }
       }
        this.axios.post('pass/baseManagement/v1/limslrcfilesaves/updateListLrcFileSave', obj, {individualType: 'json'}).then(res => {
           if (res.succeed) {
            this.$message.success('上传成功')
           }
        })
     },
      closeDialog () {
       this.formData = {
            empNo: '',
            empName: '',
            contId: '',
            contractTitle: '',
            contractType: '',
            onTrial: '',
            unlimited: '',
            fileNumber: '',
            trialEndDate: '',
            signDate: '',
            strDate: '',
            endDate: '',
            attachment01: {oldFileName: '', fileName: ''},
            attachment02: {oldFileName: '', fileName: ''},
            attachment03: {oldFileName: '', fileName: ''},
            memo: ''  
        }
        this.fileUpload = {}
        this.fileUpload2 = {}
        this.fileUpload3 = {}
      //  this.$emit('refresh')
     },
     },
   watch: {
    showFlag2 (arg) {
        this.dialogTableVisible = true;
        if (this.Params2 !== null) {
          this.formData = Object.assign({},this.Params2)
          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.contIds.push(this.Params[i].contId);
          }
          this.formType = 0;
          this.seenNo = false;
          // this.formData.empNo = this.Params[0].empNo;
          // this.formData.contId = this.Params[0].contId;
          // this.formData.empName = this.Params[0].empName;
          this.formTypeDsc = '新增人员合同信息';
        }
      }
        }
}
</script>
<style lang="less">
.tb-row {
    margin-bottom: 2px !important;
}
</style>
