<template>
  <div class="admin-disassembly-orders">
    <!-- 统计卡片 -->
    <div class="disassembly-stats">
      <div class="stat-card">
        <div class="stat-icon total">
          <el-icon><Document /></el-icon>
        </div>
        <div class="stat-info">
          <div class="stat-number">{{ disassemblyStats.total }}</div>
          <div class="stat-label">{{ t('admin.disassemblyOrders.stats.total') }}</div>
        </div>
      </div>
      <div class="stat-card">
        <div class="stat-icon pending">
          <el-icon><Clock /></el-icon>
        </div>
        <div class="stat-info">
          <div class="stat-number">{{ disassemblyStats.pending }}</div>
          <div class="stat-label">{{ t('admin.disassemblyOrders.stats.pending') }}</div>
        </div>
      </div>
      <!-- <div class="stat-card">
        <div class="stat-icon processing">
          <el-icon><Loading /></el-icon>
        </div>
        <div class="stat-info">
          <div class="stat-number">{{ disassemblyStats.processing }}</div>
          <div class="stat-label">{{ t('admin.disassemblyOrders.stats.processing') }}</div>
        </div>
      </div> -->
    </div>

    <!-- 筛选搜索区域 -->
    <div class="disassembly-filters">
      <div class="search-group">
        <el-input
          v-model="searchQuery"
          :placeholder="$t('admin.review.search')"
          prefix-icon="Search"
          clearable
        />
      </div>

      <div class="filter-actions">
        <el-button 
          @click="resetFilters"
        >
          <el-icon><Refresh /></el-icon>
          {{ $t('admin.disassemblyOrders.filters.refresh') }}
        </el-button>
      </div>
    </div>

    <!-- 订单列表区域 -->
    <div class="disassembly-table">
      <el-table 
        :data="paginatedOrders" 
        stripe 
        style="width: 100%"
        v-loading="loading"
      >
        <el-table-column
          prop="id"
          :label="$t('admin.disassemblyOrders.list.id')"
          width="80"
        />
        
        <el-table-column
          :label="$t('admin.orders.image')"
          width="80"
        >
          <template #default="{ row }">
            <img 
              v-if="row?.modelData?.imageUrl || row?.order_info?.modelData?.imageUrl" 
              :src="row.modelData?.imageUrl || row.order_info.modelData?.imageUrl" 
              alt="商品图片" 
              class="order-item-image"
              @click.self="previewImage(row.modelData?.imageUrl || row.order_info.modelData?.imageUrl)"
            >
            <span v-else>-</span>
          </template>
        </el-table-column>
        
        <el-table-column
          prop="orderNumber"
          :label="$t('admin.disassemblyOrders.list.orderNumber')"
          width="150"
        />
        
        <el-table-column
          prop="creatorName"
          :label="$t('admin.disassemblyOrders.list.creatorName')"
          width="120"
        />
        
        <el-table-column
          prop="status"
          :label="$t('admin.disassemblyOrders.list.status')"
          width="100"
        >
          <template #default="{row}">
            <el-tag :type="getStatusTagType(row).status">
              {{ t(`${getStatusTagType(row).label}`) }}
            </el-tag>
          </template>
        </el-table-column>
        
        <el-table-column
          prop="createTime"
          :label="$t('admin.disassemblyOrders.list.createTime')"
          width="160"
        >
          <template #default="scope">
            {{ formatDate(scope.row.createTime) }}
          </template>
        </el-table-column>
        
        <el-table-column
          :label="$t('admin.disassemblyOrders.list.actions')"
          width="200"
          fixed="right"
        >
          <template #default="scope">
            <div class="actions-container">
              <el-button
                type="primary"
                size="small"
                :icon="Tools"
                @click="handleDisassemble(scope.row)"
              >
                {{ $t('admin.disassemblyOrders.list.disassembly') }}
              </el-button>
              <el-button
                type="success"
                size="small"
                :icon="Check"
                @click="handleCompleteDisassembly(scope.row)"
              >
                {{ $t('admin.disassemblyOrders.list.completeDisassembly') }}
              </el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    
    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 50, 100]"
        :total="totalReviews"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    
    <!-- 图片预览对话框 -->
    <el-dialog 
      v-model="imagePreviewVisible" 
      :title="t('admin.review.previewImage')" 
      width="80%"
    >
      <div class="image-preview-container">
        <img :src="previewImageUrl" alt="预览图片" class="preview-image" />
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { orderStatus } from '@deotaland/utils'
import {AdminOrders} from './AdminOrders/AdminOrders'
import { ref, reactive, computed, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useI18n } from 'vue-i18n'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Refresh, Search, Document, Clock, Loading, Check, Van, Tools } from '@element-plus/icons-vue'
const adminOrders = new AdminOrders();
const getStatusTagType = (status) => {
  return orderStatus.getOrderStatusOptions(status)
}
const { t } = useI18n()
const router = useRouter()
const totalReviews = ref(0)
// 响应式数据
const loading = ref(false)
const currentPage = ref(1)
const pageSize = ref(10)
const ordersList = ref([])
const selectedStatus = ref('')
const searchQuery = ref('')
// 图片预览
const imagePreviewVisible = ref(false)
const previewImageUrl = ref('')

