<template>
  <div class="page-container">
    <n-card>
      <!-- 搜索表单 -->
      <div class="search-form">
        <n-form inline :model="searchForm" label-placement="left">
          <n-form-item label="质检编号">
            <n-input v-model:value="searchForm.qcNo" placeholder="请输入质检编号" clearable />
          </n-form-item>
          <n-form-item label="质检状态">
            <n-select
                v-model:value="searchForm.status"
                :options="qcStatusOptions"
                placeholder="请选择质检状态"
                clearable
                style="width: 200px"
            />
          </n-form-item>
          <n-form-item>
            <n-space>
              <n-button type="primary" @click="handleSearch">
                <template #icon><n-icon><SearchOutline /></n-icon></template>
                搜索
              </n-button>
              <n-button @click="handleReset">
                <template #icon><n-icon><RefreshOutline /></n-icon></template>
                重置
              </n-button>
            </n-space>
          </n-form-item>
        </n-form>
      </div>

      <!-- 工具栏 -->
      <div class="table-toolbar">
        <n-space>
          <n-button type="primary" @click="handleAdd">
            <template #icon><n-icon><AddOutline /></n-icon></template>
            新增
          </n-button>
          <n-button @click="importModalVisible = true">
            <template #icon><n-icon><CloudUploadOutline /></n-icon></template>
            导入
          </n-button>
          <n-button @click="handleExport">
            <template #icon><n-icon><DownloadOutline /></n-icon></template>
            导出{{ selectedIds.length > 0 ? `(${selectedIds.length})` : '' }}
          </n-button>
          <n-button type="error" :disabled="selectedIds.length === 0" @click="handleBatchDelete">
            <template #icon><n-icon><TrashOutline /></n-icon></template>
            删除
          </n-button>
        </n-space>
      </div>

      <!-- 表格 -->
      <n-data-table
          :columns="columns"
          :data="tableData"
          :loading="loading"
           size="small"
            remote
            :border="false"
            :single-line="false"
            striped
      />
      <div class="pagination-container" style="display: flex; justify-content: flex-end; margin-top: 12px">
          <n-pagination
            v-model:page="pagination.page"
            v-model:page-size="pagination.pageSize"
            :item-count="pagination.itemCount"
            :page-sizes="[10, 20, 50, 100]"
            show-size-picker
            show-quick-jumper
            @update:page="handlePageChange"
            @update:page-size="handlePageSizeChange"
          >
            <template #prefix>
              共 {{ pagination.itemCount }} 条
            </template>
          </n-pagination>
         </div>
    </n-card>

    <!-- 新增/编辑弹窗 -->
    <n-modal v-model:show="modalVisible" preset="card" :title="modalTitle" style="width: 600px">
      <n-form ref="formRef" :model="formData" :rules="formRules" label-placement="left" label-width="100px">
        <n-form-item label="质检编号">
          <n-input v-model:value="formData.qcNo" placeholder="请输入质检编号" disabled />
        </n-form-item>


        <n-grid x-gap="12" :cols="2">
          <n-gi>
            <n-form-item label="来源类型" path="sourceType">
              <n-select
                  v-model:value="formData.sourceType"
                  :options="qcSourceTypeOptions"
                  placeholder="请选择来源类型"
                  clearable
                  style="width: 200px"
              />
            </n-form-item>
          </n-gi>
          <n-gi>
            <n-form-item label="业务来源" path="sourceId">
              <n-select
                  v-model:value="formData.sourceId"
                  :options="assingWork"
                  placeholder="请选择业务来源"
                  clearable
                  :disabled="assingWorkDisabled"
                  style="width: 200px"
              />
            </n-form-item>
          </n-gi>
        </n-grid>
        <n-grid x-gap="12" :cols="2">
          <n-gi>
            <n-form-item label="报检总数" path="totalQty">
              <n-input-number v-model:value="formData.totalQty" placeholder="请输入报检总数" :min="1" />
            </n-form-item>
          </n-gi>
          <n-gi>
            <n-form-item label="质检状态" >
              <n-select v-model:value="formData.status" placeholder="请选择状态:0待检/1检验中/2已判定/3已关闭" :options="qcStatusOptions" />
            </n-form-item>
          </n-gi>
        </n-grid>
        <n-form-item label="追溯类型" path="traceType">
          <n-select
              v-model:value="formData.traceType"
              :options="qcTraceTypeOptions"
              placeholder="请选择追溯类型:0无/1批次/2SN"
              clearable
              style="width: 200px"
          />
        </n-form-item>

      </n-form>
      <template #footer>
        <n-space justify="end">
          <n-button @click="modalVisible = false">取消</n-button>
          <n-button type="primary" @click="handleSubmit">确定</n-button>
        </n-space>
      </template>
    </n-modal>

    <!-- 导入弹窗 -->
    <n-modal v-model:show="importModalVisible" preset="card" title="导入质检单表" style="width: 500px">
      <n-space vertical>
        <n-alert type="info">
          <template #header>导入说明</template>
          <ul style="margin: 0; padding-left: 16px; line-height: 1.8">
            <li>请先下载导入模板，按模板格式填写数据</li>
            <li>支持 .xlsx 或 .xls 格式</li>
          </ul>
        </n-alert>
        <n-space>
          <n-button type="primary" @click="handleDownloadTemplate">
            <template #icon><n-icon><DownloadOutline /></n-icon></template>
            下载模板
          </n-button>
        </n-space>
        <n-upload :max="1" accept=".xlsx,.xls" :show-file-list="true" :custom-request="handleImportUpload">
          <n-upload-dragger>
            <div style="margin-bottom: 12px">
              <n-icon size="48" :depth="3"><CloudUploadOutline /></n-icon>
            </div>
            <n-text style="font-size: 16px">点击或拖拽文件到此处上传</n-text>
            <n-p depth="3" style="margin: 8px 0 0 0">支持 .xlsx 或 .xls 格式</n-p>
          </n-upload-dragger>
        </n-upload>
      </n-space>
      <template #footer>
        <n-button @click="importModalVisible = false">关闭</n-button>
      </template>
    </n-modal>


    <!--质检单详情面板-->
    <n-modal v-model:show="detailVisible" preset="card" :title="detailTitle" style="width: 800px">
      <n-card hoverable style="margin-bottom: 12px" >
        <template #header>
          <div style="display:flex;justify-content:space-between;align-items:center;width:100%">
            <span>主表信息</span>
            <n-button text @click="infoIsCollapse = !infoIsCollapse">
              <n-icon :component="infoIsCollapse ? ArrowDownOutline : ArrowUpOutline" />
            </n-button>
          </div>
        </template>
        <n-descriptions :column="3" bordered v-show="!infoIsCollapse">
          <n-descriptions-item
              v-for="item in mappedInfoList"
              :key="item.key"
              :label="item.label"
          >
            {{ formatDisplayValue(item.key, item.value) }}
          </n-descriptions-item>
        </n-descriptions>
      </n-card>
      <n-card hoverable style="margin-bottom: 12px">
         <template #header>
          <div style="display:flex;justify-content:space-between;align-items:center;width:100%">
            <span>明细列表</span>
            <n-button text @click="detailIsCollapse = !detailIsCollapse">
              <n-icon :component="detailIsCollapse ? ArrowDownOutline : ArrowUpOutline" />
            </n-button>
          </div>
        </template>
        <n-data-table
             v-show="!detailIsCollapse"
            :columns="detailColumns"
            :data="detailTableData"
            :loading="loading"
            :row-key="(row:any) => row.id"
            :scroll-x="700"
            max-height="320"
            :pagination="{ pageSize:4 }"
        />
      </n-card>
