<template>
  <div class='inspection-record'>
    <el-card shadow='never'>
      <template #header>
        <div class='card-header'>
          <span>质检记录</span>
          <el-button type='primary' icon='Refresh' @click='getList'>刷新</el-button>
        </div>
      </template>

      <el-table
        v-loading='loading'
        :data='recordList'
        border
        stripe
        style='width: 100%'
      >
        <el-table-column label='序号' align='center' type='index' width='55' />
        <el-table-column label='检测单号' align='center' prop='inspectionNo' />
        <el-table-column label='模板名称' align='center' prop='templateName' />
        <el-table-column label='物料编码' align='center' prop='materialCode' />
        <el-table-column label='物料名称' align='center' prop='materialName' show-overflow-tooltip />
        <el-table-column label='工序名称' align='center' prop='processName' />
        <el-table-column label='工位名称' align='center' prop='stationName' />
        <el-table-column label='质检数量' align='center' prop='inspectionQty' />
        <el-table-column label='合格数' align='center' prop='qualifiedQty' />
        <el-table-column label='不合格数' align='center' prop='unqualifiedQty' />
        <el-table-column label='质检结果' align='center' prop='result'>
          <template #default='scope'>
            <dict-tag :options='qc_result' :value='scope.row.result' />
          </template>
        </el-table-column>
        <el-table-column label='车间' align='center' prop='workshop' />
        <el-table-column label='检测类别' align='center' prop='typeName' />
        <el-table-column label='单据状态' align='center' prop='status'>
          <template #default='scope'>
            <dict-tag :options='qc_status' :value='scope.row.status' />
          </template>
        </el-table-column>
        <el-table-column label='检测人员' align='center' prop='inspector' />
        <el-table-column label='班次' align='center' prop='shift' />
        <el-table-column label='班组' align='center' prop='team' />
        <el-table-column label='检验时间' align='center' prop='inspectionStartTime' width='180'>
          <template #default='scope'>
            <span>{{ parseTime(scope.row.inspectionStartTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label='检验结束时间' align='center' prop='inspectionEndTime' width='180'>
          <template #default='scope'>
            <span>{{ parseTime(scope.row.inspectionEndTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label='业务来源单号' align='center' prop='productionOrder' />
        <el-table-column label='批次号' align='center' prop='batchNo' />
        <el-table-column label='条码号' align='center' prop='barcode' />
        <el-table-column label='供应商名称' align='center' prop='supplierName' />
        <el-table-column label='备注' align='center' prop='remark' show-overflow-tooltip />
      </el-table>

      <pagination
        v-show='total > 0'
        :total='total'
        v-model:page='queryParams.pageNum'
        v-model:limit='queryParams.pageSize'
        @pagination='getList'
      />
    </el-card>
  </div>
</template>

<script setup name='InspectionRecord' lang='ts'>
import { ref, onMounted, getCurrentInstance, toRefs } from 'vue';
import { listQcInspectionMain } from '@/api/qms/QcInspectionMain';
import { QcInspectionMainVO, QcInspectionMainQuery } from '@/api/qms/QcInspectionMain/types';
import { parseTime } from '@/utils/ruoyi';

const { proxy } = getCurrentInstance() as any;
const { qc_result, qc_status } = toRefs<any>(proxy?.useDict('qc_result', 'qc_status'));

const props = defineProps<{
  workstationId?: string | number;
  workstation?: string;
}>();

const loading = ref(false);
const recordList = ref<QcInspectionMainVO[]>([]);
const total = ref(0);

const queryParams = ref<QcInspectionMainQuery>({
  pageNum: 1,
  pageSize: 10
});

// 获取质检记录列表
const getList = async () => {
  loading.value = true;
  try {
    const query: QcInspectionMainQuery = {
      ...queryParams.value
    };

    // 如果有工位名称，添加到查询条件中
    if (props.workstation) {
      query.stationName = props.workstation;
    }

    const res = await listQcInspectionMain(query);
    recordList.value = res.rows || [];
    total.value = res.total || 0;
  } catch (error) {
    console.error('获取质检记录失败:', error);
    recordList.value = [];
    total.value = 0;
  } finally {
    loading.value = false;
  }
};

// 刷新数据
const refresh = () => {
  getList();
};

defineExpose({
  refresh
});

onMounted(() => {
  getList();
});
</script>

<style scoped lang='scss'>
.inspection-record {
  width: 100%;
  height: 100%;

  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
}
</style>
