import 'package:fis_common/index.dart';
import 'package:fis_i18n/i18n.dart';
import 'package:fis_jsonrpc/rpc.dart';
import 'package:fis_theme/theme.dart';
import 'package:fis_ui/base_define/page.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/router/router.dart';
import 'package:flyinsono/architecture/utils/prompt_box.dart';
import 'package:flyinsono/pages/college/index.dart';
import 'package:flyinsono/pages/remedical/consultation/widgets/fcustom_flat_button.dart';
import 'package:flyinsono/pages/remote_controller/index.dart';
import 'package:get/get.dart';

import 'custom_card.dart';

class ApplayReviewMobile extends FStatefulWidget implements FPage {
  @override
  String get pageName => "ApplayReviewMobile";

  final bool isApplyReview;
  final String? userGroupCode;

  ApplayReviewMobile({this.isApplyReview = true, this.userGroupCode});

  @override
  FState<FStatefulWidget> createState() {
    return _ApplayReviewMobileState();
  }
}

class _ApplayReviewMobileState extends FState<ApplayReviewMobile> {
  final controller = Get.find<FlyinsonoCollegeController>();
  bool get isApplyReview => widget.isApplyReview;
  @override
  FWidget build(BuildContext context) {
    List<UserGroupStudentsDTO> students;
    if (isApplyReview) {
      students = controller.state.applayStudents;
    } else {
      students = controller.state.students;
    }
    return FScaffold(
      appBar: FAppBar.simple(
        title: isApplyReview
            ? i18nBook.college.applyForReview.t
            : i18nBook.college.viewStudent.t,
        onBackClicked: router.back,
      ),
      body: FContainer(
        child: FListView(
          padding: EdgeInsets.symmetric(horizontal: 15),
          shrinkWrap: true,
          children: students
              .map(
                /// TODO BAKA server 更改
                (e) => FCustomCard(
                  title: e.name ?? '',
                  line2: FTitleField(
                    i18nBook.user.OrganizationLocated.t,
                    FText(
                      e.organizationName ?? '',
                      style: TextStyle(color: Colors.black54),
                    ),
                    isBold: false,
                    sizeBetween: 20,
                    titleColor: Colors.black54,
                  ),
                  line3: FTitleField(
                    i18nBook.common.mobileNumber.t,
                    FText(
                      e.phone.isNotNullOrEmpty
                          ? e.phone!
                          : i18nBook.remedical.notFill.t,
                      style: TextStyle(color: Colors.black54),
                    ),
                    isBold: false,
                    sizeBetween: 20,
                    titleColor: Colors.black54,
                  ),
                  operateButtons: FRow(
                    children: [
                      if (isApplyReview) ...[
                        FCustomFlatButton(
                          i18nBook.remedical.agree.t,
                          () async {
                            var result = await controller.agreeApply(
                              e.code!,
                              widget.userGroupCode!,
                            );
                            if (result) {
                              controller.state.applayStudents.remove(e);
                              setState(() {});
                            }
                          },
                          businessParent: this.widget,
                          fontColor: FTheme.ins.colorScheme.primary,
                        ),
                        FContainer(
                          height: 30,
                          child: FVerticalDivider(
                            width: 1,
                            thickness: 1,
                            color: FTheme.ins.colorScheme.line,
                          ),
                        ),
                        FCustomFlatButton(
                          i18nBook.user.refuse.t,
                          () async {
                            var result = await controller.refuseApply(
                              e.code!,
                              widget.userGroupCode!,
                            );
                            if (result) {
                              controller.state.applayStudents.remove(e);
                              setState(() {});
                            }
                          },
                          businessParent: this.widget,
                          fontColor: Colors.red,
                        ),
                      ] else ...[
                        FCustomFlatButton(
                          i18nBook.common.remove.t,
                          () async {
                            var result = await controller.removeStudent(
                              e.code!,
                              widget.userGroupCode!,
                            );
                            if (result) {
                              controller.state.students.remove(e);
                              PromptBox.toast(i18nBook.user.deleteSuccess.t);
                              setState(() {});
                            }
                          },
                          businessParent: this.widget,
                          fontColor: Colors.red,
                        ),
                      ]
                    ],
                  ),
                ),
              )
              .toList(),
        ),
      ),
    );
  }
}