<!--      <n-card title="追溯码" hoverable>-->
<!--        追溯码：{{}}-->
<!--      </n-card>-->
    </n-modal>

    <!-- 提交判定明细抽屉 -->
     <n-drawer v-model:show="showSubmitOuter" :width="800">
        <SumbitDetailCard
          :showOkTab="true"
          :showScrapTab="true"
          :showReworkTab = true
          :showConnessionTab="true"
          :showTraceCode = "true"
          :qcId="qcId"
          @qcSumitDetail="handleSubmitDetail"
        />

        <n-space justify="center" style="width: 100%; margin-top: 16px;">
                <n-button size="small" @click="handleReset">重置</n-button>
                <n-button type="primary" size="small" @click="handleSubmitResultDetail">提交</n-button>
        </n-space>
     </n-drawer>


  </div>
</template>

<script setup lang="ts">
import { ref, reactive, h, onMounted } from 'vue'
import {
  NButton, NSpace, NIcon, NUpload,NTag, useMessage, useDialog, type DataTableColumns, type UploadCustomRequestOptions,
  NDropdown
} from 'naive-ui'

import {
  SearchOutline, RefreshOutline, AddOutline, TrashOutline, CreateOutline, CloudUploadOutline, DownloadOutline,
  EllipsisHorizontalOutline, ArrowDownOutline, ArrowUpOutline,ArchiveOutline as ArchiveIcon
} from '@vicons/ionicons5'
import { qualityTestingApi, type QualityTesting } from '@/api/qualityTesting'
import {assingReworkApi} from '@/api/assingRework'
import { dictDataApi } from '@/api/org'

