<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="productCodeValue"
            placeholder="请选择产品型号"
            filterable
            clearable
            size="small"
            style="width: 200px">
            <el-option
              v-for="item in productTypes"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              size="mini"/>
          </el-select>
          <span style="margin-left: 10px">检测项目：</span>
          <el-input v-model="checkItemValue" placeholder="请输入检测项目" clearable size="small" style="width: 200px;"/>
        </div>
        <div style="margin-top: 10px">
          <span style="margin-left: 10px">是否合格：</span>
          <el-select
            v-model="isQualifiedValue"
            placeholder="请选择"
            clearable
            size="small"
            style="width: 120px">
            <el-option
              v-for="item in isQualifiedOptions"
              :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: 280px;"
            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: 10px" @click="selectDate()">查询</el-button>
          <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="580px"
          style="width: 100%;max-height: 700px;margin-top: 10px"
          border
          size="mini">
          <el-table-column type="index" width="60" label="序号" align="center"/>
          <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="100">
            <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="检测项目" min-width="120">
            <template slot-scope="scope">
              {{ scope.row.检测项目 }}
            </template>
          </el-table-column>
          <!-- <el-table-column align="center" label="检测项目代码" width="130">
            <template slot-scope="scope">
              {{ scope.row.检测项目代码 }}
            </template>
          </el-table-column> -->
          <el-table-column align="center" label="测量值" width="250">
            <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="80">
            <template slot-scope="scope">
              {{ scope.row.下限值 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="单位" width="60">
            <template slot-scope="scope">
              {{ scope.row.单位 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="是否合格" width="90">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.是否合格 === 1" type="success" size="mini">合格</el-tag>
              <el-tag v-else-if="scope.row.是否合格 === 2" type="danger" size="mini">不合格</el-tag>
              <span v-else>--</span>
            </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="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>
        <div class="block" style="margin-top: 10px;">
          <el-pagination
            v-if="!loading"
            :current-page="pageCurrent"
            :page-sizes="[15, 30, 50, 100]"
            :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 {
      stationNumberValue: '',
      stationNumber: [],
      productCodeValue: '',
      productTypes: [],
      checkItemValue: '',
      isQualifiedValue: '',
      isQualifiedOptions: [
        { label: '全部', value: '' },
        { label: '合格', value: '1' },
        { label: '不合格', value: '2' }
      ],
      dateTime: [],
      loading: false,
      tableData: [],
      total: 0,
      pageSize: 30,
      pageCurrent: 1,
      exportLoading: false
    }
  },
  created() {
    // 默认查询今天
    const today = this.formatDate(new Date())
    this.dateTime = [today, today]
    this.getStationNumber()
    this.getProductTypes()
  },
  mounted() {
    this.selectDate()
  },
  methods: {
    formatDate(date) {
      const year = date.getFullYear()
      const month = (date.getMonth() + 1).toString().padStart(2, '0')
      const day = date.getDate().toString().padStart(2, '0')
      return `${year}-${month}-${day}`
    },
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 === 0) {
        return 'black'
      } else {
        return 'red'
      }
    },
    getStationNumber() {
      this.stationNumber = []
      const param = []
      const Data = this.CreateData('11', '电子看板_质量数据_首件数据_工位表_查询', 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].工位号
          })
        }
      })
    },
    getProductTypes() {
      const param = []
      this.productTypes = []
      const Data = this.CreateData('11', 'CATL_基础_整机_产品代码_查询', param)
      this.ExecDatabase(Data).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.productTypes.push({
            label: response.data[i]['产品名称'],
            value: response.data[i]['产品代码']
          })
        }
      })
    },
    selectDate() {
      if (!this.dateTime || this.dateTime.length !== 2) {
        this.$message.warning('请选择查询时间范围！')
        return
      }
      this.loading = true
      this.tableData = []
      const param = []
      param.push(['工位号', this.stationNumberValue || ''])
      param.push(['产品代码', this.productCodeValue || ''])
      param.push(['检测项目', this.checkItemValue || ''])
      param.push(['是否合格', this.isQualifiedValue || ''])
      param.push(['开始时间', this.dateTime[0] + ' 00:00:00'])
      param.push(['结束时间', this.dateTime[1] + ' 23:59:59'])
      param.push(['PageCurrent', this.pageCurrent])
      param.push(['PageSize', this.pageSize])
      param.push(['PageCount', '0', 'int', '1'])
      param.push(['ItemCount', '0', 'int', '1'])
      const Data = this.CreateData('11', '电子看板_质量数据_首件数据_moby_历史_分页_查询', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data.result && response.data.result.length > 0) {
          this.tableData = response.data.result
        }
        this.total = parseInt(response.data.output[0].ItemCount) || 0
        this.loading = false
      }).catch(err => {
        this.loading = false
        this.$message.error('查询失败，请稍后重试！')
        console.error('selectDate error:', err)
      })
    },
    exportExcel() {
      if (this.tableData.length === 0) {
        this.$message.warning('暂无数据，无法导出！')
        return
      }

      var param = []
      param.push(['工位号', this.stationNumberValue || ''])
      param.push(['产品代码', this.productCodeValue || ''])
      param.push(['检测项目', this.checkItemValue || ''])
      param.push(['是否合格', this.isQualifiedValue || ''])
      param.push(['开始时间', this.dateTime[0] + ' 00:00:00'])
      param.push(['结束时间', this.dateTime[1] + ' 23:59:59'])
      param.push(['PageCurrent', 1])
      param.push(['PageSize', 9999999])
      param.push(['PageCount', '0', 'int', '1'])
      param.push(['ItemCount', '0', 'int', '1'])
      var exportData = JSON.parse(this.CreateData('2005', '电子看板_质量数据_首件数据_moby_历史_分页_查询', param))
      exportData.exportFileName = '首件记录查询'
      exportData.exportColumns = '工位号,产品型号,班次,检测项目,测量值,上限值,下限值,单位,是否合格,检测人,上传成功时间=上传时间'
      var Data = JSON.stringify(exportData)
      this.runExcelExport(Data, 'exportLoading')
    },
    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>
