<template>
  <div>
    <el-card>
      <div class="search-container" @keyup.enter="searchTable">
        <span class="show-text">生产订单:</span>
        <el-input
          v-model="orderNo"
          placeholder="请输入生产订单"
          clearable
          style="width: 150px; margin-right: 10px"
          @click="searchTable()"
        />
        <span class="show-text">销售订单:</span>
        <el-input
          v-model="contractNo"
          placeholder="请输入销售订单"
          clearable
          style="width: 150px; margin-right: 10px"
        />
        <span class="show-text">物料名称:</span>
        <el-input
          v-model="partName"
          placeholder="请输入物料名称"
          clearable
          style="width: 150px; margin-right: 10px"
        />
        <span class="show-text">物料编码:</span>
        <el-input
          v-model="partNo"
          placeholder="请输入物料编号"
          clearable
          style="width: 150px; margin-right: 10px"
        />
        <span class="show-text">属性</span>
        <el-select
          v-model="attribute"
          style="width: 150px; margin-right: 10px"
          filterable
        >
          <el-option label="装配" value="2" />
          <el-option label="机加" value="1" />
          <el-option label="全部" value="0" />
        </el-select>
        <span class="show-text">检测结果</span>
        <el-select
          v-model="inspectionResult"
          clearable
          style="width: 150px; margin-right: 10px"
          filterable
        >
          <el-option label="合格" value="1" />
          <el-option label="不合格" value="0" />
        </el-select>
        <span class="show-text">检测人:</span>
        <el-select
          v-model="checker"
          placeholder="请选择检测人"
          clearable
          style="width: 150px; margin-right: 10px"
          filterable
        >
          <el-option
            v-for="item in checkerList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-button
          :disabled="loading"
          icon="el-icon-search"
          plain
          type="primary"
          @click="searchTable()"
        >查询</el-button>
      </div>

      <el-table
        v-loading="loading"
        :data="tableData"
        :height="'67vh'"
        border
        element-loading-background="rgba(0, 0, 0, 0.2)"
        element-loading-spinner="el-icon-loading"
        highlight-current-row
        size="mini"
        style="width: 100%; margin-top: 20px"
        tooltip-effect="dark"
        :row-class-name="tableRowClassName"
      >
        <el-table-column align="center" label="序号" type="index" width="50" fixed />
        <el-table-column align="center" label="销售订单" width="100" prop="合同号" />
        <el-table-column align="center" label="生产订单" width="100" prop="计划号" />
        <el-table-column align="center" label="物料名称" width="100" prop="零件名称" />
        <el-table-column align="center" label="物料编码" width="100" prop="零件编码" />
        <el-table-column align="center" label="工序名称" width="80" prop="工序名称" />
        <el-table-column align="center" label="工位名称" width="80" prop="工位名称" />
        <el-table-column align="center" label="供应商" width="80" prop="供应商" />
        <el-table-column align="center" label="采购收货单" width="100" prop="采购收货单" />
        <el-table-column align="center" label="加急数量" width="120" prop="加急总数" />
        <el-table-column align="center" label="完成数量" width="100" prop="完成数量" />
        <el-table-column align="center" label="检验数量" width="120" prop="检验数量" />
        <el-table-column align="center" label="检验合格数量" width="110" prop="合格数" />
        <el-table-column align="center" label="检验不合格数量" width="120" prop="不合格数" />
        <el-table-column align="center" label="检验操作人" width="130" prop="检测人" />
        <el-table-column align="center" label="检验时间" width="180" prop="检测时间" />
        <el-table-column align="center" label="报工结束时间" width="180" prop="结束时间" />
        <el-table-column align="center" label="超期天数" width="90" prop="超期天数" />
        <el-table-column align="center" label="检测说明" width="180" prop="检测说明" />
      </el-table>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'PunctualInspectionReport',
  data() {
    return {
      contractNo: '',
      orderNo: '',
      partNo: '',
      partName: '',
      completionTime: '',
      inspectionDate: '',
      checker: '',
      checkerList: [],
      inspectionResult: '',
      tableData: [],
      loading: false,
      attribute: '0'
    }
  },
  created() {
    this.getCheckerList()
    this.searchTable()
  },
  methods: {
    getCheckerList() {
      this.checkerList = []
      const data = this.CreateData('11', '人员管理_人员信息查询_质检部门_查询')
      this.ExecDatabase(data).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.checkerList.push({
            label: response.data[i].Personnel_Name,
            value: response.data[i].Personnel_Number
          })
        }
      })
    },
    tableRowClassName({ row }) {
      if (row.加急状态 === 1) {
        return 'priority-row'
      }
      return ''
    },
    async searchTable() {
      this.loading = true
      this.tableData = []

      const param = []
      param.push(['合同号', this.contractNo])
      param.push(['订单号', this.orderNo])
      param.push(['零件编号', this.partNo])
      param.push(['零件名称', this.partName])
      param.push(['完工日期_Start', this.completionTime ? this.completionTime[0] : ''])
      param.push(['完工日期_End', this.completionTime ? this.completionTime[1] : ''])
      param.push(['检测日期_Start', this.inspectionDate ? this.inspectionDate[0] : ''])
      param.push(['检测日期_End', this.inspectionDate ? this.inspectionDate[1] : ''])
      param.push(['检测结果', this.inspectionResult])
      const selectedChecker = this.checkerList.find(item => item.value === this.checker)
      param.push(['检测人', selectedChecker ? selectedChecker.label : ''])
      param.push(['检测类别', ''])
      param.push(['属性', this.attribute])

      const data = this.CreateData('11', '质量管理_及时检验报表_查询', param)
      try {
        const response = await this.ExecDatabase(data)
        const list = response.data || []
        this.tableData = list

        if (this.tableData.length === 0) {
          this.$message.info('未查询到超过2天未检验的数据')
        }
      } catch (error) {
        console.error('查询失败:', error)
        this.$message.error('查询失败')
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style scoped>
.search-container {
  margin-bottom: 20px;
}

.show-text {
  margin-right: 5px;
  line-height: 32px;
}

::v-deep .priority-row {
  background-color: #f56c6c !important;
  color: #000000 !important;
}

::v-deep .priority-row td {
  background-color: #f56c6c !important;
  color: #000000 !important;
  border: #f56c6c;
}
</style>
