<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="merchName" label="客商名称">
              <el-input v-model="baseData.merchName" readonly disabled size="small"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item prop="merchTypeName" label="客商类型">
              <el-input v-model="baseData.merchTypeName" readonly disabled size="small"></el-input>
            </el-form-item>
          </el-col>
          </el-row>
          <el-row>
          <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="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="evaLevelName" label="评价等级">
             <el-select v-model="evaLevelNameTypeIndex" filterable collapse-tags size="small">
                <el-option v-for="(item,index) in evaLevelNameType" :key="index" :label="item.baseName" :value="index">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item prop="memo" label="备注">
              <el-input v-model="baseData.memo" size="small"></el-input>
            </el-form-item>
          </el-col>
         </el-row>
         <el-row>
           <el-col :span="20">
            <el-form-item label="文件上传" prop="filePath">
              <el-input
                v-model="baseData.filePath"
                size="small"
                readonly
                disabled
                style="width: 262px; display: inline-block"
              ></el-input>
              <el-upload
                class="upload"
                action=""
                v-if="baseData.filePath - 0 === 0"
                style="width: 40px; display: inline-block"
                :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-button
                v-else
                type="primary"
                size="mini"
                class="el-icon-circle-close"
                @click="dropFile"
              ></el-button>
            </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="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,
            matClassNameTypeIndex: 0,
            matNameTypeIndex: 0,
            evaLevelNameTypeIndex: 0,
            matClassNameType: [],
            matNameType: [],
            evaLevelNameType: [],
            tableData: [],
            baseData: {
              merchName: '',
              merchTypeName: '',
              memo: '',
              filePath: ''
            },
            ruleValidate: {
              //matName: [{ required: true, message: '该项不能为空', trigger: 'change' }],
              //merchName: [{ required: true, message: '该项不能为空', trigger: 'change' }],
              // matName: [{ required: true, message: '该项不能为空', trigger: 'change' }]
             }
        }
    },
    mounted () {
      this.getMatClassNameList (),
      this.getEvaLevelName ()
    },
    methods: {
      cancel () {
        let _this = this;
        _this.tableData = [];
        _this.matClassNameTypeIndex = 0,
        _this.matNameTypeIndex = 0,
        _this.evaLevelNameTypeIndex = 0,
        _this.baseData = {
              merchName: '',
              merchTypeName: '',
              memo: ''
        }
        _this.dialogTableVisible = false
        _this.$emit('refresh')
      },
       open(mag, typ, time) {
        this.$message({
          showClose: true,
          message: mag,
          type: typ,
          duration: time
        });
      },
      selectTrigger (val) {
        let _this = this;
         _this.matNameType = []
        // _this.monitorForm.tableData[row].companyNo = _this.companyNameType[val].companyNo
         _this.axios.get('pass/baseManagement/v1/limsbasematerialss/' + '?matClassName=' + _this.matClassNameType[val].matClassName + '&validFlag=1')
            .then(res => {
                for (let i = 0; i < res.data.list.length; i++) {
                  _this.$set(_this.matNameType,i,res.data.list[i])
                } 
            })
      },
      // 文件上传
      async httpRequest (e) {
          let _this = this;
          if (!e.file) {
            return false
          }
          let name = e.file.name;
          let aaa = name.substr(name.indexOf(".")+1);
          if(aaa != "pdf"){
            this.$message.error("请上传pdf文件！")
            return
          }
          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.$message.success('上传成功')
              this.baseData.filePath = res.data[0].fileName
            } else {
              this.$message.error(res.message)
            }
          }).catch(e => {
            return this.$message.error(e.message)
        })
      },
      // 删除已有文件
      async dropFile () {
        let result = await this.$confirm('确认删除该文件重新上传吗？').catch(() => 'cancel')
        if (result == 'cancel') return
        var data
        try {
          if(this.baseData.filePath){
            const data = await this.axios.get('pass/baseManagement/v1/file/delFile/'+ this.baseData.filePath, { individualType: 'json' })
            if (data === '1') {
              this.baseData.filePath = ""
              this.$message.success('文件删除成功')
            } else {
              this.$message.error(data.message)
            }
          }else{
            this.baseData.filePath = ""
            this.$message.success('文件删除成功')
          }
        } catch (e) {
        }
      },
      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.matNo = _this.matNameType[_this.matNameTypeIndex].matNo;
             _this.baseData.matName = _this.matNameType[_this.matNameTypeIndex].matName;
             _this.baseData.matClassNo = _this.matClassNameType[_this.matClassNameTypeIndex].matClassNo;
             _this.baseData.matClassName = _this.matClassNameType[_this.matClassNameTypeIndex].matClassName;
             _this.baseData.evaLevelCode = _this.evaLevelNameType[_this.evaLevelNameTypeIndex].baseCode;
             _this.baseData.evaLevelName = _this.evaLevelNameType[_this.evaLevelNameTypeIndex].baseName;
             _this.tableData.push(_this.baseData)
             _this.axios.post('pass/baseManagement/v1/limsmerchantsmats/', _this.tableData, {individualType: 'json'}).then(res => {
                if (res.succeed) {
                 this.open('添加成功', 'success', 1200)
                 _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.matNo = _this.matNameType[_this.matNameTypeIndex].matNo;
        _this.baseData.matName = _this.matNameType[_this.matNameTypeIndex].matName;
        _this.baseData.matClassNo = _this.matClassNameType[_this.matClassNameTypeIndex].matClassNo;
        _this.baseData.matClassName = _this.matClassNameType[_this.matClassNameTypeIndex].matClassName;
        _this.baseData.evaLevelCode = _this.evaLevelNameType[_this.evaLevelNameTypeIndex].baseCode;
        _this.baseData.evaLevelName = _this.evaLevelNameType[_this.evaLevelNameTypeIndex].baseName;
        _this.tableData.push(_this.baseData)
        _this.axios.put('pass/baseManagement/v1/limsmerchantsmats/', _this.baseData, {individualType: 'json'}).then(res => {
           if (res.succeed) {
            this.open('修改成功', 'success', 1200)
            _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 = {
              merchName: '',
              merchTypeName: '',
              memo: ''
        }
       _this.seen = true;
       _this.seenNo = true;
       _this.beInvalid = false;
       _this.$emit('refresh')
     },
     getMatClassNameList () {
         this.axios.get('pass/baseManagement/v1/limsbasematerialsclasss/'+ '?validFlag=1')
            .then(res => {
              this.matClassNameType = res.data.list;
              this.getMatNameList (res.data.list[0].matClassName)
            }).catch(e => {
              // this.dataLoading = false;
            });
      },
      getMatNameList (matClassName) {
         this.axios.get('pass/baseManagement/v1/limsbasematerialss/'+'?matClassName=' + matClassName +'&validFlag=1')
            .then(res => {
              console.log(res.data.list)
              this.matNameType = res.data.list;
            })
      },
      getEvaLevelName () {
         this.axios.post("pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode",{validFlag:1,baseCode:4837}, {individualType: 'json'}).then(res => {
              this.evaLevelNameType = 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.matNameType.length; j++) {
          if (_this.matNameType[j].matName === _this.baseData.matName) {
             _this.matNameTypeIndex = j;
          }
          }
          for (let j = 0; j < _this.matClassNameType.length; j++) {
          if (_this.matClassNameType[j].matClassName === _this.baseData.matClassName) {
             _this.matClassNameTypeIndex = j;
          }
          }
          for (let j = 0; j < _this.evaLevelNameType.length; j++) {
          if (_this.evaLevelNameType[j].baseName === _this.baseData.evaLevelName) {
             _this.evaLevelNameTypeIndex = j;
          }
          }
        } else {
          _this.tableType = 0;
          _this.seenNo = false;
          _this.baseData.merchName = _this.Params.merchName;
          _this.baseData.merchTypeName = _this.Params.merchTypeName;
          _this.baseData.merchNo = _this.Params.merchNo;
          _this.tableTypeDsc = '新增客商产品信誉信息';
        }
      }
    }
}
</script>
<style lang="less">
.customWidth_syst{
  width: 600px !important
}
</style>
