<template>
  <div class="examination">
    <div class="common-head-search">
      <el-form :inline='true'>
        <el-form-item label='客商类型'>
          <el-select v-model="search.merchTypeName" clearable filterable collapse-tags size="small" style="width:200px;margin-right: 10px;" placeholder="请选择查询内容">
            <el-option v-for="item in merchType" :key="item.baseName" :label="item.baseName"
              :value="item.baseName">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label='客商名称'>
          <el-input v-model="search.merchName" size="small" placeholder="请输入查询内容" style="width: 200px" @keyup.enter.native="searchData"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="goon" size="mini" @click="searchData">查询</el-button>
          <el-button icon="el-icon-refresh" type="info" plain size="mini" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="common-title-div">
      <div class="common-title-name">
        <img style="width: 25px; height: 25px" src='../../../../assets/img/imgScreen/logo.png' />
        客商信息表
      </div>
      <div>
      <el-button icon="el-icon-circle-plus-outline" type="goon" size="mini" @click="addData">新增</el-button>
      <el-button icon="el-icon-edit" type="goon" size="mini" @click="modifyData">修改</el-button>
      <el-button icon="el-icon-remove-outline" type="danger" size="mini" @click="deleteData">停用</el-button>
      <el-button icon="el-icon-check" type="goon" size="mini" @click="recoverData">恢复</el-button>
      <el-button icon="el-icon-download" type="goon" size="mini" :loading='createFileLoading' @click="createFile">生成客商文件</el-button>
      </div>
    </div>
    <div class="common-table-div" style="margin-bottom:0;margin-top:0">
       <el-table v-loading="dataLoading" ref="dataTable" border :height="height_top" highlight-current-row @selection-change="handleSelectionChange" @current-change="handleCurrentChange" :data="table.list" :icore-filter-flag="icoreFilterFlag"
       :header-cell-style="tableHeaderCellStyle" :cell-class-name="tableRowClassName" :summary-method="getSummaries" show-summary>
        <el-table-column type="index" label="NO" fixed="left" align="center">
        </el-table-column>
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="180px" prop="merchName" label="客商名称" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="140px" prop="merchChnAbbr" label="客商简称" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="80px" prop="merchTypeName" label="客商类型" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="80px" prop="merchAtrrName" label="客商属性" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="80px" prop="merchLinkman" label="联系人"  align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="120px" prop="merchTel" label="联系电话"  align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="80px"  prop="validFlag1" label="数据状态" align="center"></el-table-column>
        <!-- <el-table-column sortable width="" prop="memo" label="备注" align="center"></el-table-column> -->
        <el-table-column sortable :show-overflow-tooltip="true" min-width="80px" prop="createMan" label="创建人" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="160px" prop="createTime" label="创建时间" align="center"></el-table-column>
      </el-table>
    </div>
    <div class="common-foot-style">
        <el-pagination v-loading="dataLoading" @size-change="pageSizeChange" @current-change="pageCurrentChange"
          :current-page="page.pageNum" :page-sizes="[10, 20, 50, 100, 200]" :page-size="page.pageSize" layout="total, sizes, prev, pager, next, jumper"
          :total="page.totalPage" background>
        </el-pagination>
    </div>
    <div class="common-title-div">
      <div class="common-title-name">
        <img style="width: 25px; height: 25px" src='../../../../assets/img/imgScreen/logo.png' />
        客商产品信誉表
      </div>
      <div>
      <el-button icon="el-icon-circle-plus-outline" type="goon" size="mini" @click="addData2">新增</el-button>
      <el-button icon="el-icon-edit" type="goon" size="mini" @click="modifyData2">修改</el-button>
      <el-button icon="el-icon-remove-outline" type="danger" size="mini" @click="deleteData2">停用</el-button>
      <el-button icon="el-icon-check" type="goon" size="mini" @click="recoverData2">恢复</el-button>
      </div>
    </div>
    <div class="common-table-div" style="margin-bottom:0;margin-top:0">
       <el-table v-loading="dataLoading2" ref="dataTable2" border :height="height_bot" highlight-current-row @selection-change="handleSelectionChange2" :data="table.list2" :icore-filter-flag="icoreFilterFlag"
       :header-cell-style="tableHeaderCellStyle" :cell-class-name="tableRowClassName2" :summary-method="getSummaries2" show-summary>
        <el-table-column type="index" label="NO" fixed="left" align="center">
        </el-table-column>
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="180px" prop="merchName" label="客商名称" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="80px" prop="merchTypeName" label="客商类型" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="160px" prop="matClassName" label="物料类型" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="matName" label="物料名称" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="80px" prop="evaLevelName" label="客户等级" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="80px"  prop="validFlag1" label="数据状态" align="center">
          <!-- <template slot-scope="scope">
              <span v-if="scope.row.validFlag == 1">已恢复</span>
              <span v-else-if="scope.row.validFlag == 0">已停用</span>
          </template> -->
        </el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="80px" prop="memo" label="备注" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="160px" prop="filePath" label="附件" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="160px" prop="evaTime" label="评价时间" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="160px" prop="evaMan" label="评价人" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="160px" prop="createTime" label="创建时间" align="center"></el-table-column>       
        <!-- <el-table-column width="95px" prop="createMan" label="创建人" align="center"></el-table-column>
        <el-table-column width="160px" prop="createTime" label="创建时间" align="center"></el-table-column>
        <el-table-column width="95px" prop="updateMan" label="修改人" align="center"></el-table-column>
        <el-table-column width="160px" prop="updateTime" label="修改时间" align="center"></el-table-column> -->
      </el-table>
    </div>
    <!-- <div class="common-foot-style">
        <el-pagination v-loading="dataLoading2" @size-change="pageSizeChange2" @current-change="pageCurrentChange2"
          :current-page="page2.pageNum" :page-sizes="[10, 20, 50, 100, 200]" :page-size="page2.pageSize" layout="total, sizes, prev, pager, next, jumper"
          :total="page2.totalPage" background>
        </el-pagination>
    </div> -->
    <div is="alertComponets" :showFlag="showFlag" :Params="Params" @refresh='refresh'></div>
    <div is="alertComponets2" :showFlag2="showFlag2" :Params="Params" :Params2="Params2" @refresh='refresh2'></div>
  </div>
