<template>
  <el-drawer
    v-model="visible"
    title="流程详情"
    direction="rtl"
    size="55%"
    :close-on-click-modal="false"
    :destroy-on-close="true"
    @closed="handleClose"
  >
    <div v-loading="loading" class="detail-content">
      <FilePreview v-model="fileUpload.previewVisible" :url="fileUpload.previewUrl" />
      <!-- 流程基本信息 -->
      <div class="info-section">
        <el-descriptions :column="3" size="small" border>
          <el-descriptions-item label="流程名称">{{ progress?.definition?.name }}</el-descriptions-item>
          <el-descriptions-item label="实例编号">{{ progress?.instance?.instanceNo || progress?.instance?.id }}</el-descriptions-item>
          <el-descriptions-item label="状态">
            <el-tag :type="instanceStatusTagType(progress?.instance?.status)">
              {{ instanceStatusText(progress?.instance?.status) }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="剩余节点">
            <el-tag type="warning">{{ progress?.remainingNodeCount || 0 }} 个待审批</el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="发起时间">{{ progress?.instance?.startTime }}</el-descriptions-item>
          <el-descriptions-item label="结束时间">{{ progress?.instance?.endTime || '-' }}</el-descriptions-item>
        </el-descriptions>
      </div>

      <!-- 表单数据 -->
      <div v-if="formDataDisplay" class="form-data-section">
        <div class="section-title">
          <h4>表单数据</h4>
          <el-button v-if="canEditForm && !formEditing" type="primary" size="small" link @click="startEditForm">补充/编辑材料</el-button>
        </div>
        <template v-if="formEditing">
          <FlowFormFields
            ref="formFieldsRef"
            :definition="progress?.definition"
            v-model="editFormData"
          />
          <div class="form-edit-actions">
            <el-button size="small" @click="cancelEditForm">取消</el-button>
            <el-button type="primary" size="small" :loading="savingForm" @click="saveFormData">保存</el-button>
          </div>
        </template>
        <FormDataDisplay
          v-else
          :form-schema="progress?.definition?.formSchema"
          :form-data="progress?.instance?.formData"
        />
      </div>

      <!-- TODO: 接入OSS后重新启用附件功能 -->
      <!-- 附件列表 -->
      <div v-if="false" class="attachment-section">
        <h4>附件列表</h4>
        <div v-if="attachments.length > 0" class="attachment-list">
          <div v-for="att in attachments" :key="att.id" class="attachment-item">
            <el-link type="primary" @click="fileUpload.openPreview(att.fileUrl)">
              <el-icon><Document /></el-icon>
              <span class="attachment-name" :title="att.fileName">{{ att.fileName }}</span>
            </el-link>
            <span class="attachment-meta">
              {{ att.nodeName || '发起' }}
              <template v-if="att.uploaderName">· {{ att.uploaderName }}</template>
              <template v-if="att.createTime">· {{ att.createTime }}</template>
            </span>
            <el-image
              v-if="isImage(att.fileUrl)"
              :src="getFileUrl(att.fileUrl)"
              :preview-src-list="imagePreviewList"
              fit="cover"
              style="width: 60px; height: 60px; margin-left: 8px;"
            />
          </div>
        </div>
        <el-empty v-else description="暂无附件" :image-size="60" style="padding: 10px 0;" />
      </div>

      <el-divider />

      <div class="detail-body">
        <!-- 左侧：流程进度（线性主线 + 分支路线） -->
        <div class="timeline-section">
          <h4>流程进度</h4>
          <FlowTimeline :key="timelineVersion" :progress="progress" />
        </div>

        <!-- 右侧：审批操作区 -->
        <div class="action-section">
          <template v-if="myPendingTask">
            <h4>审批操作</h4>
            <el-form ref="formRef" :model="form" label-width="80px">
              <el-form-item label="操作">
                <el-radio-group v-model="form.action">
                  <el-radio label="pass">通过</el-radio>
                  <el-radio label="reject">拒绝</el-radio>
                  <el-radio label="rollback">回退</el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item v-if="form.action === 'pass' && nextNodes.length > 0" label="选择分支">
                <el-select v-model="form.targetNodeId" placeholder="请选择下一节点" style="width: 100%">
                  <el-option v-for="n in nextNodes" :key="n.nodeId" :label="n.nodeName" :value="n.nodeId" />
                </el-select>
              </el-form-item>
              <el-form-item v-if="form.action === 'rollback'" label="回退说明">
                <el-alert type="warning" :closable="false" show-icon>
                  <template #title>
                    提交后将自动回退到上一个审批节点
                    <div v-if="lastRollbackRecord" class="rollback-reason">
                      上次回退原因：{{ lastRollbackRecord.comment }}
                    </div>
                  </template>
                </el-alert>
              </el-form-item>
              <!-- TODO: 接入OSS后重新启用附件功能 -->
              <el-form-item v-if="false" label="附件">
                <el-upload
                  v-model:file-list="fileUpload.attachmentList"
                  action="#"
                  :http-request="fileUpload.handleUpload"
                  :before-upload="beforeFileUpload"
                  :before-remove="() => true"
                  :limit="5"
                  multiple
                  :on-preview="fileUpload.handleFilePreview"
                >
                  <el-button type="primary" size="small">上传附件</el-button>
                </el-upload>
              </el-form-item>
              <el-form-item label="意见">
                <el-input v-model="form.comment" type="textarea" placeholder="请输入审批意见" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" :loading="submitting" @click="submitApprove">提交</el-button>
                <el-button :loading="submitting" @click="handleAddSign">加签</el-button>
              </el-form-item>
            </el-form>
          </template>
          <template v-else>
            <el-empty description="当前没有需要您处理的审批任务" />
          </template>

        </div>
      </div>
    </div>
  </el-drawer>

  <!-- 加签弹窗 -->
  <el-dialog
    v-model="addSignDialogVisible"
    title="加签"
    width="400px"
    :close-on-click-modal="false"
  >
    <el-select v-model="addSignUserId" filterable placeholder="请选择加签人" style="width: 100%">
      <el-option v-for="u in userList" :key="u.id" :label="u.realName || u.username" :value="u.id" />
    </el-select>
    <template #footer>
      <el-button @click="addSignDialogVisible = false">取消</el-button>
      <el-button type="primary" :loading="submitting" @click="confirmAddSign">确认</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, computed, watch } from 'vue'
