<template>
  <!-- 查询大宗物料收发数据 -->
  <div class="getDzMaterialReal">
    <div class="box">
        <div class="box-top">
          <el-form style="overflow: hidden;" :style="filterForm.show ? null : { height: '0px' }"
            class="box-top-form" size="mini" label-width="60px">
            <el-row>
              <el-col :span="4">
                <el-form-item label="过磅日期">
                  <el-input clearable v-model="filterForm.data.gbsj"
                    placeholder="过磅日期: 2022-01-01" style="width: 95%;" ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="3">
                <el-form-item label="物料分类">
                  <el-input clearable v-model="filterForm.data.cinvcname"
                    placeholder="物料分类: 其它原料" style="width: 95%;" ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="3">
                <el-form-item label="物料名称">
                  <el-input clearable v-model="filterForm.data.cinvname"
                    placeholder="物料名称: 废钢" style="width: 98%;"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item label="规格">
                  <el-input clearable v-model="filterForm.data.cinvstd"
                    placeholder="规格: 直径≥10mm,长≤100mm" style="width: 95%;" ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item label="仓库名称">
                  <el-input clearable v-model="filterForm.data.cwhname"
                    placeholder="仓库名称: 原材料厂区库" style="width: 98%;" ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="3">
                <el-form-item label="单据类型">
                  <el-input clearable v-model="filterForm.data.djlx"
                    placeholder="单据类型: 收货" style="width: 100%;" ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="3">
                <el-form-item label="去向">
                  <el-input clearable v-model="filterForm.data.place"
                    placeholder="去向: 废钢中心" style="width: 100%;" ></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <div class="box-top-gjl">
            <div style="float:right; text-align: right;">
                <el-button class="button" type="primary" size="mini" icon="el-icon-search"
                  v-privilege="activeMenu + 'QUERY'" :loading="rightTable.loading" @click="getTableData()" >查询</el-button>
                <el-button class="button" type="primary" size="mini" icon="el-icon-refresh"
                  v-privilege="activeMenu + 'QUERY'" :loading="rightTable.loading" @click="tableDataCancel();" >重置</el-button>
                <el-button size="mini" :icon="filterForm.show ? 'el-icon-caret-top' : 'el-icon-caret-bottom'"
                  :title="filterForm.show ? '收起搜索区' : '展开搜索区'" @click="filterForm.show = !filterForm.show;
                    $nextTick(() => { singleTableHeight = getRoleHeight($refs['singleTable'].$el) - 45; });"
                >{{ filterForm.show ? '收起' : '展开' }}</el-button>
            </div>
          </div>
        </div>
        <div class="box-bottom">
            <el-table stripe border highlight-current-row id="singleTable" ref="singleTable" style="width: 100%;" size="mini"
              :data="frontEndPageChange" v-loading="rightTable.loading" :height="singleTableHeight" show-summary :summary-method="getSummaries">
                <el-table-column sortable prop="gbsj" label="过磅日期" min-width="80px" align="center"
                  :show-overflow-tooltip="true">
                  </el-table-column>
                <el-table-column sortable prop="cinvccode" label="物料分类编码" min-width="86px" align="center"
                  :show-overflow-tooltip="true">
                </el-table-column>
                <el-table-column sortable prop="cinvcname" label="物料分类名称" min-width="100px" align="center"
                  :show-overflow-tooltip="true">
                </el-table-column>
                <el-table-column sortable prop="cinvcode" label="物料编码" min-width="100px" align="center"
                  :show-overflow-tooltip="true">
                </el-table-column>
                <el-table-column sortable prop="cinvname" label="物料名称" min-width="100px" align="center"
                  :show-overflow-tooltip="true">
                </el-table-column>
                <el-table-column sortable prop="cinvstd" label="规格" min-width="70px" align="center"
                  :show-overflow-tooltip="true"></el-table-column>
                <el-table-column sortable prop="cwhcode" label="仓库编码" min-width="70px" align="center"
                  :show-overflow-tooltip="true" ></el-table-column>
                <el-table-column sortable prop="cwhname" label="仓库名称" min-width="90px" align="center"
                  :show-overflow-tooltip="true" ></el-table-column>
                <el-table-column sortable prop="mz" label="毛重" min-width="70px" align="center"
                  :show-overflow-tooltip="true" :formatter="rounding" ></el-table-column>
                <el-table-column sortable prop="pz" label="皮重" min-width="70px" align="center"
                  :show-overflow-tooltip="true" :formatter="rounding" ></el-table-column>
                <el-table-column sortable prop="jz" label="净重" min-width="70px" align="center"
                  :show-overflow-tooltip="true" :formatter="rounding" ></el-table-column>
                <el-table-column sortable prop="djlx" label="单据类型" min-width="70px" align="center"
                  :show-overflow-tooltip="true" ></el-table-column>
                <el-table-column sortable prop="pzdl" label="配置单料" min-width="70px" align="center"
                  :show-overflow-tooltip="true" ></el-table-column>
                <el-table-column sortable prop="tab" label="所属表" min-width="70px" align="center"
                  :show-overflow-tooltip="true" ></el-table-column>
                <el-table-column sortable prop="place" label="去向" min-width="120px" align="center"
                  :show-overflow-tooltip="true" ></el-table-column>
                <el-table-column sortable prop="bfsfclWater" label="水分" min-width="70px" align="center"
                  :show-overflow-tooltip="true" :formatter="rounding" ></el-table-column>
                <el-table-column sortable prop="bfsfclZazhi" label="杂质" min-width="70px" align="center"
                  :show-overflow-tooltip="true" :formatter="rounding" ></el-table-column>
                <el-table-column sortable prop="bfsfclKz" label="扣杂" min-width="70px" align="center"
                  :show-overflow-tooltip="true" :formatter="rounding" ></el-table-column>
            </el-table>
          <el-pagination layout="total, sizes, prev, pager, next, jumper" style="text-align: right;margin-top: 10px;"
            :page-sizes="[10, 20, 50, 100, 500, 1000]"
            :total="rightTable.total" :page-size="rightTable.pageSize" :current-page.sync="rightTable.pageNum"
            @size-change="tableSizeChange" ></el-pagination>
        </div>
    </div>
  </div>
