<template>
  <div>
    <el-card>
      <span class="show-text">工位:</span>
      <el-select
        v-model="OpName"
        clearable
        filterable
        placeholder="请选择工位号"
        size="small"
        style="width: 200px">
        <el-option
          v-for="item in OpNameList"
          :key="item.value"
          :label="item.label"
          :value="item.value"
          size="mini"/>
      </el-select>
      <span class="show-text">订单号:</span>
      <el-input v-model="Order" clearable placeholder="请输入订单号" style="width: 200px;margin-right: 15px"/>
      <span class="show-text">机型:</span>
      <el-input v-model="EngineType" clearable placeholder="请输入机型" style="width: 200px;margin-right: 15px"/>
      <!--      <span class="show-text">数据名称:</span>-->
      <!--      <el-input v-model="TagName" clearable style="width: 280px;margin-right: 26px" placeholder="请输入数据名称"/>-->
      <span class="show-text">采集时间:</span>
      <el-date-picker
        v-model="collectionTime"
        :clearable="false"
        end-placeholder="结束时间"
        format="yyyy-MM-dd HH:mm:ss"
        popper-class="virtual-cell-time-picker"
        range-separator="-"
        start-placeholder="开始时间"
        style="width: 380px;margin-right: 16px;vertical-align: top"
        type="datetimerange"
        value-format="yyyy-MM-dd HH:mm:ss"/>
      <el-button :disabled="loading" icon="el-icon-search" plain type="primary" @click="searchTable()">查询</el-button>
      <div style="margin-top: 10px">
        <el-table
          v-loading="loading"
          :cell-style="set_cell_style"
          :data="tableData"
          border
          element-loading-background="rgba(0, 0, 0, 0.2)"
          element-loading-spinner="el-icon-loading"
          element-loading-text="数据加载中"
          height="707"
          highlight-current-row
          size="mini"
          stripe
          style="width: 1200px"
          tooltip-effect="dark">
          <el-table-column align="center" label=" " type="index" width="60"/>
          <el-table-column align="center" label="工位号" width="150">
            <template slot-scope="scope">
              {{ scope.row.工位号 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="订单号" width="150">
            <template slot-scope="scope">
              {{ scope.row.订单号 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="型号" width="150">
            <template slot-scope="scope">
              {{ scope.row.EngineType }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="是否合格">
            <template slot-scope="scope">
              {{ scope.row.IsOK == 1 ? '合格' : '不合格' }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="合格数">
            <template slot-scope="scope">
              {{ scope.row.IsOK == 1 ? '1' : '0' }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="不合格数">
            <template slot-scope="scope">
              {{ scope.row.IsOK != 1 ? '1' : '0' }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="采集时间" width="200">
            <template slot-scope="scope">
              {{ scope.row.操作时间 }}
            </template>
          </el-table-column>
        </el-table>
        <div class="block" style="margin-top: 10px;">
          <el-pagination
            v-if="!loading"
            :current-page="pageList"
            :page-size="pageSize"
            :page-sizes="[50, 100, 200, 300]"
            :total="total"
            background
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"/>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // pickerOptions: {
      //   disabledDate(time) {
      //     return time.getTime() > Date.now()
      //   }
      // },
      TagName: '',
      OpName: '',
      Order: '',
      EngineType: '',
      OpNameList: [],
      collectionTime: [],
      loading: false,
      tableData: [],
      total: 0, // 总条数
      pageSize: 50, // 每页显示条数
      pageList: 1 // 后台获取页
    }
  },
  created() {
    this.getOpName()
    var now = new Date()
    var start = new Date(now.getFullYear(), now.getMonth() - 1, now.getDate(), 0, 0, 0)
    var end = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 23, 59, 59)
    this.collectionTime = [start, end]
    this.searchTable()
  },
  methods: {
    // 查询工位号
    getOpName() {
      var param = []
      var Data = this.CreateData('11', '工位与名称_查询', param)
      this.ExecDatabase(Data).then(response => {
        this.OpNameList = []
        for (const item of response.data) {
          this.OpNameList.push({ label: item.工位号, value: item.工位号 })
        }
      })
    },
    // 查询
    searchTable() {
      if (this.collectionTime.length === 0) {
        this.$message.error('请选择需要查询的时间段！')
        return
      }
      this.loading = true
      this.tableData = []
      var param = []
      param[0] = ['工位号', this.OpName]
      param[1] = ['开始时间', this.collectionTime[0]]
      param[2] = ['结束时间', this.collectionTime[1]]
      param[3] = ['PageCurrent', this.pageList]
      param[4] = ['PageSize', this.pageSize]
      param[5] = ['PageCount', '1111', 'int', '1']
      param[6] = ['ItemCount', '1111', 'int', '1']
      param[7] = ['订单号', this.Order]
      param[8] = ['机型', this.EngineType]
      var Data = this.CreateData('11', '测量数据合格_索引_查询', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data.length !== 0) {
          this.tableData = response.data.result
        }
        this.total = parseInt(response.data.output[0].ItemCount)
        this.loading = false
      })
    },
    // 导出数据
    searchTableALL() {
      if (this.collectionTime.length === 0) {
        this.$message.error('请选择需要查询的时间段！')
        return
      }
      var param = []
      if (this.tableData.length === 0) {
        this.$message.warning('请查询要导出的数据')
        return
      }
      param[0] = ['TagName', this.TagName]
      param[1] = ['开始时间', this.collectionTime[0]]
      param[2] = ['结束时间', this.collectionTime[1]]
      var Data = this.CreateData('2003', '测量数据_查询_导出', param)
      const blob = 'data:image/gif;base64,R0lGODlhCQACAIAAAMzMzP///yH5BAEAAAEALAAAAAAJAAIAAAIEjI9pUAA7'
      this.exportExcel_NPOI(Data, blob)
    },
    // 设置单元格颜色
    set_cell_style({ row, column, rowIndex, columnIndex }) {
      const Value = parseFloat(row.Duration)
      const LimitUp1 = parseFloat(row.LimitUp1)
      const LimitDown1 = parseFloat(row.LimitDown1)
      const LimitUp2 = parseFloat(row.LimitUp2)
      const LimitDown2 = parseFloat(row.LimitDown2)
      if (Value.toString() === 'NaN' || LimitUp1.toString() === 'NaN' || LimitDown1.toString() === 'NaN' || LimitUp2.toString() === 'NaN' || LimitDown2.toString() === 'NaN') {
        return ''
      } else {
        if (Value > LimitUp2) {
          return {
            'background': '#fcd4d4'
          }
        }
        if (Value < LimitDown2) {
          return {
            'background': '#fcd4d4'
          }
        }
        if (Value >= LimitDown2 && Value < LimitDown1) {
          return {
            'background': '#fcefb9'
          }
        }
        if (Value > LimitUp1 && Value <= LimitUp2) {
          return {
            'background': '#fcefb9'
          }
        }
      }
    },
    // 分页
    handleSizeChange(val) {
      this.pageList = 1
      this.pageSize = val
      this.searchTable()
    },
    handleCurrentChange(val) {
      this.pageList = val
      this.searchTable()
    }
  }
}
</script>
