<template>
  <div class="app-container">
    <el-card>
      <!--      <span>工位号:</span>-->
      <el-select v-model="MachineValue" filterable size="small" style="width:160px;margin:0px 10px" placeholder="工位号">
        <el-option
          v-for="item in Machine"
          :key="item.value"
          :label="item.label"
          :value="item.value">
          <div style="float: left">{{ item.label }}</div>
          <div style="float: right; color: #8492a6; font-size: 13px">{{ item.value2 }}</div>
        </el-option>
      </el-select>
      <!--      <span>日期:</span>-->
      <el-date-picker
        v-model="Time"
        value-format="yyyy-MM-dd"
        size="small"
        style="width: 160px"
        type="date"
        placeholder="选择日期"/>
      <el-button type="success" size="small" icon="el-icon-search" style="margin:0px 10px" @click="pageSize=10; pageCurrent= 1;searchData()">查询</el-button>
    </el-card>
    <el-card>
      <el-table
        :data="tableDataA"
        border
        style="width: 100%;max-height: 450px"
        stripe
        height="450px"
        size="small">
        <el-table-column
          type="index"
          label="序号"
          align="center"
          width="50"/>
        <el-table-column label="工位号" align="center">
          <template slot-scope="scope">
            {{ scope.row.工位号 }}
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center">
          <template slot-scope="scope">
            {{ scope.row.状态 }}
          </template>
        </el-table-column>
        <el-table-column label="电流" align="center">
          <template slot-scope="scope">
            {{ scope.row.电流 }}
          </template>
        </el-table-column>
        <el-table-column label="开始时间" align="center">
          <template slot-scope="scope">
            {{ scope.row.开始时间 }}
          </template>
        </el-table-column>
        <el-table-column label="结束时间" align="center">
          <template slot-scope="scope">
            {{ scope.row.结束时间 }}
          </template>
        </el-table-column>
        <el-table-column label="持续时间" align="center">
          <template slot-scope="scope">
            {{ scope.row.持续时间 }}
          </template>
        </el-table-column>
        <el-table-column label="日期" align="center">
          <template slot-scope="scope">
            {{ scope.row.日期.split(' ')[0] }}
          </template>
        </el-table-column>
      </el-table>
      <!--      <div class="block">-->
      <!--        <el-pagination-->
      <!--          :current-page="pageCurrent"-->
      <!--          :page-sizes="[10, 20, 30, 40]"-->
      <!--          :page-size="pageSize"-->
      <!--          :total="total"-->
      <!--          layout="total, sizes, prev, pager, next, jumper"-->
      <!--          @size-change="handleSizeChange"-->
      <!--          @current-change="handleCurrentChange"/>-->
      <!--      </div>-->
    </el-card>
    <el-card style="width: 49.8%; float: left;margin-right: 5px">
      <div id="myChart1" style="width: 1000px; height: 570px; margin: auto"/>
    </el-card>
    <el-card style="width: 49.7%;margin-left: 5px">
      <div id="myChart2" style="width: 1000px; height: 570px; margin: auto"/>
    </el-card>
    <el-card>
      <span>工位号:</span>
      <el-select v-model="MachineValue1" filterable size="small" style="width:160px;margin-bottom:10px" placeholder="工位号">
        <el-option
          v-for="item in Machine"
          :key="item.value"
          :label="item.label"
          :value="item.value">
          <div style="float: left">{{ item.label }}</div>
          <div style="float: right; color: #8492a6; font-size: 13px">{{ item.value2 }}</div>
        </el-option>
      </el-select>
      <span style="margin-left: 10px">时间段:</span>
      <el-date-picker
        v-model="date1"
        style="width:160px;margin:10px;"
        size="small"
        type="date"
        value-format="yyyy-MM-dd"
        format="yyyy-MM-dd"
        placeholder="选择日期"/>
      <span>~</span>
      <el-date-picker
        v-model="date2"
        style="width:160px;margin:10px;"
        size="small"
        type="date"
        value-format="yyyy-MM-dd"
        format="yyyy-MM-dd"
        placeholder="选择日期"/>
      <el-button type="success" size="small" icon="el-icon-search" style="margin-left: 15px" @click="searchData1()">查询</el-button>
    </el-card>
    <el-card>
      <div id="myChart" style="width: 1000px; height: 570px; margin: auto"/>
    </el-card>
    <el-card>
      <span>工位号:</span>
      <el-select v-model="MachineValue2" filterable size="small" style="width:160px;margin-bottom:10px" placeholder="工位号">
        <el-option
          v-for="item in Machine"
          :key="item.value"
          :label="item.label"
          :value="item.value">
          <div style="float: left">{{ item.label }}</div>
          <div style="float: right; color: #8492a6; font-size: 13px">{{ item.value2 }}</div>
        </el-option>
      </el-select>
      <span style="margin-left: 10px">时间段:</span>
      <el-date-picker
        v-model="date3"
        style="width:160px;margin:10px;"
        size="small"
        type="date"
        value-format="yyyy-MM-dd"
        format="yyyy-MM-dd"
        placeholder="选择日期"/>
      <span>~</span>
      <el-date-picker
        v-model="date4"
        style="width:160px;margin:10px;"
        size="small"
        type="date"
        value-format="yyyy-MM-dd"
        format="yyyy-MM-dd"
        placeholder="选择日期"/>
      <el-button type="success" size="small" icon="el-icon-search" style="margin-left: 15px" @click="searchData2()">查询</el-button>
    </el-card>
    <el-card>
      <div id="myChart3" style="width: 100%; height: 520px; margin: auto"/>
    </el-card>
  </div>