</template>

<script>
import { zCheckNumber1 } from '~/utils/validator.js';
import store from '@/store/index.js';
import {formatDate, XtcommonSummaries} from '@/utils/util.js';
export default {
  name: 'getDzMaterialReal',
  data () {
    return {
      activeMenu: '',
      singleTableHeight: 100,
      tableData: {
          arr: [],
          obj: {}
        },
      filterForm: {
        show: true,
        data: {
          gbsj: '',
          cinvcname: '',
          cinvname: '',
          cinvstd: '',
          cwhname: '',
          djlx: '',
          place: ''
        }
      },
      rightTable: {
        total: 0,
        pageNum: 1,
        pageSize: 20,
        loading: false,
        tableLoading: false,
        tableData: {
          arr: [],
          obj: {}
        }
      }
    }
  },
  created () {
      this.activeMenu = window.localStorage.getItem('activeMenu');
  },
  mounted () {
    let that = this;
    window.PEDataObj = {
      // 将数据绑定到window上，供main页面使用
      vm: that,
      // tableArr：用于导出成Excel的表格的信息
      tableArr: [{
        name: '',
        id: 'singleTable'
      }]
    };
    that.$nextTick(() => {
      // 立即获取的height有一定偏差，通过setTimeout延迟来解决
      setTimeout(() => {
        that.singleTableHeight = that.getRoleHeight(that.$refs['singleTable'].$el) - 45;
      }, 1);
    });
    // 这里写页面加载需要调用的代码
    that.getTableData();
  },
  computed: {
    frontEndPageChange () {
      let that = this;
      let Tdata = [];
      let start = (that.rightTable.pageNum - 1) * that.rightTable.pageSize;
      if (start >= that.rightTable.tableData.arr.length) start = 0;
      let end = that.rightTable.pageNum * that.rightTable.pageSize;
      if (end >= that.rightTable.tableData.arr.length) end = that.rightTable.tableData.arr.length;
      // that.rightTable.tableData.arr = that.rightTable.tableData.arr.slice(start, end);
      that.rightTable.total = that.rightTable.tableData.arr.length;
      Tdata = that.rightTable.tableData.arr;
      Tdata = Tdata.filter(data => !that.filterForm.data.gbsj || data.gbsj.toLowerCase()
        .includes(that.filterForm.data.gbsj.toLowerCase()));
      Tdata = Tdata.filter(data => !that.filterForm.data.cinvcname || data.cinvcname.toLowerCase()
        .includes(that.filterForm.data.cinvcname.toLowerCase()));
      Tdata = Tdata.filter(data => (!that.filterForm.data.cinvname || !data.cinvname) || data.cinvname.toLowerCase()
        .includes(that.filterForm.data.cinvname.toLowerCase()));
      Tdata = Tdata.filter(data => (!that.filterForm.data.cinvstd || !data.cinvstd) || data.cinvstd.toLowerCase()
        .includes(that.filterForm.data.cinvstd.toLowerCase()));
      Tdata = Tdata.filter(data => (!that.filterForm.data.cwhname || !data.cwhname) || data.cwhname.toLowerCase()
        .includes(that.filterForm.data.cwhname.toLowerCase()));
      Tdata = Tdata.filter(data => (!that.filterForm.data.djlx || !data.djlx) || data.djlx.toLowerCase()
        .includes(that.filterForm.data.djlx.toLowerCase()));
      Tdata = Tdata.filter(data => (!that.filterForm.data.place || !data.place) || data.place.toLowerCase()
        .includes(that.filterForm.data.place.toLowerCase()));
      that.rightTable.total = Tdata.length;
      return Tdata.slice(start, end);
    }
  },
  methods: {
    //  计算树区域高度
    getRoleHeight (dom) {
        return window.innerHeight - dom.offsetTop;
    },
    // 自定义的表格合计方法：只给指定列进行合计
    getSummaries (param) {
        const prop = ['mz', 'pz', 'jz', 'bfsfclWater', 'bfsfclZazhi', 'bfsfclKz']; // 合计列绑定的prop
        return XtcommonSummaries(param, prop);
    },
    // 获取表格中的数据
    getTableData (pageNum) {
        let that = this, obj = {};
        let url = 'pass/product/v1/dematerialSfDb/getDzMaterialSf';
        that.rightTable.loading = true;
        that.axios.get(url)
          .then(function (res) {
            if (res) {
              that.tableData.arr = res;
              that.rightTable.tableData.arr = res.sort((a, b) => {
                // 排序基于的数据
                let agbsj = a.gbsj;
                let bgbsj = b.gbsj;
                agbsj = agbsj.replace(/-/g, '/');
                bgbsj = bgbsj.replace(/-/g, '/');
                let atime = new Date(agbsj).getTime();
                let btime = new Date(bgbsj).getTime();
                return atime - btime;
                });
            }
            that.rightTable.loading = false;
          }).catch(function () {
            that.rightTable.loading = false;
          });
    },
    // 改变表格显示条数
    tableSizeChange (val) {
        let that = this;
        that.rightTable.pageSize = val;
    },
    // 重置搜索
    tableDataCancel () {
      let that = this;
      that.filterForm.data.gbsj = '';
      that.filterForm.data.cinvcname = '';
      that.filterForm.data.cinvname = '';
      that.filterForm.data.cinvstd = '';
      that.filterForm.data.cwhname = '';
      that.filterForm.data.djlx = '';
      that.filterForm.data.place = '';
    },
    rounding (row, column) {
      return parseFloat(row[column.property]).toFixed(2)
    }
  }
}
</script>

