<template>
  <div class="app-container">
    <el-card class="topCard">
      <div class="topDiv">
        <div>
          <span style="margin-left: 10px">工位号：</span>
          <el-select
            v-model="stationNumberValue"
            placeholder="请选择工位号"
            filterable
            clearable
            size="small"
            style="width: 240px">
            <el-option
              v-for="item in stationNumber"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              size="mini"/>
          </el-select>
          <span style="margin-left: 10px">生产日期：</span>
          <el-date-picker
            :clearable = "false"
            v-model="dateTime"
            size="small"
            type="daterange"
            align="center"
            style="width: 240px;"
            value-format="yyyy-MM-dd"
            format="yyyy-MM-dd"
            range-separator="～"
            start-placeholder="开始日期"
            end-placeholder="结束日期"/>
          <el-button type="primary" plain size="small" icon="el-icon-search" style="margin-left: 5px" @click="selectDate()">查询</el-button>
<!--          <el-button type="primary" size="small" icon="el-icon-download" style="margin-left: 5px" @click="exportExcel()">Excel</el-button>-->
        </div>
        <el-table
          v-loading="loading"
          ref="multipleTable"
          :data="tableData"
          :header-cell-style="{background:'#eef1f6',color:'#606266'}"
          :cell-class-name="tableRowClassName"
          element-loading-text="数据加载中"
          element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(0, 0, 0, 0.2)"
          class="main"
          tooltip-effect="dark"
          height="300px"
          style="width: 50%;max-height: 700px"
          border
          size="mini">
          <!--        <el-table-column :selectable="selectable" type="selection" width="45" align="center"/>-->
          <el-table-column type="index" width="60" label="序号" align="center"/>
          <!--          <el-table-column align="center" label="产品编号">-->
          <!--            <template slot-scope="scope">-->
          <!--              {{ scope.row.EngineID }}-->
          <!--            </template>-->
          <!--          </el-table-column>-->
          <el-table-column align="center" width="100px" label="工位号">
            <template slot-scope="scope">
              {{ scope.row.工位号 }}
            </template>
          </el-table-column>
          <!--          <el-table-column align="center" label="工位名称">-->
          <!--            <template slot-scope="scope">-->
          <!--              {{ scope.row.工位名称 }}-->
          <!--            </template>-->
          <!--          </el-table-column>-->

          <el-table-column align="center" label="测量项目">
            <template slot-scope="scope">
              {{ scope.row.测量项目 }}
            </template>
          </el-table-column>
          <el-table-column align="center" width="150px" label="不合格次数">
            <template slot-scope="scope">
              {{ scope.row.不合格次数 }}
            </template>
          </el-table-column>
          <!--          <el-table-column align="center" label="理论值" width="80px">-->
          <!--            <template slot-scope="scope">-->
          <!--              {{ scope.row.理论值 }}-->
          <!--            </template>-->
          <!--          </el-table-column>-->
          <!--          <el-table-column align="center" label="值上限" width="80px">-->
          <!--            <template slot-scope="scope">-->
          <!--              {{ scope.row.上限值 }}-->
          <!--            </template>-->
          <!--          </el-table-column>-->
          <!--          <el-table-column align="center" label="值下限" width="80px">-->
          <!--            <template slot-scope="scope">-->
          <!--              {{ scope.row.下限值 }}-->
          <!--            </template>-->
          <!--          </el-table-column>-->
        </el-table>
        <div style="height: 300px;border: 1px solid #e4e7ed; margin-top: 10px">
          <div id="myChart" style="height: 270px" />
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
let myChart = null
export default {
  data() {
    return {
      traceabilityCodeArr: [], // 精确追溯码数组
      traceabilityCode: '', // 精确追溯码
      assemblyNumber: [], // 总成编号
      assemblyNumberValue: '', // 总成编号
      orderNumberValue: '', // 订单号
      orderNumber: [], // 订单号
      stationNumberValue: '', // 工位号
      stationNumber: [], // 工位号
      measuringPosition: [], // 测量位置
      measuringPositionValue: '', // 测量位置
      measuringProject: [], // 测量项目
      measuringProjectValue: '', // 测量项目
      measuringProjectTime: '', // 测量时间
      engineTypeValue: '', // 机型
      engineType: [], // 机型
      batchBarcode: [], // 批次条码
      batchBarcodeValue: '', // 批次条码
      isQualified: [
        {
          label: '合格',
          value: 1
        }, {
          label: '不合格',
          value: 0
        }, {
          label: '全部',
          value: 2
        }
      ], // 是否合格
      isQualifiedValue: 2, // 是否合格
      dateTime: '', // 时间
      loading: false, // 时间
      ftt: {
        allCount: '0',
        oKCount: '0',
        firstOKCount: '0',
        firstOKRate: '0'
      },
      tableData: [], // 时间
      tableDataFilter: [], // 时间
      date: new Date(),
      timer: '',
      total: 0, // 总条数
      pageList: 30, // 每页显示条数
      pageSize: 30, // 每页显示条数
      pageCurrent: 1 // 后台获取页
    }
  },
  created() {
    var year, month, day, audiTime
    this.date = new Date() // 修改数据date
    year = new Date().getFullYear()
    month = new Date().getMonth() + 1 < 10 ? '0' + (new Date().getMonth() + 1) : new Date().getMonth() + 1
    day = new Date().getDate() < 10 ? '0' + new Date().getDate() : new Date().getDate()
    audiTime = year + '-' + month + '-' + day
    console.log(audiTime, 'audiTime')
    this.dateTime = [audiTime, audiTime]
    this.getStationNumber()
    this.getEngineType()
  },
  mounted() {
  },
  methods: {
    // 表格颜色变化
    tableRowClassName({ row, rowIndex }) {
      // console.log(row, rowIndex)
      if (rowIndex % 2 === 0) {
        return 'black'
      } else {
        return 'red'
      }
    },
    // 工位号
    getStationNumber() {
      this.stationNumber = []
      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.stationNumber.push({
            label: `【${response.data[i].工位号}】${response.data[i].工位名称 || ''}`,
            value: response.data[i].工位号
          })
        }
      }).then(() => {
        this.getBatchBarcode()
        this.getMeasuringPosition()
      })
    },
    // 机型
    getEngineType() {
      this.engineType = []
      var param = []
      var Data = this.CreateData('11', 'CATL_基础信息_机型信息_查询', param)
      this.ExecDatabase(Data).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.engineType.push({
            label: response.data[i].机型号,
            value: response.data[i].机型ID,
            labelName: response.data[i].机型号
          })
        }
      })
    },
    selectDate() {
      this.tableData = []
      if (this.stationNumberValue === '') {
        this.$message.warning('请选择工位号')
        return
      }
      var param = []
      param[0] = ['开始时间', this.dateTime[0] + ' ' + '00:00:00']
      param[1] = ['结束时间', this.dateTime[1] + ' ' + '23:59:59']
      param[2] = ['工位号', this.stationNumberValue]
      var Data = this.CreateData('11', '质量数据_发动机质量数据_各个工位_视图_发动机型号_NG综合查询', param)
      this.ExecDatabase(Data).then(response => {
        this.tableData = []
        if (response.data.length !== 0) {
          this.tableData = response.data
        } else {
          this.$message.warning('暂无数据')
        }
        this.drawChart()
        this.loading = false
      })
    },
    drawChart() {
      if (myChart != null && myChart !== '' && myChart !== undefined) {
        myChart.dispose()
      }
      if (this.tableData.length === 0) {
        return
      }
      const max = this.tableData.length > 10 ? 30 : 100
      // 基于准备好的dom，初始化echarts实例
      myChart = this.$echarts.init(document.getElementById('myChart'))
      // 指定图表的配置项和数据
      var option = {
        title: {
          text: '不合格次数统计'
        },
        tooltip: {},
        legend: {
          data: ['不合格次数']
        },
        xAxis: {
          data: this.tableData.map(item => item.测量项目),
          axisLabel: {
            interval: 0,
            rotate: 30 // 旋转30度
          }
        },
        yAxis: {
          name: '不合格次数 (次)'
        },
        series: [
          {
            name: '不合格次数',
            type: 'bar',
            data: this.tableData.map(item => item.不合格次数),
            label: {
              normal: {
                show: true, // 开启显示
                position: 'top', // 在上方显示
                formatter: '{c}' // 显示百分号
              }
            }
          }
        ],
        dataZoom: [
          {
            type: 'slider',
            show: true,
            xAxisIndex: [0],
            start: 0,
            end: max,
            bottom: '0px'
          },
          {
            type: 'inside',
            xAxisIndex: [0],
            start: 0,
            end: max,
            bottom: '0px'
          }
        ]
      }
      // 使用刚指定的配置项和数据显示图表。
      myChart.setOption(option)
    },
    exportExcel() {
      if (this.tableDataFilter.length !== 0) {
        if (this.stationNumberValue === '') {
          this.$message.warning('请选择工位号')
          return
        }
        var param = []
        param[0] = ['开始时间', this.dateTime[0] + ' ' + '00:00:00']
        param[1] = ['结束时间', this.dateTime[1] + ' ' + '23:59:59']
        param[2] = ['工位号', this.stationNumberValue]
        var exportData = JSON.parse(this.CreateData('2005', '质量数据_发动机质量数据_各个工位_视图_发动机型号_NG综合查询', param))
        exportData.exportFileName = '质量NG分析'
        exportData.exportColumns = '工位号,测量项目,不合格次数'
        var Data = JSON.stringify(exportData)
        this.exportExcel_NPOI(Data)
      } else {
        this.$message.warning('暂无数据')
      }
    },
    // 分页
    handleSizeChange(val) {
      this.pageCurrent = 1
      this.pageSize = val
      this.selectDate()
    },
    handleCurrentChange(val) {
      this.pageCurrent = val
      this.selectDate()
    }
  }
}
</script>

<style scoped>
  .topCard{
    margin: 5px;
  }
  .topDiv{
    margin-bottom: 10px;
  }
</style>
