<template>
  <view class="page-container">
    <view class="fixed-header">
      <NavBar :title="t('equipmentInspectionTasks.detailTitle')" />
    </view>

    <scroll-view scroll-y class="detail-scroll">
      <view class="content-section">
        <view class="info-card">
          <view class="card-title">{{ t('equipmentInspectionTasks.basicInfo') }}</view>
          <view class="info-row">
            <text class="info-label">{{ t('equipmentInspectionTasks.name') }}</text>
            <text class="info-value">{{ textValue(detailData.name) }}</text>
          </view>
          <view class="info-row">
            <text class="info-label">{{ t('equipmentInspectionTasks.taskType') }}</text>
            <text class="info-value">{{ taskTypeText }}</text>
          </view>
          <view class="info-row">
            <text class="info-label">{{ t('equipmentInspectionTasks.deviceList') }}</text>
            <text class="info-value">{{ deviceListText }}</text>
          </view>
          <view class="info-row">
            <text class="info-label">{{ t('equipmentInspectionTasks.projectFormName') }}</text>
            <text class="info-value">{{ textValue(detailData.projectFormName) }}</text>
          </view>
          <view class="info-row">
            <text class="info-label">{{ t('equipmentInspectionTasks.dateRange') }}</text>
            <text class="info-value">{{ formatDate(detailData.startDate) }} ~ {{ formatDate(detailData.endDate) }}</text>
          </view>
          <view class="info-row">
            <text class="info-label">{{ t('equipmentInspectionTasks.cronExpression') }}</text>
            <text class="info-value">{{ textValue(detailData.cronExpression) }}</text>
          </view>
          <view class="info-row">
            <text class="info-label">{{ t('equipmentInspectionTasks.operableUsers') }}</text>
            <text class="info-value">{{ textValue(detailData.operableUsers) }}</text>
          </view>
          <view class="info-row">
            <text class="info-label">{{ t('equipmentInspectionTasks.enabled') }}</text>
            <text :class="['info-value', isEnabled(detailData.enabled) ? 'text-success' : 'text-danger']">{{ enabledText(detailData.enabled) }}</text>
          </view>
          <view class="info-row">
            <text class="info-label">{{ t('equipmentInspectionTasks.creatorName') }}</text>
            <text class="info-value">{{ textValue(detailData.creatorName) }}</text>
          </view>
          <view class="info-row">
            <text class="info-label">{{ t('equipmentInspectionTasks.createTime') }}</text>
            <text class="info-value">{{ formatDateTime(detailData.createTime) }}</text>
          </view>
          <view class="info-row no-border">
            <text class="info-label">{{ t('equipmentInspectionTasks.updateTime') }}</text>
            <text class="info-value">{{ formatDateTime(detailData.updateTime) }}</text>
          </view>
        </view>

        <view class="info-card">
          <view class="card-title">{{ t('equipmentInspectionTasks.subjectListTitle') }}</view>
          <view v-if="subjectLoading" class="hint">{{ t('functionCommon.loading') }}</view>
          <view v-else-if="!subjectList.length" class="hint">{{ t('equipmentInspectionTasks.noSubjectData') }}</view>
          <view v-else class="subject-list">
            <view v-for="(item, index) in subjectList" :key="item.id || index" class="subject-card">
              <view class="subject-index">{{ index + 1 }}</view>
              <view class="subject-body">
                <view class="subject-row">
                  <text class="subject-label">{{ t('equipmentInspectionTasks.subjectCode') }}</text>
                  <text class="subject-value">{{ textValue(item.subjectCode) }}</text>
                </view>
                <view class="subject-row">
                  <text class="subject-label">{{ t('equipmentInspectionTasks.subjectName') }}</text>
                  <text class="subject-value">{{ textValue(item.subjectName) }}</text>
                </view>
                <view class="subject-row">
                  <text class="subject-label">{{ t('equipmentInspectionTasks.inspectionMethod') }}</text>
                  <text class="subject-value">{{ inspectionMethodText(item.inspectionMethod) }}</text>
                </view>
                <view class="subject-row">
                  <text class="subject-label">{{ t('equipmentInspectionTasks.judgmentCriteria') }}</text>
                  <text class="subject-value">{{ textValue(item.judgmentCriteria) }}</text>
                </view>
              </view>
            </view>
          </view>
        </view>
      </view>
    </scroll-view>

    <view class="action-bar">
      <view :class="['action-btn', ticketLoading ? 'action-btn-disabled' : '']" @click="handleCreateTicket">
        {{ t('equipmentInspectionTasks.createTicket') }}
      </view>
    </view>
  </view>
