import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:intl/intl.dart';
import 'package:niogu_app/core/constants/app_color.dart';
import 'package:niogu_app/core/constants/app_font_size.dart';
import 'package:niogu_app/core/router/app_route.dart';
import 'package:niogu_app/core/widgets/custom_form_input.dart';
import 'package:niogu_app/features/stock_in/domain/entities/stock_in_purchase.dart';
import 'package:sizer/sizer.dart';

class HeaderStockInPurchaseSection extends StatelessWidget {
  final DateTime selectedDate;
  final bool isSupplierFormVisible;
  final SelectedSupplier? selectedSupplier;
  final TextEditingController nameController;
  final TextEditingController phoneNumberController;
  final TextEditingController addressController;
  final VoidCallback onTap;
  final VoidCallback onSupplierFormTap;
  final VoidCallback? onTapCloseSelectedSupplier;
  const HeaderStockInPurchaseSection({
    super.key,
    required this.selectedDate,
    required this.isSupplierFormVisible,
    required this.selectedSupplier,
    required this.nameController,
    required this.phoneNumberController,
    required this.addressController,
    required this.onTap,
    required this.onSupplierFormTap,
    required this.onTapCloseSelectedSupplier,
  });

  @override
  Widget build(BuildContext context) {
    final bool isTablet = 100.w >= 600;
    return Container(
      padding: EdgeInsets.all(4.w),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(2.5.w),
        border: Border.all(color: Colors.grey.shade200),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.02),
            blurRadius: 5,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          InkWell(
            onTap: onTap,
            child: Row(
              children: [
                Container(
                  padding: EdgeInsets.all(2.w),
                  decoration: BoxDecoration(
                    color: Colors.blue[50],
                    borderRadius: BorderRadius.circular(2.w),
                  ),
                  child: Icon(
                    Icons.calendar_today_rounded,
                    color: AppColor.primaryColor,
                    size: 5.w,
                  ),
                ),
                SizedBox(width: 3.w),
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        "Tanggal Pembelian",
                        style: TextStyle(
                          color: Colors.grey[600],
                          fontSize: isTablet
                              ? AppFontSize.medium.sp
                              : AppFontSize.small.sp,
                        ),
                      ),
                      SizedBox(height: 0.5.h),
                      Text(
                        DateFormat('EEEE, dd MMMM yyyy').format(selectedDate),
                        style: TextStyle(
                          fontWeight: FontWeight.bold,
                          fontSize: isTablet
                              ? AppFontSize.medium.sp
                              : AppFontSize.small.sp,
                        ),
                      ),
                    ],
                  ),
                ),
                Icon(Icons.edit_outlined, size: 5.w, color: Colors.grey[400]),
              ],
            ),
          ),
          Divider(height: 3.h, color: Colors.grey[100]),
          Container(
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(2.5.w),
              border: Border.all(color: Colors.grey.shade300),
            ),
            child: Column(
              children: [
                InkWell(
                  onTap: onSupplierFormTap,
                  borderRadius: BorderRadius.vertical(
                    top: Radius.circular(2.5.w),
                    bottom: Radius.circular(isSupplierFormVisible ? 0 : 2.5.w),
                  ),
                  child: Padding(
                    padding: EdgeInsets.symmetric(
                      horizontal: 4.w,
                      vertical: 1.5.h,
                    ),
                    child: Row(
                      children: [
                        Container(
                          padding: EdgeInsets.all(1.5.w),
                          decoration: BoxDecoration(
                            color: isSupplierFormVisible
                                ? AppColor.primaryColor.withOpacity(0.1)
                                : Colors.grey[100],
                            shape: BoxShape.circle,
                          ),
                          child: Icon(
                            Icons.person_outline_rounded,
                            color: isSupplierFormVisible
                                ? AppColor.primaryColor
                                : Colors.grey,
                            size: 5.w,
                          ),
                        ),
                        SizedBox(width: 3.w),
                        Expanded(
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(
                                "Informasi Pemasok",
                                style: TextStyle(
                                  fontWeight: FontWeight.bold,
                                  fontSize: isTablet
                                      ? AppFontSize.medium.sp
                                      : AppFontSize.small.sp,
                                  color: Colors.black87,
                                ),
                              ),
                              SizedBox(height: 1.h),
                              Text(
                                "Opsional (Nama, No. Hp / WA)",
                                style: TextStyle(
                                  fontSize: isTablet
                                      ? AppFontSize.medium.sp
                                      : AppFontSize.small.sp,
                                  color: Colors.grey,
                                ),
                              ),
                            ],
                          ),
                        ),
                        Icon(
                          isSupplierFormVisible
                              ? Icons.keyboard_arrow_up_rounded
                              : Icons.keyboard_arrow_down_rounded,
                          color: Colors.grey,
                          size: 6.w,
                        ),
                      ],
                    ),
                  ),
                ),
                if (isSupplierFormVisible) ...[
                  Divider(height: 1, color: Colors.grey.shade200),
                  Padding(
                    padding: EdgeInsets.all(4.w),
                    child: selectedSupplier != null
                        ? Container(
                            width: double.infinity,
                            padding: EdgeInsets.all(4.w),
                            decoration: BoxDecoration(
                              color: AppColor.primaryColor.withOpacity(0.05),
                              borderRadius: BorderRadius.circular(2.5.w),
                              border: Border.all(
                                color: AppColor.primaryColor.withOpacity(0.3),
                              ),
                            ),
                            child: Row(
                              children: [
                                Container(
                                  padding: EdgeInsets.all(2.w),
                                  decoration: BoxDecoration(
                                    color: Colors.white,
                                    shape: BoxShape.circle,
                                    border: Border.all(
                                      color: AppColor.primaryColor.withOpacity(
                                        0.2,
                                      ),
                                    ),
                                  ),
                                  child: Icon(
                                    Icons.person,
                                    color: AppColor.primaryColor,
                                    size: 5.w,
                                  ),
                                ),
                                SizedBox(width: 3.w),
                                Expanded(
                                  child: Column(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: [
                                      Text(
                                        selectedSupplier!.name,
                                        style: TextStyle(
                                          fontSize: isTablet
                                              ? AppFontSize.medium.sp
                                              : AppFontSize.small.sp,
                                          fontWeight: FontWeight.bold,
                                          color: Colors.black87,
                                        ),
                                      ),
                                      if (selectedSupplier!
                                          .phoneNumber
                                          .isNotEmpty) ...[
                                        SizedBox(height: 0.75.h),
                                        Row(
                                          children: [
                                            Icon(
                                              Icons.phone_android,
                                              size: 5.w,
                                              color: Colors.grey[600],
                                            ),
                                            SizedBox(width: 1.w),
                                            Text(
                                              selectedSupplier!.phoneNumber,
                                              style: TextStyle(
                                                fontSize: isTablet
                                                    ? AppFontSize.medium.sp
                                                    : AppFontSize.small.sp,
                                                color: Colors.grey[700],
                                              ),
                                            ),
                                          ],
                                        ),
                                      ],

                                      if (selectedSupplier!
                                          .address
                                          .isNotEmpty) ...[
                                        SizedBox(height: 0.75.h),
                                        Row(
                                          crossAxisAlignment:
                                              CrossAxisAlignment.start,
                                          children: [
                                            Icon(
                                              Icons.location_on_outlined,
                                              size: 5.w,
                                              color: Colors.grey[600],
                                            ),
                                            SizedBox(width: 1.w),
                                            Expanded(
                                              child: Text(
                                                selectedSupplier!.address,
                                                style: TextStyle(
                                                  fontSize: isTablet
                                                      ? AppFontSize.medium.sp
                                                      : AppFontSize.small.sp,
                                                  color: Colors.grey[700],
                                                ),
                                              ),
                                            ),
                                          ],
                                        ),
                                      ],
                                    ],
                                  ),
                                ),
                                InkWell(
                                  onTap: onTapCloseSelectedSupplier,
                                  child: Icon(
                                    Icons.close,
                                    color: Colors.grey,
                                    size: 5.w,
                                  ),
                                ),
                              ],
                            ),
                          )
                        : Column(
                            children: [
                              SizedBox(
                                width: double.infinity,
                                height: 5.5.h,
                                child: OutlinedButton.icon(
                                  onPressed: () {
                                    context.pushNamed(
                                      AppRoute.alreadySupplierScreen,
                                    );
                                  },
                                  style: OutlinedButton.styleFrom(
                                    side: BorderSide(
                                      color: AppColor.primaryColor,
                                    ),
                                    shape: RoundedRectangleBorder(
                                      borderRadius: BorderRadius.circular(
                                        2.5.w,
                                      ),
                                    ),
                                    backgroundColor: Colors.blue[50],
                                  ),
                                  icon: Icon(
                                    Icons.people_alt_rounded,
                                    color: AppColor.primaryColor,
                                    size: 5.w,
                                  ),
                                  label: Text(
                                    "Pilih Pemasok Tersedia",
                                    style: TextStyle(
                                      color: AppColor.primaryColor,
                                      fontWeight: FontWeight.bold,
                                      fontSize: AppFontSize.medium.sp,
                                    ),
                                  ),
                                ),
                              ),
                              SizedBox(height: 2.h),
                              Row(
                                children: [
                                  Expanded(
                                    child: Divider(color: Colors.grey.shade300),
                                  ),
                                  Padding(
                                    padding: EdgeInsets.symmetric(
                                      horizontal: 3.w,
                                    ),
                                    child: Text(
                                      "Atau Input Manual",
                                      style: TextStyle(
                                        fontSize: isTablet
                                            ? (AppFontSize.medium - 2).sp
                                            : (AppFontSize.small - 2).sp,
                                        fontWeight: FontWeight.bold,
                                        color: Colors.grey[400],
                                      ),
                                    ),
                                  ),
                                  Expanded(
                                    child: Divider(color: Colors.grey.shade300),
                                  ),
                                ],
                              ),
                              SizedBox(height: 2.h),
                              CustomFormInput(
                                label: "Nama Pemasok",
                                icon: Icons.person,
                                controller: nameController,
                              ),
                              SizedBox(height: 1.5.h),
                              CustomFormInput(
                                label: "No. Hp / WA",
                                hint: "Opsional",
                                icon: Icons.phone_android,
                                controller: phoneNumberController,
                                inputType: TextInputType.phone,
                              ),
                              /**
                              SizedBox(height: 1.5.h),
                              CustomFormInput(
                                label: "Alamat",
                                hint: "Opsional",
                                icon: Icons.location_on_outlined,
                                controller: addressController,
                              ),
                              */
                            ],
                          ),
                  ),
                ],
              ],
            ),
          ),
        ],
      ),
    );
  }
}
