<template>
  <div class="examination">
    <el-dialog @close="closeDialog" :visible.sync="dialogTableVisible">
      <div slot="title" class="titleBox" style="display: flex;">
        <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="100px" :rules="rules" :model="baseData">
        <el-row>
          <el-col :span="12">
            <el-form-item label="证书名称">
              <el-input v-model="baseData.cretName" size="small" disabled style="width:100%;" ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop='cretItemName' label="证书项目">
              <el-input v-model="baseData.cretItemName" size="small" style="width:100%;"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item prop='cretItemtCode' label="项目类型">
              <el-select v-model="baseData.cretItemtCode" filterable collapse-tags size="small" style="width: 100%;">
                <el-option v-for="(item,index) in cretItemtCodeType" :key="index" :label="item.baseName"
                  :value="item.baseCode">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop='dataTypeCode' label="数据类型">
              <el-select v-model="baseData.dataTypeCode" filterable collapse-tags size="small" style="width: 100%;">
                <el-option v-for="(item,index) in dataTypeNameType" :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 prop='strRowD' label="项目起始行">
              <el-input v-model="baseData.strRowD" size="small" style="width:100%;" ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop='strColD' label="项目起始列">
              <el-input v-model="baseData.strColD" size="small" style="width:100%;"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="标准起始行">
              <el-input v-model="baseData.strRowStd" size="small" style="width:100%;" ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="标准起始列">
              <el-input v-model="baseData.strColStd" size="small" style="width:100%;"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="行步长">
              <el-input v-model="baseData.rowStep" size="small" style="width:100%;" ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="列步长">
              <el-input v-model="baseData.colStep" size="small" style="width:100%;"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="默认值">
              <el-input v-model="baseData.defaultValue" size="small" style="width:100%;" ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="证书图片">
              <el-row :gutter='5'>
                <el-col :span="18">
                  <el-input v-model="baseData.cretImg.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-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="表列名">
              <el-select v-model="baseData.colName" clearable filterable collapse-tags size="small" style="width: 100%;">
                <el-option v-for="(item,index) in colNameType" :key="index" :label="item.comments"
                  :value="item.columnName">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop='manySmp' label="多样类型">
              <el-select v-model="baseData.manySmp" filterable collapse-tags size="small" style="width: 100%;">
                <el-option v-for="(item,index) in diverseType" :key="index" :label="item.label"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input v-model="baseData.memo" size="small" style="width:100%;" ></el-input>
            </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" @click="itemConfiguration">子项配置</el-button> -->
        <el-button type="goon" size="mini" icon="el-icon-check" :loading='buttonLoading' @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';
import {certificateSubitemConfiguration} from '../certificateSubitemConfiguration/certificateSubitemConfiguration'
let userName = getCookie(cookieUserName);
let userId = getCookie(cookieUserId);
export default {
  props: ['showFlag2', 'Params', 'Params2'],
    data () {
      return {
        dialogTableVisible: false,
        tableType: 0,
        colNameType:[],
        tableTypeDsc: '新增证书项目配置信息',
        dataTypeNameType:[],
        cretItemtCodeType:[],
        buttonLoading:false,
        diverseType:[
          {id:'00',label:'00'},
          {id:'10',label:'10'},
          {id:'01',label:'01'},
        ],
        baseData:{
          isSon:'0',
          isMerge:'0',
          cretImg: {oldFileName: '', fileName: ''},
        },
        rules:{
          strRowD: [{ required: true, message: '不能为空', trigger: 'blur' }],
          strColD: [{ required: true, message: '不能为空', trigger: 'blur' }],
          cretItemName: [{ required: true, message: '不能为空', trigger: 'blur' }],
          dataTypeCode: [{ required: true, message: '不能为空', trigger: 'change' }],
          cretItemtCode: [{ required: true, message: '不能为空', trigger: 'change' }],
          manySmp: [{ required: true, message: '不能为空', trigger: 'change' }]
        },
        isMergeType:[
          {id:'0',name:'否'},
          {id:'1',name:'是'}
        ],
        uploadFile: {}
      }
    },
    mounted () {
      this.axios.post("pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode",{validFlag:1,baseCode:4825}, {individualType: 'json'}).then(res => {
        this.dataTypeNameType = res.data;
      })
      this.axios.post("pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode",{validFlag:1,baseCode:4822}, {individualType: 'json'}).then(res => {
        this.cretItemtCodeType = res.data;
      })
      this.axios.post("pass/baseManagement/v1/limscretprods/queryTabelColumn",{tableName:'LIMS_CRET_PROD'}, {individualType: 'json'}).then(res => {
        this.colNameType = 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={
          isSon:'0',
          isMerge:'0',
          cretImg: {oldFileName: '', fileName: ''},
        },
        this.buttonLoading = false
        this.dialogTableVisible = false
      },
      addData () {
        this.$refs['baseData'].validate((valid) => {
          if (valid) {
            this.buttonLoading = true
            this.baseData.dataTypeName = this.getCode(this.baseData.dataTypeCode,this.dataTypeNameType,'baseCode','baseName')
            this.baseData.cretItemtName = this.getCode(this.baseData.cretItemtCode,this.cretItemtCodeType,'baseCode','baseName')
            this.baseData.colNo = this.getCode(this.baseData.colName,this.colNameType,'columnName','comments')
            this.axios.post('pass/baseManagement/v1/limscrettmpms/insert', 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.buttonLoading = false
            }).catch(e => {
              this.buttonLoading = false
              this.$message.error(e.message)
            })
          }
        })
      },
      itemConfiguration () {
        let _this = this;
        _this.$router.push({name: 'certificateSubitemConfiguration'})
      },
      modifyFile (cretNoM) {
       let obj = {
         list:[
           this.uploadFile.filePk
         ],
         object:{
           objId: cretNoM,
           objName: "LIMS_CRET_TMP_M",
         }
       }
        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.cretImg.oldFileName = res.data[0].oldFileName
                 this.baseData.cretImg.fileName = res.data[0].fileName
            } else {
               this.$message.error(res.message)
            }
            }).catch(e => {
               this.$message.error(e.message)
          })
       },
      closeDialog () {
         this.baseData={
          isSon:'0',
          isMerge:'0',
          cretImg: {oldFileName: '', fileName: ''},
        }
        this.buttonLoading = false
      }
    },
    watch: {
      showFlag2 (arg) {
        if (this.Params2 !== null) {
          this.tableType = 1;
          this.tableTypeDsc = '修改证书项目配置信息';
          this.baseData = Object.assign({},this.Params2)
        } else {
          this.tableType = 0;
          this.baseData.cretNo = this.Params.cretNo;
          this.baseData.cretName = this.Params.cretName;
          this.baseData.merchNo = this.Params.merchNo;
          this.tableTypeDsc = '新增证书项目配置信息';
        }
        this.dialogTableVisible = true;
      }
    }
}
</script>
<style lang="less">
</style>

