<!-- 新增文件维护记录弹窗 -->
<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="65px" size="mini" :model="formData">
         <el-row>
         <!-- <el-col :span="8">
          <el-form-item label="记录编号" prop="fileIdD">
            <el-input v-model="formData.fileIdD" :disabled="true"></el-input>
          </el-form-item>
        </el-col> -->
         <!-- <el-col :span="8">
          <el-form-item label="文件ID" prop="fileId">
            <el-input v-model="formData.fileId" :disabled="true"></el-input>
          </el-form-item>
        </el-col> -->
         <el-col :span="8">
          <el-form-item label="修改原因" prop="mdfReason">
            <el-input v-model="formData.mdfReason"></el-input>
          </el-form-item>
        </el-col>
         <el-col :span="8">
          <el-form-item label="修改内容" prop="mdfContent">
            <el-input v-model="formData.mdfContent"></el-input>
          </el-form-item>
        </el-col>
          <el-col :span="8">
          <el-form-item label="文件版本号" prop="versionNo" style="white-space:nowrap">
            <el-input v-model="formData.versionNo"></el-input>
          </el-form-item>
        </el-col>
        </el-row>
         <el-row>
          <el-col :span="8">
          <el-form-item label="有效期起" prop="validDateStr">
            <el-date-picker v-model="formData.validDateStr" 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="validDateEnd">
            <el-date-picker v-model="formData.validDateEnd" 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="pubDate">
            <el-date-picker v-model="formData.pubDate" 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="24">
         <el-form-item 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,
        formData : {
          fileIdD: '',
          fileId: '',
          versionNo: '',
          mdfReason: '',
          mdfContent: '',
          pubDate: '',
          validDateStr: '',
          validDateEnd: '',
          attachment01: {oldFileName: '', fileName: ''},
          attachment02: {oldFileName: '', fileName: ''},
          attachment03: {oldFileName: '', fileName: ''},
          memo: ''
        },
        fileUpload: {},
        fileUpload2: {},
        fileUpload3: {}
    }
  },
    methods: {
      cancel () {
        this.formData = {
             fileIdD: '',
             fileId: '',
             versionNo: '',
             mdfReason: '',
             mdfContent: '',
             pubDate: '',
             validDateStr: '',
             validDateEnd: '',
             attachment01: {oldFileName: '', fileName: ''},
             attachment02: {oldFileName: '', fileName: ''},
             attachment03: {oldFileName: '', fileName: ''},
             memo: ''
        };
        this.fileUpload = {}
        this.fileUpload2 = {}
        this.fileUpload3 = {}
        this.dialogTableVisible = false;
      },
      addData () {
          this.$refs['formData'].validate((valid) => {
            if (valid) {
               this.formData.createMan = userName;
               this.formData.createNo = userId;
               this.axios.post('pass/baseManagement/v1/limslrcfileds/', this.formData, {individualType: 'json'}).then(res => {
                      if (res.succeed) {
                       JSON.stringify(this.uploadFile)=='{}'||JSON.stringify(this.uploadFile2)=='{}'||JSON.stringify(this.uploadFile3)=='{}'? '' : 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/limslrcfileds/', this.formData, {individualType: 'json'}).then(res => {
               if (res.succeed) {
                JSON.stringify(this.uploadFile)=='{}'||JSON.stringify(this.uploadFile2)=='{}'||JSON.stringify(this.uploadFile3)=='{}'? '' : 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.oldFileName = this.fileUpload.oldFileName
        this.formData.attachment01.fileName = this.fileUpload.fileName
      },
      httpRequest2: function (e) {
         this.fileUpload2 = this.getFileUpload(e.file)
         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.oldFileName = this.fileUpload3.oldFileName
         this.formData.attachment02.fileName = this.fileUpload2.fileName
       },
      getFileUpload (fileUpload) {
        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') {
                return res.data[0]
            } else {
               this.$message.error(res.message)
            }
            }).catch(e => {
               this.$message.error(e.message)
          })
      },
      modifyFile (rowId) {
       let obj = {
         list:[
           this.uploadFile.filePk,
           this.uploadFile2.filePk,
           this.uploadFile3.filePk,
         ],
         object:{
           objId: rowId,
           objName: "LIMS_LRC_FILE_D",
         }
       }
        this.axios.post('pass/baseManagement/v1/limslrcfilesaves/updateListLrcFileSave', obj, {individualType: 'json'}).then(res => {
           if (res.succeed) {
            this.$message.success('上传成功')
           }
        })
     },
     closeDialog () {
          this.formData = {
                fileIdD: '',
                fileId: '',
                versionNo: '',
                mdfReason: '',
                mdfContent: '',
                pubDate: '',
                validDateStr: '',
                validDateEnd: '',
                attachment01: {oldFileName: '', fileName: ''},
                attachment02: {oldFileName: '', fileName: ''},
                attachment03: {oldFileName: '', fileName: ''},
                memo: ''
          }
          this.fileUpload = {}
          this.fileUpload2 = {}
          this.fileUpload3 = {}
      },
    },
   watch: {
     showFlag2 (arg) {
        this.dialogTableVisible = true;
        if (this.Params2 !== null) {
          this.formData = Object.assign({},this.Params2)
          this.formType = 1;
          this.formTypeDsc = '修改文件操作记录';
          this.beInvalid = true;
          this.seen = false;
        } else {
        this.formType = 0;
        this.seenNo = false;
        // this.formData.fileNo = this.Params.fileNo;
        this.formData.fileIdD = this.Params.fileIdD;
        this.formData.fileId = this.Params.fileId;
        this.formTypeDsc = '新增文件操作记录';
        }
      }
        }
}
</script>
<style lang="less">
.tb-row {
    margin-bottom: 2px !important;
}
</style>