import { ElMessage } from 'element-plus'
import { getProgress, getInstanceAttachments, updateInstanceFormData } from '@/api/flow/instance'
import { approveTask, rejectTask, returnTask, addSignTask, getTransferableUsers, listNextNodes } from '@/api/flow/task'
import type { NextNode } from '@/types/flow'
import { beforeFileUpload, getFileUrl, getFileName, parseAttachments, collectAttachmentUrls } from '@/utils/file'
import { useFileUpload } from '@/composables/useFileUpload'
import { instanceStatusText, instanceStatusTagType } from '@/utils/flow'
import FilePreview from '@/components/FilePreview/index.vue'
import FormDataDisplay from '@/components/FormDataDisplay/index.vue'
import FlowFormFields from '@/components/FlowFormFields/index.vue'
import type { ProcessProgress, FlowTask, ApprovalAction, Attachment } from '@/types/flow'
import type { User } from '@/types/system'
import { Document } from '@element-plus/icons-vue'
import FlowTimeline from './FlowTimeline.vue'

const props = defineProps<{
  modelValue: boolean
  instanceId: number
}>()

const emit = defineEmits<{
  (e: 'update:modelValue', val: boolean): void
  (e: 'approved'): void
}>()

const visible = computed({
  get: () => props.modelValue,
  set: (val) => emit('update:modelValue', val)
})

const loading = ref(false)
const progress = ref<ProcessProgress | null>(null)
// 每次加载进度成功后自增，用于强制重建流程进度组件，保证审批后进度刷新
const timelineVersion = ref(0)

type ApprovalActionType = 'pass' | 'reject' | 'rollback'