import {qcResultDetailApi, type QcResultDetail} from "@/api/qcResultDetail.ts";
import SumbitDetailCard from '@/components/SumbitDetailCard.vue'

import router from "@/router";


const message = useMessage()
const dialog = useDialog()

//提交明细抽屉
const showSubmitOuter = ref<Boolean>(false) 
const qcId = ref<number>(-1)


// 表格数据
const tableData = ref<QualityTesting[]>([])
const loading = ref(false)
const selectedIds = ref<number[]>([])
const pagination = reactive({
  page: 1,
  pageSize: 10,
  itemCount: 0,
  showSizePicker: true,
  pageSizes: [10, 20, 50]
})

// 搜索表单
const searchForm = reactive({
  qcNo:  null as string | null,
  status:  null as number | null,
})

// 弹窗
const modalVisible = ref(false)
const modalTitle = ref('')
const importModalVisible = ref(false)
//质检单详情
const detailVisible = ref(false)
const detailTitle = ref('')

const infoIsCollapse = ref(false)
const detailIsCollapse = ref(true)

//提交判定明细
// const submitDetailModalVisible = ref(false)
const traceCodeShow = ref(false)
//提交明细/部分提交明细标识
const showTraceFlag = ref<Boolean>()


const customOrderKeys = [
  'materialCode',
  'materialName',
  'materialQty',
  'processCode',
  'processName',
  'processQty',
  'assingCode',
  'workerName',
  'assingQty',
  'qcNo',
  'sourceType',
  'totalQty',
  'status',
  'inspector',
  'inspectTime',
  'traceType'
]

const infoDataMapping = {
  
  materialCode: '物料编码',
  materialName: '物料名称',
  materialQty: '物料数量',
  processCode:'工序编号',
  processName:'工序名称',
  processQty:'生产总数',
  assingCode:'派工编号',
  workerName:'派工人',
  assingQty:'派工数量',
  qcNo: '质检单编号',
  sourceType: '来源类型',
  totalQty: '报检总数',
  status: '质检状态',
  inspector: '质检员',
  inspectTime: '质检时间',
  traceType: '追溯类型'
}

const mappedInfoList = ref<Array<{ label: string; key: string; value: any }>>([])
//详情列表名
const detailColumns: DataTableColumns<QcResultDetail> = [
  {
    title: '判定类型',
    key: 'resultType',
    align: 'center',
    width: 80,
    render: (row) => {
      const val = row.resultType
      const opt = qcResultTypeOptions.value.find(o => o.value === val || String(o.value) === String(val))
        if (!opt) return val ?? '-'
     
       return h(NTag, { type: opt.class, size: 'small' }, { default: () => opt.label })
    }
  },
  {
    title: '判定数量',
    key: 'qty',
    align: 'center',
    width: 80
  },
  {
    title: '批次号',
    key: 'traceCode',
    width: 80,
    align: 'center',
    render: (row) => {
      const val = row.traceCode
      return val ?? '-'
    }
  },
  {
    title: '不良代码',
    key: 'defectCode',
    width: 80,
    align: 'center',
    render: (row) => {
      const val = row.defectCode
      return val ?? '-'
    }
  },
  {
    title: '后续动作',
    key: 'handleAction',
    width: 80,
    align: 'center'
  },
  {
    title: '动作执行状态',
    key: 'actionStatus',
    width: 80,
    align: 'center',
    render: (row) => {
      const val = row.actionStatus
      const opt = qcActionStatusOptions.value.find(o => o.value === val || String(o.value) === String(val))
        if (!opt) return val ?? '-'
     
       return h(NTag, { type: opt.class, size: 'small' }, { default: () => opt.label })
    }
  }
]