</template>

<script setup>
import { computed, reactive, ref } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import { useI18n } from 'vue-i18n'
import NavBar from '@/components/common/NavBar.vue'
import { createTaskManagementTicket, getPlanMaintenanceSubjectList } from '@/api/mes/taskManagement'
import { getDeviceLedgerList } from '@/api/mes/deviceLedger'
import { DICT_TYPE, getDictLabel, initAllDict } from '@/utils/dict'
const { t } = useI18n()

const detailData = reactive({})
const ticketLoading = ref(false)
const deviceOptions = ref([])
const subjectList = ref([])
const subjectLoading = ref(false)
const taskTypeText = computed(() => {
  const normalized = Number(detailData.taskType)
  if (normalized === 1) return t('equipmentInspectionTasks.taskTypeInspect')
  if (normalized === 2) return t('equipmentInspectionTasks.taskTypeMaintain')
  return textValue(detailData.taskType)
})
async function ensureDeviceOptionsLoaded() {
  try {
    const res = await getDeviceLedgerList()
    const root = res && res.data !== undefined ? res.data : res
    const data = Array.isArray(root) ? root : (Array.isArray(root?.data) ? root.data : root?.list || [])
    deviceOptions.value = (Array.isArray(data) ? data : [])
        .filter((item) => item && item.id !== undefined && item.id !== null)
        .map((item) => ({
          id: item.id,
          label: `${item.deviceCode || ''} ${item.deviceName || ''}`.trim(),
          deviceName: item.deviceName || '',
          raw: item
        }))
  } catch (error) {
    deviceOptions.value = []
  }
}
const deviceListText = computed(() => {
  const value = detailData.deviceListText || detailData.deviceList
 /* if (Array.isArray(value)) return value.join('，') || '-'
  return textValue(value)*/
  const ids = parseIdsValue(value)
  if (!ids.length) return '-'
  const names = ids
      .map((id) => {
        const matched = deviceOptions.value.find((item) => String(item.id) === id)
        return matched?.deviceName || matched?.label || id
      })
      .filter(Boolean)
  return names.length ? names.join('，') : '-'
})

function parseIdsValue(value) {
  if (!value) return []
  if (Array.isArray(value)) return value.map((item) => String(item).trim()).filter(Boolean)
  return String(value).split(',').map((item) => item.trim()).filter(Boolean)
}

function firstIdValue(value) {
  return parseIdsValue(value)[0] || ''
}

function inspectionMethodText(value) {
  return getDictLabel(DICT_TYPE.INSPECTION_METHOD, value, textValue(value))
}

function normalizeListData(res) {
  const root = res && res.data !== undefined ? res.data : res
  const data = Array.isArray(root) ? root : (root?.list || root?.data?.list || root?.records || root?.data || [])
  return Array.isArray(data) ? data : []
}

async function loadSubjectList() {
  const planId = firstIdValue(detailData.projectForm)
  if (!planId) {
    subjectList.value = []
    return
  }
  subjectLoading.value = true
  try {
    const res = await getPlanMaintenanceSubjectList(planId)
    subjectList.value = normalizeListData(res)
  } catch (error) {
    subjectList.value = []
    uni.showToast({ title: t('functionCommon.loadFailed'), icon: 'none' })
  } finally {
    subjectLoading.value = false
  }
}

onLoad(async () => {
  try {
    const cached = uni.getStorageSync('equipmentInspectionTasksDetail')
    if (cached) {
      Object.assign(detailData, JSON.parse(cached))
      uni.removeStorageSync('equipmentInspectionTasksDetail')
    }
    await initAllDict()
    await Promise.all([ensureDeviceOptionsLoaded(), loadSubjectList()])
  } catch (error) {
  }
})

