<template>
  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column :label="t('QualityManagement.ZjTask.index')" type="index" width="80" />
      <el-table-column :label="t('QualityManagement.ZjTask.code')" align="center" prop="code" min-width="160" />
      <el-table-column :label="t('QualityManagement.ZjTask.type')" align="center" width="120">
        <template #default="scope">
          <DictTag type="mes_zj_task_type" :value="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column :label="t('QualityManagement.ZjTask.status')" align="center" prop="status" width="120">
        <template #default="scope">
          <DictTag :type="'job_status'" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column :label="t('QualityManagement.ZjTask.ticketCode')" align="center" prop="ticketCode" min-width="160" />
      <el-table-column :label="t('QualityManagement.ZjTask.orgType')" align="center" prop="orgType" min-width="120">
        <template #default="scope">
          <DictTag :type="DICT_TYPE.MES_ORG_TYPE" :value="scope.row.orgType" />
        </template>
      </el-table-column>
      <el-table-column :label="t('QualityManagement.ZjTask.schemaName')" align="center" prop="schemaName" min-width="160" />
      <el-table-column :label="t('QualityManagement.ZjTask.managerName')" align="center" prop="managerName" min-width="120" />
      <el-table-column :label="t('QualityManagement.ZjTask.executorName')" align="center" prop="executorName" min-width="120" />
      <el-table-column :label="t('QualityManagement.ZjTask.executeTime')" align="center" prop="executeTime" min-width="180" :formatter="dateFormatter" />
      <el-table-column :label="t('QualityManagement.ZjTask.remark')" align="center" prop="remark" min-width="160" />
      <el-table-column :label="t('QualityManagement.ZjTask.result')" align="center" prop="result" width="120">
        <template #default="scope">
          <el-tag v-if="scope.row.result === '1' || scope.row.result === 1" type="success">
            {{ t('QualityManagement.ZjTask.resultPass') }}
          </el-tag>
          <el-tag v-else-if="scope.row.result === '2' || scope.row.result === 2" type="danger">
            {{ t('QualityManagement.ZjTask.resultFail') }}
          </el-tag>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column :label="t('QualityManagement.ZjTask.operate')" align="center" width="160" fixed="right">
        <template #default="scope">
          <el-button
            v-if="scope.row.status === 0"
            link
            type="warning"
            @click.stop="handleRowClick(scope.row)"
          >
            {{ t('QualityManagement.ZjTask.inspect') }}
          </el-button>
          <span v-else>-</span>
        </template>
      </el-table-column>
    </el-table>
  </ContentWrap>

  <ZjTaskResultDialog ref="resultDialogRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import { ZjTaskApi, ZjTaskVO } from '@/api/mes/zjtask'
import { DictTag } from '@/components/DictTag'
import { DICT_TYPE } from '@/utils/dict'
import ZjTaskResultDialog from '@/views/mes/zjTask/components/ZjTaskResultDialog.vue'

/** 质量管理-质检参数 列表 */
defineOptions({ name: 'ZjProductPreList' })

type ZjTaskListRow = ZjTaskVO & { ticketCode?: string; executeTime?: string }

const props = defineProps<{
  ticket?: number | string // 工单（父列表的 id 字段）
}>()

const emit = defineEmits<{
  inspectableChange: [hasPending: boolean]
}>()

const { t } = useI18n()

const loading = ref(false) // 列表的加载中
const list = ref<ZjTaskListRow[]>([]) // 列表的数据
const resultDialogRef = ref()

/** 查询列表 */
const getList = async () => {
  const ticket = props.ticket === undefined || props.ticket === null ? '' : String(props.ticket)
  if (!ticket) {
    list.value = []
    emit('inspectableChange', false)
    return
  }
  loading.value = true
  try {
    const data = await ZjTaskApi.getZjTaskList({ ticket })
    list.value = Array.isArray(data) ? data : data?.list ?? data?.data ?? []
    const hasPending = list.value.some((item) => String(item?.status) === '0')
    emit('inspectableChange', hasPending)
  } finally {
    loading.value = false
  }
}

const handleRowClick = (row: ZjTaskListRow) => {
  if (!row?.id) return
  resultDialogRef.value?.open({ id: row.id, status: row.status, cancelReason: (row as any).cancelReason })
}

watch(
  () => props.ticket,
  () => {
    getList()
  },
  { immediate: true }
)

</script>
