<template>
  <div class="app-container">
    <el-card style="height: 835px">
      <div style="margin-top: 7px; margin-bottom: 7px">
        <span style="font-size: 13px;color: black;">时间</span>
        <el-date-picker
          v-model="dateRange"
          size="small"
          type="daterange"
          align="center"
          style="width: 240px;"
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
          range-separator="-"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          @change="pageCurrent=1;searchTable();getEquiptmentNumber();getToolNumber()"/>
        <el-select v-model="EquipmentNumberValue" style="width:200px" placeholder="设备编号" size="small" filterable clearable @change="pageCurrent=1;searchTable();">
          <el-option
            v-for="item in EquipmentNumber"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
        <el-select v-model="ToolNumberValue" style="width:200px" placeholder="刀具代码" size="small" filterable clearable @change="pageCurrent=1;searchTable()">
          <el-option
            v-for="item in ToolNumber"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
      </div>
      <div>
        <el-table
          v-loading="loading"
          ref="table"
          :data="tableData"
          highlight-current-row
          show-summary
          border
          stripe
          size="small"
          style="width: 1581px"
          height="740px">
          <el-table-column type="index" label="序号" align="center" width="70px"/>
          <el-table-column label="产线" align="center" prop="产线" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.ProductionLine }}
            </template>
          </el-table-column>
          <el-table-column label="工位号" align="center" sortable="custom" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.OpName }}
            </template>
          </el-table-column>
          <el-table-column label="设备编号" align="center" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.EquipmentNumber }}
            </template>
          </el-table-column>
          <el-table-column label="设备名称" align="center" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.EquipmentName }}
            </template>
          </el-table-column>
          <el-table-column label="刀具代码" align="center" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.ToolNumber }}
            </template>
          </el-table-column>
          <el-table-column label="刀具名称" align="center" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.ToolName }}
            </template>
          </el-table-column>
          <el-table-column label="规格型号" align="center" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.ToolModel }}
            </template>
          </el-table-column>
          <el-table-column width="120px" label="寿命" align="center" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.ToolLife }}
            </template>
          </el-table-column>
          <el-table-column width="100px" label="换刀时间" align="center" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.ChangeTime }}
            </template>
          </el-table-column>
          <el-table-column width="100px" label="换刀人" align="center" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.PeopleName }}
            </template>
          </el-table-column>
        </el-table>
        <div class="block" style="margin: 10px 0;">
          <el-pagination
            :current-page="pageCurrent"
            :page-sizes="[10, 20, 30, 40, 100]"
            :page-size="pageSize"
            :total="total"
            :pager-count="5"
            style="display:inline-block;width:50%"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChanges"
            @current-change="handleCurrentChanges"/>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      dateRange: '',
      EquipmentNumberValue: '', // 设备编号
      EquipmentNumber: [], // 设备数组
      ToolNumberValue: '', // 刀具代码
      ToolNumber: [], // 刀具数组
      orderNumberTypeValue: '', // 订单号
      orderNumberType: [], // 订单号数组
      loading: false, // 数据加载
      total: 0, // 分页总数
      tableData: [], // 合同信息表
      pageCurrent: 1, // 分页当前页
      pageSize: 30, // 分页每页显示条数
      orderName: '', // 排序列名
      order: '' // 排序方式
    }
  },
  computed: {
    ...mapGetters([
      'id',
      'token'
    ])
  },
  updated() {
    this.$nextTick(() => {
      this.$refs['table'].doLayout()
    })
  },
  created() {
    this.searchTable()
    this.getEquiptmentNumber()
    this.getToolNumber()
  },
  methods: {
    getEquiptmentNumber() {
      this.EquipmentNumber = []
      let dateRange_check
      this.dateRange ? dateRange_check = 1 : (dateRange_check = 0, this.dateRange = '')
      var param = []
      param[0] = ['time_check', dateRange_check]
      param[1] = ['StartTime', this.dateRange[0]]
      param[2] = ['EndTime', this.dateRange[1]]
      var Data = this.CreateData('11', 'RequisitionTool_Equipment_Search', param)
      this.ExecDatabase(Data).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.EquipmentNumber.push({
            label: response.data[i].EquipmentName,
            value: response.data[i].EquipmentNumber
          })
        }
      })
    },
    getToolNumber() {
      this.ToolNumber = []
      let dateRange_check
      this.dateRange ? dateRange_check = 1 : (dateRange_check = 0, this.dateRange = '')
      var param = []
      param[0] = ['time_check', dateRange_check]
      param[1] = ['StartTime', this.dateRange[0]]
      param[2] = ['EndTime', this.dateRange[1]]
      var Data = this.CreateData('11', 'RequisitionTool_Tool_Search', param)
      this.ExecDatabase(Data).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.ToolNumber.push({
            label: response.data[i].ToolName,
            value: response.data[i].ToolNumber
          })
        }
      })
    },
    searchTable() {
      let dateRange_check, ToolNumberValue_check, EquipmentNumberValue_check
      this.dateRange ? dateRange_check = 1 : (dateRange_check = 0, this.dateRange = '')
      this.EquipmentNumberValue ? EquipmentNumberValue_check = 1 : EquipmentNumberValue_check = 0
      this.ToolNumberValue ? ToolNumberValue_check = 1 : ToolNumberValue_check = 0
      var param = []
      param[0] = ['EquipmentNumber_check', EquipmentNumberValue_check]
      param[1] = ['EquipmentNumber', this.EquipmentNumberValue]
      param[2] = ['ToolNumber_check', ToolNumberValue_check]
      param[3] = ['ToolNumber', this.ToolNumberValue]
      param[4] = ['time_check', dateRange_check]
      param[5] = ['StartTime', this.dateRange[0]]
      param[6] = ['EndTime', this.dateRange[1]]
      param[7] = ['PageCurrent', this.pageCurrent]
      param[8] = ['PageSize', this.pageSize]
      param[9] = ['PageCount', '1111', 'int', '1']
      param[10] = ['ItemCount', '1111', 'int', '1']
      var Data = this.CreateData('11', 'RequisitionTool_Search', param)
      this.ExecDatabase(Data).then(response => {
        this.tableData = response.data.result
        this.total = parseInt(response.data.output[0].ItemCount) || 0
      })
    },
    getSummaries(param) {
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计'
          return
        } else if (index === 4) {
          const values = data.map(item => Number(item[column.property]))
          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr)
              if (!isNaN(value)) {
                return prev + curr
              } else {
                return prev
              }
            }, 0)
            sums[index] = sums[index]
          } else {
            sums[index] = 'N/A'
          }
        } else if (index === 5) {
          const values = data.map(item => Number(item[column.property]))
          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr)
              if (!isNaN(value)) {
                return prev + curr
              } else {
                return prev
              }
            }, 0)
            sums[index] = sums[index].toFixed(2)
          } else {
            sums[index] = 'N/A'
          }
        } else if (index === 8) {
          const values = data.map(item => Number(item[column.property]))
          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr)
              if (!isNaN(value)) {
                return prev + curr
              } else {
                return prev
              }
            }, 0)
            sums[index] = sums[index]
          } else {
            sums[index] = 'N/A'
          }
        }
      })
      return sums
    },
    sortChange(column) {
      if (column.prop === '采购件') {
        this.orderName = 1
      } else if (column.prop === '交货日期') {
        this.orderName = 2
      } else if (column.prop === '任务下达 ') {
        this.orderName = 3
      } else if (column.prop === '采购日期 ') {
        this.orderName = 4
      } else {
        this.orderName = 0
      }
      if (column.order === 'ascending') {
        this.order = 1
      } else if (column.order === 'descending') { this.order = 2 } else {
        this.order = ''
      }
      this.searchTable()
    },
    handleSizeChanges(val) {
      this.pageCurrent = 1
      this.pageSize = val
      this.searchTable()
    },
    handleCurrentChanges(val) {
      this.pageCurrent = val
      this.searchTable()
    }
  }
}
</script>