const form = reactive<{
  action: ApprovalActionType
  comment: string
  targetNodeId?: string
}>({
  action: 'pass',
  comment: '',
  targetNodeId: undefined
})

const fileUpload = reactive(useFileUpload())
const submitting = ref(false)
const addSignDialogVisible = ref(false)
const addSignUserId = ref<number | undefined>(undefined)
const userList = ref<User[]>([])

async function loadTransferableUsers() {
  const task = myPendingTask.value
  if (!task) {
    userList.value = []
    return
  }
  try {
    userList.value = await getTransferableUsers(task.id)
  } catch {
    userList.value = []
  }
}

// 是否有表单数据用于展示
const formDataDisplay = computed(() => {
  return !!progress.value?.instance?.formData
})

const attachments = ref<Attachment[]>([])

// 实例附件列表（从附件表读取，包含所有节点上传的历史附件）
const instanceAttachments = computed(() => {
  return attachments.value.map(a => a.fileUrl)
})

// 图片预览列表
const imagePreviewList = computed(() => {
  return attachments.value.filter(a => isImage(a.fileUrl)).map(a => getFileUrl(a.fileUrl))
})

async function loadAttachments() {
  if (!props.instanceId) return
  try {
    attachments.value = await getInstanceAttachments(props.instanceId)
  } catch {
    attachments.value = []
  }
}

function isImage(url: string): boolean {
  return /\.(png|jpe?g|gif|webp|bmp)$/i.test(url)
}

// 查找当前登录用户需要处理的待办任务
// 分支选择
const nextNodes = ref<NextNode[]>([])

async function loadNextNodes(taskId: number) {
  try { nextNodes.value = await listNextNodes(taskId) } catch { nextNodes.value = [] }
}

const myPendingTask = computed<FlowTask | null>(() => {
  if (!progress.value) return null
  for (const node of progress.value.nodes) {
    if (node.isMyTurn && node.tasks) {
      const pending = node.tasks.find(t => t.status === 0)
      if (pending) return pending
    }
    if (node.subNodes) {
      for (const sub of node.subNodes) {
        if (sub.isMyTurn && sub.tasks) {
          const pending = sub.tasks.find(t => t.status === 0)
          if (pending) {
            // 给子节点任务附加父节点名称，便于审批操作区展示
            pending.nodeName = `${node.nodeName} / ${sub.subNodeName}`
            return pending
          }
        }
      }
    }
  }
  return null
})

async function loadProgress() {
  if (!props.instanceId) return
  loading.value = true
  try {
    // 静默加载进度，无权限时不在详情页弹全局错误提示
    const res = await getProgress(props.instanceId, { silent: true })
    progress.value = res
    // 强制重建进度组件，确保审批操作后进度立即刷新
    timelineVersion.value++
  } catch {
    progress.value = null
  } finally {
    loading.value = false
  }
}

async function submitApprove() {
  const task = myPendingTask.value
  if (!task) return
  if (form.action === 'pass' && nextNodes.value.length > 0 && !form.targetNodeId) {
    ElMessage.warning('存在多个下游分支，请选择一个目标节点')
    return
  }
  submitting.value = true
  try {
    const data: ApprovalAction = {
      action: form.action,
      comment: form.comment
    }
    if (fileUpload.attachmentList.length > 0) {
      data.attachmentUrls = collectAttachmentUrls(fileUpload.attachmentList)
    }
    if (form.action === 'pass' && form.targetNodeId) {
      data.targetNodeId = form.targetNodeId
    }
    switch (form.action) {
      case 'pass':
        await approveTask(task.id, data)
        break
      case 'reject':
        await rejectTask(task.id, data)
        break
      case 'rollback':
        await returnTask(task.id, data)
        break
    }
    ElMessage.success('审批提交成功')
    form.action = 'pass'
    form.comment = ''
    fileUpload.resetFileUpload()
    // 刷新进度展示最新状态，不关闭抽屉
    await loadProgress()
    emit('approved')
  } finally {
    submitting.value = false
  }
}

function handleClose() {
  progress.value = null
  fileUpload.resetFileUpload()
  attachments.value = []
  cancelEditForm()
}

