<script setup lang="ts">
import { onMounted } from 'vue'

import {
  closeAdminTask,
  markAdminTaskManualReview,
  rebindAdminTaskKuaishouCloudRole,
  retryAdminTask,
} from '@/services/admin'

import AdminTaskKuaishouCloudPanel from './components/AdminTaskKuaishouCloudPanel.vue'
import AdminTaskActionBar from './components/AdminTaskActionBar.vue'
import AdminTaskEventsSection from './components/AdminTaskEventsSection.vue'
import AdminTaskHeroSection from './components/AdminTaskHeroSection.vue'
import AdminTaskInfoSection from './components/AdminTaskInfoSection.vue'
import AdminTaskManualDispatchSection from './components/AdminTaskManualDispatchSection.vue'
import AdminTaskRedeemResolutionSection from './components/AdminTaskRedeemResolutionSection.vue'
import AdminTaskScreenshotSection from './components/AdminTaskScreenshotSection.vue'
import { useAdminTaskDetail } from './composables/useAdminTaskDetail'
import { useAdminTaskKuaishouCloud } from './composables/useAdminTaskKuaishouCloud'

const {
  loading,
  actionLoading,
  errorMessage,
  detail,
  screenshotPreviewUrl,
  manualDispatchForm,
  canManageTaskLifecycle,
  canCloseTasks,
  canViewSensitiveTaskData,
  claimUrl,
  claimLinkInvalid,
  screenshotSectionTitle,
  loadDetail,
  runAction,
  submitManualDispatch,
  copyText,
  openExternalLink,
} = useAdminTaskDetail()

const {
  kuaishouCloudFlow,
  isKuaishouCloudTask,
  summaryCards,
  kuaishouRoleInfoEntries,
  kuaishouCloudChecklist,
  submitKuaishouCloudPrepare,
  submitKuaishouCloudDispatch,
  submitKuaishouCloudRefreshRoleInfo,
  submitKuaishouCloudReturnNumber,
} = useAdminTaskKuaishouCloud(detail, runAction)

onMounted(loadDetail)
</script>

<template>
  <section class="admin-panel">
    <AdminPageHeader title="任务详情" description="查看任务全量信息并执行人工操作。" />

    <el-alert v-if="errorMessage" :title="errorMessage" type="error" show-icon :closable="false" />

    <el-skeleton v-if="loading" :rows="6" animated />

    <template v-else-if="detail">
      <AdminTaskHeroSection
        :detail="detail"
        :is-kuaishou-cloud-task="isKuaishouCloudTask"
        :summary-cards="summaryCards"
        :claim-url="claimUrl"
        :claim-link-invalid="claimLinkInvalid"
      />

      <AdminTaskActionBar
        :detail="detail"
        :action-loading="actionLoading"
        :can-manage-task-lifecycle="canManageTaskLifecycle"
        :can-close-tasks="canCloseTasks"
        @retry="
          runAction(
            () => retryAdminTask(detail!.task.taskId),
            '任务已重试',
            `确认重试任务 ${detail!.task.taskNo} 吗？`,
          )
        "
        @rebind-role="
          runAction(
            () => rebindAdminTaskKuaishouCloudRole(detail!.task.taskId),
            '角色换绑资源已准备完成',
            `确认为任务 ${detail!.task.taskNo} 换绑角色吗？当前虚拟号会退还，并生成新的绑定二维码。`,
          )
        "
        @prepare-kuaishou-cloud="submitKuaishouCloudPrepare"
        @dispatch-kuaishou-cloud="submitKuaishouCloudDispatch"
        @return-kuaishou-cloud="submitKuaishouCloudReturnNumber"
        @mark-manual-review="
          runAction(
            () => markAdminTaskManualReview(detail!.task.taskId),
            '任务已转人工处理',
            `确认将任务 ${detail!.task.taskNo} 转为人工处理吗？`,
          )
        "
        @submit-manual-dispatch="submitManualDispatch"
        @close-task="
          runAction(
            () => closeAdminTask(detail!.task.taskId),
            '任务已关闭',
            `确认关闭任务 ${detail!.task.taskNo} 吗？关闭后不会自动继续推进。`,
          )
        "
      />

      <AdminTaskInfoSection
        v-if="!isKuaishouCloudTask"
        :detail="detail"
        :can-view-sensitive-task-data="canViewSensitiveTaskData"
        :claim-url="claimUrl"
        :claim-link-invalid="claimLinkInvalid"
      />

      <AdminTaskKuaishouCloudPanel
        v-if="isKuaishouCloudTask && kuaishouCloudFlow"
        :detail="detail"
        :flow="kuaishouCloudFlow"
        :role-info-entries="kuaishouRoleInfoEntries"
        :checklist="kuaishouCloudChecklist"
        :claim-url="claimUrl"
        :action-loading="actionLoading"
        @refresh-role="submitKuaishouCloudRefreshRoleInfo"
        @copy-bind-url="copyText($event, '绑定链接已复制')"
        @open-bind-url="openExternalLink"
      />

      <AdminTaskRedeemResolutionSection v-if="detail.redeemResolution" :detail="detail" />

      <AdminTaskManualDispatchSection
        v-if="detail.task.executorKey === 'manual_dispatch'"
        :detail="detail"
        :form="manualDispatchForm"
        :can-manage-task-lifecycle="canManageTaskLifecycle"
        :action-loading="actionLoading"
        @update:form="Object.assign(manualDispatchForm, $event)"
        @submit="submitManualDispatch"
      />

      <AdminTaskEventsSection v-if="detail.events.length > 0" :detail="detail" />

      <AdminTaskScreenshotSection
        v-if="detail.screenshotUrl"
        :screenshot-preview-url="screenshotPreviewUrl"
        :section-title="screenshotSectionTitle"
      />
    </template>

    <el-empty v-else description="任务不存在或已被删除" />
  </section>
</template>

<style scoped src="./AdminTaskDetail.css"></style>
