<template>
  <div class="examination">
    <div class="common-head-search">
      <el-form :inline="true">
        <el-form-item label="异常类型">
          <el-select v-model="search.objTypeName" clearable filterable collapse-tags size="small" style="width:200px;margin-right: 10px;" placeholder="请选择查询内容">
            <el-option v-for="item in objType" :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.matName" size="small" placeholder="请输入查询内容" style="width: 200px"></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>
      </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 :row-key="getRowKeys" :expand-row-keys="expands" @expand-change="expandChange">
        <el-table-column type="expand" fixed="left">
             <el-table v-loading="dataLoading" ref="dataTable3" border  highlight-current-row @selection-change="handleSelectionChange2" :data="table.list4"
             :header-cell-style="tableHeaderCellStyle2">
             <el-table-column type="index" label="NO" fixed="left" align="center">
             </el-table-column>
             <el-table-column :show-overflow-tooltip="true" min-width="140px" prop="objTypeName" label="异常类型" align="center"></el-table-column>
             <el-table-column :show-overflow-tooltip="true" min-width="140px" prop="matName" label="物料名称" align="center"></el-table-column>
             <el-table-column :show-overflow-tooltip="true" min-width="150px" prop="bugTypeName" label="缺陷类型" align="center"></el-table-column>
             <el-table-column :show-overflow-tooltip="true" min-width="115px" prop="objectNum" label="异常数量" align="center"></el-table-column>
             <el-table-column :show-overflow-tooltip="true" min-width="140px" prop="unitName" label="异常单位" align="center"></el-table-column>
             <el-table-column :show-overflow-tooltip="true" min-width="195px" prop="objctDesc" label="异常说明" align="center"></el-table-column>
             <el-table-column :show-overflow-tooltip="true" min-width="140px" prop="annexName1" label="附件1" align="center"></el-table-column>
             <el-table-column :show-overflow-tooltip="true" min-width="140px" prop="annexName2" label="附件2" align="center"></el-table-column>
             </el-table>
        </el-table-column>
        <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="100px" prop="objTypeName" label="异常类型" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="140px" prop="matName" label="物料名称" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="120px" prop="lotsNo" label="批次信息" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="200px" prop="memo" label="备注"  align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="90px"  prop="objFlag1" label="状态" align="center">
          <!-- <template slot-scope="scope">
              <span v-if="scope.row.objFlag == 2">未处理</span>
              <span v-else-if="scope.row.objFlag == 3">已处理</span>
          </template> -->
        </el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="95px" prop="declareName" label="申报人" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="160px" prop="declareTime" label="申报时间" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="95px" prop="handMan" label="处理人" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="160px" prop="handTime" 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>
      </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"  :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="100px" prop="objTypeName" label="异常类型" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="140px" prop="matName" label="物料名称" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="160px" prop="prcResultName" label="处理结果" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="bugQty" label="缺陷数量" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="140px" prop="bugWgt" label="缺陷重量（t）" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="120px" prop="outPkt" label="赔偿金额" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="180px" prop="lostWgt" label="退货" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="180px" prop="cutPrice" 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="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 alertComponets2 from './alertComponents2';
