<template>
  <div class="app-container">
    <el-card class="topCard">
      <el-row>
        <span>
          时间:
        </span>
        <el-date-picker
          v-model="timeFrame"
          :picker-options="pickerOptions"
          type="datetimerange"
          size="small"
          value-format="yyyy-MM-dd HH:mm:ss"
          format="yyyy-MM-dd HH:mm:ss"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          align="right"/>
        <span class="promptText">
          工位号:
        </span>
        <el-select v-model="station" size="small" multiple collapse-tags placeholder="请选择" @change="select1()">
          <el-option
            v-for="item in stationOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-button type="primary" icon="el-icon-search" size="small" plain @click="searchTable()">查询</el-button>
        <el-button style="margin-left: 0" size="small" type="info" icon="el-icon-download" @click="exportExcel()">Excel</el-button>
        <span v-show="isExporting" style="color: #16a188">正在导出....</span>
      </el-row>
      <el-row>
        <div>
          <el-table
            v-loading="loading"
            :data="tableData"
            :header-cell-style="{background:'#eef1f6',color:'#606266'}"
            element-loading-text="数据加载中"
            element-loading-spinner="el-icon-loading"
            element-loading-background="rgba(0, 0, 0, 0.2)"
            highlight-current-row
            border
            stripe
            tooltip-effect="dark"
            height="730px">
            <el-table-column type="index" align="center" label="序号" width="60px"/>
            <el-table-column width="80px" label="工位" align="center">
              <template slot-scope="scope">
                {{ scope.row.StationName }}
              </template>
            </el-table-column>
            <el-table-column width="90px" label="OEE(%)" align="center">
              <template slot-scope="scope">
                {{ scope.row.AB }}
              </template>
            </el-table-column>
            <el-table-column
              v-for="(item, index) in tableHeader"
              :key="index"
              :label="item.时间"
              min-width="111px"
              align="center">
              <template slot-scope="scope">
                {{ scope.row[item.时间] }}
              </template>
            </el-table-column>
          </el-table>
        <!--<div class="block" style="margin-top: 10px;">
          <el-pagination
            :current-page="pageCurrent"
            :page-sizes="[10, 20, 30, 40, 100]"
            :page-size="pageSize"
            :total="total"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChanges"
            @current-change="handleCurrentChanges"/>
        </div>-->
        </div>
      </el-row>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isExporting: false,
      // 时间范围
      timeFrame: '',
      tableHeader: [],
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      // 两个复选框
      alarmNameOptions: [],
      stationOptions: [],
      station: [],
      stationString: '',
      alarmCode: [],
      alarmCodeString: '',
      // 表格
      tableData: [],
      loading: false,
      // 分页
      pageCurrent: 1, // 分页当前页
      pageSize: 10, // 分页每页显示条数
      total: 0 // 分页总数
    }
  },
  created() {
    this.getStation()
  },
  methods: {
    select1() {
      this.stationString = ''
      if (this.station.length > 0) {
        this.stationString = "'" + this.station[0] + "'"
        for (let i = 1; i < this.station.length; i++) {
          this.stationString = this.stationString + ",'" + this.station[i] + "'"
        }
      }
    },
    getStation() {
      this.stationOptions = []
      const Data = this.CreateData('11', 'MES_计划BOM_工位与名称_查询')
      this.ExecDatabase(Data).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.stationOptions.push({
            label: response.data[i].工位号,
            value: response.data[i].工位号
          })
        }
      })
    },
    searchTable() {
      let timeFrame_check, Station_check
      this.timeFrame ? timeFrame_check = 1 : timeFrame_check = 0
      this.stationString ? Station_check = 1 : Station_check = 0
      if (timeFrame_check === 0) {
        this.$message({
          message: '请选择开始时间与结束时间！',
          type: 'error'
        })
        return
      }
      this.tableData = []
      const param = []
      param[0] = ['timeFrame_check', timeFrame_check]
      param[1] = ['QueryStartTime', this.timeFrame[0]]
      param[2] = ['QueryEndTime', this.timeFrame[1]]
      param[3] = ['Station_check', Station_check]
      param[4] = ['Station', this.stationString]
      const Data = this.CreateData('11', 'SP_His_StationOEE_TableHeaderQuery', param)
      this.ExecDatabase(Data).then(response => {
        console.log('表头', response)
        if (response.data.length > 0) {
          this.tableHeader = response.data
          // 查询表的信息
          this.loading = true
          const param1 = []
          param1[0] = ['timeFrame_check', timeFrame_check]
          param1[1] = ['QueryStartTime', this.timeFrame[0]]
          param1[2] = ['QueryEndTime', this.timeFrame[1]]
          param1[3] = ['Station_check', Station_check]
          param1[4] = ['Station', this.stationString]
          const Data1 = this.CreateData('11', 'SP_His_StationOEE_Query', param1)
          this.ExecDatabase(Data1).then(response => {
            console.log(response)
            this.tableData = response.data
            this.total = response.data.length
            this.loading = false
          })
        } else {
          this.$message({
            message: '选择的该时间段没有数据！',
            type: 'error'
          })
        }
      })
    },
    exportExcel() {
      if (this.tableData.length !== 0) {
        let timeFrame_check, Station_check
        this.timeFrame ? timeFrame_check = 1 : timeFrame_check = 0
        this.stationString ? Station_check = 1 : Station_check = 0
        if (timeFrame_check === 0) {
          this.$message({
            message: '请选择开始时间与结束时间！',
            type: 'error'
          })
          return
        }
        const param = []
        param[0] = ['timeFrame_check', timeFrame_check]
        param[1] = ['QueryStartTime', this.timeFrame[0]]
        param[2] = ['QueryEndTime', this.timeFrame[1]]
        param[3] = ['Station_check', Station_check]
        param[4] = ['Station', this.stationString]
        const Data = this.CreateData('11', 'SP_His_StationOEE_TableHeaderQuery', param)
        this.ExecDatabase(Data).then(response => {
          if (response.data.length > 0) {
            this.isExporting = true
            const param1 = []
            param1[0] = ['timeFrame_check', timeFrame_check]
            param1[1] = ['QueryStartTime', this.timeFrame[0]]
            param1[2] = ['QueryEndTime', this.timeFrame[1]]
            param1[3] = ['Station_check', Station_check]
            param1[4] = ['Station', this.stationString]
            const Data1 = this.CreateData('2001', 'SP_His_StationOEE_ExportExcel', param1)
            this.exportExcel_NPOI(Data1).then(res => {
              this.isExporting = false
            })
          } else {
            this.$message({
              message: '选择的该时间段没有数据！',
              type: 'error'
            })
          }
        })
      } else {
        this.$message.warning('暂无数据')
      }
    },
    handleSizeChanges(val) {
      this.pageCurrent = 1
      this.pageSize = val
      this.searchTable()
    },
    handleCurrentChanges(val) {
      this.pageCurrent = val
      this.searchTable()
    }
  }
}
</script>

<style scoped>

</style>
