<template>
  <div class="app-table">
    <div class="search">
      <el-date-picker v-model="table.getdataListParm.parammaps.operatetime" class="filter-item" type="date" style="width: 250px;" format="yyyy-MM-dd" value-format="yyyy-MM-dd" :clearable="true"  placeholder="年/月/日" />
      <el-button class="successBorder" @click="form_search">查询</el-button>
      <el-button class="successBorder" @click="handleRefresh">重置</el-button>
    </div>
    <div class="table">
      <el-table
        :key="table.tableKey"
        v-loading="table.listLoading"
        element-loading-text="给我一点时间"
        :data="table.list"
        border
        fit
        highlight-current-row
        style="width: 100%;"
        :row-style="rowStyle"
        :cell-style="cellStyle"
        class="elTable table-fixed"
        :span-method="objectSpanMethod"
      >
        <el-table-column :key="1" label="序号" prop="sort" align="center" width="50px" />
        <el-table-column :key="2" label="栏舍名称" prop="barname" min-width="90px" align="center" />
        <el-table-column :key="3" label="班次" prop="times" min-width="90px" align="center" />
        <el-table-column :key="4" label="第一层" min-width="90px" align="center">
          <el-table-column label="比例 (%)" prop="onerate" min-width="90px" align="center" />
          <el-table-column label="重量 (g)" prop="oneweight" min-width="90px" align="center" />
        </el-table-column>
        <el-table-column :key="5" label="第二层" min-width="90px" align="center">
          <el-table-column label="比例 (%)" prop="tworate" min-width="90px" align="center" />
          <el-table-column label="重量 (g)" prop="twoweight" min-width="90px" align="center" />
        </el-table-column>
        <el-table-column :key="6" label="第三层" min-width="90px" align="center">
          <el-table-column label="比例 (%)" prop="threerate" min-width="90px" align="center" />
          <el-table-column label="重量 (g)" prop="threeweight" min-width="90px" align="center" />
        </el-table-column>
        <el-table-column :key="7" label="第四层" min-width="90px" align="center">
          <el-table-column label="比例 (%)" prop="fourrate" min-width="90px" align="center" />
          <el-table-column label="重量 (g)" prop="fourweight" min-width="90px" align="center" />
        </el-table-column>
        <el-table-column :key="8" label="操作" prop="operation" align="center" width="300" class-name="small-padding fixed-width" fixed="right">
          <template slot-scope="{row}">
            <a class="correcting" @click="form_seeHistory (row)">{{ row.operation }}</a>
          </template>
        </el-table-column>
      </el-table>
      <pagination2 v-show="table.total>0" :total="table.total" :page.sync="table.getdataListParm.offset" :limit.sync="table.getdataListParm.pagecount" @pagination="getList" />
    </div>
    <el-dialog :visible.sync="seeHistory.dialogFormVisible" append-to-body :title="textMap[seeHistory.dialogStatus]" :close-on-click-modal="false" width="90%">
      <div class="seeHistory">
        <div class="search">
          <el-date-picker v-model="seeHistory.getdataListParm.parammaps.inputDatetime" class="inputDatetime filter-item" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 250px;" />
          <el-button class="successBorder" @click="form_search2">查询</el-button>
          <el-button class="successBorder" @click="handleRefresh2">重置</el-button>
        </div>
        <div class="table">
          <el-table
            :key="seeHistory.tableKey"
            v-loading="seeHistory.listLoading"
            element-loading-text="给我一点时间"
            :data="seeHistory.list"
            border
            fit
            highlight-current-row
            style="width: 100%;"
            :row-style="rowStyle"
            :cell-style="cellStyle"
            class="elTable table-fixed"
            :span-method="objectSpanMethod2"
          >
            <el-table-column :key="1" label="序号" prop="sort" align="center" width="50px" />
            <el-table-column :key="2" label="修改时间" prop="operatetime1" min-width="90px" align="center" />
            <el-table-column :key="3" label="班次" prop="times" min-width="90px" align="center" />
            <el-table-column :key="4" label="第一层" min-width="90px" align="center">
              <el-table-column label="比例 (%)" prop="onerate" min-width="90px" align="center" />
              <el-table-column label="重量 (g)" prop="oneweight" min-width="90px" align="center" />
            </el-table-column>
            <el-table-column :key="5" label="第二层" min-width="90px" align="center">
              <el-table-column label="比例 (%)" prop="tworate" min-width="90px" align="center" />
              <el-table-column label="重量 (g)" prop="twoweight" min-width="90px" align="center" />
            </el-table-column>
            <el-table-column :key="6" label="第三层" min-width="90px" align="center">
              <el-table-column label="比例 (%)" prop="threerate" min-width="90px" align="center" />
              <el-table-column label="重量 (g)" prop="threeweight" min-width="90px" align="center" />
            </el-table-column>
            <el-table-column :key="7" label="第四层" min-width="90px" align="center">
              <el-table-column label="比例 (%)" prop="fourrate" min-width="90px" align="center" />
              <el-table-column label="重量 (g)" prop="fourweight" min-width="90px" align="center" />
            </el-table-column>
          </el-table>
          <pagination2 v-show="seeHistory.total>0" :total="seeHistory.total" :page.sync="seeHistory.getdataListParm.offset" :limit.sync="seeHistory.getdataListParm.pagecount" @pagination="getSeeHistoryList" />
        </div>
        <div slot="footer" class="dialog-footer">
          <el-button class="cancelClose cancelClose1" @click="seeHistory.dialogFormVisible = false; ">关闭</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { GetDataByName } from '@/api/common'
