<template>
  <div class="app-container">
    <el-card class="topCard">
      <div class="topDiv">
        <div>
          <span style="margin-left: 10px">工位号：</span>
          <el-select
            v-model="stationNumberValue"
            clearable
            filterable
            placeholder="请选择工位号"
            size="small"
            style="width: 150px">
            <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-select
            v-model="deviceStatus"
            clearable
            filterable
            placeholder="请选择设备状态"
            size="small"
            style="width: 150px">
            <el-option
              label="加工"
              size="mini"
              value="1"/>
            <el-option
              label="空闲"
              size="mini"
              value="2"/>
            <el-option
              label="故障"
              size="mini"
              value="3"/>
            <el-option
              label="关机"
              size="mini"
              value="4"/>
          </el-select>
          <span style="margin-left: 10px">开始时间：</span>
          <el-date-picker
            v-model="dateTimeStart"
            :clearable="false"
            default-time="8:00:00"
            format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择日期时间"
            size="mini"
            style="width: 200px"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"/>
          <span style="margin-left: 10px">结束时间：</span>
          <el-date-picker
            v-model="dateTimeEnd"
            :clearable="false"
            default-time="12:00:00"
            format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择日期时间"
            size="mini"
            style="width: 200px"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"/>
          <el-button
            icon="el-icon-search"
            plain
            size="small"
            style="margin-left: 5px"
            type="primary"
            @click="selectDate()">查询
          </el-button>
          <el-button icon="el-icon-download" size="small" type="info" @click="exportExcel()">Excel</el-button>
        </div>
        <el-table
          v-loading="loading"
          ref="multipleTable"
          :cell-class-name="tableRowClassName"
          :data="tableData"
          :header-cell-style="{background:'#eef1f6',color:'#606266'}"
          border
          class="main"
          element-loading-background="rgba(0, 0, 0, 0.2)"
          element-loading-spinner="el-icon-loading"
          element-loading-text="数据加载中"
          height="760px"
          size="mini"
          style="width: 40%;max-height: 700px;margin-top: 10px"
          tooltip-effect="dark">
          <el-table-column align="center" label="序号" type="index" width="60"/>
          <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">
              <el-tag v-if="parseInt(scope.row.TagValue) === 1" size="mini" style="width: 70px" type="success">
                加工中
              </el-tag>
              <el-tag v-if="parseInt(scope.row.TagValue) === 2" size="mini" style="width: 70px" type="info">
                空闲
              </el-tag>
              <el-tag v-if="parseInt(scope.row.TagValue) === 3" size="mini" style="width: 70px" type="warning">
                故障
              </el-tag>
              <el-tag v-if="parseInt(scope.row.TagValue) === 4" size="mini" style="width: 70px" type="text">
                关机
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column align="center" label="记录时间">
            <template slot-scope="scope">
              {{ scope.row.lastupdatetime }}
            </template>
          </el-table-column>
        </el-table>
        <div class="block" style="margin-top: 10px;">
          <el-pagination
            v-if="!loading"
            :current-page="pageCurrent"
            :page-size="pageSize"
            :page-sizes="[30, 50, 100,200]"
            :total="total"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"/>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      traceabilityCodeArr: [], // 精确追溯码数组
      traceabilityCode: '', // 精确追溯码
      assemblyNumber: [], // 总成编号
      assemblyNumberValue: '', // 总成编号
      orderNumberValue: '', // 订单号
      deviceStatus: '', // 设备状态
      orderNumber: [], // 订单号
      stationNumberValue: '', // 工位号
      stationNumber: [], // 工位号
      stationNumberValueN: '', // 工位号N
      stationNumberN: [], // 工位号N
      measuringPosition: [], // 测量位置
      measuringPositionValue: '', // 测量位置
      measuringProject: [], // 测量项目
      measuringProjectValue: '', // 测量项目
      engineTypeValue: '', // 机型
      engineType: [], // 机型
      batchBarcode: [], // 批次条码
      batchBarcodeValue: '', // 批次条码
      isQualified: [
        {
          label: '合格',
          value: 1
        }, {
          label: '不合格',
          value: 2
        }, {
          label: '全部',
          value: 0
        }
      ], // 是否合格
      isQualifiedValue: 1, // 是否合格
      dateTimeStart: '', // 时间
      dateTimeEnd: '', // 时间
      loading: false, // 时间
      tableData: [], // 时间
      date: new Date(),
      timer: '',
      total: 0, // 总条数
      pageList: 30, // 每页显示条数
      pageSize: 50, // 每页显示条数
      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.dateTimeStart = audiTime + ' 08:00:00'
    this.dateTimeEnd = audiTime + ' 18:00:00'
    this.getStationNumber()
    this.getStationNumberN()
  },
  mounted() {
    // this.getOrderNumber()
    // this.getAssemblyNumber()
  },
  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].工位号,
            value: response.data[i].工位号
          })
        }
      })
    },
    // 工位号N
    getStationNumberN() {
      this.stationNumberN = []
      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.stationNumberN.push({
            label: response.data[i].工位号,
            value: response.data[i].工位号
          })
        }
      })
    },
    // 订单号
    getOrderNumber() {
      this.orderNumber = []
      var param = []
      param[0] = ['开始时间', this.dateTime[0] + ' ' + '00:00:00']
      param[1] = ['结束时间', this.dateTime[1] + ' ' + '23:59:59']
      // param[0] = ['开始时间', this.dateTime[0]]
      // param[1] = ['结束时间', this.dateTime[1]]
      var Data = this.CreateData('11', '测量数据发动机在线状态_订单号_查询 ', param)
      this.ExecDatabase(Data).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.orderNumber.push({
            label: response.data[i].订单号,
            value: response.data[i].订单号
          })
        }
      })
    },
    getAssemblyNumber() {
      this.assemblyNumber = []
      var orderNumberValue_check
      this.orderNumberValue ? orderNumberValue_check = 1 : orderNumberValue_check = 0
      var param = []
      param[0] = ['开始时间', this.dateTime[0] + ' ' + '00:00:00']
      param[1] = ['结束时间', this.dateTime[1] + ' ' + '23:59:59']
      // param[0] = ['开始时间', this.dateTime[0]]
      // param[1] = ['结束时间', this.dateTime[1]]
      param[2] = ['订单号_ischeck', orderNumberValue_check]
      param[3] = ['订单号', this.orderNumberValue]
      var Data = this.CreateData('11', '测量数据发动机在线状态_发动机号_查询 ', param)
      this.ExecDatabase(Data).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.assemblyNumber.push({
            label: response.data[i].发动机号,
            value: response.data[i].发动机号,
            ID: response.data[i].ID
          })
        }
      })
    },
    selectDate() {
      this.tableData = []
      let deviceStatus_check, stationNumberValue_check
      this.stationNumberValue ? stationNumberValue_check = 1 : (stationNumberValue_check = 0, this.stationNumberValue = '')
      this.deviceStatus ? deviceStatus_check = 1 : (deviceStatus_check = 0, this.deviceStatus = '')
      var param = []
      param[0] = ['开始时间', this.dateTimeStart]
      param[1] = ['结束时间', this.dateTimeEnd]
      param[2] = ['工位号_ischeck', stationNumberValue_check]
      param[3] = ['工位号', this.stationNumberValue]
      param[4] = ['设备状态_ischeck', deviceStatus_check]
      param[5] = ['设备状态', this.deviceStatus]
      param[6] = ['PageCurrent', this.pageCurrent]
      param[7] = ['PageSize', this.pageSize]
      param[8] = ['PageCount', '1111', 'int', '1']
      param[9] = ['ItemCount', '1111', 'int', '1']
      var Data = this.CreateData('11', '测量数据机床状态_查询', param)
      this.ExecDatabase(Data).then(response => {
        console.log(response, 66)
        if (response.data.result.length !== 0) {
          this.tableData = response.data.result
        }
        this.total = parseInt(response.data.output[0].ItemCount)
        this.loading = false
      })
    },
    exportExcel() {
      if (this.tableData.length !== 0) {
        let deviceStatus_check, stationNumberValue_check
        this.stationNumberValue ? stationNumberValue_check = 1 : (stationNumberValue_check = 0, this.stationNumberValue = '')
        this.deviceStatus ? deviceStatus_check = 1 : (deviceStatus_check = 0, this.deviceStatus = '')
        var param = []
        param[0] = ['开始时间', this.dateTimeStart]
        param[1] = ['结束时间', this.dateTimeEnd]
        param[2] = ['工位号_ischeck', stationNumberValue_check]
        param[3] = ['工位号', this.stationNumberValue]
        param[4] = ['设备状态_ischeck', deviceStatus_check]
        param[5] = ['设备状态', this.deviceStatus]
        param[6] = ['PageCurrent', this.pageCurrent]
        param[7] = ['PageSize', this.pageSize]
        param[8] = ['PageCount', '1111', 'int', '1']
        param[9] = ['ItemCount', '1111', 'int', '1']
        var Data = this.CreateData('2202', '', param)
        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>
