import 'dart:io';


import 'package:flutter/material.dart';
import 'package:rasadyar_chicken/data/models/response/iran_province_city/iran_province_city_model.dart';
import 'package:rasadyar_chicken/data/models/response/roles_products/roles_products.dart';
import 'package:rasadyar_chicken/data/models/response/steward_free_bar/steward_free_bar.dart';
import 'package:rasadyar_chicken/presentation/pages/sales_out_of_province/pages/buyers_page.dart';
import 'package:rasadyar_chicken/presentation/pages/sales_out_of_province/pages/sales_page.dart';
import 'package:rasadyar_core/core.dart';

import 'logic.dart';

class SalesOutOfProvincePage extends GetView<SalesOutOfProvinceLogic> {
  const SalesOutOfProvincePage({super.key});

  @override
  Widget build(BuildContext context) {
    return ObxValue((index) {
      return DefaultTabController(
        length: 2,
        initialIndex: index.value,
        child: Scaffold(
          appBar: RAppBar(
            titleTextStyle: AppFonts.yekan16Bold.copyWith(color: Colors.white),
            centerTitle: true,
            hasBack: true,
            onBackPressed: () {
              Get.back(id: 1);
            },
            leadingWidth: 155,
            leading: Row(
              mainAxisSize: MainAxisSize.min,
              spacing: 6,
              children: [
                Assets.vec.chickenSvg.svg(
                  width: 24,
                  height: 24,
                  colorFilter: const ColorFilter.mode(Colors.white, BlendMode.srcIn),
                ),
                Text('رصدطیور', style: AppFonts.yekan16Bold.copyWith(color: Colors.white)),
              ],
            ),
            additionalActions: [
              GestureDetector(
                onTap: () {
                  controller.searchIsSelected.value = !controller.searchIsSelected.value;
                },
                child: Assets.vec.searchSvg.svg(
                  width: 24,
                  height: 24,
                  colorFilter: const ColorFilter.mode(Colors.white, BlendMode.srcIn),
                ),
              ),
              SizedBox(width: 8),
              Visibility(
                visible: controller.currentIndex.value==0,
                child: GestureDetector(
                  onTap: () {
                    Get.bottomSheet(filterBottomSheet());
                  },
                  child: Assets.vec.filterOutlineSvg.svg(
                    width: 20,
                    height: 20,
                    colorFilter: const ColorFilter.mode(Colors.white, BlendMode.srcIn),
                  ),
                ),
              ),
              SizedBox(width: 8),
            ],
          ),
          body: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              routePageWidget(),
              Container(
                child: TabBar(
                  tabs: [
                    Tab(text: 'فروش' ),
                    Tab(text: 'خریداران'),
                  ],
                  onTap: (value) {
                    controller.currentIndex.value= value;
                  },
                  labelColor: AppColor.blueNormal,
                  unselectedLabelColor: AppColor.mediumGreyDarkHover,
                  indicatorColor:  AppColor.blueNormal,
                  indicatorSize: TabBarIndicatorSize.tab,
                  indicator: BoxDecoration(
                    color: AppColor.blueLight,
                    border: Border(
                      bottom: BorderSide(
                        color:AppColor.blueNormal,
                      ),
                    ),
                  ),
                ),
              ),
              Expanded(
                child: TabBarView(
                  children: [
                    SalesPage(),
                    BuyersPage()
                  ],
                ),
              ),
            ],
          ),
        ),
      );
    }, controller.currentIndex);
  }

  Row routePageWidget() {
    return Row(
      children: [
        SizedBox(width: 8),
        RichText(
          text: TextSpan(
            style: AppFonts.yekan14.copyWith(color: AppColor.blueNormal),
            children: [
              WidgetSpan(
                child: Row(
                  children: [
                    Assets.vec.cubeSearchSvg.svg(
                      width: 24,
                      height: 24,
                      colorFilter: const ColorFilter.mode(AppColor.blueNormal, BlendMode.srcIn),
                    ),
                    SizedBox(width: 6),
                  ],
                ),
              ),

              TextSpan(text: 'خارج استان'),
              TextSpan(text: '/'),
              TextSpan(text: 'فروش'),
            ],
          ),
        ),
      ],
    );
  }

  Widget buildRow(String title, String value) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 4.0),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Flexible(
            flex: 2,
            child: Text(
              title,
              textAlign: TextAlign.right,
              style: AppFonts.yekan14.copyWith(color: AppColor.darkGreyDarkHover),
            ),
          ),
          Flexible(
            flex: 2,
            child: Text(
              value,
              textAlign: TextAlign.left,

              style: AppFonts.yekan14.copyWith(color: AppColor.darkGreyDarkHover),
            ),
          ),
        ],
      ),
    );
  }

  Widget filterBottomSheet() {
    return BaseBottomSheet(
      height: 250,
      child: Column(
        spacing: 16,
        children: [
          Text('فیلترها', style: AppFonts.yekan16Bold.copyWith(color: AppColor.darkGreyDarkHover)),
          Row(
            spacing: 8,
            children: [
              Expanded(
                child: timeFilterWidget(
                  date: controller.fromDateFilter,
                  onChanged: (jalali) => controller.fromDateFilter.value = jalali,
                ),
              ),
              Expanded(
                child: timeFilterWidget(
                  isFrom: false,
                  date: controller.toDateFilter,
                  onChanged: (jalali) => controller.toDateFilter.value = jalali,
                ),
              ),
            ],
          ),
          SizedBox(height: 2),
          RElevated(
            text: 'اعمال فیلتر',
            onPressed: () {
              controller.getOutProvinceSales();
              Get.back();
            },
            height: 40,
          ),
          SizedBox(height: 16),
        ],
      ),
    );
  }

  GestureDetector timeFilterWidget({
    isFrom = true,
    required Rx<Jalali> date,
    required Function(Jalali jalali) onChanged,
  }) {
    return GestureDetector(
      onTap: () {
        Get.bottomSheet(modalDatePicker((value) => onChanged(value)));
      },
      child: Container(
        height: 35,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(8),
          border: Border.all(width: 1, color: AppColor.blueNormal),
        ),
        padding: EdgeInsets.symmetric(horizontal: 11, vertical: 4),
        child: Row(
          spacing: 8,
          children: [
            Assets.vec.calendarSvg.svg(
              width: 24,
              height: 24,
              colorFilter: const ColorFilter.mode(AppColor.blueNormal, BlendMode.srcIn),
            ),
            Text(isFrom ? 'از' : 'تا', style: AppFonts.yekan16.copyWith(color: AppColor.blueNormal)),
            Expanded(
              child: ObxValue((data) {
                return Text(
                  date.value.formatCompactDate(),
                  textAlign: TextAlign.center,
                  style: AppFonts.yekan16.copyWith(color: AppColor.lightGreyNormalActive),
                );
              }, date),
            ),
          ],
        ),
      ),
    );
  }

  Container modalDatePicker(ValueChanged<Jalali> onDateSelected) {
    Jalali? tempPickedDate;
    return Container(
      height: 250,
      color: Colors.white,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          Container(
            child: Row(
              children: [
                SizedBox(width: 20),
                RElevated(
                  height: 35,
                  width: 70,
                  textStyle: AppFonts.yekan14.copyWith(color: Colors.white),
                  onPressed: () {
                    onDateSelected(tempPickedDate ?? Jalali.now());
                    Get.back();
                  },
                  text: 'تایید',
                ),
                Spacer(),
                RElevated(
                  height: 35,
                  width: 70,
                  backgroundColor: AppColor.error,
                  textStyle: AppFonts.yekan14.copyWith(color: Colors.white),
                  onPressed: () {
                    onDateSelected(tempPickedDate ?? Jalali.now());
                    Get.back();
                  },
                  text: 'لغو',
                ),
                SizedBox(width: 20),
              ],
            ),
          ),
          Divider(height: 0, thickness: 1),
          Expanded(
            child: Container(
              child: PersianCupertinoDatePicker(
                initialDateTime: Jalali.now(),
                mode: PersianCupertinoDatePickerMode.date,
                onDateTimeChanged: (dateTime) {
                  tempPickedDate = dateTime;
                },
              ),
            ),
          ),
        ],
      ),
    );
  }

}
