import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../../constant/colors.dart';
import '../../../constant/style.dart';
import '../../../controller/admin/promo_controller.dart';
import '../../widgets/my_scafold.dart';
import '../../widgets/elevated_btn.dart';
import '../../widgets/my_textField.dart';
import '../../widgets/mydialoug.dart';

class PromoManagementPage extends StatelessWidget {
  PromoManagementPage({super.key});

  final PromoController controller = Get.put(PromoController());

  @override
  Widget build(BuildContext context) {
    return MyScafolld(
      title: 'إدارة أكواد الخصم'.tr,
      isleading: true,
      action: IconButton(
        icon: const Icon(Icons.add_circle_outline_rounded, color: AppColor.accent),
        onPressed: () => _showPromoSheet(context),
      ),
      body: [
        Obx(() => controller.isLoading.value && controller.promoList.isEmpty
            ? const Center(child: CircularProgressIndicator())
            : controller.promoList.isEmpty
                ? Center(
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Icon(Icons.confirmation_number_outlined, size: 64, color: AppColor.textMuted),
                        const SizedBox(height: 16),
                        Text('لا يوجد أكواد خصم حالياً', style: AppStyle.subtitle),
                      ],
                    ),
                  )
                : RefreshIndicator(
                    onRefresh: () => controller.getPromos(),
                    child: ListView.builder(
                      padding: const EdgeInsets.fromLTRB(16, 16, 16, 80),
                      itemCount: controller.promoList.length,
                      itemBuilder: (context, index) {
                        final promo = controller.promoList[index];
                        return _buildPromoCard(context, promo);
                      },
                    ),
                  )),
      ],
    );
  }

  Widget _buildPromoCard(BuildContext context, dynamic promo) {
    return Container(
      margin: const EdgeInsets.only(bottom: 12),
      decoration: AppStyle.cardDecoration,
      child: ListTile(
        contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
        leading: Container(
          padding: const EdgeInsets.all(10),
          decoration: BoxDecoration(
            color: AppColor.accentSoft,
            borderRadius: BorderRadius.circular(12),
          ),
          child: const Icon(Icons.local_offer_rounded, color: AppColor.accent),
        ),
        title: Text(
          promo['promo_code']?.toString() ?? 'N/A',
          style: AppStyle.title,
        ),
        subtitle: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const SizedBox(height: 4),
            Text(promo['description']?.toString() ?? '', style: AppStyle.caption),
            const SizedBox(height: 4),
            Row(
              children: [
                Icon(Icons.money_rounded, size: 14, color: AppColor.success),
                const SizedBox(width: 4),
                Text('${promo['amount']} ${'دينار'.tr}', style: AppStyle.number.copyWith(color: AppColor.success)),
                const SizedBox(width: 12),
                Icon(Icons.person_rounded, size: 14, color: AppColor.info),
                const SizedBox(width: 4),
                Text(promo['passengerID'] == 'none' ? 'عام' : 'مخصص', style: AppStyle.caption),
              ],
            ),
          ],
        ),
        trailing: PopupMenuButton(
          icon: const Icon(Icons.more_vert_rounded, color: AppColor.textSecondary),
          color: AppColor.surfaceElevated,
          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
          itemBuilder: (context) => [
            PopupMenuItem(
              value: 'edit',
              child: Row(
                children: [
                  const Icon(Icons.edit_rounded, size: 18, color: AppColor.info),
                  const SizedBox(width: 8),
                  Text('تعديل'.tr),
                ],
              ),
            ),
            PopupMenuItem(
              value: 'delete',
              child: Row(
                children: [
                  const Icon(Icons.delete_outline_rounded, size: 18, color: AppColor.danger),
                  const SizedBox(width: 8),
                  Text('حذف'.tr, style: const TextStyle(color: AppColor.danger)),
                ],
              ),
            ),
          ],
          onSelected: (value) {
            if (value == 'edit') {
              _showPromoSheet(context, promo: promo);
            } else if (value == 'delete') {
              MyDialog().getDialog(
                'حذف كود الخصم',
                'هل أنت متأكد من حذف كود الخصم ${promo['promo_code']}؟',
                () => controller.deletePromo(promo['id'].toString()).then((_) => Get.back()),
              );
            }
          },
        ),
      ),
    );
  }

  void _showPromoSheet(BuildContext context, {dynamic promo}) {
    final TextEditingController codeController = TextEditingController(text: promo?['promo_code']);
    final TextEditingController amountController = TextEditingController(text: promo?['amount']?.toString());
    final TextEditingController descController = TextEditingController(text: promo?['description']);
    final TextEditingController passengerController = TextEditingController(text: promo?['passengerID'] ?? 'none');
    final TextEditingController startDateController = TextEditingController(text: promo?['validity_start_date'] ?? DateTime.now().toString().split(' ')[0]);
    final TextEditingController endDateController = TextEditingController(text: promo?['validity_end_date'] ?? DateTime.now().add(const Duration(days: 30)).toString().split(' ')[0]);

    Get.bottomSheet(
      Container(
        padding: const EdgeInsets.all(24),
        decoration: const BoxDecoration(
          color: AppColor.surfaceElevated,
          borderRadius: BorderRadius.vertical(top: Radius.circular(24)),
        ),
        child: SingleChildScrollView(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Container(
                width: 40,
                height: 4,
                decoration: BoxDecoration(color: AppColor.divider, borderRadius: BorderRadius.circular(2)),
              ),
              const SizedBox(height: 24),
              Text(promo == null ? 'إضافة كود خصم جديد' : 'تعديل كود الخصم', style: AppStyle.headTitle),
              const SizedBox(height: 24),
              MyTextForm(
                controller: codeController,
                label: 'كود الخصم',
                hint: 'مثال: WELCOME20',
                type: TextInputType.text,
                prefixIcon: Icons.local_offer_rounded,
              ),
              MyTextForm(
                controller: amountController,
                label: 'المبلغ',
                hint: 'أدخل قيمة الخصم',
                type: TextInputType.number,
                prefixIcon: Icons.attach_money_rounded,
              ),
              MyTextForm(
                controller: descController,
                label: 'الوصف',
                hint: 'وصف كود الخصم',
                type: TextInputType.text,
                prefixIcon: Icons.description_rounded,
              ),
              MyTextForm(
                controller: passengerController,
                label: 'معرف الراكب (أو none للعام)',
                hint: 'none',
                type: TextInputType.text,
                prefixIcon: Icons.person_rounded,
              ),
              Row(
                children: [
                  Expanded(
                    child: InkWell(
                      onTap: () async {
                        DateTime? picked = await showDatePicker(
                          context: context,
                          initialDate: DateTime.tryParse(startDateController.text) ?? DateTime.now(),
                          firstDate: DateTime(2020),
                          lastDate: DateTime(2030),
                        );
                        if (picked != null) {
                          startDateController.text = picked.toString().split(' ')[0];
                        }
                      },
                      child: IgnorePointer(
                        child: MyTextForm(
                          controller: startDateController,
                          label: 'يبدأ في',
                          hint: 'YYYY-MM-DD',
                          type: TextInputType.none,
                          prefixIcon: Icons.calendar_today_rounded,
                        ),
                      ),
                    ),
                  ),
                  const SizedBox(width: 12),
                  Expanded(
                    child: InkWell(
                      onTap: () async {
                        DateTime? picked = await showDatePicker(
                          context: context,
                          initialDate: DateTime.tryParse(endDateController.text) ?? DateTime.now(),
                          firstDate: DateTime(2020),
                          lastDate: DateTime(2030),
                        );
                        if (picked != null) {
                          endDateController.text = picked.toString().split(' ')[0];
                        }
                      },
                      child: IgnorePointer(
                        child: MyTextForm(
                          controller: endDateController,
                          label: 'ينتهي في',
                          hint: 'YYYY-MM-DD',
                          type: TextInputType.none,
                          prefixIcon: Icons.event_busy_rounded,
                        ),
                      ),
                    ),
                  ),
                ],
              ),
              const SizedBox(height: 16),
              MyElevatedButton(
                title: promo == null ? 'إضافة' : 'حفظ التعديلات',
                onPressed: () async {
                  final data = {
                    if (promo != null) 'id': promo['id'].toString(),
                    'promo_code': codeController.text,
                    'amount': amountController.text,
                    'description': descController.text,
                    'passengerID': passengerController.text,
                    'validity_start_date': startDateController.text,
                    'validity_end_date': endDateController.text,
                  };
                  bool success = promo == null 
                    ? await controller.addPromo(data) 
                    : await controller.updatePromo(data);
                  if (success) Get.back();
                },
              ),
              const SizedBox(height: 16),
            ],
          ),
        ),
      ),

      isScrollControlled: true,
    );
  }
}
