<script setup lang="ts">
import { ElMessage } from 'element-plus'
import { computed, onMounted, ref } from 'vue'
import { useRoute } from 'vue-router'

import { adminCloseOrder, adminMarkOrderAbnormal, fetchAdminHandoffRecords, fetchAdminOrder, type HandoffRecord, type Order } from '@/api/orders'
import { handoffStatusLabel, orderStatusLabel } from '@/utils/statusLabels'
import { formatDateTime } from '@/utils/time'

const route = useRoute()
const loading = ref(false)
const submitting = ref(false)
const order = ref<Order | null>(null)
const handoffRecords = ref<HandoffRecord[]>([])
const actionType = ref<'close' | 'abnormal' | ''>('')
const reason = ref('')

const snapshotText = computed(() => JSON.stringify(order.value?.account_snapshot || {}, null, 2))
const actionTitle = computed(() => (actionType.value === 'close' ? '客服关闭订单' : '标记订单异常'))
const canOperate = computed(() => !!order.value && !['completed', 'cancelled', 'closed'].includes(order.value.status))

onMounted(loadOrder)

async function loadOrder() {
  loading.value = true
  try {
    order.value = await fetchAdminOrder(String(route.params.id))
    handoffRecords.value = await fetchAdminHandoffRecords(String(route.params.id))
  } finally {
    loading.value = false
  }
}

function openAction(type: 'close' | 'abnormal') {
  actionType.value = type
  reason.value = ''
}

async function submitAction() {
  if (!order.value || !actionType.value) return
  submitting.value = true
  try {
    if (actionType.value === 'close') {
      await adminCloseOrder(order.value.id, reason.value)
      ElMessage.success('订单已关闭')
    } else {
      await adminMarkOrderAbnormal(order.value.id, reason.value)
      ElMessage.success('订单已标记异常')
    }
    actionType.value = ''
    await loadOrder()
  } catch (error) {
    ElMessage.error(readError(error, '操作失败'))
  } finally {
    submitting.value = false
  }
}

function readError(error: unknown, fallback: string) {
  if (typeof error === 'object' && error && 'response' in error) {
    const response = (error as { response?: { data?: { message?: string } } }).response
    return response?.data?.message || fallback
  }
  return fallback
}
</script>

<template>
  <section class="page" v-loading="loading">
    <div v-if="order" class="page-header-row">
      <div class="page-header">
        <p class="eyebrow">{{ order.order_no }}</p>
        <h1>订单详情</h1>
        <p>{{ order.title }} · {{ order.server_region }} / {{ order.login_platform }}</p>
      </div>
      <div class="toolbar-actions">
        <RouterLink to="/admin/orders">
          <el-button>返回列表</el-button>
        </RouterLink>
        <el-button type="warning" :disabled="!canOperate" @click="openAction('abnormal')">标记异常</el-button>
        <el-button type="danger" :disabled="!canOperate" @click="openAction('close')">客服关闭</el-button>
      </div>
    </div>

    <div v-if="order" class="metric-grid dashboard-metrics">
      <div class="metric-card">
        <span>订单状态</span>
        <strong>{{ orderStatusLabel(order.status) }}</strong>
      </div>
      <div class="metric-card">
        <span>交接状态</span>
        <strong>{{ handoffStatusLabel(order.handoff_status) }}</strong>
      </div>
      <div class="metric-card">
        <span>订单金额</span>
        <strong>¥{{ order.rent_amount }}</strong>
      </div>
      <div class="metric-card">
        <span>押金</span>
        <strong>¥{{ order.deposit_amount }}</strong>
      </div>
    </div>

    <div v-if="order" class="dashboard-panels">
      <div class="order-panel dashboard-panel">
        <h2>用户信息</h2>
        <p>租客：{{ order.renter_phone || order.renter_id }}</p>
        <p>号主：{{ order.owner_phone || order.owner_id }}</p>
        <p>开始：{{ formatDateTime(order.rent_start_at, '未开始') }}</p>
        <p>预计截止：{{ formatDateTime(order.rent_end_at, '未设置') }}</p>
      </div>

      <div class="order-panel dashboard-panel">
        <h2>交接记录</h2>
        <el-empty v-if="handoffRecords.length === 0" description="暂无交接记录" />
        <div v-for="record in handoffRecords" :key="record.id" class="timeline-item">
          <strong>{{ record.type }}</strong>
          <p>{{ record.content }}</p>
          <span>{{ formatDateTime(record.created_at) }}</span>
        </div>
      </div>
    </div>

    <div v-if="order" class="order-panel dashboard-panel code-panel">
      <h2>账号快照</h2>
      <pre>{{ snapshotText }}</pre>
    </div>

    <el-dialog :model-value="!!actionType" :title="actionTitle" width="560px" @update:model-value="actionType = ''">
      <div v-if="order" class="dialog-body">
        <p><strong>{{ order.order_no }}</strong> · {{ order.title }}</p>
        <el-input v-model="reason" type="textarea" :rows="4" placeholder="填写客服操作原因，会写入审计日志并通知双方" />
      </div>
      <template #footer>
        <el-button @click="actionType = ''">取消</el-button>
        <el-button type="danger" :loading="submitting" @click="submitAction">确认操作</el-button>
      </template>
    </el-dialog>
  </section>
</template>
