<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: 200px">
            <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="engineTypeValue"
            clearable
            filterable
            placeholder="请选择机型"
            size="small"
            style="width: 240px">
            <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-select
            v-model="dataType"
            clearable
            filterable
            placeholder="请选择数据类型"
            size="small"
            style="width: 200px"
            @change="getOpInputDataList(2,$event)">
            <el-option
              v-for="item in dataTypeList"
              :key="item"
              :label="item"
              :value="item"
              size="mini"
            />
          </el-select>
          <span style="margin-left: 10px">数据名称：</span>
          <el-select
            v-model="dataName"
            clearable
            filterable
            placeholder="请选择数据名称"
            size="small"
            style="width: 200px">
            <el-option
              v-for="item in dataNameList"
              :key="item"
              :label="item"
              :value="item"
              size="mini"/>
          </el-select>
          <span style="margin-left: 10px">记录值：</span>
          <el-input
            v-model="dataValue"
            clearable
            filterable
            placeholder="请输入记录值"
            size="small"
            style="width: 200px"/>
        </div>
        <div style="margin-top: 10px">
          <span style="margin-left: 10px">总成编号：</span>
          <el-select
            v-model="assemblyNumberValue"
            allow-create
            clearable
            filterable
            placeholder="请选择总成编号"
            size="small"
            style="width: 200px;">
            <el-option
              v-for="item in assemblyNumber"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              size="mini"/>
          </el-select>
          <span style="margin-left: 10px">订单号：</span>
          <el-select
            v-model="orderNumberValue"
            clearable
            filterable
            placeholder="请选择订单"
            size="small"
            style="width: 200px">
            <el-option
              v-for="item in orderNumber"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              size="mini"/>
          </el-select>
          <span style="margin-left: 10px">时间：</span>
          <el-date-picker
            v-model="dateTime"
            :clearable="false"
            align="center"
            end-placeholder="结束日期"
            format="yyyy-MM-dd"
            range-separator="～"
            size="small"
            start-placeholder="开始日期"
            style="width: 240px;"
            type="daterange"
            value-format="yyyy-MM-dd"
            @change="getOrderNumber();getAssemblyNumber()"/>
          <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="660px"
          size="mini"
          style="width: 100%;max-height: 700px"
          tooltip-effect="dark">
          <el-table-column align="center" label="序号" type="index" width="60"/>
          <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="工件编号">
            <template slot-scope="scope">
              {{ scope.row.工件编号 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="机型" width="100px">
            <template slot-scope="scope">
              {{ scope.row.机型 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="数据类型" width="150px">
            <template slot-scope="scope">
              {{ scope.row.数据类型 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="数据名称" width="150px">
            <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="100px">
            <template slot-scope="scope">
              {{ scope.row.操作者工号 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="记录时间" width="150px">
            <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-size="pageSize"
            :page-sizes="[15, 30, 40]"
            :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: [], // 工位号
      measuringPosition: [], // 测量位置
      measuringPositionValue: '', // 测量位置
      measuringProject: [], // 测量项目
      measuringProjectValue: '', // 测量项目
      engineTypeValue: '', // 机型
      engineType: [], // 机型
      batchBarcode: [], // 批次条码
      batchBarcodeValue: '', // 批次条码
      dataType: '',
      dataName: '',
      dataValue: '',
      dataTypeList: [],
      dataNameList: [],
      isQualified: [
        {
          label: '合格',
          value: 1
        }, {
          label: '不合格',
          value: 0
        }, {
          label: '全部',
          value: 2
        }
      ], // 是否合格
      isQualifiedValue: 2, // 是否合格
      dateTime: '', // 时间
      loading: false, // 时间
      tableData: [], // 时间
      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() {
    this.getOrderNumber()
    this.getAssemblyNumber()
    this.getOpInputDataList(1)
  },
  methods: {
    getOpInputDataList(type, dataType = null) {
      if (type === 1) {
        this.dataTypeList = []
      }
      this.dataNameList = []
      var param = []
      param[0] = ['工位号', '']
      param[1] = ['数据类型', dataType]
      param[2] = ['type', type]
      var Data = this.CreateData('11', 'MES_工位手动录入_基本表_工位_查询', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data.length > 0) {
          for (const item of response.data) {
            if (type === 1) {
              this.dataTypeList.push(item.Value)
            } else if (type === 2) {
              this.dataNameList.push(item.工位号 + ':' + item.Value)
            }
          }
        } else {
          // this.$message.error('该工位无工位录入项，请在管理系统中添加后再试！')
        }
      })
    },
    // 表格颜色变化
    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].工位号
          })
        }
      }).then(() => {
      })
    },
    // 机型
    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 isQualifiedValue
      if (!this.isQualifiedValue) {
        isQualifiedValue = null
      } else {
        isQualifiedValue = this.isQualifiedValue
      }
      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] = ['合格标志', isQualifiedValue]
      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 engineTypeValue_check, stationNumberValue_check
      this.engineTypeValue ? engineTypeValue_check = 1 : engineTypeValue_check = 0
      this.stationNumberValue ? stationNumberValue_check = 1 : stationNumberValue_check = 0
      var param = []
      param[0] = ['开始时间', this.dateTime[0] + ' ' + '00:00:00']
      param[1] = ['结束时间', this.dateTime[1] + ' ' + '23:59:59']
      var Data = this.CreateData('11', 'MES_工位手动录入_记录表_发动机号查询 ', 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].发动机号
          })
        }
      })
    },
    selectDate() {
      this.tableData = []
      let engineTypeValue_check, assemblyNumberValue_check, stationNumberValue_check,
        dataNameValue_check, dataTypeValue_check, orderNumberValue_check, dataValueValue_check
      this.engineTypeValue ? engineTypeValue_check = 1 : (engineTypeValue_check = 0, this.engineTypeValue = '')
      this.assemblyNumberValue ? assemblyNumberValue_check = 1 : (assemblyNumberValue_check = 0, this.assemblyNumberValue = '')
      this.stationNumberValue ? stationNumberValue_check = 1 : (stationNumberValue_check = 0, this.stationNumberValue = '')
      this.orderNumberValue ? orderNumberValue_check = 1 : (orderNumberValue_check = 0, this.orderNumberValue = '')

      this.dataType ? dataTypeValue_check = 1 : (dataTypeValue_check = 0, this.dataType = '')
      this.dataName ? dataNameValue_check = 1 : (dataNameValue_check = 0, this.dataName = '')
      this.dataValue ? dataValueValue_check = 1 : (dataValueValue_check = 0, this.dataValue = '')
      var param = []
      param[0] = ['开始时间', this.dateTime[0] + ' ' + '00:00:00']
      param[1] = ['结束时间', this.dateTime[1] + ' ' + '23:59:59']
      param[2] = ['PageCurrent', this.pageCurrent]
      param[3] = ['PageSize', this.pageSize]
      param[4] = ['PageCount', '1111', 'int', '1']
      param[5] = ['ItemCount', '1111', 'int', '1']
      param[6] = ['发动机型号_ischeck', engineTypeValue_check]
      param[7] = ['发动机型号', this.engineTypeValue]
      param[8] = ['发动机号_ischeck', assemblyNumberValue_check]
      param[9] = ['发动机号', this.assemblyNumberValue]
      param[10] = ['工位号_ischeck', stationNumberValue_check]
      param[11] = ['工位号', this.stationNumberValue]
      param[12] = ['数据类型_ischeck', dataTypeValue_check]
      param[13] = ['数据类型', this.dataType]
      param[14] = ['数据名称_ischeck', dataNameValue_check]
      param[15] = ['数据名称', this.dataName]
      param[16] = ['订单号_ischeck', orderNumberValue_check]
      param[17] = ['订单号', this.orderNumberValue]
      param[18] = ['数据值_ischeck', dataValueValue_check]
      param[19] = ['数据值', this.dataValue]
      var Data = this.CreateData('11', 'MES_工位手动录入_记录表_查询', 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) {
        let engineTypeValue_check, assemblyNumberValue_check, stationNumberValue_check,
          dataNameValue_check, dataTypeValue_check, orderNumberValue_check, dataValueValue_check
        this.engineTypeValue ? engineTypeValue_check = 1 : (engineTypeValue_check = 0, this.engineTypeValue = '')
        this.assemblyNumberValue ? assemblyNumberValue_check = 1 : (assemblyNumberValue_check = 0, this.assemblyNumberValue = '')
        this.stationNumberValue ? stationNumberValue_check = 1 : (stationNumberValue_check = 0, this.stationNumberValue = '')
        this.orderNumberValue ? orderNumberValue_check = 1 : (orderNumberValue_check = 0, this.orderNumberValue = '')

        this.dataType ? dataTypeValue_check = 1 : (dataTypeValue_check = 0, this.dataType = '')
        this.dataName ? dataNameValue_check = 1 : (dataNameValue_check = 0, this.dataName = '')
        this.dataValue ? dataValueValue_check = 1 : (dataValueValue_check = 0, this.dataValue = '')
        var param = []
        param[0] = ['开始时间', this.dateTime[0] + ' ' + '00:00:00']
        param[1] = ['结束时间', this.dateTime[1] + ' ' + '23:59:59']
        param[2] = ['PageCurrent', this.pageCurrent]
        param[3] = ['PageSize', this.pageSize]
        param[4] = ['PageCount', '1111', 'int', '1']
        param[5] = ['ItemCount', '1111', 'int', '1']
        param[6] = ['发动机型号_ischeck', engineTypeValue_check]
        param[7] = ['发动机型号', this.engineTypeValue]
        param[8] = ['发动机号_ischeck', assemblyNumberValue_check]
        param[9] = ['发动机号', this.assemblyNumberValue]
        param[10] = ['工位号_ischeck', stationNumberValue_check]
        param[11] = ['工位号', this.stationNumberValue]
        param[12] = ['数据类型_ischeck', dataTypeValue_check]
        param[13] = ['数据类型', this.dataType]
        param[14] = ['数据名称_ischeck', dataNameValue_check]
        param[15] = ['数据名称', this.dataName]
        param[16] = ['订单号_ischeck', orderNumberValue_check]
        param[17] = ['订单号', this.orderNumberValue]
        param[18] = ['数据值_ischeck', dataValueValue_check]
        param[19] = ['数据值', this.dataValue]
        var Data = this.CreateData('2001', 'MES_工位手动录入_记录表_查询', 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>
