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

import {
  closeAdminTask,
  confirmAdminTaskAssistedRole,
  markAdminTaskManualReview,
  redeemAdminTaskAssisted,
  regenerateAdminTaskClaimLink,
  releaseAdminTaskInventoryBinding,
  retryAdminTask,
} from '@/services/admin'

import AdminTaskKuaishouCloudPanel from './components/AdminTaskKuaishouCloudPanel.vue'
import AdminTaskActionBar from './components/AdminTaskActionBar.vue'
import AdminTaskAutoDeliverySection from './components/AdminTaskAutoDeliverySection.vue'
import AdminTaskEventsSection from './components/AdminTaskEventsSection.vue'
import AdminTaskHeroSection from './components/AdminTaskHeroSection.vue'
import AdminTaskInfoSection from './components/AdminTaskInfoSection.vue'
import AdminTaskInventoryBindingsSection from './components/AdminTaskInventoryBindingsSection.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,
  resolveTaskAutoDeliveryStatus,
  resolveTaskAutoDeliverySummary,
} = 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="查看任务全量信息并执行人工操作。" />

    <p v-if="errorMessage" class="error-copy">{{ errorMessage }}</p>

    <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} 吗？`)"
        @regenerate-claim-link="runAction(() => regenerateAdminTaskClaimLink(detail!.task.taskId), '领取链接已重新生成', `确认重新生成任务 ${detail!.task.taskNo} 的领取链接吗？旧链接会失效。`)"
        @confirm-role="runAction(() => confirmAdminTaskAssistedRole(detail!.task.taskId), '角色已确认', `确认以客服身份锁定任务 ${detail!.task.taskNo} 当前识别到的角色信息吗？`)"
        @support-redeem="runAction(() => redeemAdminTaskAssisted(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"
      />

      <AdminTaskAutoDeliverySection
        v-if="detail.order?.provider === 'agiso' && detail.order?.platform === 'xianyu'"
        :detail="detail"
        :auto-delivery-status="resolveTaskAutoDeliveryStatus(detail)"
        :auto-delivery-summary="resolveTaskAutoDeliverySummary(detail)"
      />

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

      <AdminTaskInventoryBindingsSection
        v-if="!isKuaishouCloudTask"
        :detail="detail"
        :can-view-sensitive-task-data="canViewSensitiveTaskData"
        :action-loading="actionLoading"
        @release-binding="runAction(() => releaseAdminTaskInventoryBinding(detail!.task.taskId, $event), '库存绑定已释放', `确认释放该库存绑定吗？`)"
      />

      <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>