<style lang="less">
.getDzMaterialReal {
  min-width: 700px;
  height: 100%;
  .box {
    height: 100%;
    padding: 15px 15px 0 15px;
    .box-left {
      float: left;
      width: 200px;
      height: calc(100% - 3px);
      overflow: auto;
      .box-left-bt {
        line-height: 35px;
        font-size: 16px;
        border-bottom: 1px solid #ccc;
      }
      .box-left-tree {
        height: calc(100% - 50px);
        overflow: auto;
        padding: 7px 7px 0 0;
          .tree-row-on {
            color: #3279e8;
            font-weight: 600;
          }
      }
    }
    .box-top {
      .box-top-form {
        padding-left: 7px;
        .el-form-item {
          margin-bottom: 7px;
        }
      }
      .box-top-gjl {
        overflow: hidden;
        padding: 7px 0 7px 7px;
        border-top: 1px solid #ccc;
      }
    }
    .box-bottom {
      padding-left: 7px;
    }

    .el-table .el-table__row {
      height: 35px;
    }
    .el-table__body .el-form-item--mini.el-form-item {
      margin: 0px;
      .error {
        overflow: hidden;
        color: #f56c6c;
        font-size: 12px;
        line-height: 1;
      }
    }
  }
  .color_no {
    color: #7a7a7a;
  }
  .color_yes {
    color: #1a9f17;
  }
}
</style>