const detailTableData = ref<Object[]>([])


const formRef = ref()
const defaultFormData: QualityTesting = {
  qcNo: undefined,
  sourceType: undefined,
  sourceId: undefined,
  orderId: undefined,
  processId: undefined,
  itemId: undefined,
  totalQty:  undefined,
  status: undefined,
  inspectorId: undefined,
  inspectTime:  undefined,
  traceType: undefined,
}
const formData = reactive<QualityTesting>({ ...defaultFormData })

const defaultFormDetailData:QcResultDetail  = {
  qcId: undefined,
  resultType: undefined,
  qty:  0,
  traceCode: undefined,
  defectCode: undefined,
  handleAction: undefined,
  actionStatus: undefined,
  remark: '',
  title:undefined,
  images:[],
  attachments:[]
}

let formDetailOKData = reactive<QcResultDetail>({ ...defaultFormDetailData, resultType: 1,handleAction:'下一道工序',actionStatus:1 })

let formDetailScrapData = reactive<QcResultDetail>({ ...defaultFormDetailData,resultType: 2,handleAction:'报废次品',actionStatus:1 })
let formDetailReworkData = reactive<QcResultDetail>({ ...defaultFormDetailData,resultType: 3,handleAction:'返工',actionStatus:1 })
let formDetailConcessionData = reactive<QcResultDetail>({ ...defaultFormDetailData,resultType: 4,handleAction:'让步接收',actionStatus:1  })

const assingWorkDisabled = ref(true)

const assingWork = ref<{ label: string; value: any }[]>([])

// 字典选项（下拉框/单选框/复选框关联字典时使用）
const statusOptions = ref<{ label: string; value: any;class:any}[]>([])
//质检状态字典
const qcStatusOptions =  ref<{ label: string; value: any;class:any }[]>([])
//业务来源字典
const qcSourceTypeOptions =  ref<{ label: string; value: any;class:any }[]>([])
//追溯状态字段
const qcTraceTypeOptions =  ref<{ label: string; value: any;class:any }[]>([])
//判定类型
const qcResultTypeOptions =  ref<{ label: string; value: any;class:any }[]>([])
//动作执行状态
const qcActionStatusOptions =  ref<{ label: string; value: any;class:any }[]>([])
// 表单校验规则
const formRules = {
  sourceType: [{ required: true, type: 'number', message: '请选择来源类型:1工序汇报/2委外/3手工', trigger: 'change' }],
  totalQty: [{ required: true,type: 'number', message: '请输入报检总数', trigger: 'blur' }],
}