// 统计数据
const disassemblyStats = ref({
  total: 0,
  pending: 0,
  processing: 0
})

const getOrderStatistics = async () => {
  adminOrders.getOrderStatistics().then(res=>{
    if(res.code==0){
      let data = res.data.make_maps
      disassemblyStats.value = {
        total:res.data.total,
        pending: data.pending_count,
      }
    }
  })
}

// 格式化日期
const formatDate = (dateString) => {
  const date = new Date(dateString)
  return date.toLocaleString()
}
const ortherjson = ref({
  order_status:[2],
  refund_status:[0],
  payment_status:[1]
});
// 获取订单列表
const fetchOrders = () => {
  loading.value = true
    loading.value = true
  let params = {
    ...ortherjson.value,
    page_size: pageSize.value,
    page: currentPage.value,
    order_no:searchQuery.value
  }
  adminOrders.getOrderList(params).then(res=>{
    let data  = res.data || [];
    ordersList.value = data.items;
    loading.value = false;
    totalReviews.value = data.total || 0;
    // ordersList.value = [...data.items,...data.items];
    // orderStats.value.pending = data.total || 0;
  })
}

// 刷新订单列表
const refreshOrders = () => {
  fetchOrders()
  ElMessage.success(t('admin.disassemblyOrders.messages.refreshSuccess'))
}

// 重置筛选条件
const resetFilters = () => {
  selectedStatus.value = ''
  searchQuery.value = ''
  fetchOrders()
}

//订单列表
const paginatedOrders = computed(() => {
  let list = ordersList.value.map(item=>{
    return {
      ...item,
      orderNumber:item.order_no,
      creatorName:(item.order_info.shipping.firstName||'')+(item.order_info.shipping.lastName||''),
      createTime:item.created_at
    }
  })
  return list;
})

// 处理拆件操作
const handleDisassemble = (order) => {
  // 跳转到拆件页面
  router.push({
    name: 'AdminDisassemblyDetail',
    params: { id: order.id }
  })
}

// 图片预览
const previewImage = (url) => {
  if (!url || url === '-') return
  previewImageUrl.value = url
  imagePreviewVisible.value = true
}

// 处理完成拆件操作
const handleCompleteDisassembly = async (order) => {
  try {
    // 显示确认对话框
    await ElMessageBox.confirm(
      t('admin.disassemblyOrders.messages.completeDisassemblyConfirm'),
      t('admin.disassemblyOrders.messages.completeDisassemblyTitle'),
      {
        confirmButtonText: t('common.confirm'),
        cancelButtonText: t('common.cancel'),
        type: 'warning'
      }
    )
    adminOrders.completeDisassembly({
      id:order.id
    }).then(res=>{
      if(res.code==0){
        ElMessage.success(t('admin.disassemblyOrders.messages.completeDisassemblySuccess'))
        fetchOrders()
      }
    })
    // 刷新订单列表
    fetchOrders()
  } catch (error) {
    // 用户取消操作或发生错误
    if (error !== 'cancel') {
      console.error('完成拆件失败:', error)
      ElMessage.error(t('admin.disassemblyOrders.messages.completeDisassemblyError'))
    }
  }
}
// 分页相关
const handleSizeChange = (val) => {
  pageSize.value = val
  fetchOrders()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  fetchOrders()
}

// 组件挂载时获取数据
onMounted(() => {
  // 获取路由参数中的order_no并赋值给searchQuery
  const route = useRoute()
  if (route.query.order_no) {
    searchQuery.value = route.query.order_no
  }
  fetchOrders()
  getOrderStatistics()
})
</script>

<style scoped>
/* 拆件订单管理页面样式 */
.admin-disassembly-orders {
  padding: 24px;
  max-height: 100vh;
}

/* 统计卡片样式 */
.disassembly-stats {
  display: flex;
  gap: 20px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}

.stat-card {
  flex: 1;
  min-width: 200px;
  padding: 20px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  display: flex;
  align-items: center;
  transition: all 0.3s ease;
}

.stat-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.stat-icon {
  width: 48px;
  height: 48px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 15px;
  font-size: 24px;
  color: white;
}

