<template>
  <div class="app-container">
    <el-card class="topCard">
      <div class="topDiv">
        <div>
          <span style="margin-left: 10px">制造编号：</span>
          <el-input
            v-model="workCode"
            placeholder="制造编号"
            clearable
            size="small"
            style="width: 160px; margin-left: 8px" />
          <span style="margin-left: 10px">项目名称：</span>
          <el-input
            v-model="projectName"
            placeholder="项目名称"
            clearable
            size="small"
            style="width: 160px; margin-left: 8px" />
          <span style="margin-left: 10px">工位号：</span>
          <el-select
            v-model="stationNumberValue"
            placeholder="请选择工位号"
            filterable
            clearable
            size="small"
            style="width: 140px">
            <el-option v-for="item in stationNumber" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
          <span style="margin-left: 10px">合格标志：</span>
          <el-select v-model="isQualifiedValue" placeholder="请选择" size="small" style="width: 100px">
            <el-option :value="2" label="全部" />
            <el-option :value="1" label="合格" />
            <el-option :value="0" label="不合格" />
          </el-select>
        </div>
        <div style="margin-top: 10px">
          <span style="margin-left: 10px">间隔编号：</span>
          <el-input
            v-model="splitCode"
            placeholder="间隔编号"
            clearable
            size="small"
            style="width: 120px; margin-left: 8px" />
          <span style="margin-left: 10px">间隔图号：</span>
          <el-input
            v-model="drawingCode"
            placeholder="间隔图号"
            clearable
            size="small"
            style="width: 120px; margin-left: 8px" />
          <span style="margin-left: 10px">采集工具：</span>
          <el-input
            v-model="toolName"
            placeholder="采集工具"
            clearable
            size="small"
            style="width: 120px; margin-left: 8px" />
          <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="info" size="small" icon="el-icon-download" @click="exportExcel">Excel</el-button>
        </div>
        <el-table
          v-loading="loading"
          :data="tableData"
          :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
          element-loading-text="数据加载中"
          element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(0, 0, 0, 0.2)"
          class="main"
          tooltip-effect="dark"
          height="660px"
          style="width: 100%;max-height: 700px"
          border
          size="mini">
          <el-table-column type="index" width="60" label="序号" align="center" />
          <el-table-column prop="工位号" label="工位号" align="center" width="100" />
          <el-table-column prop="工件编号" label="工件编号" align="center" width="120" />
          <el-table-column prop="操作时间" label="采集时间" align="center" width="150" />
          <el-table-column prop="制造编号" label="制造编号" align="center" width="120" />
          <el-table-column prop="项目名称" label="项目名称" align="center" width="120" />
          <el-table-column prop="间隔编号" label="间隔编号" align="center" width="100" />
          <el-table-column prop="间隔图号" label="间隔图号" align="center" width="120" />
          <el-table-column prop="产品名称" label="产品名称" align="center" width="140" />
          <el-table-column prop="拧紧枪名称" label="采集工具" align="center" width="120" />
          <el-table-column prop="上限值" label="上限值" align="center" width="100" />
          <el-table-column prop="下限值" label="下限值" align="center" width="100" />
          <el-table-column prop="标准值" label="标准值" align="center" width="100" />
          <el-table-column prop="操作人1" label="操作人1" align="center" width="100" />
          <el-table-column prop="操作人2" label="操作人2" align="center" width="100" />
          <el-table-column prop="标准值" label="标志值" align="center" width="100" fixed="right" />
          <el-table-column prop="值" label="实际值" align="center" width="100" fixed="right" />
          <el-table-column prop="合格标志Txt" label="合格标志" align="center" width="100" fixed="right">
            <template slot-scope="scope">
              <el-tag :type="scope.row.合格标志Txt === '合格' ? 'success' : 'danger'" size="mini">
                {{ scope.row.合格标志Txt }}
              </el-tag>
            </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 {
      workCode: '',
      projectName: '',
      stationNumberValue: '',
      stationNumber: [],
      isQualifiedValue: 2,
      splitCode: '',
      drawingCode: '',
      toolName: '',
      dateTime: '',
      loading: false,
      tableData: [],
      total: 0,
      pageSize: 30,
      pageCurrent: 1
    }
  },
  created() {
    var year, month, day, audiTime
    this.date = new 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
    this.dateTime = [audiTime, audiTime]
    this.getStationNumber()
    this.selectDate()
  },
  methods: {
    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].工位号
          })
        }
      })
    },
    selectDate() {
      this.tableData = []
      let qualified = this.isQualifiedValue
      if (qualified === 2) qualified = 999
      if (qualified === 0) qualified = 2
      var param = []
      param[0] = ['开始时间', this.dateTime[0] + ' 00:00:00']
      param[1] = ['结束时间', this.dateTime[1] + ' 23:59:59']
      param[2] = ['制造编号', this.workCode]
      param[3] = ['项目名称', this.projectName]
      param[4] = ['编号', this.splitCode]
      param[5] = ['图号', this.drawingCode]
      param[6] = ['工位号', this.stationNumberValue]
      param[7] = ['合格标志', qualified]
      param[8] = ['测量工具', this.toolName]
      param[9] = ['PageCurrent', this.pageCurrent]
      param[10] = ['PageSize', this.pageSize]
      param[11] = ['PageCount', '1111', 'int', '1']
      param[12] = ['ItemCount', '1111', 'int', '1']
      var Data = this.CreateData('11', 'PTCHV_质量数据_视图_分页查询', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data.result && response.data.result.length !== 0) {
          this.tableData = response.data.result
        }
        if (response.data.output && response.data.output.length > 0) {
          this.total = parseInt(response.data.output[0].ItemCount)
        }
        this.loading = false
      })
    },
    exportExcel() {
      if (this.tableData.length !== 0) {
        let qualified = this.isQualifiedValue
        if (qualified === 2) qualified = 999
        if (qualified === 0) qualified = 2
        var param = []
        param[0] = ['开始时间', this.dateTime[0] + ' 00:00:00']
        param[1] = ['结束时间', this.dateTime[1] + ' 23:59:59']
        param[2] = ['制造编号', this.workCode]
        param[3] = ['项目名称', this.projectName]
        param[4] = ['编号', this.splitCode]
        param[5] = ['图号', this.drawingCode]
        param[6] = ['工位号', this.stationNumberValue]
        param[7] = ['合格标志', qualified]
        param[8] = ['测量工具', this.toolName]
        param[9] = ['PageCurrent', 1]
        param[10] = ['PageSize', 999999999]
        param[11] = ['PageCount', '1111', 'int', '1']
        param[12] = ['ItemCount', '1111', 'int', '1']
        var Data = this.CreateData('2001', 'PTCHV_质量数据_视图_分页查询', 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>