</template>

<script>import { getData, getData1, getData2, getData3, getMachine, searchState } from '@/api/ManufacturingCenter/EquipmentConditionAnalysis'

export default {
  data() {
    return {
      tableDataA: [],
      MachineValue: '',
      MachineValue1: '',
      MachineValue2: '',
      Machine: [],
      yAxisValue: [],
      colors: [],
      state: [],
      Time: '',
      date1: '',
      date2: '',
      date3: '',
      date4: '',
      time1: '',
      time2: '',
      time3: '',
      time4: '',
      time11: '',
      time22: '',
      time33: '',
      time44: '',
      // pageCurrent: 1,
      // pageSize: 10,
      // total: 0,
      table: []
    }
  },
  mounted() {
    this.drawLine()
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.MachineValue = ''
      this.Machine = []
      getMachine().then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.Machine.push({
            label: response.data[i].终端编号,
            value: response.data[i].终端编号,
            value2: response.data[i].姓名
          })
        }
      })
    },
    searchData() {
      getData(this.MachineValue, this.Time).then(response => {
        this.tableDataA = response.data
        // this.total = response.data.total
        this.time1 = response.data[0].运行持续时间
        this.time2 = response.data[0].等待持续时间
        this.time3 = response.data[0].停机持续时间
        this.time4 = response.data[0].报警持续时间
      }).then(() => {
        this.drawLine1()
      })
      getData1(this.Time).then(response => {
        this.time11 = response.data[0].运行持续时间
        this.time22 = response.data[0].等待持续时间
        this.time33 = response.data[0].停机持续时间
        this.time44 = response.data[0].报警持续时间
      }).then(() => {
        this.drawLine2()
      })
    },
    searchData1() {
      if (this.date1 === null || this.date2 === null) {
        this.$message.warning('请选择开始时间或结束时间')
      } else {
        getData2(this.MachineValue1, this.date1, this.date2).then(response => {
          this.table = response.data
        }).then(() => {
          this.drawLine()
        })
      }
    },
    searchData2() {
      if (this.date3 === null || this.date4 === null) {
        this.$message.warning('请选择开始时间或结束时间')
      } else {
        searchState().then(response => {
          for (let i = 0; i < response.data.length; i++) {
            this.colors.push(response.data[i].颜色)
            this.state.push(response.data[i].状态)
          }
        })
        getData3(this.MachineValue2, this.date3, this.date4).then(response => {
          this.table1 = response.data
          console.log(this.table1)
        }).then(() => {
          this.drawLine3()
        })
      }
    },
    //  绘制图形
    drawLine() {
      const myChart = this.$echarts.init(document.getElementById('myChart'))
      const xData = []
      const lineData = []
      for (let i = 0; i < this.table.length; i++) {
        xData.push(this.table[i].日期.split(' ')[0])
        lineData.push(this.table[i].利用率.toFixed(2))
      }
      myChart.clear()
      myChart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            crossStyle: {
              color: '#999'
            }
          }
        },
        legend: {
          data: ['利用率']
        },
        xAxis: [
          {
            type: 'category',
            name: '日期',
            data: xData,
            axisPointer: {
              type: 'shadow'
            }
          }
        ],
        yAxis: [
          {
            type: 'value',
            name: '利用率'
          }
        ],
        series: [
          {
            name: '利用率',
            type: 'line',
            label: {
              normal: {
                show: true,
                position: 'top'
              }
            },
            data: lineData
          }
        ]
      })
    },
    drawLine1() {
      const myChart = this.$echarts.init(document.getElementById('myChart1'))
      const xData = ['运行', '等待', '停机', '报警']
      const lineData = [{ name: '运行', value: this.time1 }, { name: '等待', value: this.time2 }, { name: '停机', value: this.time3 }, { name: '报警', value: this.time4 }]
      myChart.clear()
      myChart.setOption({
        title: {
          text: this.MachineValue + '设备状态' + '(' + this.Time + ')',
          x: 250
        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
          type: 'scroll',
          orient: 'vertical',
          left: 10,
          top: 20,
          bottom: 20,
          data: xData
        },
        series: [
          {
            name: '状态',
            type: 'pie',
            radius: '55%',
            center: ['40%', '50%'],
            data: lineData,
            itemStyle: {
              emphasis: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      })
    },
    drawLine2() {
      const myChart = this.$echarts.init(document.getElementById('myChart2'))
      const xData = ['运行', '等待', '停机', '报警']
      const lineData = [{ name: '运行', value: this.time11 }, { name: '等待', value: this.time22 }, { name: '停机', value: this.time33 }, { name: '报警', value: this.time44 }]
      myChart.clear()
      myChart.setOption({
        title: {
          text: '全员设备状态' + '(' + this.Time + ')',
          x: 300
        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
          type: 'scroll',
          orient: 'vertical',
          left: 10,
          top: 20,
          bottom: 20,
          data: xData
        },
        series: [
          {
            name: '状态',
            type: 'pie',
            radius: '55%',
            center: ['40%', '50%'],
            data: lineData,
            itemStyle: {
              emphasis: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      })
    },
    drawLine3() {
      const _this = this
      _this.data = []
      _this.index = 0
      _this.yAxisValue = []
      for (let i = 0; i < _this.table1.length; i++) { // 取开始结束时间HH-mm，取状态日期yyyy-MM-dd，持续时间
        _this.fixDate = _this.table1[0].日期.substr(0, 10)
        _this.table1[i].开始时间 = _this.fixDate + ' ' + _this.table1[i].开始时间.substr(11, 5)
        _this.table1[i].结束时间 = _this.fixDate + ' ' + _this.table1[i].结束时间.substr(11, 5)
        _this.table1[i].日期 = _this.table1[i].日期.substr(0, 10)
      }
      if (_this.table1.length === 1) {
        _this.data.push({
          itemStyle: { normal: { color: _this.table1[0].颜色 }}, // 条形颜色
          name: _this.table1[0].状态,
          value: [0, _this.table1[0].开始时间, _this.table1[0].结束时间]
        })
      } else {
        for (let i = 0; i < _this.table1.length; i++) {
          if (i < _this.table1.length) {
            if (i === 0) {
              _this.index = 0
              _this.yAxisValue.push(_this.table1[i].日期)
            } else if (_this.table1[i].日期 !== _this.table1[i - 1].日期) {
              _this.index++ // y轴索引
              _this.yAxisValue.push(_this.table1[i].日期)
            }
          }
          _this.data.push({
            itemStyle: { normal: { color: _this.table1[i].颜色 }}, // 条形颜色
            name: _this.table1[i].状态,
            value: [parseInt(_this.index), _this.table1[i].开始时间, _this.table1[i].结束时间]
          })
        }
      }
      const myChart = _this.$echarts.init(document.getElementById('myChart3'))
      myChart.clear()
      myChart.setOption({
        title: {
          left: 'center',
          text: '设备状态分布图'
        },
        color: _this.colors,
        tooltip: { // 提示框
          formatter(params) {
            return params.value[1].substr(11, 5) + '~' + params.value[2].substr(11, 5)
          } // 数据的值
        },
        legend: { // 图例
          left: 'left',
          selectedMode: false // 图例可点击
        },
        dataZoom: [{
          type: 'inside',
          start: 0,
          end: 100
        }, { // X轴可调
          xAxisIndex: 0,
          start: 0,
          end: 100,
          handleIcon: 'M10.7,11.9v-1.3H9.3v1.3c-4.9,0.3-8.8,4.4-8.8,9.4c0,5,3.9,9.1,8.8,9.4v1.3h1.3v-1.3c4.9-0.3,8.8-4.4,8.8-9.4C19.5,16.3,15.6,12.2,10.7,11.9z M13.3,24.4H6.7V23h6.6V24.4z M13.3,19.6H6.7v-1.4h6.6V19.6z',
          handleSize: '80%',
          handleStyle: {
            color: '#fff',
            shadowBlur: 3,
            shadowColor: 'rgba(0, 0, 0, 0.6)',
            shadowOffsetX: 2,
            shadowOffsetY: 2
          }
        }],
        grid: { // 绘图网格
          left: '3%',
          right: '3%',
          top: '10%',
          bottom: '10%',
          containLabel: true
        },
        xAxis: {
          type: 'time',
          interval: 1000 * 60 * 60 * 2,
          axisLabel: {
            formatter(value) {
              var date = new Date(value)
              return getzf(date.getHours()) + ':' + getzf(date.getMinutes())
              function getzf(num) {
                if (parseInt(num) < 10) {
                  num = '0' + num
                }
                return num
              }
            }
          }
        },
        yAxis: {
          data: _this.yAxisValue
        },
        series: [
          // 用空bar来显示几个图例
          { name: _this.state[0], type: 'bar', data: [] },
          { name: _this.state[1], type: 'bar', data: [] },
          { name: _this.state[2], type: 'bar', data: [] },
          { name: _this.state[3], type: 'bar', data: [] },
          {
            type: 'custom', renderItem(params, api) { // 开发者自定义的图形元素渲染逻辑，是通过书写 renderItem 函数实现的
              var categoryIndex = api.value(0) // 这里使用 api.value(0) 取出当前 dataItem 中第一个维度的数值。
              var start = api.coord([api.value(1), categoryIndex]) // 这里使用 api.coord(...) 将数值在当前坐标系中转换成为屏幕上的点的像素值。
              var end = api.coord([api.value(2), categoryIndex])
              var height = 10 // 柱体宽度

              return {
                type: 'rect', // 表示这个图形元素是矩形。还可以是 'circle', 'sector', 'polygon' 等等。
                shape: _this.$echarts.graphic.clipRectByRect({ // 矩形的位置和大小。
                  x: start[0],
                  y: start[1] - height / 1,
                  width: end[0] - start[0],
                  height: height * 2
                }, { // 当前坐标系的包围盒。
                  x: params.coordSys.x,
                  y: params.coordSys.y,
                  width: params.coordSys.width,
                  height: params.coordSys.height
                }),
                style: api.style()
              }
            },
            encode: {
              x: [1, 2], // data 中『维度1』和『维度2』对应到 X 轴
              y: 0 // data 中『维度0』对应到 Y 轴
            },
            data: _this.data
          }
        ]
      })
    },
    handleSizeChange(val) {
      this.pageCurrent = 1
      this.pageSize = val
      this.searchData()
    },
    handleCurrentChange(val) {
      this.pageCurrent = val
      this.searchData()
    }
  }
}
</script>

<style scoped>

</style>