.stat-icon.total {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.stat-icon.pending {
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
}

.stat-icon.processing {
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
}

.stat-info {
  flex: 1;
}

.stat-number {
  font-size: 32px;
  font-weight: 700;
  color: #1f2937;
  line-height: 1;
}

.stat-label {
  color: #6b7280;
  font-size: 14px;
  margin-top: 4px;
}

/* 筛选搜索区域 */
.disassembly-filters {
  padding: 20px;
  border-radius: 12px;
  margin-bottom: 24px;
  display: flex;
  gap: 12px;
  align-items: flex-end;
  justify-content: flex-end;
  overflow-x: auto;
  white-space: nowrap;
}

.filter-group {
  display: flex;
  gap: 8px;
  align-items: flex-end;
  flex-shrink: 0;
}

.search-group {
  flex: 1;
  min-width: 200px;
  max-width: 300px;
  flex-shrink: 0;
}

.filter-actions {
  display: flex;
  align-items: flex-end;
  flex-shrink: 0;
}

/* 表格样式 */
.disassembly-table {
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  border: 1px solid #e5e7eb;
  margin-bottom: 24px;
}

/* 表格样式优化 */
.disassembly-table :deep(.el-table) {
  width: 100% !important;
  table-layout: auto;
}

.disassembly-table :deep(.el-table__header) {
  width: 100% !important;
}

.disassembly-table :deep(.el-table__body) {
  width: 100% !important;
}

.disassembly-table :deep(.el-table__cell) {
  padding: 8px 12px;
  white-space: nowrap;
  vertical-align: middle;
  text-align: center !important;
}

.disassembly-table :deep(.el-table__row) {
  height: 66px;
}

.disassembly-table :deep(.el-table__header-wrapper) {
  background-color: #f8fafc;
}

.disassembly-table :deep(.el-table__header th) {
  background-color: #f8fafc;
  color: #374151;
  font-weight: 600;
  border-bottom: 2px solid #e5e7eb;
  white-space: nowrap !important;
  text-align: center !important;
  text-overflow: ellipsis;
  overflow: hidden;
}

.disassembly-table :deep(.el-table__header th .cell) {
  white-space: nowrap !important;
  word-break: keep-all;
  overflow: hidden;
  text-overflow: ellipsis;
  text-align: center !important;
}

/* 表格内容居中 */
.disassembly-table :deep(.el-table__body td .cell) {
  text-align: center !important;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* 操作按钮容器 */
.actions-container {
  display: flex;
  gap: 6px;
  flex-wrap: nowrap;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.actions-container .el-button {
  flex-shrink: 0;
  white-space: nowrap;
  min-width: 80px;
}

/* 完成拆件按钮样式 */
.actions-container .el-button--success {
  background: linear-gradient(135deg, #10b981 0%, #059669 100%);
  border: none;
  transition: all 0.3s ease;
}

.actions-container .el-button--success:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(16, 185, 129, 0.4);
}

/* 订单列表商品图片样式 */
.order-item-image {
  width: 40px;
  height: 40px;
  object-fit: cover;
  border-radius: 4px;
  cursor: pointer;
  transition: transform 0.2s ease;
}

.order-item-image:hover {
  transform: scale(1.1);
}

/* 图片预览容器样式 */
.image-preview-container {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
  max-height: 70vh;
  overflow: auto;
}

.preview-image {
  max-width: 100%;
  max-height: 70vh;
  object-fit: contain;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  transition: transform 0.2s ease;
}

.preview-image:hover {
  transform: scale(1.02);
}

/* 分页样式 */
.pagination-container {
  display: flex;
  justify-content: flex-end;
  padding: 16px 0;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .admin-disassembly-orders {
    padding: 16px;
  }
  
  .disassembly-stats {
    flex-direction: column;
    gap: 16px;
  }
  
  .stat-card {
    min-width: auto;
  }
  
  .disassembly-filters {
    flex-direction: column;
    align-items: stretch;
    gap: 12px;
  }
  
  .filter-group {
    flex-direction: column;
    align-items: stretch;
  }
  
  .search-group {
    min-width: auto;
    max-width: none;
  }
  
  .filter-actions {
    justify-content: center;
  }
  
  .actions-container {
    flex-direction: column;
    gap: 6px;
  }
  
  /* 移动端表格优化 */
  .disassembly-table {
    overflow-x: auto;
  }
  
  .disassembly-table :deep(.el-table) {
    min-width: 800px;
  }
  
  /* 移动端操作按钮适配 */
  .actions-container {
    flex-direction: column;
    gap: 8px;
    align-items: stretch;
  }
  
  .actions-container .el-button {
    width: 100%;
    min-width: auto;
    margin: 0;
  }
}

@media (min-width: 769px) and (max-width: 1024px) {
  .disassembly-stats {
    flex-wrap: wrap;
  }
  
  .stat-card {
    min-width: calc(50% - 10px);
  }
  
  .disassembly-filters {
    flex-wrap: wrap;
  }
}
</style>