// 表格列
const columns: DataTableColumns<QualityTesting> = [
  { type: 'selection' },
  { title: '质检编号', key: 'qcNo',align: 'center' },
  { title: '来源类型', key: 'sourceType',align: 'center',
    render(row) {
      const val = row.sourceType
      const opt = qcSourceTypeOptions.value.find(o => o.value === val || String(o.value) === String(val))
       if (!opt) return val ?? '-'
     
       return h(NTag, { type: opt.class, size: 'small' }, { default: () => opt.label })
    }
  },
  // { title: '来源业务ID(汇报单/派工单等)', key: 'sourceId' },
  // { title: '关联生产订单ID', key: 'orderId' },
  // { title: '关联工序计划ID', key: 'processId' },
  // { title: '物料ID', key: 'itemId' },
  { title: '追溯码', key: 'traceCode',align: 'center' },
  { title: '报检总数', key: 'totalQty',align: 'center' },
  { title: '已检验数量', key: 'inspectQty',align: 'center' },
  { title: '质检状态', key: 'status',align: 'center',
    render(row) {
      const val = row.status
      const opt = qcStatusOptions.value.find(o => o.value === val || String(o.value) === String(val))
      if (!opt) return val ?? '-'
     return h(NTag, { type: opt.class, size: 'small' }, { default: () => opt.label })
    }
  },
  { title: '检验员', key: 'inspector',align: 'center' },
  { title: '检验完成时间', key: 'inspectTime',align: 'center' },
  { title: '追溯类型', key: 'traceType',align: 'center',
    render(row) {
      const val = row.traceType
      const opt = qcTraceTypeOptions.value.find(o => o.value === val || String(o.value) === String(val))
        if (!opt) return val ?? '-'
     
       return h(NTag, { type: opt.class, size: 'small' }, { default: () => opt.label })
    }
  },
  { title: '创建时间', key: 'createTime',align: 'center', width: 180 },
  { title: '更新时间', key: 'updateTime',align: 'center', width: 180 },
  {
    title: '操作',
    key: 'actions',
    width: 140,
    fixed: 'right',
    render(row) {
      return h('div', { style: { display: 'flex', alignItems: 'center', gap: '8px', flexWrap: 'nowrap' } }, [
        h(NButton, { size: 'small', quaternary: true, onClick: () => handleEdit(row) }, {
          default: () => [h(NIcon, null, { default: () => h(CreateOutline) }), ' 编辑']
        }),
        h(NButton, { size: 'small', quaternary: true, type: 'error', onClick: () => handleDelete(row) }, {
          default: () => [h(NIcon, null, { default: () => h(TrashOutline) }), ' 删除']
        }),
        h(NDropdown, {
          trigger: 'click', // 点击展开
          options: [
            { label: '查看详情', key: 'view' },
            { type: 'divider' }, // 分割线
            { label: '提交判定明细', key: 'submitResultDetail',show:row.status == 1 },
            // { type: 'divider',show:row.status == 1 }, // 分割线
            // { label: '提交部分判定明细', key: 'submitBFResultDetail',show:(row.status == 1 || row.status == 2) },
            { type: 'divider',show:(row.status == 1 || row.status == 2) }, // 分割线
            { label: '查询判定明细', key: 'queryResultDetail' },
            // { type: 'divider' }, // 分割线
            // { label: '质检撤回', key: 'cancelQuality' },
          ],
          // 下拉菜单项点击事件
          onSelect: (key: string) => {
            switch (key) {
              case 'view':
                handleDetail(row)
                break
              case 'submitResultDetail':
                submitResultDetail(row)
                break
              // case 'submitBFResultDetail':
              //   submitBFResultDetail(row)
              //   break  
              case 'queryResultDetail':
                queryResultDetail(row)
                break
              // case 'cancelQuality':
              //   handleCancelQuality(row)
              //   break
            }
          }
        }, {
          // 下拉触发按钮：三点图标 / 更多文字
          default: () => h(NButton, { size: 'small', quaternary: true }, {
            default: () => [h(NIcon, null, { default: () => h(EllipsisHorizontalOutline) })]
          })
        })
      ])
    }
  }
]

// 加载数据
async function loadData() {
  loading.value = true
  try {
    const res = await qualityTestingApi.page({
      page:pagination.page,
      pageSize: pagination.pageSize,
      qcNo:searchForm.qcNo,
      status: searchForm.status
    })
    tableData.value = res.list
    pagination.itemCount = res.total
  } finally {
    loading.value = false
  }
}

// 搜索
function handleSearch() {
  pagination.page = 1
  loadData()
}

// 重置
function handleReset() {
  searchForm.qcNo =  null
  searchForm.status =  null

  handleSearch()
}

// 分页
function handlePageChange(page: number) {
  pagination.page = page
  loadData()
}

function handlePageSizeChange(pageSize: number) {
  pagination.pageSize = pageSize
  pagination.page = 1
  loadData()
}



// 选择
function handleCheck(keys: Array<string | number>) {
  selectedIds.value = keys as number[]
}

// 新增
function handleAdd() {
  modalTitle.value = '新增质检单表'
  assingWorkDisabled.value = false
  Object.assign(formData, defaultFormData)
  modalVisible.value = true
}

// 编辑
function handleEdit(row: QualityTesting) {
  modalTitle.value = '编辑质检单表'
  assingWorkDisabled.value = true
  console.log(row);
  
  Object.assign(formData, row)
  if (formData.inspectTime && typeof formData.inspectTime === 'string') {
    formData.inspectTime = new Date(formData.inspectTime.replace(' ', 'T')).getTime()
  }
  if (formData.createTime && typeof formData.createTime === 'string') {
    formData.createTime = new Date(formData.createTime.replace(' ', 'T')).getTime()
  }
  if (formData.updateTime && typeof formData.updateTime === 'string') {
    formData.updateTime = new Date(formData.updateTime.replace(' ', 'T')).getTime()
  }
  modalVisible.value = true
}

