<template>
  <div class="app-container">
    <el-card class="topCard">
      <el-row>
        <span style="margin-left: 10px">
          时间:
        </span>
        <el-date-picker
          v-model="timeFrame"
          :picker-options="pickerOptions"
          size="small"
          type="datetimerange"
          value-format="yyyy-MM-dd HH:mm:ss"
          format="yyyy-MM-dd HH:mm:ss"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          align="right"/>
        <span style="margin-left: 10px">
          工位号:
        </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" plain size="small" icon="el-icon-search" style="margin-left: 10px" @click="searchTable()">查询</el-button>
        <el-button type="info" size="small" icon="el-icon-download" @click="exportExcel()">Excel</el-button>
        <span v-show="isExporting" style="color: #16a188">正在导出....</span>
      </el-row>
      <el-row>
        <el-col :span="13">
          <div>
            <el-table
              v-loading="loading"
              ref="tableDataBeat"
              :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"
              @sort-change="sortChange">
              <el-table-column type="index" align="center" label="序号" width="70px"/>
              <el-table-column width="90px" label="工位号" prop="StationName" align="center" sortable>
                <template slot-scope="scope">
                  {{ scope.row.StationName }}
                </template>
              </el-table-column>
              <el-table-column width="160px" label="总平均节拍(s)" prop="AB" align="center" sortable>
                <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-col>
        <el-col :span="11">
          <div style="margin-top: 2px;">
            <div id="dashboardOne" style="height: 730px;width: 800px;border: 1px solid #9f9f9f"/>
          </div>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>

<script>
// import echarts from 'echarts'
export default {
  data() {
    return {
      isExporting: false,
      dataZoomStart: 0,
      dataZoomEnd: 50,
      // chartDom1: null,
      myChart1X: [],
      myChart1Y: [],
      // 时间范围
      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 // 分页总数
    }
  },
  activated() {
    // this.initCharDom()
  },
  created() {
    this.getStation()
  },
  methods: {
    sortChange({ column, prop, order }) {
      this.myChart1X = []
      this.myChart1Y = []
      for (let i = 0; i < this.$refs.tableDataBeat.tableData.length; i++) {
        this.myChart1X.push(this.$refs.tableDataBeat.tableData[i].StationName)
        this.myChart1Y.push(this.$refs.tableDataBeat.tableData[i].AB)
      }
      this.drawDashboardOne()
    },
    initCharDom() {
      this.chartDom1 = this.$echarts.init(document.getElementById('dashboardOne'))
    },
    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] + "'"
        }
      }
      if (this.station.length <= 25 && this.station.length !== 0) { this.dataZoomEnd = 100 } else { this.dataZoomEnd = 50 }
    },
    getStation() {
      this.stationOptions = []
      var param = []
      var Data = this.CreateData('11', 'MES_计划BOM_工位与名称_查询', param)
      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() {
      this.loading = true
      this.myChart1X = []
      this.myChart1Y = []
      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_StationBeat_TableHeaderQuery', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data.length > 0) {
          this.tableHeader = response.data
          // 查询表的信息
          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_StationBeat_Query', param1)
          this.ExecDatabase(Data1).then(response => {
            this.tableData = response.data
            for (let i = 0; i < this.tableData.length; i++) {
              this.myChart1X.push(this.tableData[i].StationName)
              this.myChart1Y.push(this.tableData[i].AB)
            }
            if (this.tableData.length <= 25 && this.tableData.length !== 0) { this.dataZoomEnd = 100 } else { this.dataZoomEnd = 30 }
            this.total = response.data.length
            this.loading = false
            this.drawDashboardOne()
          })
        } else {
          this.$message({
            message: '选择的该时间段没有数据！',
            type: 'error'
          })
          this.loading = false
          this.drawDashboardOne()
        }
      })
    },
    drawDashboardOne() {
      const chartDom1 = this.$echarts.init(document.getElementById('dashboardOne'))
      chartDom1.setOption({
        title: {
          text: '工位平均节拍统计',
          left: 'center'
        },
        grid: [
          {
            left: '8%',
            top: '10%',
            right: '8%',
            bottom: '10%'
          }
        ],
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        dataZoom: [
          {
            type: 'slider', // slider表示有滑动块的，inside表示内置的
            show: true,
            xAxisIndex: [0],
            start: this.dataZoomStart,
            end: this.dataZoomEnd
          }
        ],
        xAxis: {
          name: '工位号',
          type: 'category',
          data: this.myChart1X,
          axisLine: {
            show: true,
            lineStyle: {
              color: '#000'
            }
          },
          splitLine: {
            show: false
          }
        },
        yAxis: {
          type: 'value',
          name: '平均节拍',
          axisLine: {
            show: true,
            lineStyle: {
              color: '#000'
            }
          },
          splitLine: {
            show: false
          }
        },
        series: [
          {
            name: '平均节拍',
            data: this.myChart1Y,
            type: 'bar',
            itemStyle: '#209de2',
            label: {
              show: true,
              color: '#000',
              formatter: '{c}',
              position: 'top'
            }
          }
        ]
      })
    },
    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_StationBeat_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_StationBeat_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>
