<template>
  <div class="app-container">
    <el-card class="topCard">
      <el-row>
        <span>
          时间:
        </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>
          工位号:
        </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>
        <span>
          报警类型:
        </span>
        <el-select v-model="alarmCode" size="small" multiple collapse-tags placeholder="请选择" @change="select2()">
          <el-option
            v-for="item in alarmNameOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-button style="margin-left: 10px" type="primary" size="small" icon="el-icon-search" plain @click="searchTable()">查询</el-button>
        <el-button style="margin-left: 10px" 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>
        <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
            size="mini"
            tooltip-effect="dark"
            style="width: 1580px;"
            height="380px">
            <el-table-column type="index" align="center" label="序号" width="60px"/>
            <el-table-column width="160px" label="工位" align="center">
              <template slot-scope="scope">
                {{ scope.row.StationName }}
              </template>
            </el-table-column>
            <el-table-column label="名称" align="center">
              <template slot-scope="scope">
                {{ scope.row.StationNameDesignation }}
              </template>
            </el-table-column>
            <el-table-column width="190px" label="报警内容" align="center">
              <template slot-scope="scope">
                {{ scope.row.AlarmName }}
              </template>
            </el-table-column>
            <el-table-column width="190px" label="报警时间" align="center">
              <template slot-scope="scope">
                {{ scope.row.AlarmTime }}
              </template>
            </el-table-column>
            <el-table-column width="190px" label="认可时间" align="center">
              <template slot-scope="scope">
                {{ scope.row.SureTime }}
              </template>
            </el-table-column>
            <el-table-column width="190px" label="报警时长" align="center">
              <template slot-scope="scope">
                {{ scope.row.AlarmDuration }}
              </template>
            </el-table-column>
            <el-table-column width="190px" label="处理情况" align="center">
              <template slot-scope="scope">
                {{ scope.row.Treatment }}
              </template>
            </el-table-column>
            <el-table-column width="170px" label="处理人员" align="center">
              <template slot-scope="scope">
                {{ scope.row.Personnel_Name }}
              </template>
            </el-table-column>
          </el-table>
          <div class="block" style="margin-top: 15px;">
            <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 id="myChart5" style="width: 750px;height: 800px;text-align: center;float: left"/>
          <div id="myChart4" style="width: 750px;height: 400px;text-align: center;float: left;margin-left: 20px"/>
          <div id="myChart3" style="width: 750px;height: 400px;text-align: center;float: left;margin-left: 20px"/>
        </div>
      </el-row>
    </el-card>
  </div>
</template>