let userName = getCookie(cookieUserName);
let userId = getCookie(cookieUserId);
export default {
  components: {alertComponets2},
  data () {
    return {
      table: {
        list: [],
        list2: [],
        list4: []
      },
      dataLoading: false,
      dataLoading2: false,
      icoreFilterFlag: true,
      expands: [],
      getRowKeys (row) {
        return row.objId
      },
      page: {
        pageSize: 50,
        pageNum: 1,
        totalPage: 0
      },
      objId: '',
      page2: {
        pageSize: 990,
        pageNum: 1,
        totalPage: 0
      },
      showFlag: false,
      Params: {},
      showFlag2: false,
      Params2: {},
      objType: [],
      multipleSelection3: [],
      multipleSelection4: [],
      search: {
        matName: '',
        objTypeName: '',
      },
      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.addScreen();
    this.addScreen2();
    this.getobjTypeList ()
  },
  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;
    },
    refresh () {
      let _this = this;
      _this.getDataList();
      _this.getDataList2(_this.objId);
    },
    refresh2 () {
      let _this = this;
      _this.getDataList();
      _this.getDataList2(_this.objId);
    },
    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 {
        for (var j= 0; j < _this.multipleSelection3.length; j++) {
          if (_this.multipleSelection3[j].validFlag === '0') {
          this.$message.warning('请勾选状态为有效的数据！')  
          return 
          }
        }
        _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/limsobjheats/abolish', _this.multipleSelection3, {individualType: 'json'}).then(res => {
        if (res.succeed) {
         this.$message.success('停用操作成功');
         _this.refresh()
        } else {
         this.$message.error(res.message)
        }
        }).catch(e => {
         this.$message.error(e.message)
       })
      }
    },
    recoverData () {
       let _this = this;
        if (_this.multipleSelection3.length < 1) {
        this.$message.warning('请勾选您要恢复的数据！')
        } else {
          for (var j= 0; j < _this.multipleSelection3.length; j++) {
          if (_this.multipleSelection3[j].validFlag === '1') {
          this.$message.warning('请勾选状态为无效的数据！')  
          return 
          }
        }
        _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/limsobjheats/recover', _this.multipleSelection3, {individualType: 'json'}).then(res => {
        if (res.succeed) {
         this.$message.success('恢复成功');
         _this.refresh()
        } else {
         this.$message.error(res.message)
        }
        }).catch(e => {
         this.$message.error(e.message)
       })
      }
    },
    submitData () {
       let _this = this;
        if (_this.multipleSelection3.length < 1) {
        this.$message.warning('请勾选您要提交的数据！')
        } else {
          for (var j= 0; j < _this.multipleSelection3.length; j++) {
          if (_this.multipleSelection3[j].objFlag === '1') {
          this.$message.warning('请勾选状态为申报的数据！')  
          return 
          }
        }
        _this.dialogTableVisible = false;
        for (var i = 0; i < _this.multipleSelection3.length; i++) {
        _this.multipleSelection3[i].apprMan = userName;
        _this.multipleSelection3[i].apprNo = userId
        }
        _this.axios.post('pass/baseManagement/v1/limsobjheats/submit', _this.multipleSelection3, {individualType: 'json'}).then(res => {
        if (res.succeed) {
         this.$message.success('提交成功');
         _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].handMan = userName;
        _this.multipleSelection4[i].handNo = userId
        }
        _this.axios.put('pass/baseManagement/v1/limsobjhandles/delete', _this.multipleSelection4, {individualType: 'json'}).then(res => {
        if (res.succeed) {
         this.$message.success('删除成功');
         _this.refresh()
        } 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/limsobjhandles/recover', _this.multipleSelection4, {individualType: 'json'}).then(res => {
        if (res.succeed) {
         this.$message.success('恢复成功');
         _this.refresh()
        } 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/limsobjheats/' + '?pageNum=' + _this.page.pageNum + '&pageSize=' + _this.page.pageSize +
          '&matName=' + _this.search.matName + '&objTypeName=' + _this.search.objTypeName + '&objFlag1=2&objFlag2=3')
        .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].objFlag === '2') {
            this.table.list[i].objFlag1 = '未处理'
           } else if (this.table.list[i].objFlag === '3') {
           this.table.list[i].objFlag1 = '已处理'
           }
           }
          _this.page.totalPage = res.data.total;
        }).catch(e => {
          _this.dataLoading = false;
        });
      },
    reset () {
      let _this = this;
      _this.search.matName = '';
      _this.search.objTypeName = '';
      _this.searchData();
      _this.getobjTypeList ()
    },
    handleSelectionChange (val) {
      this.multipleSelection3 = val;
      if (val.length > 0) {
        this.objId = val[val.length-1].objId
        this.getDataList2(this.objId);
      }
    },
    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.objId);
    },
    pageCurrentChange2 (index) {
      this.page2.pageNum = index;
      this.getDataList2(this.objId);
    },
    handleCurrentChange (val) {
      if (val.objId === null) {
        this.objId = '';
      } else {
        this.objId = val.objId;
      }
      this.getDataList2 (this.objId)
    },
    addScreen () {
      this.$nextTick(() => {
          let ss = document.createElement('div');
          ss.innerHTML = '筛选';
          ss.style = 'text-align: center;'
          let aaa = this.$refs.dataTable.$el.getElementsByClassName('el-table__fixed')[1].getElementsByTagName('td')[1];
          if (aaa !== undefined) {
              aaa.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/limsobjheats/' + '?pageNum=' + this.page.pageNum + '&pageSize=' + this.page.pageSize + '&objFlag1=2&objFlag2=3&objFlag3=4')
            .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].objFlag === '2') {
                 this.table.list[i].objFlag1 = '未处理'
                } else if (this.table.list[i].objFlag === '3') {
                this.table.list[i].objFlag1 = '已处理'
                }
                }
              this.page.totalPage = res.data.total;
            }).catch(e => {
              this.dataLoading = false;
            });
    },
     getDataList2 (objId) {
      this.dataLoading2 = true;
      if (objId === null || objId === '') {
        this.table.list2 = [];
        this.dataLoading2 = false;
        return 0
      } else {
      this.axios.get('pass/baseManagement/v1/limsobjhandles/' + '?pageNum=' + this.page2.pageNum + '&pageSize=' + this.page2.pageSize + '&objId=' + objId)
            .then(res => {
              this.dataLoading2 = false;
              this.table.list2 = res.data.list;
              this.page2.totalPage = res.data.total;
            }).catch(e => {
              this.dataLoading2 = false;
            });
      }
    },
   getobjTypeList () {
      this.axios.post("pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode",{validFlag:1,baseCode:4826}, {individualType: 'json'}).then(res => {
              this.dataLoading = false;
              this.objType = res.data
            })
   },
   tableRowClassName ({
        row, column, rowIndex, columnIndex
      }) {
        if (columnIndex === 7) {
        if (row.objFlag === '3') {
          return 'error-green';
        } else if (row.objFlag === '4') {
          return 'error-yellow';
        }
        return '';
        }
      },
    expandChange(row, expandedRows) { 
       if (expandedRows.length) {
        this.expands = []
        if (row) {
          this.expands.push (row.objId)
          this.objId = row.objId
          this.table.list4 = []
          this.axios.get('pass/baseManagement/v1/limsobjlines/' + '?objId=' + this.objId)
         .then(res => {
        //    this.dataLoading = false;
           this.table.list3 = res.data.list;
           for (var i = 0; i < this.table.list3.length; i++) {
           this.$set(this.table.list4,i,this.table.list3[i])
           }            
         }).catch(e => {
        //    this.dataLoading = false;
         });
        }
      } else {
        this.expands = []
      }
    },
    tableHeaderCellStyle () {
      return 'color: rgb(255,255,255);border-bottom: 1px solid #DCDFE7;'
    },
    tableHeaderCellStyle2 () {
      return 'color: rgb(255,255,255);border-bottom: 1px solid #DCDFE7;'
    }
  }
}
</script>
<style  scoped>
</style>