</template>
<script>
import {
  getCookie
} from '@/utils/util.js';
import {
  cookieUserId,
  cookieUserName
} from '@/config/config.js';
import alertComponets from './alertComponents';
import alertComponets2 from './alertComponents2';
let userName = getCookie(cookieUserName);
let userId = getCookie(cookieUserId);
export default {
  components: {alertComponets, alertComponets2},
  data () {
    return {
      table: {
        list: [],
        list2: []
      },
      dataLoading: false,
      dataLoading2: false,
      createFileLoading: false,
      icoreFilterFlag: true,
      page: {
        pageSize: 50,
        pageNum: 1,
        totalPage: 0
      },
      page2: {
        pageSize: 999,
        pageNum: 1,
        totalPage: 0
      },
      showFlag: false,
      Params: {},
      merchNo: '',
      showFlag2: false,
      Params2: {},
      merchType: [
        //   {ruleTypeCode: '483801', merchTypeName: '组批'},
        //   {ruleTypeCode: '483802', merchTypeName: '抽签'},
        //   {ruleTypeCode: '483803', merchTypeName: '批号'}
      ],
      multipleSelection3: [],
      // multipleSelection3: {},
      // multipleSelection4: {},
      multipleSelection4: [],
      search: {
        merchName: '',
        merchTypeName: ''
      },
      height_top:0,
      height_bot:0
    }
  },
  created() {
    this.height_top = window.innerHeight / 2 - 130 < 249 ? 249 : window.innerHeight / 2 - 100;
    this.height_bot =  window.innerHeight / 2 - 130 < 249 ? 249 : window.innerHeight / 2 - 100;
  },
 mounted () {
    this.getDataList();
    this.getDataList2(this.merchNo);
    this.addScreen();
    this.addScreen2();
    this.getMerchTypeList ()
  },
  methods: {
    getSummaries (param) {
      const { columns, data } = param;
      const sums = [];
       columns.forEach((column, index) => {
          if (index === 0) {
            sums[index] = '合计';
            return;
          }
          if (index === 2) {
            sums[index] = this.page.totalPage;
          }
       });
        return sums;
    },
    getSummaries2 (param) {
      const { columns, data } = param;
      const sums = [];
       columns.forEach((column, index) => {
          if (index === 0) {
            sums[index] = '合计';
            return;
          }
          if (index === 2) {
            sums[index] = this.page2.totalPage;
          }
       });
        return sums;
    },
     open(mag, typ, time) {
        this.$message({
          showClose: true,
          message: mag,
          type: typ,
          duration: time
        });
      },
    refresh () {
      let _this = this;
      _this.getDataList();
      _this.getDataList2(this.merchNo);
      // _this.multipleSelection.splice(0, 1);
      // _this.multipleSelection2 = []
      // console.log(_this.multipleSelection)
    },
    refresh2 () {
      let _this = this;
      _this.getDataList2(this.merchNo);
    },
    addData () {
      let _this = this;
      _this.showFlag = !_this.showFlag;
      _this.Params = null;
    },
    modifyData () {
      let _this = this;
      if (_this.multipleSelection3.length !== 1) {
        this.$message.warning('请勾选您要修改的一条数据！')
      } else {
        _this.showFlag = !_this.showFlag;
        _this.Params = _this.multipleSelection3
      }
    },
    deleteData () {
       let _this = this;
        if (_this.multipleSelection3.length < 1) {
        this.$message.warning('请勾选您要停用的数据！')
       } else {
        _this.dialogTableVisible = false;
        // _this.multipleSelection = []
        // _this.multipleSelection.push(_this.multipleSelection3)
        for (var i = 0; i < _this.multipleSelection3.length; i++) {
        _this.multipleSelection3[i].updateMan = userName;
        _this.multipleSelection3[i].updateNo = userId
        }
        _this.axios.post('pass/baseManagement/v1/limsmerchantsinfos/abolish', _this.multipleSelection3, {individualType: 'json'}).then(res => {
        if (res.succeed) {
         this.open('停用操作成功', 'success', 1200)
         _this.refresh()
        } else {
         this.$message.error(res.message)
        }
        }).catch(e => {
         this.$message.error(e.message)
       })
      }
    },
    createFile(){
      if(this.multipleSelection3.length != 1){
        this.$message.warning('请勾选您要导出的一条数据！')
        return
      }
      this.createFileLoading = true
      this.axios.post('pass/baseManagement/v1/limsmerchantsinfos/createFile/'+this.multipleSelection3[0].id,{individualType: 'json'}).then(res=>{
        if(res.code == 0 || res.code ==  200){
          this.$message.success('操作成功,请到供应商管理审核！');
        }else{
          this.$message.error(res.message)
        }
        this.createFileLoading = false
      }).catch(e=>{
        this.createFileLoading = false
        this.$message.error(e.message)
      })
    },
    recoverData () {
       let _this = this;
        if (_this.multipleSelection3.length < 1) {
        this.$message.warning('请勾选您要恢复的数据！')
        } else {
        _this.dialogTableVisible = false;
        for (var i = 0; i < _this.multipleSelection3.length; i++) {
        _this.multipleSelection3[i].updateMan = userName;
        _this.multipleSelection3[i].updateNo = userId
        }
        _this.axios.post('pass/baseManagement/v1/limsmerchantsinfos/recover', _this.multipleSelection3, {individualType: 'json'}).then(res => {
        if (res.succeed) {
         this.open('恢复成功', 'success', 1200)
         _this.refresh()
        } else {
         this.$message.error(res.message)
        }
        }).catch(e => {
         this.$message.error(e.message)
       })
      }
    },
    addData2 () {
      let _this = this;
     if (_this.multipleSelection3.length !== 1) {
       this.$message.warning('请选择一条客商信息数据！')
     } else {
      _this.showFlag2 = !_this.showFlag2;
      _this.Params2 = null;
      _this.Params = _this.multipleSelection3[0]
      }
      //  if (_this.multipleSelection.length !== 1) {
      //   this.$message.warning('请选择一条业务规则信息数据！')
      // } else {
      // _this.showFlag2 = !_this.showFlag2;
      // _this.Params2 = null;
      // _this.Params = _this.multipleSelection
      // }
    },
    modifyData2 () {
      let _this = this;
     if (_this.multipleSelection4.length !== 1) {
        this.$message.warning('请勾选您要修改的一条数据！')
      } else {
        _this.showFlag2 = !_this.showFlag2;
        _this.Params2 = _this.multipleSelection4[0];
        _this.Params = null
      }
    },
    deleteData2 () {
       let _this = this;
       if (_this.multipleSelection4.length < 1) {
        this.$message.warning('请勾选您要停用的数据！')
      } else {
        _this.dialogTableVisible = false;
        for (var i = 0; i < _this.multipleSelection4.length; i++) {
        _this.multipleSelection4[i].evaMan = userName;
        _this.multipleSelection4[i].evaNo = userId
        }
        _this.axios.post('pass/baseManagement/v1/limsmerchantsmats/abolish', _this.multipleSelection4, {individualType: 'json'}).then(res => {
        if (res.succeed) {
         this.open('停用操作成功', 'success', 1200)
         _this.refresh2()
        } else {
         this.$message.error(res.message)
        }
        }).catch(e => {
         this.$message.error(e.message)
       })
      }
    },
    recoverData2 () {
       let _this = this;
        if (_this.multipleSelection4.length < 1) {
        this.$message.warning('请勾选您要恢复的数据！')
      } else {
        _this.dialogTableVisible2 = false;
        for (var i = 0; i < _this.multipleSelection4.length; i++) {
        _this.multipleSelection4[i].evaMan = userName;
        _this.multipleSelection4[i].evaNo = userId
        }
        _this.axios.post('pass/baseManagement/v1/limsmerchantsmats/recover', _this.multipleSelection4, {individualType: 'json'}).then(res => {
        if (res.succeed) {
         this.open('恢复成功', 'success', 1200)
         _this.refresh2()
        } else {
         this.$message.error(res.message)
        }
        }).catch(e => {
         this.$message.error(e.message)
       })
      }
    },
    searchData () {
        let _this = this;
        _this.dataLoading = true;
        _this.page.pageNum = 1;
        _this.axios.get('pass/baseManagement/v1/limsmerchantsinfos/' + 'like/?pageNum=' + _this.page.pageNum + '&pageSize=' + _this.page.pageSize +
          '&merchName=' + _this.search.merchName + '&merchTypeName=' + _this.search.merchTypeName)
        .then(res => {
          _this.dataLoading = false;
          _this.table.list = res.data.list;
           for (let i = 0; i < this.table.list.length; i++) {
           if (this.table.list[i].validFlag === '0') {
             this.table.list[i].validFlag1 = '停用'
           } else if (this.table.list[i].validFlag === '1') {
             this.table.list[i].validFlag1 = '正常'
           }
           }
          _this.page.totalPage = res.data.total;
        }).catch(e => {
          _this.dataLoading = false;
        });
      },
    reset () {
      let _this = this;
      _this.search.merchName = '';
      _this.search.merchTypeName = '';
      _this.searchData();
      _this.getMerchTypeList ()
    },
    handleSelectionChange (val) {
      this.multipleSelection3 = val;
       if(val.length > 0) {
         this.merchNo = val[val.length-1].merchNo;
         this.getDataList2 (this.merchNo);
       }
      // if (val.length === 1) {
      //    this.merchNo = '';
      //    this.merchNo = val[0].merchNo;
      //    this.getDataList2 (this.merchNo);
      // } else {
      //    this.merchNo = '';
      //    this.getDataList2 (this.merchNo);
      // }

    },
    pageSizeChange (size) {
      this.page.pageSize = size;
      this.getDataList();
    },
    pageCurrentChange (index) {
      this.page.pageNum = index;
      this.getDataList();
    },
    handleSelectionChange2 (val) {
      // this.multipleSelection2 = [];
      // this.multipleSelection2.push(val);
      this.multipleSelection4 = val;
    },
    pageSizeChange2 (size) {
      this.page2.pageSize = size;
      this.getDataList2(this.merchNo);
    },
    pageCurrentChange2 (index) {
      this.page2.pageNum = index;
      this.getDataList2(this.merchNo);
    },
    handleCurrentChange (val) {
      if (val.merchNo === null) {
        this.merchNo = '';
      } else {
        this.merchNo = val.merchNo;
      }
      this.getDataList2 (this.merchNo);
    },
    addScreen () {
      this.$nextTick(() => {
          let ss = document.createElement('div');
          ss.innerHTML = '筛选';
          ss.style = 'text-align: center;'
          this.$refs.dataTable.$el.getElementsByClassName('el-table__fixed')[0].getElementsByTagName('td')[0].appendChild(ss);
       });
    },
    addScreen2 () {
     this.$nextTick(() => {
          let ss = document.createElement('div');
          ss.innerHTML = '筛选';
          ss.style = 'text-align: center;'
          this.$refs.dataTable2.$el.getElementsByClassName('el-table__fixed')[0].getElementsByTagName('td')[0].appendChild(ss);
       });
    },
    getDataList () {
      this.dataLoading = true;
      this.axios.get('pass/baseManagement/v1/limsmerchantsinfos/' + '?pageNum=' + this.page.pageNum + '&pageSize=' + this.page.pageSize)
            .then(res => {
              this.dataLoading = false;
              this.table.list = res.data.list;
              for (let i = 0; i < this.table.list.length; i++) {
              if (this.table.list[i].validFlag === '0') {
                this.table.list[i].validFlag1 = '停用'
              } else if (this.table.list[i].validFlag === '1') {
                this.table.list[i].validFlag1 = '正常'
              }
              }
              this.page.totalPage = res.data.total;
            }).catch(e => {
              this.dataLoading = false;
            });
    },
     getDataList2 (merchNo) {
      this.dataLoading2 = true;
      if (merchNo === null || merchNo === '') {
        this.table.list2 = []
        this.dataLoading2 = false;
        return 0
      } else {
         this.axios.get('pass/baseManagement/v1/limsmerchantsmats/' + '?pageNum=' + this.page2.pageNum + '&pageSize=' + this.page2.pageSize + '&merchNo=' + merchNo)
            .then(res => {
              this.dataLoading2 = false;
              this.table.list2 = res.data.list;
              for (let i = 0; i < this.table.list2.length; i++) {
              if (this.table.list2[i].validFlag === '0') {
                this.table.list2[i].validFlag1 = '停用'
              } else if (this.table.list2[i].validFlag === '1') {
                this.table.list2[i].validFlag1 = '正常'
              }
              }
              this.page2.totalPage = res.data.total;
            }).catch(e => {
              this.dataLoading2 = false;
            });
      }
    },
   getMerchTypeList () {
    this.axios.post("pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode",{validFlag:1,baseCode:4832}, {individualType: 'json'}).then(res => {
      this.dataLoading = false;
      this.merchType = res.data;
    })
   },
   tableRowClassName ({
        row, column, rowIndex, columnIndex
      }) {
        if (columnIndex === 8) {
        if (row.validFlag !== '1') {
          return 'error-row';
        }
        return '';
        }
      },
    tableRowClassName2 ({
        row, column, rowIndex, columnIndex
      }) {
        if (columnIndex === 7) {
        if (row.validFlag !== '1') {
          return 'error-row';
        }
        return '';
        }
      },
    tableHeaderCellStyle () {
      return 'color: rgb(255,255,255);border-bottom: 1px solid #DCDFE7;'
    }
  }
}
</script>
<style  scoped>
</style>