<script>
import * as echarts from 'echarts'
export default {
  data() {
    return {
      isExporting: false,
      // 时间范围
      timeFrame: '',
      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: '',
      AndonName: [],
      Andon: [],
      workStationName: [],
      workStation: [],
      color: ['red', 'blue', 'yellow'],
      // 表格
      tableData: [],
      loading: false,
      // 分页
      pageCurrent: 1, // 分页当前页
      pageSize: 100, // 分页每页显示条数
      total: 0 // 分页总数
    }
  },
  created() {
    this.getAlarmName()
    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] + "'"
        }
      }
    },
    select2() {
      this.alarmCodeString = ''
      if (this.alarmCode.length > 0) {
        this.alarmCodeString = "'" + this.alarmCode[0] + "'"
        for (let i = 1; i < this.alarmCode.length; i++) {
          this.alarmCodeString = this.alarmCodeString + ",'" + this.alarmCode[i] + "'"
        }
      }
    },
    getAlarmName() {
      this.alarmNameOptions = []
      const Data = this.CreateData('11', 'SP_AndonNameType_Query')
      this.ExecDatabase(Data).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.alarmNameOptions.push({
            label: response.data[i].AlarmName,
            value: response.data[i].AlarmCode
          })
        }
      })
    },
    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, AlarmCode_check
      this.timeFrame ? timeFrame_check = 1 : timeFrame_check = 0
      if (timeFrame_check === 0) {
        this.$message({
          message: '请选择开始时间与结束时间！',
          type: 'error'
        })
        return
      }
      this.stationString ? Station_check = 1 : Station_check = 0
      this.alarmCodeString ? AlarmCode_check = 1 : AlarmCode_check = 0
      this.loading = true
      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]
      param[5] = ['AlarmCode_check', AlarmCode_check]
      param[6] = ['AlarmCode', this.alarmCodeString]
      param[7] = ['PageCurrent', this.pageCurrent]
      param[8] = ['PageSize', this.pageSize]
      param[9] = ['PageCount', '1111', 'int', '1']
      param[10] = ['ItemCount', '1111', 'int', '1']
      const Data = this.CreateData('11', 'SP_His_Andon_Query', param)
      this.ExecDatabase(Data).then(response => {
        this.tableData = response.data.result
        this.total = parseInt(response.data.output[0].ItemCount)
        this.loading = false
      })
      this.searchTable1()
      this.searchTable2()
    },
    searchTable1() {
      let timeFrame_check, Station_check, AlarmCode_check
      this.timeFrame ? timeFrame_check = 1 : timeFrame_check = 0
      this.stationString ? Station_check = 1 : Station_check = 0
      this.alarmCodeString ? AlarmCode_check = 1 : AlarmCode_check = 0
      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]
      param[5] = ['AlarmCode_check', AlarmCode_check]
      param[6] = ['AlarmCode', this.alarmCodeString]
      const Data = this.CreateData('11', 'SP_His_AndonChart_Query', param)
      this.ExecDatabase(Data).then(response => {
        this.Andon = []
        this.AndonName = []
        if (response.data.length > 0) {
          for (let i = 0; i < response.data.length; i++) {
            this.AndonName.push(response.data[i].AlarmName)
            this.Andon.push({ value: response.data[i].number, name: response.data[i].AlarmName })
          }
        }
      }).then(() => {
        this.drawLine2()
        this.drawLine4()
      })
    },
    searchTable2() {
      let timeFrame_check, Station_check, AlarmCode_check
      this.timeFrame ? timeFrame_check = 1 : timeFrame_check = 0
      this.stationString ? Station_check = 1 : Station_check = 0
      this.alarmCodeString ? AlarmCode_check = 1 : AlarmCode_check = 0
      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]
      param[5] = ['AlarmCode_check', AlarmCode_check]
      param[6] = ['AlarmCode', this.alarmCodeString]
      const Data = this.CreateData('11', 'SP_His_AndonStationChart_Query', param)
      this.ExecDatabase(Data).then(response => {
        this.workStationName = []
        this.workStation = []
        if (response.data.length > 0) {
          for (let i = 0; i < response.data.length; i++) {
            this.workStationName.push(response.data[i].StationName)
            this.workStation.push({ value: response.data[i].number, name: response.data[i].StationName })
          }
        }
      }).then(() => {
        this.drawLine3()
      })
    },
    drawLine2() {
      this.myChart4 = echarts.init(document.getElementById('myChart4'))
      this.myChart4.clear()
      this.myChart4.setOption({
        title: {
          text: '分类统计'
        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b}: {c} ({d}%)'
        },
        legend: {
          orient: 'vertical',
          top: 1,
          right: 10,
          data: this.AndonName
        },
        series: [
          {
            name: '类型',
            type: 'pie',
            radius: '50%',
            data: this.Andon,
            itemStyle: {
              normal: {
                color: function(params) {
                  const colorList = ['rgb(84,112,198)', 'rgb(115,192,222)', 'rgb(97,160,168)']
                  return colorList[params.dataIndex]
                },
                label: {
                  show: true,
                  position: 'top'
                }
              }
            }
          }
        ]
      })
    },
    drawLine3() {
      this.myChart5 = this.$echarts.init(document.getElementById('myChart5'))
      this.myChart5.clear()
      this.myChart5.setOption({
        title: {
          text: '工位统计'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        legend: {},
        // grid: {
        //   left: '3%',
        //   right: '4%',
        //   bottom: '3%',
        //   containLabel: true
        // },
        xAxis: {
          type: 'value',
          boundaryGap: [0, 0.01]
        },
        yAxis: {
          type: 'category',
          data: this.workStationName
        },
        // 底部Zoom
        dataZoom: [
          {
            start: 80,
            end: 100,
            show: true,
            yAxisIndex: 0,
            filterMode: 'empty',
            width: 30,
            height: '83%',
            showDataShadow: false,
            left: '93%'
          }
        ],
        series: [
          {
            type: 'bar',
            data: this.workStation,
            itemStyle: {
              normal: {
                label: {
                  show: true,
                  position: 'right'
                },
                color: 'rgb(22,161,138)'
              }
            }
          }
        ]
      })
    },
    drawLine4() {
      this.myChart3 = this.$echarts.init(document.getElementById('myChart3'))
      this.myChart3.clear()
      this.myChart3.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        legend: {},
        // grid: {
        //   left: '3%',
        //   right: '4%',
        //   bottom: '3%',
        //   containLabel: true
        // },
        xAxis: {
          type: 'category',
          data: this.AndonName
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            data: this.Andon,
            itemStyle: {
              normal: {
                color: function(params) {
                  const colorList = ['rgb(84,112,198)', 'rgb(115,192,222)', 'rgb(97,160,168)']
                  return colorList[params.dataIndex]
                },
                label: {
                  show: true,
                  position: 'top'
                }
              }
            },
            type: 'bar'
          }
        ]
      })
    },
    exportExcel() {
      if (this.tableData.length !== 0) {
        let timeFrame_check, Station_check, AlarmCode_check
        this.timeFrame ? timeFrame_check = 1 : timeFrame_check = 0
        if (timeFrame_check === 0) {
          this.$message({
            message: '请选择开始时间与结束时间！',
            type: 'error'
          })
          return
        }
        this.stationString ? Station_check = 1 : Station_check = 0
        this.alarmCodeString ? AlarmCode_check = 1 : AlarmCode_check = 0
        this.isExporting = true
        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]
        param[5] = ['AlarmCode_check', AlarmCode_check]
        param[6] = ['AlarmCode', this.alarmCodeString]
        const Data = this.CreateData('2001', 'SP_His_AndonInformation_ExportExcel', param)
        this.exportExcel_NPOI(Data).then(res => {
          this.isExporting = false
        })
      } else {
        this.$message.warning('暂无数据')
      }
    },
    handleSizeChanges(val) {
      this.pageCurrent = 1
      this.pageSize = val
      this.searchTable()
    },
    handleCurrentChanges(val) {
      this.pageCurrent = val
      this.searchTable()
    }
  }
}
</script>