// 表单补充/编辑
const formEditing = ref(false)
const savingForm = ref(false)
const editFormData = ref<Record<string, unknown>>({})
const formFieldsRef = ref<{ validate: () => boolean } | null>(null)

const canEditForm = computed(() => !!myPendingTask.value)

function parseInstanceFormData(): Record<string, unknown> {
  const fd = progress.value?.instance?.formData
  if (!fd) return {}
  if (fd !== null && typeof fd === 'object') return { ...fd as Record<string, unknown> }
  try {
    return JSON.parse(fd as string)
  } catch {
    return {}
  }
}

function startEditForm() {
  editFormData.value = parseInstanceFormData()
  formEditing.value = true
}

function cancelEditForm() {
  formEditing.value = false
  editFormData.value = {}
}

async function saveFormData() {
  const valid = formFieldsRef.value?.validate?.()
  if (valid === false) return
  savingForm.value = true
  try {
    await updateInstanceFormData(props.instanceId, editFormData.value)
    ElMessage.success('材料已更新')
    cancelEditForm()
    await loadProgress()
    emit('approved')
  } finally {
    savingForm.value = false
  }
}

// 当前节点最近一次被回退的记录
const lastRollbackRecord = computed(() => {
  if (!progress.value?.records) return null
  const currentNodeId = progress.value.instance?.currentNode
  const list = progress.value.records.filter(r =>
    r.nodeId === currentNodeId && (r.actionType === 'RETURN' || r.actionResult === 'RETURN')
  )
  return list.length > 0 ? list[list.length - 1] : null
})

function handleAddSign() {
  const task = myPendingTask.value
  if (!task) return
  addSignUserId.value = undefined
  addSignDialogVisible.value = true
}

async function confirmAddSign() {
  const task = myPendingTask.value
  if (!task || !addSignUserId.value) {
    ElMessage.warning('请选择加签人')
    return
  }
  submitting.value = true
  try {
    await addSignTask(task.id, addSignUserId.value)
    ElMessage.success('加签成功')
    addSignDialogVisible.value = false
    addSignUserId.value = undefined
    await loadProgress()
  } finally {
    submitting.value = false
  }
}

watch(() => props.instanceId, (id) => {
  if (id && props.modelValue) {
    loadProgress()
  }
})

// 切换到"通过"或当前待办任务变化时加载下游节点列表
watch(
  [() => form.action, () => myPendingTask.value?.id],
  ([action, taskId]) => {
    nextNodes.value = []
    form.targetNodeId = undefined
    if (action === 'pass' && taskId) {
      loadNextNodes(taskId)
    }
  }
)

watch(() => props.modelValue, async (val) => {
  if (val && props.instanceId) {
    await loadProgress()
    await loadTransferableUsers()
    loadAttachments()
  }
})
</script>

<style scoped>
.detail-content {
  height: calc(100vh - 120px);
  overflow-y: auto;
  padding-right: 8px;
}
.info-section {
  margin-bottom: 10px;
}
.form-data-section,
.attachment-section {
  margin-bottom: 10px;
}
.form-data-section h4,
.attachment-section h4 {
  margin: 0 0 8px 0;
  font-size: 15px;
  color: #303133;
}
.attachment-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.attachment-item {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  padding: 8px 12px;
  background: #f5f7fa;
  border-radius: 4px;
}
.attachment-name {
  max-width: 220px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.attachment-meta {
  font-size: 12px;
  color: #909399;
}
.detail-body {
  display: flex;
  gap: 24px;
}
.timeline-section {
  flex: 1;
  min-width: 0;
}
.action-section {
  flex: 1;
  min-width: 0;
  border-left: 1px solid #ebeef5;
  padding-left: 24px;
}
.timeline-section h4,
.action-section h4 {
  margin: 0 0 16px 0;
  font-size: 15px;
  color: #303133;
}
.section-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}
.section-title h4 {
  margin: 0;
}
.form-edit-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 12px;
}
.rollback-reason {
  margin-top: 4px;
  font-size: 13px;
  color: #e6a23c;
  line-height: 1.5;
}
</style>