import draggable from 'vuedraggable'
import Cookies from 'js-cookie'
import { parseTime, handleTableSpan, handleObjectSpanMethod } from '@/utils/index.js'
import Pagination2 from '@/components/Pagination2'
export default {
  name: 'MaterialIssuancePlan',
  display: 'Two list header slot',
  order: 14,
  components: { Pagination2 },
  data() {
    return {
      table: {
        getdataListParm: {
          name: 'getPennsieveHistory',
          page: 1,
          offset: 1,
          pagecount: 12,
          returntype: 'Map',
          parammaps: {
            pastureid: Cookies.get('pastureid'),
            operatetime: ''
          }
        },
        list: [],
        total: 0,
        tableKey: 0,
        listLoading: false,
        temp: {},
        changeList: [],
        startObj: {}
      },
      spanObj: {},
      mergekeys: ['barname', 'operation'],
      textMap: {
        seeHistory: '宾州筛修改记录'
      },
      seeHistory: {
        dialogFormVisible: false, dialogStatus: '',
        total: 0, tableKey: 0, listLoading: false,
        getdataListParm: {
          name: 'getPennsieveHistoryBar', page: 1, offset: 1, pagecount: 10, returntype: 'Map',
          parammaps: {
            pastureid: Cookies.get('pastureid'),
            barid: '',
            inputDatetime: '',
            time1: '',
            time2: ''
          }
        }
      },
      spanObj2: {},
      mergekeys2: ['operatetime1'],
      rowStyle: { maxHeight: 30 + 'px', height: 30 + 'px' },
      cellStyle: { padding: 0 + 'px' }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    changeDate() {
      this.getList()
    },
    getList() {
      this.table.listLoading = true
      GetDataByName(this.table.getdataListParm).then(response => {
        console.log('table数据', response.data.list)
        if (response.data.list !== null) {
          this.table.list = response.data.list
          this.handleSpan()
          this.table.pageNum = response.data.pageNum
          this.table.pageSize = response.data.pageSize
          this.table.total = response.data.total
        } else {
          this.table.list = []
        }
        setTimeout(() => {
          this.table.listLoading = false
        }, 100)
      })
    },
    handleSpan() {
      this.mergekeys.forEach(key => {
        this.spanObj[key] = []
        let position = 0
        this.table.list.forEach((item, index) => {
          if (index === 0) {
            this.spanObj[key].push(1)
            position = 0
          } else {
            if (key == 'operation') {
              if (this.table.list[index][key] === this.table.list[index - 1][key] && this.table.list[index]['barname'] === this.table.list[index - 1]['barname']) {
                this.spanObj[key][position] += 1
                this.spanObj[key].push(0)
              } else {
                this.spanObj[key].push(1)
                position = index
              }
            } else {
              if (this.table.list[index][key] === this.table.list[index - 1][key]) {
                this.spanObj[key][position] += 1
                this.spanObj[key].push(0)
              } else {
                this.spanObj[key].push(1)
                position = index
              }
            }
          }
        })
      })
    },
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      for (let i = 0; i < this.mergekeys.length; i++) {
        if (column.property === this.mergekeys[i]) {
          const _row = this.spanObj[this.mergekeys[i]][rowIndex]
          const _col = _row > 0 ? 1 : 0
          return {
            rowspan: _row,
            colspan: _col
          }
        }
      }
    },
    form_search() {
      if (this.table.getdataListParm.parammaps.operatetime == '' || this.table.getdataListParm.parammaps.operatetime == null ) {
        this.table.getdataListParm.parammaps.operatetime = ''
      } else {
        this.table.getdataListParm.parammaps.operatetime = parseTime(this.table.getdataListParm.parammaps.operatetime, '{y}-{m}-{d}')
      }
      this.getList()
    },
    handleRefresh() {
      this.table.getdataListParm.parammaps.operatetime = ''
      this.getList()
    },
    form_seeHistory(row) {
      console.log(row)
      this.textMap.seeHistory = '宾州筛修改记录——栏舍：' + row.barname
      this.seeHistory.dialogFormVisible = true
      this.seeHistory.dialogStatus = 'seeHistory'
      this.seeHistory.getdataListParm.parammaps.barid = row.barids
      this.seeHistory.getdataListParm.parammaps.inputDatetime = ''
      this.seeHistory.getdataListParm.parammaps.time1 = ''
      this.seeHistory.getdataListParm.parammaps.time2 = ''
      this.getSeeHistoryList()
    },
    getSeeHistoryList() {
      this.seeHistory.listLoading = true
      GetDataByName(this.seeHistory.getdataListParm).then(response => {
        console.log('table数据', response.data.list)
        if (response.data.list !== null) {
          this.seeHistory.list = response.data.list
          this.handleSpan2()
          this.seeHistory.pageNum = response.data.pageNum
          this.seeHistory.pageSize = response.data.pageSize
          this.seeHistory.total = response.data.total
        } else {
          this.seeHistory.list = []
        }
        setTimeout(() => {
          this.seeHistory.listLoading = false
        }, 100)
      })
    },
    handleSpan2() {
      this.mergekeys2.forEach(key => {
        this.spanObj2[key] = []
        let position = 0
        this.seeHistory.list.forEach((item, index) => {
          if (index === 0) {
            this.spanObj2[key].push(1)
            position = 0
          } else {
            if (key == 'operation') {
              if (this.seeHistory.list[index][key] === this.seeHistory.list[index - 1][key] && this.seeHistory.list[index]['barname'] === this.seeHistory.list[index - 1]['barname']) {
                this.spanObj2[key][position] += 1
                this.spanObj2[key].push(0)
              } else {
                this.spanObj2[key].push(1)
                position = index
              }
            } else {
              if (this.seeHistory.list[index][key] === this.seeHistory.list[index - 1][key]) {
                this.spanObj2[key][position] += 1
                this.spanObj2[key].push(0)
              } else {
                this.spanObj2[key].push(1)
                position = index
              }
            }
          }
        })
      })
    },
    objectSpanMethod2({ row, column, rowIndex, columnIndex }) {
      for (let i = 0; i < this.mergekeys2.length; i++) {
        if (column.property === this.mergekeys2[i]) {
          const _row = this.spanObj2[this.mergekeys2[i]][rowIndex]
          const _col = _row > 0 ? 1 : 0
          return {
            rowspan: _row,
            colspan: _col
          }
        }
      }
    },
    form_search2() {
      if (this.seeHistory.getdataListParm.parammaps.inputDatetime !== '' && this.seeHistory.getdataListParm.parammaps.inputDatetime !== null) {
        this.seeHistory.getdataListParm.parammaps.time1 = parseTime(this.seeHistory.getdataListParm.parammaps.inputDatetime[0], '{y}-{m}-{d}')
        this.seeHistory.getdataListParm.parammaps.time2 = parseTime(this.seeHistory.getdataListParm.parammaps.inputDatetime[1], '{y}-{m}-{d}')
      } else {
        this.seeHistory.getdataListParm.parammaps.inputDatetime = ''
        this.seeHistory.getdataListParm.parammaps.time1 = ''
        this.seeHistory.getdataListParm.parammaps.time2 = ''
      }
      this.seeHistory.getdataListParm.offset = 1
      this.getSeeHistoryList()
    },
    handleRefresh2() {
      this.seeHistory.getdataListParm.parammaps.time1 = ''
      this.seeHistory.getdataListParm.parammaps.time2 = ''
      this.seeHistory.getdataListParm.parammaps.inputDatetime = ''
      this.getSeeHistoryList()
    }
  }
}
</script>

<style  lang="scss" scoped>
 .search{margin-bottom: 10px;}
</style>
