<template>
    <div class="examination">
     <el-dialog @close="closeDialog" :visible.sync="dialogTableVisible" customClass="customWidth_syst">
        <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="80px" :rules="ruleValidate" :model="baseData">
         <el-row>
          <el-col :span="10">
            <el-form-item prop="objTypeName" label="异常类型">
              <el-input v-model="baseData.objTypeName" readonly disabled size="small"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item prop="matName" label="物料名称">
              <el-input v-model="baseData.matName" readonly disabled size="small"></el-input>
            </el-form-item>
          </el-col>
          </el-row>
          <el-row>
            <el-col :span="10">
            <el-form-item prop="bugTypeName" label="缺陷类型">
             <el-select v-model="bugTypeNameTypeIndex" filterable collapse-tags size="small">
                <el-option v-for="(item,index) in bugTypeNameType" :key="index" :label="item.baseName" :value="index">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="10">
            <el-form-item prop="matClassName" label="缺陷类型">
              <el-select v-model="matClassNameTypeIndex" filterable collapse-tags size="small" @change="selectTrigger(matClassNameTypeIndex)">
                <el-option v-for="(item,index) in matClassNameType" :key="index" :label="item.matClassName" :value="index">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col> -->
           <el-col :span="10">
            <el-form-item prop="objectNum" label="异常数量">
              <el-input v-model="baseData.objectNum" size="small"></el-input>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="10">
            <el-form-item prop="matName" label="异常单位">
             <el-select v-model="matNameTypeIndex" filterable collapse-tags size="small">
                <el-option v-for="(item,index) in matNameType" :key="index" :label="item.matName" :value="index">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col> -->
         </el-row>
          <el-row>
             <el-col :span="10">
            <el-form-item prop="unitName" label="异常单位">
              <el-input v-model="baseData.unitName" size="small"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item prop="objctDesc" label="异常说明">
              <el-input v-model="baseData.objctDesc" size="small"></el-input>
            </el-form-item>
          </el-col>
         </el-row>
         <el-row>
           <el-col :span="14">
              <el-form-item prop="annexName1" label="附件1" >
               <el-input  v-model="baseData.annexName1" size="small" style="width: 240px" readonly disabled></el-input>
               </el-form-item>
           </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">浏览</el-button>
                  </el-upload>
            </el-col>
         </el-row>
         <el-row>
           <el-col :span="14">
              <el-form-item prop="annexName2" label="附件2" >
               <el-input  v-model="baseData.annexName2" size="small" style="width: 240px" readonly disabled></el-input>
               </el-form-item>
           </el-col>
           <el-col :span="4">
                  <el-upload
                    class="upload"
                    action=""
                    :multiple="false"
                    :show-file-list="false"
                    accept=""
                    :http-request="httpRequest2">
                    <el-button type="goon" size="mini">浏览</el-button>
                  </el-upload>
            </el-col>
         </el-row>
       </el-form>
       <span slot="footer" class="dialog-footer">
        <el-button type="goon" size="mini" icon="el-icon-check" @click="tableType === 0 ? addData():modifyData()">确定</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: ['showFlag2', 'Params', 'Params2'],
    data () {
        return {
            dialogTableVisible: false,
            tableTypeDsc: '新增异常申报明细信息',
            tableType: 0,
            seen: true,
            seenNo: true,
            beInvalid: false,
            bugTypeNameTypeIndex: 0,
            bugTypeNameType: [],
            tableData: [],
            baseData: {
              objTypeName: '',
              matName: '',
              memo: '',
              objectNum: '',
              unitName: '',
              objctDesc: '',
              annexName1: '',
              annexName2: '',
            },
            ruleValidate: {
              //matName: [{ required: true, message: '该项不能为空', trigger: 'change' }],
              //objTypeName: [{ required: true, message: '该项不能为空', trigger: 'change' }],
              // matName: [{ required: true, message: '该项不能为空', trigger: 'change' }]
             }
        }
    },
    mounted () {
      // this.getMatClassNameList (),
      this.getbugTypeName ()
    },
    methods: {
      cancel () {
        let _this = this;
        _this.tableData = [];
        _this.bugTypeNameTypeIndex = 0,
        _this.baseData = {
              objTypeName: '',
              matName: '',
              memo: '',
              objectNum: '',
              unitName: '',
              objctDesc: '',
              annexName1: '',
              annexName2: '',
        }
        _this.dialogTableVisible = false
        _this.$emit('refresh')
      },
      httpRequest: function (e) {
        let _this = this;
        let file = e.file;
        if (!file) {
          // 没有文件
           return false
        } 
//        else if (!/\.(wav|cda|mp3|mp3pro|wma)$/.test(file.name.toLowerCase())) {
//           // 格式根据自己需求定义
//           this.$message.error('上传文件格式不正确，请上传音频文件！')
//           return false
//         }
//         let arr = file.name.split('.');
//         _this.file = file;
//         _this.baseData.voiceFileName = arr[0];
//         _this.baseData.voiceFileType = arr[1];
        //  _this.monitorForm.tableData[_this.curRowIndex].annexName1 = file.name
        //_this.baseData.annexName1 = file.name;
         _this.$set(_this.baseData,'annexName1',file.name)
         let uploadFile = new FormData()
         uploadFile.append('file', file)
         let config = {
           headers: {
             'Content-Type': 'multipart/form-data'
           }
         }
         // _this.axios.post('pass/baseManagement/v1/download/testSingleUpload', uploadFile, config).then(res => {
         // 文件目录
         let category = 'WJ/';
         _this.axios.post('pass/seaweedfs/v1/download/' + category, uploadFile, config).then(res => {
            if (res && res.name) {
              _this.baseData.annexPath1 = category + res.name
            } else {
              _this.$message.error('文件上传失败')
            }
         }).catch(e => {
            _this.$message.error(e.message)
         })
      },
      httpRequest2: function (e) {
        let _this = this;
        let file = e.file;
        if (!file) {
          // 没有文件
           return false
        } 
//        else if (!/\.(wav|cda|mp3|mp3pro|wma)$/.test(file.name.toLowerCase())) {
//           // 格式根据自己需求定义
//           this.$message.error('上传文件格式不正确，请上传音频文件！')
//           return false
//         }
//         let arr = file.name.split('.');
//         _this.file = file;
//         _this.baseData.voiceFileName = arr[0];
//         _this.baseData.voiceFileType = arr[1];
        _this.$set(_this.baseData,'annexName2',file.name)
        let uploadFile = new FormData()
        uploadFile.append('file', file)
        let config = {
          headers: {
            'Content-Type': 'multipart/form-data'
          }
        }
        // _this.axios.post('pass/baseManagement/v1/download/testSingleUpload', uploadFile, config).then(res => {
        // 文件目录
        let category = 'WJ/';
        _this.axios.post('pass/seaweedfs/v1/download/' + category, uploadFile, config).then(res => {
           if (res.succeed) {
             _this.baseData.annexPath2 = category + res.name
           } else {
             _this.$message.error('文件上传失败')
           }
        }).catch(e => {
            _this.$message.error(e.message)
        })
      },
      addData () {
        let _this = this;
         _this.tableData = [];
         _this.$refs['baseData'].validate((valid) => {
          if (valid) {
             _this.dialogTableVisible = false;
             _this.baseData.createMan = userName;
             _this.baseData.createNo = userId;
             _this.baseData.bugTypeCode = _this.bugTypeNameType[_this.bugTypeNameTypeIndex].baseCode;
             _this.baseData.bugTypeName = _this.bugTypeNameType[_this.bugTypeNameTypeIndex].baseName;
             _this.tableData.push(_this.baseData)
             _this.axios.post('pass/baseManagement/v1/limsobjlines/', _this.tableData, {individualType: 'json'}).then(res => {
                if (res.succeed) {
                 this.$message.success('添加成功')
                 _this.$emit('refresh')
                } else {
                 this.$message.error(res.message)
                }
                }).catch(e => {
                 this.$message.error(e.message)
               })
         }
       })
      },
      modifyData () {
        let _this = this;
         _this.tableData = [];
        _this.$refs['baseData'].validate((valid) => {
        if (valid) {
        _this.dialogTableVisible = false;
        _this.baseData.updateMan = userName;
        _this.baseData.updateNo = userId;
        _this.baseData.bugTypeCode = _this.bugTypeNameType[_this.bugTypeNameTypeIndex].baseCode;
        _this.baseData.bugTypeName = _this.bugTypeNameType[_this.bugTypeNameTypeIndex].baseName;
        _this.tableData.push(_this.baseData)
        _this.axios.put('pass/baseManagement/v1/limsobjlines/', _this.baseData, {individualType: 'json'}).then(res => {
           if (res.succeed) {
            this.$message.success('修改成功')
            _this.$emit('refresh')
           } else {
            this.$message.error(res.message)
           }
           }).catch(e => {
            this.$message.error(e.message)
          })
        }
        })
      },
      renderHeader (h) {
         return (
             <div style="cursor: pointer ;line-height:20px" onClick={() => this.addRow()}>
             <i class="el-icon-plus" style="font-size:20px; color:rgb(18, 52, 243)"></i>
             </div>
         )
      },
     closeDialog () {
       let _this = this;
       _this.tableData = [];
       _this.baseData = {
              objTypeName: '',
              matName: '',
              memo: ''
        }
       _this.seen = true;
       _this.seenNo = true;
       _this.beInvalid = false;
       _this.$emit('refresh')
     },
     getbugTypeName () {
        this.axios.post("pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode",{validFlag:1,baseCode:4827}, {individualType: 'json'}).then(res => {
              this.bugTypeNameType = res.data
            }).catch(e => {
              // this.dataLoading = false;
            });
      },
      tableHeaderCellStyle () {
      return 'background-color: rgb(249,249,249) !important;color: rgb(73, 72, 72);border-bottom: 1px solid #DCDFE7;border-top:3px solid #3ac1ea;'
      }
    },
    watch: {
      showFlag2 (arg) {
        let _this = this;
        _this.dialogTableVisible = true;
        if (_this.Params2 !== null) {
          _this.baseData = _this.Params2;
          _this.tableType = 1;
          _this.tableTypeDsc = '修改异常申报明细信息';
          _this.seen = false;
          _this.beInvalid = true;
          for (let j = 0; j < _this.bugTypeNameType.length; j++) {
          if (_this.bugTypeNameType[j].baseName === _this.baseData.bugTypeName) {
             _this.bugTypeNameTypeIndex = j;
          }
          }
        } else {
          _this.tableType = 0;
          _this.seenNo = false;
          _this.baseData.objTypeName = _this.Params.objTypeName;
          _this.baseData.matName = _this.Params.matName;
          _this.baseData.matNo = _this.Params.matNo;
          _this.baseData.objTypeCode = _this.Params.objTypeCode;
          _this.baseData.objId = _this.Params.objId;
          _this.tableTypeDsc = '新增异常申报明细信息';
        }
      }
    }
}
</script>
<style lang="less">
.customWidth_syst{
  width: 600px !important
}
</style>