async function handleCreateTicket() {
  if (!detailData.id || ticketLoading.value) return
  uni.showModal({
    title: t('functionCommon.confirmTitle'),
    content: t('equipmentInspectionTasks.confirmCreateTicketContent', { name: textValue(detailData.name) }),
    success: async (res) => {
      if (!res.confirm) return
      ticketLoading.value = true
      try {
        await createTaskManagementTicket(detailData.id)
        uni.showToast({ title: t('equipmentInspectionTasks.createTicketSuccess'), icon: 'success' })
      } catch (error) {
        uni.showToast({ title: t('equipmentInspectionTasks.createTicketFail'), icon: 'none' })
      } finally {
        ticketLoading.value = false
      }
    }
  })
}

function isEnabled(value) {
  return value === true || value === 1 || value === '1' || value === 'true'
}

function enabledText(value) {
  return isEnabled(value) ? t('functionCommon.yes') : t('functionCommon.no')
}

function textValue(value) {
  if (value === 0) return '0'
  if (value === null || value === undefined) return '-'
  const text = String(value).trim()
  return text || '-'
}

function formatDate(value) {
  if (!value) return '-'
  if (Array.isArray(value) && value.length >= 3) {
    const [year, month, day] = value
    const pad = (num) => String(num).padStart(2, '0')
    return `${year}-${pad(month)}-${pad(day)}`
  }
  return String(value).trim().substring(0, 10) || '-'
}

function formatDateTime(value) {
  if (!value) return '-'
  if (Array.isArray(value) && value.length >= 3) {
    const [year, month, day, hour = 0, minute = 0, second = 0] = value
    const pad = (num) => String(num).padStart(2, '0')
    return `${year}-${pad(month)}-${pad(day)} ${pad(hour)}:${pad(minute)}:${pad(second)}`
  }
  const date = new Date(Number(value))
  if (Number.isNaN(date.getTime())) return textValue(value)
  const pad = (num) => String(num).padStart(2, '0')
  return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}`
}
</script>

<style lang="scss" scoped>
.page-container { min-height: 100vh; background-color: #f0f2f5; }
.fixed-header { position: sticky; top: 0; z-index: 20; }
.detail-scroll { height: calc(100vh - 200rpx); }
.content-section { padding: 0 24rpx 24rpx; }
.info-card { margin-top: 20rpx; background: #fff; border-radius: 20rpx; padding: 28rpx; box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.05); }
.card-title { font-size: 32rpx; color: #1a3a5c; font-weight: 700; margin-bottom: 18rpx; }
.info-row { display: flex; justify-content: space-between; align-items: flex-start; padding: 18rpx 0; border-bottom: 1rpx solid #edf0f3; }
.no-border { border-bottom: none; }
.info-label { width: 220rpx; font-size: 27rpx; color: #8a9099; }
.info-value { flex: 1; text-align: right; font-size: 28rpx; color: #303133; line-height: 1.45; }
.text-success { color: #18bc37; }
.text-danger { color: #e34d59; }
.action-bar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 20rpx 24rpx calc(20rpx + env(safe-area-inset-bottom));
  background: #f0f2f5;
}
.action-btn {
  height: 88rpx;
  border-radius: 16rpx;
  background: #1f4b79;
  color: #ffffff;
  font-size: 30rpx;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
}
.action-btn-disabled {
  background: #94a3b8;
}
.hint { text-align: center; color: #909399; padding: 24rpx 0; font-size: 26rpx; }
.subject-list { display: flex; flex-direction: column; gap: 16rpx; }
.subject-card { display: flex; background: #f7f9fc; border-radius: 14rpx; padding: 20rpx; gap: 16rpx; }
.subject-index { min-width: 48rpx; height: 48rpx; border-radius: 10rpx; background: #1a3a5c; color: #fff; display: flex; align-items: center; justify-content: center; font-size: 24rpx; font-weight: 700; }
.subject-body { flex: 1; display: flex; flex-direction: column; gap: 8rpx; }
.subject-row { display: flex; justify-content: space-between; align-items: center; }
.subject-label { font-size: 24rpx; color: #8a9099; }
.subject-value { font-size: 26rpx; color: #30363d; max-width: 60%; text-align: right; }
</style>