// 提交
async function handleSubmit() {
  await formRef.value?.validate()
  try {
    const submitData = { ...formData } as QualityTesting
    if (typeof submitData.inspectTime === 'number') {
      submitData.inspectTime = new Date(submitData.inspectTime).toISOString().slice(0, 19).replace('T', ' ')
    }
    if (typeof submitData.createTime === 'number') {
      submitData.createTime = new Date(submitData.createTime).toISOString().slice(0, 19).replace('T', ' ')
    }
    if (typeof submitData.updateTime === 'number') {
      submitData.updateTime = new Date(submitData.updateTime).toISOString().slice(0, 19).replace('T', ' ')
    }
    if (submitData.id) {
      await qualityTestingApi.update(submitData)
      message.success('修改成功')
    } else {
      await qualityTestingApi.create(submitData)
      message.success('新增成功')
    }
    modalVisible.value = false
    loadData()
  } catch (error) {
    // 错误已在拦截器处理
  }
}

// 删除
function handleDelete(row: QualityTesting) {
  dialog.warning({
    title: '提示',
    content: '确定要删除该记录吗？',
    positiveText: '确定',
    negativeText: '取消',
    onPositiveClick: async () => {
      try {
        await qualityTestingApi.delete([row.id!])
        message.success('删除成功')
        loadData()
      } catch (error) {
        // 错误已在拦截器处理
      }
    }
  })
}

// 批量删除
function handleBatchDelete() {
  dialog.warning({
    title: '提示',
    content: `确定要删除选中的 ${selectedIds.value.length} 条记录吗？`,
    positiveText: '确定',
    negativeText: '取消',
    onPositiveClick: async () => {
      try {
        await qualityTestingApi.delete(selectedIds.value)
        message.success('删除成功')
        selectedIds.value = []
        loadData()
      } catch (error) {
        // 错误已在拦截器处理
      }
    }
  })
}

// 导出
async function handleExport() {
  try {
    const params: Record<string, any> = {}
    if (selectedIds.value.length > 0) params.ids = selectedIds.value
    if (searchForm.qcNo != null) params.id = searchForm.qcNo
    if (searchForm.status != null) params.status = searchForm.status
    const blob = await qualityTestingApi.export(params)
    const url = window.URL.createObjectURL(blob)
    const link = document.createElement('a')
    link.href = url
    link.download = '质检单表数据.xlsx'
    link.click()
    window.URL.revokeObjectURL(url)
  } catch (error) {
    // 错误已在拦截器处理
  }
}

// 下载导入模板
async function handleDownloadTemplate() {
  try {
    const blob = await qualityTestingApi.downloadTemplate()
    const url = window.URL.createObjectURL(blob)
    const link = document.createElement('a')
    link.href = url
    link.download = '质检单表导入模板.xlsx'
    link.click()
    window.URL.revokeObjectURL(url)
  } catch (error) {
    // 错误已在拦截器处理
  }
}

// 导入上传
async function handleImportUpload({ file }: UploadCustomRequestOptions) {
  if (!file.file) return
  try {
    const result = await qualityTestingApi.importData(file.file)
    if (result.fail > 0) {
      dialog.warning({
        title: '导入结果',
        content: `成功: ${result.success} 条，失败: ${result.fail} 条\n错误信息: ${(result.errors || []).join('\n') || '无'}`,
        positiveText: '确定'
      })
    } else {
      message.success(`导入成功，共 ${result.success} 条数据`)
      importModalVisible.value = false
    }
    loadData()
  } catch (error) {
    // 错误已在拦截器处理
  }
}

// 加载字典选项
async function loadDictOptions() {
  try {
    const data = await dictDataApi.listByType('sys_status')
    statusOptions.value = data.map(d => ({ label: d.dictLabel, value: (Number(d.dictValue) || d.dictValue),class: d.listClass }))
  } catch {}

  try {
    const data = await dictDataApi.listByType('qc_status')
    qcStatusOptions.value = data.map(d => ({ label: d.dictLabel, value: (Number(d.dictValue) || d.dictValue),class: d.listClass }))
  }catch {}
  try {
    const data = await dictDataApi.listByType('source_type')
    qcSourceTypeOptions.value = data.map(d => ({ label: d.dictLabel, value: (Number(d.dictValue) || d.dictValue),class: d.listClass }))
  }catch {}
  try {
    const data = await dictDataApi.listByType('qc_trace_type')
    qcTraceTypeOptions.value = data.map(d => ({ label: d.dictLabel, value: (Number(d.dictValue) || d.dictValue),class: d.listClass }))
  }catch {}
  try {
    const data = await dictDataApi.listByType('qc_result_type')
    qcResultTypeOptions.value = data.map(d => ({ label: d.dictLabel, value: (Number(d.dictValue) || d.dictValue),class: d.listClass }))
  }catch {}
  try {
    const data = await dictDataApi.listByType('qc_action_status')
    qcActionStatusOptions.value = data.map(d => ({ label: d.dictLabel, value: (Number(d.dictValue) || d.dictValue),class: d.listClass }))
  }catch {}
}

