import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/extensions/context_extension.dart';
import 'package:test_sa/extensions/int_extensions.dart';
import 'package:test_sa/extensions/text_extensions.dart';
import 'package:test_sa/extensions/widget_extensions.dart';
import 'package:test_sa/new_views/app_style/app_color.dart';
import 'package:test_sa/new_views/common_widgets/default_app_bar.dart';
import 'package:test_sa/service_request_latest/request_detail_provider.dart';
import 'package:test_sa/service_request_latest/views/components/history_log_view.dart';

import 'components/request_detail_view.dart';

class RequestDetailMain extends StatefulWidget {
  final int requestId;

  RequestDetailMain({Key? key, required this.requestId}) : super(key: key);

  @override
  _RequestDetailMainState createState() {
    return _RequestDetailMainState();
  }
}

class _RequestDetailMainState extends State<RequestDetailMain> {
  late RequestDetailProvider _requestProvider;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      _requestProvider = Provider.of<RequestDetailProvider>(context, listen: false);
      _requestProvider.getWorkOrderById(id: widget.requestId);
    });
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        // Implement custom back button handling logic here
        RequestDetailProvider requestDetailProvider = Provider.of<RequestDetailProvider>(context,listen: false);
        if(requestDetailProvider.timer!=null&&requestDetailProvider.timer!.isActive){
          requestDetailProvider.stopTimer();
        }
        return true; // Return true if you want to allow popping the screen, false otherwise
      },
      child: Scaffold(
        backgroundColor: AppColor.neutral100,
        appBar:AppBar(
          automaticallyImplyLeading: false,
          titleSpacing: 16,
          title: Row(
            children: [
              const Icon(Icons.arrow_back_ios).onPress(() {
                RequestDetailProvider requestDetailProvider = Provider.of<RequestDetailProvider>(context,listen: false);
                if(requestDetailProvider.timer!=null&&requestDetailProvider.timer!.isActive){
                 requestDetailProvider.stopTimer();
                }
                Navigator.pop(context);

              }),
              10.width,
              Text(
                context.translation.serviceDetails ,
                style: AppTextStyles.heading3.copyWith(fontWeight: FontWeight.w500, color: context.isDark ? AppColor.neutral30 : AppColor.neutral50),
              ).expanded,
            ],
          ),
        ),
        body: DefaultTabController(
          length: 2,
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              Container(
                margin: EdgeInsets.only(left: 16.toScreenWidth, right: 16.toScreenWidth, top: 12.toScreenHeight),
                decoration: BoxDecoration(color: context.isDark ? AppColor.neutral50 : AppColor.white10, borderRadius: BorderRadius.circular(10)),
                child: TabBar(
                  //controller: _tabController,
                  padding: EdgeInsets.symmetric(vertical: 4.toScreenHeight, horizontal: 4.toScreenWidth),
                  labelColor: context.isDark ? AppColor.neutral30 : AppColor.black20,
                  unselectedLabelColor: context.isDark ? AppColor.neutral30 : AppColor.black20,
                  unselectedLabelStyle: AppTextStyles.bodyText,
                  labelStyle: AppTextStyles.bodyText,
                  indicatorPadding: EdgeInsets.zero,
                  indicatorSize: TabBarIndicatorSize.tab,
                  dividerColor: Colors.transparent,
                  indicator: BoxDecoration(color: context.isDark ? AppColor.neutral60 : AppColor.neutral110, borderRadius: BorderRadius.circular(7)),
                  onTap: (index) {
                    // setState(() {});
                  },
                  tabs: [
                    Tab(text: context.translation.requestDetail, height: 57.toScreenHeight),
                    Tab(text: context.translation.historyLogs, height: 57.toScreenHeight),
                  ],
                ),
              ),
              12.height,
              TabBarView(
                children: [
                  WorkOrderDetailView(),
                  const HistoryLogView(),
                ],
              ).expanded,
            ],
          ),
        ),
      ),
    );
  }
}
