
import 'package:diplomaticquarterapp/core/viewModels/er/rrt-view-model.dart';
import 'package:diplomaticquarterapp/pages/ErService/rapid-response-team/rrt-logs-page.dart';
import 'package:diplomaticquarterapp/pages/ErService/rapid-response-team/rrt-request-page.dart';
import 'package:diplomaticquarterapp/pages/base/base_view.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class RRTMainScreen extends StatefulWidget{
  @override
  State<StatefulWidget> createState() => RRTMainScreenState();
}

class RRTMainScreenState  extends State<RRTMainScreen> with SingleTickerProviderStateMixin{

  int currentIndex = 0;
  TabController tabController;
  PageController pageController = PageController(initialPage: 0, keepPage: true);

  RRTViewModel viewModel;
  bool loadingData;

  @override
  void initState() {
    super.initState();
    tabController = TabController(length: 2, vsync: this);
  }

  TranslationBase localize;
  @override
  Widget build(BuildContext context) {
    localize = TranslationBase.of(context);

    return AppScaffold(
      appBarTitle: localize.rapidResponseTeam,
      isShowAppBar: true,
      body: BaseView<RRTViewModel>(
        onModelReady: (vm) async {
          viewModel = vm;
          loadingData = true;
          await vm.loadRequiredData().then((value){
          }).whenComplete(() => setState(() => loadingData = false));
        },
        builder: (ctx, vm, widget) => content(),
      )
    );
  }

  Widget content(){
    if(loadingData == true){
      return Center(child: CircularProgressIndicator());
      // else if(viewModel.state == ViewState.Error)

    }else if(viewModel.rrtServiceData != null && viewModel.rrtServiceData.servicePrice != null){
      return Column(
        children: [
          tabBar(),
          Expanded(
              child: contentPager()
          )
        ],
      );
    }else{
      return Container(
        alignment: Alignment.center,
        child: Text(localize.somethingWentWrongTryLater, style:  TextStyle(color: Colors.red), maxLines: 5,),
      );
    }
  }

  Widget tabBar() => Container(
    margin: EdgeInsets.all(15),
    clipBehavior: Clip.hardEdge,
    decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(10)),
    child: TabBar(
        onTap: onPageChanged,
        indicatorWeight: 3,
        indicatorColor: Color(0xFFc5272d),
        isScrollable: false,
        controller: tabController,
        indicatorSize: TabBarIndicatorSize.label,
        tabs: [
          Tab(
            child: Text(localize.rapidResponseTeam, style: TextStyle(color: Theme.of(context).appBarTheme.color),),
          ),
          Tab(
            child: Text(localize.orderLog, style: TextStyle(color: Theme.of(context).appBarTheme.color),),
          ),
        ]
    ),
  );

  Widget contentPager() => PageView(
    onPageChanged: onPageChanged,
    controller: pageController,
    children: [
      RRTRequestPage(servicePrice: viewModel.rrtServiceData.servicePrice, pendingOrders: viewModel.rrtServiceData.pendingOrders),
      RRTLogPage(orders: viewModel.rrtServiceData.completedOrders),
    ],
  );

  void onPageChanged(int index) {
    pageController.animateToPage(index, duration: Duration(milliseconds: 200), curve: Curves.easeInOut);
    tabController.animateTo(index);
  }
}
