import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../core/theme/app_colors.dart';
import '../shell/nav_bar_config.dart';
import '../../core/utils/date_utils.dart' as du;
import '../../core/i18n/app_localizations.dart';
import 'outing_log_list_controller.dart';

class OutingLogDetailPage extends ConsumerWidget {
  final String id;
  const OutingLogDetailPage({super.key, required this.id});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final log = mockOutingLogs.firstWhere(
      (e) => e.id == id,
      orElse: () => mockOutingLogs.first,
    );
    final l10n = AppLocalizations.of(context);

    ref
        .read(navBarConfigProvider.notifier)
        .update(
          NavBarConfig(
            title: l10n.get('outingLogDetail'),
            showBack: true,
            onBack: () => context.pop(),
          ),
        );
    return SingleChildScrollView(
      child: Column(
        children: [
          // 地图占位区
          Container(
            width: double.infinity,
            height: 160,
            padding: const EdgeInsets.fromLTRB(16, 16, 16, 0),
            child: Container(
              decoration: BoxDecoration(
                color: const Color(0xFFE8F4FD),
                borderRadius: BorderRadius.circular(8),
              ),
              child: const Center(
                child: Icon(
                  Icons.map_outlined,
                  size: 40,
                  color: AppColors.primary,
                ),
              ),
            ),
          ),

          // 信息卡片区
          Padding(
            padding: const EdgeInsets.all(16),
            child: Column(
              children: [
                // 客户信息
                Container(
                  width: double.infinity,
                  padding: const EdgeInsets.all(16),
                  decoration: BoxDecoration(
                    color: AppColors.bgCard,
                    borderRadius: BorderRadius.circular(8),
                  ),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        log.customerName,
                        style: const TextStyle(
                          fontSize: 18,
                          fontWeight: FontWeight.w700,
                          color: AppColors.textPrimary,
                        ),
                      ),
                      const SizedBox(height: 8),
                      Text(
                        '${log.visitNo} · ${du.DateUtils.formatDate(log.visitDate)}',
                        style: const TextStyle(
                          fontSize: 12,
                          color: AppColors.textSecondary,
                        ),
                      ),
                      const SizedBox(height: 4),
                      Text(
                        log.visitLocation,
                        style: const TextStyle(
                          fontSize: 13,
                          color: AppColors.textSecondary,
                        ),
                      ),
                    ],
                  ),
                ),

                const SizedBox(height: 16),

                // 工作总结
                Container(
                  width: double.infinity,
                  padding: const EdgeInsets.all(16),
                  decoration: BoxDecoration(
                    color: AppColors.bgCard,
                    borderRadius: BorderRadius.circular(8),
                  ),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      const Text(
                        '工作总结',
                        style: TextStyle(
                          fontSize: 14,
                          fontWeight: FontWeight.w600,
                          color: AppColors.textPrimary,
                        ),
                      ),
                      const SizedBox(height: 8),
                      Text(
                        log.visitSummary.isNotEmpty
                            ? log.visitSummary
                            : '暂无工作总结',
                        style: const TextStyle(
                          fontSize: 14,
                          color: AppColors.textSecondary,
                          height: 1.5,
                        ),
                      ),
                    ],
                  ),
                ),

                const SizedBox(height: 16),

                // 后续计划
                Container(
                  width: double.infinity,
                  padding: const EdgeInsets.all(16),
                  decoration: BoxDecoration(
                    color: AppColors.bgCard,
                    borderRadius: BorderRadius.circular(8),
                  ),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      const Text(
                        '后续计划',
                        style: TextStyle(
                          fontSize: 14,
                          fontWeight: FontWeight.w600,
                          color: AppColors.textPrimary,
                        ),
                      ),
                      const SizedBox(height: 8),
                      Text(
                        log.nextVisitContent.isNotEmpty
                            ? log.nextVisitContent
                            : '${du.DateUtils.formatDate(log.nextVisitTime)} · 暂无计划',
                        style: const TextStyle(
                          fontSize: 14,
                          color: AppColors.textSecondary,
                          height: 1.5,
                        ),
                      ),
                    ],
                  ),
                ),

                const SizedBox(height: 16),

                // 现场照片
                Container(
                  width: double.infinity,
                  padding: const EdgeInsets.all(16),
                  decoration: BoxDecoration(
                    color: AppColors.bgCard,
                    borderRadius: BorderRadius.circular(8),
                  ),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      const Text(
                        '现场照片',
                        style: TextStyle(
                          fontSize: 14,
                          fontWeight: FontWeight.w600,
                          color: AppColors.textPrimary,
                        ),
                      ),
                      const SizedBox(height: 12),
                      Row(
                        children: [
                          _buildPhotoPlaceholder(),
                          const SizedBox(width: 8),
                          _buildPhotoPlaceholder(),
                          const SizedBox(width: 8),
                          _buildPhotoPlaceholder(),
                        ],
                      ),
                    ],
                  ),
                ),

                const SizedBox(height: 16),

                // 主管点评
                Container(
                  width: double.infinity,
                  padding: const EdgeInsets.all(12),
                  decoration: BoxDecoration(
                    color: AppColors.primaryLight,
                    borderRadius: BorderRadius.circular(8),
                  ),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          const Text(
                            '王经理 · 销售总监',
                            style: TextStyle(
                              fontSize: 14,
                              fontWeight: FontWeight.w600,
                              color: AppColors.textPrimary,
                            ),
                          ),
                          const Row(
                            children: [
                              Icon(
                                Icons.star,
                                size: 16,
                                color: AppColors.warning,
                              ),
                              Icon(
                                Icons.star,
                                size: 16,
                                color: AppColors.warning,
                              ),
                              Icon(
                                Icons.star,
                                size: 16,
                                color: AppColors.warning,
                              ),
                              Icon(
                                Icons.star,
                                size: 16,
                                color: AppColors.warning,
                              ),
                              Icon(
                                Icons.star_border,
                                size: 16,
                                color: AppColors.textPlaceholder,
                              ),
                            ],
                          ),
                        ],
                      ),
                      const SizedBox(height: 8),
                      const Text(
                        '本次拜访准备充分，方案汇报专业到位。建议后续跟进加快节奏，争取本月内签单。',
                        style: TextStyle(
                          fontSize: 14,
                          color: AppColors.textSecondary,
                          height: 1.5,
                        ),
                      ),
                      const SizedBox(height: 8),
                      Text(
                        du.DateUtils.formatDateTime(log.updateTime),
                        style: const TextStyle(
                          fontSize: 12,
                          color: AppColors.textPlaceholder,
                        ),
                      ),
                    ],
                  ),
                ),

                // 底部点评输入区
                const SizedBox(height: 16),
                Container(
                  width: double.infinity,
                  padding: const EdgeInsets.all(16),
                  decoration: BoxDecoration(
                    color: AppColors.bgCard,
                    borderRadius: BorderRadius.circular(8),
                  ),
                  child: Row(
                    children: [
                      const Row(
                        children: [
                          Icon(
                            Icons.star_border,
                            size: 20,
                            color: AppColors.warning,
                          ),
                          SizedBox(width: 2),
                          Icon(
                            Icons.star_border,
                            size: 20,
                            color: AppColors.warning,
                          ),
                          SizedBox(width: 2),
                          Icon(
                            Icons.star_border,
                            size: 20,
                            color: AppColors.warning,
                          ),
                          SizedBox(width: 2),
                          Icon(
                            Icons.star_border,
                            size: 20,
                            color: AppColors.warning,
                          ),
                          SizedBox(width: 2),
                          Icon(
                            Icons.star_border,
                            size: 20,
                            color: AppColors.textPlaceholder,
                          ),
                        ],
                      ),
                      const SizedBox(width: 12),
                      Expanded(
                        child: Container(
                          padding: const EdgeInsets.symmetric(
                            horizontal: 12,
                            vertical: 8,
                          ),
                          decoration: BoxDecoration(
                            color: AppColors.bgPage,
                            borderRadius: BorderRadius.circular(18),
                          ),
                          child: const Text(
                            '输入点评…',
                            style: TextStyle(
                              fontSize: 14,
                              color: AppColors.textPlaceholder,
                            ),
                          ),
                        ),
                      ),
                      const SizedBox(width: 8),
                      const Text(
                        '发送',
                        style: TextStyle(
                          fontSize: 16,
                          fontWeight: FontWeight.w600,
                          color: AppColors.primary,
                        ),
                      ),
                    ],
                  ),
                ),
                const SizedBox(height: 24),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildPhotoPlaceholder() {
    return Container(
      width: 105,
      height: 105,
      decoration: BoxDecoration(
        color: const Color(0xFFD0E8F8),
        borderRadius: BorderRadius.circular(4),
      ),
    );
  }
}
