<template>
  <div class="app-container">
    <el-card class="topCard">
      <div class="topDiv">
        <div style="margin-top: 10px">
          <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-select-->
          <!--            v-model="stationNumberValueN"-->
          <!--            placeholder="请选择工位号"-->
          <!--            filterable-->
          <!--            clearable-->
          <!--            size="small"-->
          <!--            style="width: 150px">-->
          <!--            <el-option-->
          <!--              v-for="item in stationNumberN"-->
          <!--              :key="item.value"-->
          <!--              :label="item.label"-->
          <!--              :value="item.value"-->
          <!--              size="mini"/>-->
          <!--          </el-select>-->
          <span style="margin-left: 10px">产品型号：</span>
          <el-select
            v-model="engineTypeValue"
            placeholder="请选择产品型号"
            filterable
            clearable
            size="small"
            style="width: 150px">
            <el-option
              v-for="item in engineType"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              size="mini"/>
          </el-select>
          <span style="margin-left: 10px">产品编号：</span>
          <el-input v-model="assemblyNumberValue" placeholder="模糊查询产品编号" clearable size="small" style="width: 200px;"/>
          <el-button type="primary" plain size="small" icon="el-icon-search" style="margin-left: 5px" @click="selectDate()">查询</el-button>

        </div>
        <div style="margin-top: 10px">
          <span style="margin-left: 10px">订单号：</span>
          <el-input v-model="orderNumberValue" placeholder="请输入订单号" clearable size="small" style="width: 200px;"/>

          <span style="margin-left: 10px">生产日期：</span>
          <el-date-picker
            :clearable="false"
            v-model="dateTime"
            size="small"
            type="datetimerange"
            align="center"
            style="width: 360px;"
            value-format="yyyy-MM-dd HH:mm:ss"
            format="yyyy-MM-dd HH:mm:ss"
            range-separator="～"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            @change="getOrderNumber()"/>
          <el-button :loading="exportLoading" :disabled="exportLoading" type="info" 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="600px"
          style="width: 90%;max-height: 700px;margin-top: 10px"
          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="订单号" width="180">
            <template slot-scope="scope">
              {{ scope.row.订单号 }}
            </template>
          </el-table-column> -->
          <el-table-column align="center" label="产品型号" width="80">
            <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="工位号" width="100">
            <template slot-scope="scope">
              {{ scope.row.工位号 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="托盘编号" width="80">
            <template slot-scope="scope">
              {{ scope.row.托盘编号 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="到达时间" width="150">
            <template slot-scope="scope">
              {{ scope.row.到达时间 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="离开时间" width="150">
            <template slot-scope="scope">
              {{ scope.row.离开时间 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="生产节拍(m)" width="150">
            <template slot-scope="scope">
              {{ scope.row.在线时间 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="是否合格" width="80">
            <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>
        <div class="block" style="margin-top: 10px;">
          <el-pagination
            v-if="!loading"
            :current-page="pageCurrent"
            :page-sizes="[15, 30, 40]"
            :page-size="pageSize"
            :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: '', // 订单号
      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, // 是否合格
      dateTime: '', // 时间
      loading: false,
      tableData: [],
      date: new Date(),
      timer: '',
      total: 0, // 总条数
      pageList: 30, // 每页显示条数
      pageSize: 30, // 每页显示条数
      pageCurrent: 1, // 后台获取页
      exportLoading: false // Excel导出中
    }
  },
  created() {
    this.date = new Date()
    const now = new Date()
    const pad = n => String(n).padStart(2, '0')
    const todayStart = `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())} 00:00:00`
    const todayEnd = `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())} 23:59:59`
    this.dateTime = [todayStart, todayEnd]
    this.getStationNumber()
    this.getStationNumberN()
    this.getEngineType()
  },
  mounted() {
    this.getOrderNumber()
  },
  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].工位号
          })
        }
      })
    },
    // 工位号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].工位号}】${response.data[i].工位名称 || ''}`,
            value: response.data[i].工位号
          })
        }
      })
    },
    // 产品型号
    getEngineType() {
      this.engineType = []
      var param = []
      var Data = this.CreateData('11', 'MES_基本变量_机型代码_查询', 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].发动机型号
          })
        }
      })
    },
    // 订单号
    getOrderNumber() {
      this.orderNumber = []
      var param = []
      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].订单号
          })
        }
      })
    },
    getParam(pageCurrent, pageSize) {
      let orderNumberValue_check, stationNumberValue_check, stationNumberValueN_check, assemblyNumberValue_check, engineTypeValue_check
      this.assemblyNumberValue ? assemblyNumberValue_check = 1 : (assemblyNumberValue_check = 0, this.assemblyNumberValue = '')
      this.stationNumberValue ? stationNumberValue_check = 1 : (stationNumberValue_check = 0, this.stationNumberValue = '')
      this.stationNumberValueN ? stationNumberValueN_check = 1 : (stationNumberValueN_check = 0, this.stationNumberValueN = '')
      this.orderNumberValue ? orderNumberValue_check = 1 : (orderNumberValue_check = 0, this.orderNumberValue = '')
      this.engineTypeValue ? engineTypeValue_check = 1 : (engineTypeValue_check = 0, this.engineTypeValue = '')
      var param = []
      param[0] = ['开始时间', this.dateTime[0]]
      param[1] = ['结束时间', this.dateTime[1]]
      param[2] = ['工位号N_ischeck', stationNumberValueN_check]
      param[3] = ['工位号N', this.stationNumberValueN]
      param[4] = ['发动机号_ischeck', assemblyNumberValue_check]
      param[5] = ['发动机号', this.assemblyNumberValue]
      param[6] = ['工位号_ischeck', stationNumberValue_check]
      param[7] = ['工位号', this.stationNumberValue]
      param[8] = ['订单号_ischeck', orderNumberValue_check]
      param[9] = ['订单号', this.orderNumberValue]
      param[10] = ['机型_ischeck', engineTypeValue_check]
      param[11] = ['机型', this.engineTypeValue]
      param[12] = ['PageCurrent', pageCurrent]
      param[13] = ['PageSize', pageSize]
      param[14] = ['PageCount', '1111', 'int', '1']
      param[15] = ['ItemCount', '1111', 'int', '1']
      return param
    },
    selectDate() {
      this.tableData = []
      this.loading = true
      var param = this.getParam(this.pageCurrent, this.pageSize)
      var Data = this.CreateData('11', '质量数据查询_测量数据发动机在线状态_查询_NEW', param)
      this.ExecDatabase(Data).then(response => {
        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) {
        var param = this.getParam(1, 99999999)
        var exportData = JSON.parse(this.CreateData('2005', '质量数据查询_测量数据发动机在线状态_查询_NEW', param))
        exportData.exportFileName = '过站信息查询'
        exportData.exportColumns = '机型=产品型号,发动机号=产品编号,工位号,托盘编号,到达时间,离开时间,在线时间=生产节拍(m),是否合格'
        var Data = JSON.stringify(exportData)
        this.runExcelExport(Data, 'exportLoading')
      } 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>
