<template>
  <div class="examination">
    <el-dialog @close="closeDialog" :visible.sync="dialogTableVisible">
      <div slot="title" class="titleBox">
        <i :class="tableType === 0? 'el-icon-document':'el-icon-edit-outline'" style="font-size: 20px"></i>
        <span style="margin-left:10px;font-size:18px">{{tableTypeDsc}}</span>
      </div>
      <el-form ref="baseData" label-width="80px" :rules="rules" :model="baseData">
        <el-row>
          <el-col :span="12">
            <el-form-item prop='cretName' label="证书名称">
              <el-input v-model="baseData.cretName" size="mini" style="width:100%;"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop='cretTypeCode' label="证书类型">
              <el-select v-model="baseData.cretTypeCode" filterable collapse-tags size="mini" style="width:100%;">
                  <el-option v-for="(item,index) in cretTypeNameType" :key="index" :label="item.baseName"
                    :value="item.baseCode">
                  </el-option>
                </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="证书文件">
              <el-row :gutter="5">
                <el-col :span="18">
                  <el-input v-model="baseData.cretFilePath.oldFileName" size="small" readonly disabled></el-input>
                </el-col>
                <el-col :span="4">
                  <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-col>
              </el-row>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注">
              <el-input v-model="baseData.memo" size="mini" style="width:100%;"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="goon" size="mini" icon="el-icon-check" :loading='addLoading' @click="addData()">确定</el-button>
        <el-button size="mini" type="goon" 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: ['showFlag', 'Params'],
  data () {
    return {
      dialogTableVisible: false,
      tableTypeDsc: '新增证书头信息',
      rules:{
        cretName: [{ required: true, message: '不能为空', trigger: 'blur' }],
        cretTypeCode: [{ required: true, message: '不能为空', trigger: 'change' }]
      },
      baseData:{
        cretName: '',
        cretTypeCode: '',
        cretFilePath: {oldFileName: '', fileName: ''},
        memo: ''
      },
      addLoading:false,
      tableType: 0,
      cretTypeNameType: [],
      uploadFile: {}
    }
  },
  mounted () {
    //证书类型
    this.axios.post("pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode",{validFlag:1,baseCode:4820}, {individualType: 'json'}).then(res => {
      this.cretTypeNameType = res.data;
    })
  },
  methods: {
    getCode (code, arr, codeKey, nameKey) {
        if (!code) {
          return '';
        }
        let targetRow = arr.find(item => {
          return item[codeKey] === code;
        });
        return targetRow ? targetRow[nameKey] : '';
    },
    cancel () {
      this.baseData = {
        cretName: '',
        cretTypeCode: '',
        cretFilePath: {oldFileName: '', fileName: ''},
        memo: ''
      }
      this.fileUpload = {}
      this.addLoading = false
      this.dialogTableVisible = false
    },
    addData () {
      this.$refs['baseData'].validate((valid) => {
        if (valid) {
          this.addLoading = true
          this.baseData.cretTypeName = this.getCode(this.baseData.cretTypeCode,this.cretTypeNameType,'baseCode','baseName')
          this.axios.post('pass/baseManagement/v1/limscrettmps/insertupdatelimscret', this.baseData, {individualType: 'json'}).then(res => {
            if (res.code == 0 || res.code == 200) {
              JSON.stringify(this.uploadFile)=='{}' ? '' : this.modifyFile(res.data)
              this.dialogTableVisible = false;
              this.$message.success(res.message)
              this.$emit('refresh')
            } else {
              this.$message.error(res.message)
            }
            this.addLoading = false
          }).catch(e => {
            this.addLoading = false
            this.$message.error(e.message)
          })
        }
      })
    },
    modifyFile (cretNo) {
       let obj = {
         list:[
           this.uploadFile.filePk
         ],
         object:{
           objId: cretNo,
           objName: "LIMS_CRET_TMP",
         }
       }
        this.axios.post('pass/baseManagement/v1/limslrcfilesaves/updateListLrcFileSave', obj, {individualType: 'json'}).then(res => {
           if (res.succeed) {
            this.$message.success('上传成功')
           }
        })
     },
     httpRequest: function (e) {
        if (!e.file) {
           return false
        } 
         let file = new FormData ()
         file.append('file', e.file)
          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') { 
                 this.uploadFile = res.data[0]
                 this.baseData.cretFilePath.oldFileName = res.data[0].oldFileName 
                 this.baseData.cretFilePath.fileName = res.data[0].fileName
            } else {
               this.$message.error(res.message)
            }
            }).catch(e => {
               this.$message.error(e.message)
          })
       },
    closeDialog () {
      this.baseData = {
        cretName: '',
        cretTypeCode: '',
        cretFilePath: {oldFileName: '', fileName: ''},
        memo: ''
      }
      this.fileUpload = {}
      this.addLoading = false
    }
  },
  watch: {
    showFlag (arg) {
      if (this.Params !== null) {
        this.baseData = Object.assign({},this.Params[0])
        this.tableType = 1;
        this.tableTypeDsc = '修改证书头信息';
      } else {
        this.tableType = 0;
        this.tableTypeDsc = '新增证书头信息';
      }
      this.dialogTableVisible = true;
    }
  }
}
</script>
<style lang="less">
</style>
