<template>
    <div class="examination">
      <el-dialog @close="closeDialog" :visible.sync="dialogTableVisible" width='70%'>
        <div slot="title" class="titleBox">
        <i :class="tableType === 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">{{tableTypeDsc}}</span>
        </div>
        <el-form ref="baseData" label-width="90px" :rules="rules" :model="baseData">
          <el-row>
            <el-col :span='8'>
              <el-form-item label='默认值'>
                <el-input v-model="baseData.anlyValue" size="small" style="width: 100%;"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span='8'>
              <el-form-item prop='dataTypeCode' label='结果类型'>
                <el-select v-model="baseData.dataTypeCode" @change="selectDataType" filterable collapse-tags size="small" style="width: 100%;">
                  <el-option v-for="(item,index) in dataType" :key="index" :label="item.baseName"
                    :value="item.baseCode">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span='8'>
              <el-form-item label='上传图片'>
                <el-row :gutter='5'>
                  <el-col :span='17'>
                    <el-input :disabled='true' v-model="baseData.savePath1.oldFileName" size="small" style="width: 100%;"></el-input>
                  </el-col>
                  <el-col :span='6'>
                    <el-upload
                      class="upload"
                      style="display:inline-block"
                      action=""
                      :multiple="false"
                      :show-file-list="false"
                      accept=""
                      :http-request="httpRequest">
                      <el-button :disabled='baseData.dataTypeCode != 482503' type="goon" size="mini" icon="el-icon-upload2"></el-button>
                    </el-upload>
                  </el-col>
                </el-row>
              </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='addLoding' @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,
  formatDate
} from '@/utils/util.js';
import {
  cookieUserId,
  cookieUserName
} from '@/config/config.js';
let userName = getCookie(cookieUserName);
let userId = getCookie(cookieUserId);
import { momentImmutableMethods } from 'moment-immutable-methods'
import moment from 'moment'
momentImmutableMethods(moment)
export default {
  props: ['showFlag2', 'Params2'],
    data () {
      return {
        dialogTableVisible: false,
        tableTypeDsc: '新增',
        tableType: 0,
        baseData:{
          savePath1:{},
        },
        anlyType:[],
        sectionClass3:[],
        sectionClass2:[],
        unitNameType:[],
        dataType:[],
        suppNameType:[],
        addLoding:false,
        rules: {
          dataTypeCode:[{ required: true, message: '不能为空', trigger: 'change' },]
        },
        uploadFile:{}
      }
    },
    mounted () {
      this.axios.post("pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode",{validFlag:1,baseCode:4825}, {individualType: 'json'}).then(res => {
        this.dataType = res.data;
      })
    },
    methods: {
      getCode (code, arr, codeKey, nameKey) {
        if (!code) {
          return '';
        }
        let targetRow = arr.find(item => {
          return item[codeKey] === code;
        });
        // console.log(targetRow, 'targetRow');
        return targetRow ? targetRow[nameKey] : '';
      },
      selectDataType(){
        this.baseData.savePath1 = {}
      },
      cancel () {
        this.baseData = {
          savePath1:{},
          companyNo:'',
          smpCompanyNo:'',
          deptNo:'',
          smpDeptNo:'',
          sectionNo:'',
          smpSectionNo:''
        }
        this.addLoding = false
        this.dialogTableVisible = false;
      },
      addData () {
        this.$refs['baseData'].validate((valid) => {
          if (valid) {
            this.addLoding = true
            this.baseData.dataTypeName = this.getCode(this.baseData.dataTypeCode,this.dataType,'baseCode','baseName')
            this.axios.post('pass/testManagement/v1/limstestdatads/insertupdate', [this.baseData], {individualType: 'json'}).then(res => {
              if (res.code == 0 || res.code == 200) {
                if (this.baseData.dataTypeCode == 482503) {
                  let obj = {
                    list:[this.uploadFile.filePk],
                    object:{
                      objId: this.baseData.id ? this.baseData.id : res.data,
                      objName: "LIMS_TEST_DATA",
                    }
                  } 
                  this.saveFile(obj)
                }else{
                  this.$emit('refresh2')
                  this.addLoding = false
                  this.dialogTableVisible = false;
                  this.$message.success(res.message)
                }
              } else {
                this.addLoding = false
                this.$message.error(res.message)
              }
            }).catch(e => {
              this.addLoding = false
              this.$message.error(e.message)
            })
          }
       })
      },
      saveFile(obj){
        this.axios.post('pass/baseManagement/v1/limslrcfilesaves/updateListLrcFileSave', obj, {individualType: 'json'}).then(res => {
          if (res.code == 0 || res.code == 200) {
            this.$emit('refresh2')
            this.dialogTableVisible = false;
            this.$message.success(res.message)
          }else{
            this.$message.error(res.message)
          }
          this.addLoding = false
        }).catch(e =>{
          this.addLoding = false
          this.$message.error(e.message)
        })
      },
      closeDialog () {
        this.baseData = {
          savePath1:{},
          companyNo:'',
          smpCompanyNo:'',
          deptNo:'',
          smpDeptNo:'',
          sectionNo:'',
          smpSectionNo:''
        }
        this.addLoding = false
      },
      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.savePath1 = {}
            this.baseData.savePath1.oldFileName = this.uploadFile.oldFileName
            this.baseData.savePath1.fileName = this.uploadFile.fileName
          } else {
            this.$message.error(res.message)
          }
        }).catch(e => {
           this.$message.error(e.message)
        })
      },
    },
    watch: {
      showFlag2 (arg) {
        let _this = this;
        if (_this.Params2 !== null) {
          this.baseData = Object.assign({clearFlag:true},this.Params2)
          if (  this.baseData.savePath1 == null) {
            this.baseData.savePath1 = {}
          }
          _this.tableType = 1;
          _this.tableTypeDsc = '修改';
        } else {
        }
        this.dialogTableVisible = true;
      }
    }
}
</script>
<style lang="less">
</style>
