<template>
  <div class="app-container">
    <el-card class="topCard">
      <div class="topDiv">
        <span style="margin-left: 10px">产品编号：</span>
        <el-input v-model="productNoValue" placeholder="请输入产品编号" clearable size="small" style="width: 190px" @keyup.enter.native="handleSearch" />

        <span style="margin-left: 10px">产品型号：</span>
        <el-input v-model="productTypeValue" placeholder="请输入产品型号" clearable size="small" style="width: 150px" @keyup.enter.native="handleSearch" />

        <span style="margin-left: 10px">操作人：</span>
        <el-input v-model="operatorValue" placeholder="请输入操作人" clearable size="small" style="width: 120px" @keyup.enter.native="handleSearch" />

        <span style="margin-left: 10px">校验状态：</span>
        <el-select v-model="verifyStatusValue" placeholder="请选择" clearable size="small" style="width: 120px">
          <el-option label="全部" value="" />
          <el-option label="进行中" value="0" />
          <el-option label="成功" value="1" />
          <el-option label="失败" value="2" />
        </el-select>

        <span style="margin-left: 10px">记录时间：</span>
        <el-date-picker
          v-model="dateTime"
          :clearable="false"
          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="结束时间" />

        <el-button type="primary" plain size="small" icon="el-icon-search" style="margin-left: 5px" @click="handleSearch">查询</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"
        :data="tableData"
        :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
        element-loading-text="数据加载中"
        highlight-current-row
        border
        size="mini"
        height="620px"
        style="width: 100%">
        <el-table-column width="60" label="序号" type="index" align="center" />
        <el-table-column label="产品编号" width="200" align="center" prop="产品编号" show-overflow-tooltip />
        <el-table-column label="产品型号" width="120" align="center" prop="产品型号" show-overflow-tooltip />
        <el-table-column label="机型代码" width="90" align="center" prop="机型代码" show-overflow-tooltip />
        <el-table-column label="扫描箱体码" width="250" align="center" prop="扫描箱体码" show-overflow-tooltip />
        <el-table-column label="扫描客户标签码" min-width="200" align="center" prop="扫描客户标签码" show-overflow-tooltip />
        <el-table-column label="箱体码结果" width="100" align="center">
          <template slot-scope="scope">
            <el-tag :type="getResultType(scope.row.箱体码校验结果)" size="mini">{{ getResultText(scope.row.箱体码校验结果) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="客户标签结果" width="110" align="center">
          <template slot-scope="scope">
            <el-tag :type="getResultType(scope.row.客户标签码校验结果)" size="mini">{{ getResultText(scope.row.客户标签码校验结果) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="校验状态" width="100" align="center">
          <template slot-scope="scope">
            <el-tag :type="getStatusType(scope.row.校验状态)" size="mini">{{ getStatusText(scope.row.校验状态) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作人" width="110" align="center" prop="操作人" show-overflow-tooltip />
        <el-table-column label="创建时间" width="160" align="center" prop="创建时间" show-overflow-tooltip />
        <el-table-column label="完成时间" width="160" align="center" prop="完成时间" show-overflow-tooltip />
      </el-table>

      <div class="block" style="margin-top: 10px">
        <el-pagination
          v-if="!loading"
          :current-page="pageCurrent"
          :page-sizes="[50, 100, 200]"
          :page-size="pageSize"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange" />
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading: false,
      tableData: [],
      productNoValue: '',
      productTypeValue: '',
      operatorValue: '',
      verifyStatusValue: '',
      dateTime: '',
      total: 0,
      pageSize: 50,
      pageCurrent: 1,
      exportLoading: false
    }
  },
  created() {
    this.initDateTime()
    this.searchTable()
  },
  methods: {
    initDateTime() {
      const end = new Date()
      const start = new Date(end.getTime() - 7 * 24 * 60 * 60 * 1000)
      this.dateTime = [this.formatDateTime(start, '00:00:00'), this.formatDateTime(end, '23:59:59')]
    },
    formatDateTime(date, timeText) {
      const year = date.getFullYear()
      const month = String(date.getMonth() + 1).padStart(2, '0')
      const day = String(date.getDate()).padStart(2, '0')
      return `${year}-${month}-${day} ${timeText}`
    },
    getParam(pageCurrent, pageSize) {
      const param = []
      param[0] = ['开始时间', this.dateTime && this.dateTime[0] ? this.dateTime[0] : '']
      param[1] = ['结束时间', this.dateTime && this.dateTime[1] ? this.dateTime[1] : '']
      param[2] = ['产品编号', this.productNoValue || '']
      param[3] = ['产品型号', this.productTypeValue || '']
      param[4] = ['操作人', this.operatorValue || '']
      param[5] = ['校验状态', this.verifyStatusValue || '']
      param[6] = ['PageCurrent', pageCurrent]
      param[7] = ['PageSize', pageSize]
      param[8] = ['PageCount', '1111', 'int', '1']
      param[9] = ['ItemCount', '1111', 'int', '1']
      return param
    },
    searchTable() {
      this.loading = true
      this.tableData = []
      const Data = this.CreateData('11', 'PACK下线三码校验_记录_分页查询', this.getParam(this.pageCurrent, this.pageSize))
      this.ExecDatabase(Data).then(response => {
        this.tableData = response.data && response.data.result ? response.data.result : []
        this.total = response.data && response.data.output && response.data.output[0] ? parseInt(response.data.output[0].ItemCount) || 0 : 0
        this.loading = false
      }).catch(error => {
        console.error('查询三码校验记录失败:', error)
        this.$message.error('查询三码校验记录失败')
        this.loading = false
      })
    },
    handleSearch() {
      this.pageCurrent = 1
      this.pageSize = 50
      this.searchTable()
    },
    exportExcel() {
      if (this.tableData.length === 0) {
        this.$message.warning('暂无数据')
        return
      }
      const exportData = JSON.parse(this.CreateData('2005', 'PACK下线三码校验_记录_分页查询', this.getParam(1, 99999999)))
      exportData.exportFileName = '三码校验记录'
      exportData.exportColumns = '产品编号,产品型号,机型代码,扫描箱体码,扫描客户标签码,箱体码校验结果=箱体码结果,客户标签码校验结果=客户标签结果,校验状态,操作人,创建时间,完成时间'
      const Data = JSON.stringify(exportData)
      this.runExcelExport(Data, 'exportLoading')
    },
    getResultType(value) {
      const status = Number(value)
      if (status === 1) return 'success'
      if (status === 2) return 'danger'
      return 'warning'
    },
    getResultText(value) {
      const status = Number(value)
      if (status === 1) return '成功'
      if (status === 2) return '失败'
      return '进行中'
    },
    getStatusType(value) {
      return this.getResultType(value)
    },
    getStatusText(value) {
      return this.getResultText(value)
    },
    handleSizeChange(val) {
      this.pageCurrent = 1
      this.pageSize = val
      this.searchTable()
    },
    handleCurrentChange(val) {
      this.pageCurrent = val
      this.searchTable()
    }
  }
}
</script>

<style scoped>
.topCard {
  margin: 5px;
}

.topDiv {
  margin-bottom: 10px;
}

.block {
  text-align: left;
}
</style>
