<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="60px">
      <el-form-item label="任务名称" prop="taskName">
        <el-input
          v-model="queryParams.taskName"
          placeholder="请输入任务名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 上方任务列表表格 -->
    <div class="mb10">
      <div class="table-title">检验历史列表</div>
      <KLPTable ref="historyTable" height="300px" highlight-current-row v-loading="loading" :data="checkTaskList" @row-click="handleRowClick" row-class-name="tableRowClassName">
      <el-table-column label="任务名称" align="center" prop="taskName" />
      <el-table-column label="工段" align="center" prop="workshopSection" />
      <el-table-column label="工序" align="center" prop="process" />
      <el-table-column label="机组" align="center" prop="unitGroup" />
      <el-table-column label="检验结果" align="center" prop="inspectionResult">
        <template slot-scope="scope">
          <el-tag :type="scope.row.inspectionResult === 1 ? 'success' : (scope.row.inspectionResult === 2 ? 'danger' : 'info')">
            {{ scope.row.inspectionResult === 0 ? '未检验' : (scope.row.inspectionResult === 1 ? '合格' : (scope.row.inspectionResult === 2 ? '不合格' : '')) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="检验类型" align="center" prop="inspectionType" />
      <el-table-column label="备注" align="center" prop="remark" />
      </KLPTable>

    </div>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 下方检测详情表格 -->
    <div class="detail-section">
      <div class="table-title">检测详情</div>
      <div v-if="selectedTask && detailData.itemList && detailData.itemList.length > 0">
        <KLPTable v-loading="loading" :data="detailData.itemList" style="margin-top: 10px;">
          <el-table-column label="检查项" align="center" prop="itemName">
            <template slot-scope="scope">
              <el-tag type="info">{{ scope.row.itemName }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="定性定量" align="center">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.qualitativeQuantitative !== undefined" :type="scope.row.qualitativeQuantitative === 0 ? 'primary' : 'warning'">
                {{ scope.row.qualitativeQuantitative === 0 ? '定性' : '定量' }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="标准目标" align="center">
            <template slot-scope="scope">
              {{ scope.row.standardTarget }} {{ scope.row.unit || '' }}
            </template>
          </el-table-column>
          <el-table-column label="目标上限" align="center">
            <template slot-scope="scope">
              {{ scope.row.targetUpper }} {{ scope.row.unit || '' }}
            </template>
          </el-table-column>
          <el-table-column label="目标下限" align="center">
            <template slot-scope="scope">
              {{ scope.row.targetLower }} {{ scope.row.unit || '' }}
            </template>
          </el-table-column>
          <el-table-column label="实测值" align="center">
            <template slot-scope="scope">
              {{ scope.row.actualMeasure }} {{ scope.row.unit || '' }}
            </template>
          </el-table-column>
          <el-table-column label="状态" align="center">
            <template slot-scope="scope">
              <el-tag
                :type="scope.row.status == 1 ? 'success' : (scope.row.status == 2 ? 'danger' : 'info')"
              >
                <span v-if="scope.row.status == 0">未检测</span>
                <span v-else-if="scope.row.status == 1">通过</span>
                <span v-else-if="scope.row.status == 2">不通过</span>
                <span v-else>未知</span>
              </el-tag>
            </template>
          </el-table-column>
        </KLPTable>
      </div>
      <div v-else class="empty-tip">
        <el-empty description="请选择一个检验历史任务查看详情"></el-empty>
      </div>
    </div>
  </div>
</template>

<script>
import { listHistoryCheckTask, getCheckTask } from '@/api/mes/qc/checkTask'

export default {
  name: 'CheckTaskHistory',
  data() {
    return {
      loading: true,
      showSearch: true,
      total: 0,
      checkTaskList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        taskName: undefined,
      },
      detailData: {},
      // 当前选中的任务（单选）
      selectedTask: null,
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      listHistoryCheckTask(this.queryParams).then(response => {
        this.checkTaskList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    
    // 点击行选中任务
    handleRowClick(row) {
      if (this.selectedTask && this.selectedTask.taskId === row.taskId) {
        // 点击当前选中行，取消选中
        this.selectedTask = null
        this.detailData = {}
      } else {
        // 选中新行
        this.selectedTask = row
        this.loadTaskDetail(row.taskId)
      }
    },
    
    // 表格行样式
    tableRowClassName({row, rowIndex}) {
      return this.selectedTask && this.selectedTask.taskId === row.taskId ? 'current-row' : ''
    },
    
    // 加载任务详情
    loadTaskDetail(taskId) {
      this.loading = true
      getCheckTask(taskId).then(response => {
        this.detailData = response.data
      }).finally(() => {
        this.loading = false
      })
    },
    handleExport() {
      this.download('qc/checkTask/export', {
        ...this.queryParams
      }, `checkTask_${new Date().getTime()}.xlsx`)
    }
  },
  
  // 添加样式
  computed: {
    styles() {
      return {
        mb10: { marginBottom: '10px' },
        detailSection: { 
          marginTop: '20px', 
          border: '1px solid #e6e6e6', 
          borderRadius: '4px', 
          padding: '10px'
        },
        tableTitle: { 
          fontSize: '16px', 
          fontWeight: 'bold', 
          marginBottom: '10px',
          color: '#333'
        },
        emptyTip: { 
          textAlign: 'center', 
          padding: '40px 0',
          backgroundColor: '#fafafa',
          borderRadius: '4px'
        }
      }
    }
  }
}
</script>
