import 'dart:convert';

import 'package:siro_driver/constant/box_name.dart';
import 'package:siro_driver/constant/links.dart';
import 'package:siro_driver/controller/functions/crud.dart';
import 'package:siro_driver/views/widgets/my_scafold.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../../../main.dart';
import '../../../../print.dart';

class VipOrderPage extends StatelessWidget {
  const VipOrderPage({super.key});

  @override
  Widget build(BuildContext context) {
    Get.put(VipOrderController());
    return MyScafolld(
      title: 'VIP Order'.tr,
      body: [
        GetBuilder<VipOrderController>(builder: (vipOrderController) {
          if (vipOrderController.isLoading) {
            return const Center(
              child: CircularProgressIndicator(),
            );
          }

          if (vipOrderController.tripData.isEmpty) {
            return Center(
              child: Text('No orders available'.tr),
            );
          }
          final order = vipOrderController.tripData[0];
          Log.print('order: ${order}');
          return SafeArea(
            child: Padding(
              padding: const EdgeInsets.all(8.0),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  // Passenger Section
                  _buildSectionTitle('Passenger Information'.tr),
                  _buildInfoCard(
                    children: [
                      _buildDetailRow('Name'.tr,
                          '${order['passengerName'] ?? 'Unknown'} ${order['passengerLastName'] ?? ''}'),
                      _buildDetailRow(
                          'Phone'.tr, order['passengerPhone'] ?? 'Unknown'),
                      _buildDetailRow(
                          'Gender'.tr, order['passengergender'] ?? 'Unknown'),
                      _buildDetailRow('time Selected'.tr,
                          order['timeSelected'] ?? 'Unknown'),
                      _buildDetailRow(
                          'Ride Status'.tr, order['status'] ?? 'Unknown'),
                      // _buildDetailRow('Ride Status'.tr,
                      //     vipOrderController.myList[4] ?? 'Unknown'),
                      IconButton(
                        onPressed: () {
                          // print(vipOrderController.myList);
                        },
                        icon: const Icon(Icons.add),
                      ),
                    ],
                  ),

                  const SizedBox(height: 24),

                  // Action Buttons
                  _buildActionButtons(context),
                ],
              ),
            ),
          );
        })
      ],
      isleading: true,
    );
  }
}

Widget _buildSectionTitle(String title) {
  return Padding(
    padding: const EdgeInsets.only(bottom: 8.0),
    child: Text(
      title,
      style: const TextStyle(
        fontSize: 18,
        fontWeight: FontWeight.bold,
        color: Colors.black87,
      ),
    ),
  );
}

Widget _buildInfoCard({required List<Widget> children}) {
  return Container(
    decoration: BoxDecoration(
      color: Colors.white,
      borderRadius: BorderRadius.circular(12),
      boxShadow: [
        BoxShadow(
          color: Colors.grey.withOpacity(0.2),
          spreadRadius: 1,
          blurRadius: 5,
          offset: const Offset(0, 3),
        ),
      ],
    ),
    child: Padding(
      padding: const EdgeInsets.all(16.0),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: children,
      ),
    ),
  );
}

Widget _buildDetailRow(String label, String value) {
  return Padding(
    padding: const EdgeInsets.symmetric(vertical: 8.0),
    child: Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Expanded(
          flex: 2,
          child: Text(
            label,
            style: const TextStyle(
              fontWeight: FontWeight.bold,
              color: Colors.black87,
            ),
          ),
        ),
        Expanded(
          flex: 3,
          child: Text(
            value,
            style: const TextStyle(color: Colors.black54),
          ),
        ),
      ],
    ),
  );
}

Widget _buildActionButtons(BuildContext context) {
  return Row(
    children: [
      Expanded(
        child: ElevatedButton(
          onPressed: () => _onReject(context),
          style: ElevatedButton.styleFrom(
            backgroundColor: Colors.red,
            padding: const EdgeInsets.symmetric(vertical: 16),
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(12),
            ),
          ),
          child: Text(
            'Reject'.tr,
            style: const TextStyle(
              color: Colors.white,
              fontWeight: FontWeight.bold,
            ),
          ),
        ),
      ),
      const SizedBox(width: 16),
      Expanded(
        child: ElevatedButton(
          onPressed: () => _onApply(context),
          style: ElevatedButton.styleFrom(
            backgroundColor: Colors.green,
            padding: const EdgeInsets.symmetric(vertical: 16),
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(12),
            ),
          ),
          child: Text(
            'Apply'.tr,
            style: const TextStyle(
              color: Colors.white,
              fontWeight: FontWeight.bold,
            ),
          ),
        ),
      ),
    ],
  );
}

void _onReject(BuildContext context) {
  ScaffoldMessenger.of(context).showSnackBar(
    const SnackBar(
      content: Text('Ride Rejected'),
      backgroundColor: Colors.red,
    ),
  );
}

void _onApply(BuildContext context) {
  // TODO: Implement application logic
  ScaffoldMessenger.of(context).showSnackBar(
    const SnackBar(
      content: Text('Ride Applied'),
      backgroundColor: Colors.green,
    ),
  );
}

class VipOrderController extends GetxController {
  bool isLoading = false;
  List tripData = [];

  fetchOrder() async {
    isLoading = true; // Set loading state
    update(); // Notify listeners
    var res = await CRUD().get(link: AppLink.getMishwariDriver, payload: {
      'driverId': box.read(BoxName.driverID).toString(),
    });
    if (res != 'failure') {
      tripData = jsonDecode(res)['message'];
    } else {
      tripData = [];
    }
    isLoading = false; // Loading complete
    update(); // Notify listeners
  }

  @override
  void onInit() async {
    fetchOrder();
    super.onInit();
  }
}
