import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:mohem_flutter_app/api/profile_api_client.dart';
import 'package:mohem_flutter_app/classes/colors.dart';
import 'package:mohem_flutter_app/classes/utils.dart';
import 'package:mohem_flutter_app/config/routes.dart';
import 'package:mohem_flutter_app/extensions/widget_extensions.dart';
import 'package:mohem_flutter_app/generated/locale_keys.g.dart';
import 'package:mohem_flutter_app/models/get_approves_list_model.dart';
import 'package:mohem_flutter_app/models/profile/submit_contact_transaction_list_model.dart';
import 'package:mohem_flutter_app/ui/misc/request_submit_screen.dart';
import 'package:mohem_flutter_app/widgets/app_bar_widget.dart';
import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:mohem_flutter_app/widgets/button/default_button.dart';
import 'package:mohem_flutter_app/widgets/dynamic_forms/dynamic_textfield_widget.dart';

class DeleteFamilyMember extends StatefulWidget {
  int relationId;
  DeleteFamilyMember(@required this.relationId, {Key? key}) : super(key: key);

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

class _DeleteFamilyMemberState extends State<DeleteFamilyMember> {
  String pFunctionName = "HR_PERINFO_SS";
  String actionType = "DELETE";
  // int? contactRelationId;
  String? date = "MM/DD/YYYY";
  String? datePar;
  // String? itemKey;

  SubmitContactTransactionList? submitContactTransactionList;
  GetApprovesList? getApprovesList;

  //int? relationId;
  @override
  void initState() {
    print(widget.relationId);
    super.initState();
  }

  void deleteFamilyMember(datePar) async {
    try {
      Utils.showLoading(context);
      submitContactTransactionList = await ProfileApiClient().submetConatctTransaction(pFunctionName.toString(), actionType.toString(), widget.relationId!.toInt(), datePar.toString());
      var transactionId = submitContactTransactionList!.pTRANSACTIONID;
      var itemKey = submitContactTransactionList!.pITEMKEY;
      Utils.hideLoading(context);
      //  getApprovesList = await ProfileApiClient().getApproves(transactionId!.toInt(), itemKey.toString() );
      Navigator.pushNamed(context, AppRoutes.requestSubmitScreen,
          arguments: RequestSubmitScreenParams(LocaleKeys.profile_familyDetails.tr(), transactionId!.toInt(), itemKey.toString(), 'family_member'));

      setState(() {});
    } catch (ex) {
      Utils.hideLoading(context);
      Utils.handleException(ex, context, null);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBarWidget(
          context,
          title: LocaleKeys.profile_familyDetails.tr(),
        ),
        backgroundColor: MyColors.backgroundColor,
        body: Column(
          children: [
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  DynamicTextFieldWidget(
                    LocaleKeys.endDate.tr(), date.toString(),
                    isEnable: false,
                    onTap: () async {
                      DateTime dateValue = await _selectDate(context);
                      date = DateFormat('yyyy/MM/dd').format(dateValue);
                      datePar = DateFormat('yyyy/MM/dd hh:mm:ss').format(dateValue);
                      setState(() {});
                    },
                  ).paddingOnly(bottom: 12),
                ],
              ),
            ),
            DefaultButton(
                LocaleKeys.next.tr(), () async {
                deleteFamilyMember(datePar);}
            ).insideContainer,
          ],
        ));
  }

  Widget footer() {
    return Container(
      color: MyColors.white,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Container(
              color: MyColors.gradiantEndColor,
              margin: EdgeInsets.only(bottom: 20, top: 20),
              padding: EdgeInsets.only(left: 50, right: 50),
              child: TextButton(
                style: TextButton.styleFrom(
                  primary: MyColors.white,
                  onSurface: MyColors.white,
                  backgroundColor: MyColors.gradiantEndColor,
                ),
                onPressed: () {
                  deleteFamilyMember(datePar);
                },
                child: Text(
                  LocaleKeys.next.tr(),
                  style: TextStyle(fontStyle: FontStyle.normal, fontSize: 16, color: Colors.white, fontWeight: FontWeight.w600),
                ),
              )),
        ],
      ),
    );
  }

  DateTime selectedDate = DateTime.now();

  Future<DateTime> _selectDate(BuildContext context) async {
    DateTime time = selectedDate;
    if (!Platform.isIOS) {
      await showCupertinoModalPopup(
        context: context,
        builder: (cxt) => Container(
          height: 250,
          color: Colors.white,
          child: CupertinoDatePicker(
            backgroundColor: Colors.white,
            mode: CupertinoDatePickerMode.date,
            onDateTimeChanged: (value) {
              if (value != null && value != selectedDate) {
                time = value;
              }
            },
            initialDateTime: selectedDate,
          ),
        ),
      );
    } else {
      DateTime? picked =
          await showDatePicker(context: context, initialDate: selectedDate, initialEntryMode: DatePickerEntryMode.calendarOnly, firstDate: DateTime(2015, 8), lastDate: DateTime(2101));
      if (picked != null && picked != selectedDate) {
        time = picked;
      }
    }
    return time;
  }
}
