import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/core/app_export.dart';
import 'package:hmg_patient_app_new/core/utils/date_util.dart';
import 'package:hmg_patient_app_new/core/utils/utils.dart';
import 'package:hmg_patient_app_new/extensions/string_extensions.dart';
import 'package:hmg_patient_app_new/features/hmg_services/hmg_services_view_model.dart';
import 'package:hmg_patient_app_new/features/hmg_services/models/resq_models/search_e_referral_resp_model.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/theme/colors.dart';
import 'package:hmg_patient_app_new/widgets/appbar/collapsing_list_view.dart';
import 'package:provider/provider.dart';
import 'package:smooth_corner/smooth_corner.dart';

class SearchResultPage extends StatefulWidget {
  const SearchResultPage({super.key});

  @override
  _SearchResultPageState createState() => _SearchResultPageState();
}

class _SearchResultPageState extends State<SearchResultPage> {
  HmgServicesViewModel? hmgServicesVM;

  String _selectedFilter = 'All';

  @override
  void initState() {
    hmgServicesVM = context.read<HmgServicesViewModel>();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return CollapsingListView(
      title: LocaleKeys.searchResult.tr(context: context),
      child: Column(
        children: [
          // List of referrals
          ListView.builder(
            padding: EdgeInsets.zero,
            shrinkWrap: true,
            itemCount: hmgServicesVM?.searchReferralList.length,
            itemBuilder: (context, index) {
              return _buildReferralCard(hmgServicesVM!.searchReferralList[index]);
            },
          ),
        ],
      ),
    );
  }

  Widget _buildReferralCard(SearchEReferralResponseModel referral) {
    return SmoothCard(
      borderRadius: BorderRadius.circular(16.h),
      margin: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
      color: AppColors.whiteColor,
      child: Padding(
        padding: EdgeInsets.all(16.h),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                  LocaleKeys.referralNo.tr(context: context, namedArgs: {'number': '${referral.referralNumber}'}).toText18(isBold: true, color: AppColors.textColor),
                Container(
                  padding: EdgeInsets.symmetric(horizontal: 12, vertical: 6),
                  decoration: BoxDecoration(
                    color: _getStatusColor(referral.status!),
                    borderRadius: BorderRadius.circular(20),
                  ),
                  child:
                    referral.status!.toText12(color: AppColors.whiteColor,
                    // style: TextStyle(
                    //   color: Colors.white,
                    //   fontSize: 12,
                    //   isBold: true,
                    // ),
                  ),
                ),
              ],
            ),

            SizedBox(height: 16),

            // Patient information
            Row(
              children: [
                Container(
                  width: 50,
                  height: 50,
                  decoration: BoxDecoration(
                    color:  AppColors.lightGrayBGColor,
                    shape: BoxShape.circle,
                  ),
                  child: Icon(
                    Icons.person,
                    color: AppColors.lightGrayBGColor,
                    size: 30,
                  ),
                ),
                SizedBox(width: 12),
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      //Text(
                        referral.patientName!.toText16(isBold: true, color: AppColors.textColor),
                        // style: TextStyle(
                        //   fontSize: 16,
                        //   fontWeight: FontWeight.bold,
                        // ),
                     // ),
                      SizedBox(height: 4),
                    //  Text(
                        'ID: ${referral.identityNumber}'.toText14(color: AppColors.greyTextColor),
                        // style: TextStyle(
                        //   color: Colors.grey[600],
                        // ),
                     // ),
                    ],
                  ),
                ),
              ],
            ),

            SizedBox(height: 16),

            // Details row
            Row(
              children: [
                _buildDetailItem(
                  Icons.phone,
                  referral.patientContactNo!,
                ),
                SizedBox(width: 16),
                _buildDetailItem(Icons.calendar_today, Utils.getDayMonthYearDateFormatted(DateUtil.convertStringToDateNoTimeZone(referral.referralDate!))),
              ],
            ),

            SizedBox(height: 16),

            // Requester information
            Container(
              padding: EdgeInsets.all(5.h),
              decoration: BoxDecoration(
                color: AppColors.lightGrayBGColor,
                borderRadius: BorderRadius.circular(8),
              ),
              child: Row(
                children: [
                  Icon(
                    Icons.person_outline,
                    color: Colors.grey[600],
                    size: 20,
                  ),
                  SizedBox(width: 8),
                  //Text(
                    'Requester: ${referral.requesterName}'.toText14(),
                    // style: TextStyle(
                    //   color: Colors.grey[700],
                    // ),
                 // ),
                  SizedBox(width: 16),
                  Icon(
                    Icons.phone,
                    color: Colors.grey[600],
                    size: 16,
                  ),
                  SizedBox(width: 4),
                 // Text(
                    referral.requesterContactNo!.toText14(),
                  //   style: TextStyle(
                  //     color: Colors.grey[700],
                  //   ),
                  // ),
                ],
              ),
            ),

            SizedBox(height: 12),

            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Row(
                  children: [
                    Icon(
                      Icons.group,
                      color: Colors.grey[600],
                      size: 16,
                    ),
                    SizedBox(width: 4),
                    //Text(
                      'Relationship: ${referral.relationshipType?.text}'.toText14(color:AppColors.greyTextColor),
                    //   style: TextStyle(
                    //     color: Colors.grey[700],
                    //     fontSize: 14,
                    //   ),
                    // ),
                  ],
                ),
                Row(
                  children: [
                    Icon(
                      Icons.attach_file,
                      color: Colors.grey[600],
                      size: 16,
                    ),
                    SizedBox(width: 4),
                  //  Text(
                      '${referral.medicalReportAttachment?.length} file(s)'.toText14(color:AppColors.greyTextColor),
                    //   style: TextStyle(
                    //     color: Colors.grey[700],
                    //     fontSize: 14,
                    //   ),
                    // ),
                  ],
                ),
              ],
            ),

            SizedBox(height: 16),

            // Action buttons
          ],
        ),
      ),
    );
  }

  Widget _buildDetailItem(IconData icon, String text) {
    return Row(
      children: [
        Icon(
          icon,
          color: Colors.grey[600],
          size: 16,
        ),
        SizedBox(width: 4),

          text.toText14(color: AppColors.greyTextColor),

      ],
    );
  }

  Color _getStatusColor(String status) {
    switch (status) {
      case 'Pending':
        return AppColors.alertColor;
      case 'Completed':
        return AppColors.bgGreenColor;
      case 'Rejected':
        return AppColors.primaryRedColor;
      default:
        return AppColors.lightGrayColor;
    }
  }


  Widget _buildFilterOption(String filter) {
    return ListTile(
      leading: Icon(
        _selectedFilter == filter ? Icons.radio_button_checked : Icons.radio_button_off,
        color: _selectedFilter == filter ? Colors.blue[700] : Colors.grey,
      ),
      title: Text(filter),
      onTap: () {
        setState(() {
          _selectedFilter = filter;
        });
        Navigator.pop(context);
      },
    );
  }
}