//查看质检单详情
async function handleDetail(row: QualityTesting) {
  detailTitle.value = '查看质检单详情'
  detailVisible.value = true
  const data = await qualityTestingApi.detail(row.id!)

  // // 映射后的数据
  // mappedInfoList.value = Object.entries(data.info)
  //     .filter(([key]) => infoDataMap.has(key)) // 只保留配置里有的字段
  //     .map(([key, value]) => ({
  //       label: infoDataMap.get(key) || key, // 中文标签
  //       key,                         // 原字段名
  //       value                        // 原始值
  //     }))
  setMappedInfoList(data.info)

  detailTableData.value = data.detail
}

// 组装列表时按上面自定义顺序循环
function setMappedInfoList(rawData: Record<string, any>) {
  mappedInfoList.value = customOrderKeys.map(key => ({
    key,
    label: infoDataMapping[key as keyof typeof infoDataMapping],
    value: rawData[key]
  }))
}

function formatDisplayValue(key: string, value: any): string {
  if (value === null || value === undefined) return '-'

  // 根据字段类型格式化
  switch (key) {
    case 'sourceType': {
      const sourceOpt = qcSourceTypeOptions.value.find(o => o.value === value || String(o.value) === String(value))
      return sourceOpt ? sourceOpt.label : String(value)
    }
    case 'status': {
      const statusOpt = qcStatusOptions.value.find(o => o.value === value || String(o.value) === String(value))
      return statusOpt ? statusOpt.label : String(value)
    }
    case 'traceType': {
      const traceOpt = qcTraceTypeOptions.value.find(o => o.value === value || String(o.value) === String(value))
      return traceOpt ? traceOpt.label : String(value)
    }
    case 'inspectTime':
    case 'createTime':
    case 'updateTime':
      return value ? new Date(value).toLocaleString('zh-CN') : '-'
    default:
      return String(value)
  }
}


//加载派工单
async function loadAssignWorkOrder() {
  const data = await assingReworkApi.allList()
  console.log(data)
  assingWork.value = data.map((d:any) => ({ label: d.assingCode, value: (Number(d.id) || d.id) }))
  console.log(assingWork)
}

//提交判定明细
async function submitResultDetail(row:any) {
  showSubmitOuter.value = true
  qcId.value = row.id
}


function handleSubmitDetail(submitDetail:any){
    formDetailOKData = submitDetail.okData ? submitDetail.okData : formDetailOKData ;
    formDetailScrapData = submitDetail.scrapData ? submitDetail.scrapData : formDetailScrapData
    formDetailReworkData = submitDetail.reworkData ? submitDetail.reworkData  : formDetailReworkData
    formDetailConcessionData = submitDetail.concessionData ?  submitDetail.concessionData : formDetailConcessionData

}



async function handleSubmitResultDetail() {

  formDetailOKData.qcId = qcId.value
  formDetailScrapData.qcId = qcId.value
  formDetailReworkData.qcId = qcId.value
  formDetailConcessionData.qcId =qcId.value


  const submitParam = [
    formDetailOKData,
    formDetailScrapData,
    formDetailReworkData,
    formDetailConcessionData]

    console.log(submitParam);
    
  
  await qualityTestingApi.submit(submitParam) 
  
  message.success('提交成功')
  router.go(-1)
 
}


//查询判定明细
function queryResultDetail(row:any) {
    let path = `detail/${row.id}`;
    router.push(path)
}




onMounted(() => {
  loadData()
  loadDictOptions()
  loadAssignWorkOrder()
})
</script>

<style scoped>
.search-form {
  margin-bottom: 16px;
}

.table-toolbar {
  margin-bottom: 16px;
}
</style>