import 'package:fis_common/index.dart';
import 'package:fis_i18n/i18n.dart';
import 'package:fis_jsonrpc/rpc.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/pages/college/controller.dart';
import 'package:get/get.dart';

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

  final bool isApplyReview;
  final String? userGroupCode;

  StudentsReview({this.isApplyReview = true, this.userGroupCode});
  @override
  FState<FStatefulWidget> createState() {
    return ApplyReviewState();
  }
}

class ApplyReviewState extends FState<StudentsReview> {
  final controller = Get.find<FlyinsonoCollegeController>();
  @override
  FWidget build(BuildContext context) {
    return FSimpleDialog(
      okString: i18nBook.common.confirm.t,
      cancelString: i18nBook.common.cancel.t,
      title: FText(
        widget.isApplyReview
            ? i18nBook.college.applyForReview.t
            : i18nBook.college.viewStudent.t,
        style: TextStyle(color: Colors.white),
      ),
      onClose: router.back,
      children: [
        FContainer(
          width: 600,
          height: 400,
          child: FTable<UserGroupStudentsDTO>(
            autoHeight: false,
            noDataHintText: i18nBook.common.noData.t,
            columns: _getTableDatas(),
            source: widget.isApplyReview
                ? controller.state.applayStudents
                : controller.state.students,
          ),
        )
      ],
    );
  }

  String _getUserOrganizationName(String? organizationName) {
    if (organizationName.isNullOrEmpty) return i18nBook.homePage.notYet.t;
    return organizationName!;
  }

  List<FTableColumn<UserGroupStudentsDTO>> _getTableDatas() {
    return [
      FTableColumn<UserGroupStudentsDTO>(
        headerText: i18nBook.common.fullName.t,
        render: (user, i) {
          return FAlign(
            alignment: Alignment.center,
            child: FText(user.name!),
          );
        },
      ),
      FTableColumn<UserGroupStudentsDTO>(
        headerText: i18nBook.user.OrganizationLocated.t,
        render: (user, i) {
          return FAlign(
            alignment: Alignment.center,
            child: FText(
              _getUserOrganizationName(
                user.organizationName,
              ),
            ),
          );
        },
      ),
      FTableColumn<UserGroupStudentsDTO>(
        headerText: i18nBook.common.operation.t,
        render: (user, i) {
          if (widget.isApplyReview) {
            return FContainer(
              width: 200,
              height: 50,
              child: FRow(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: [
                  FElevatedButton(
                    child: FText(i18nBook.remedical.agree.t),
                    onPressed: () async {
                      var result = await controller.agreeApply(
                        user.code!,
                        widget.userGroupCode!,
                      );
                      if (result) {
                        controller.state.applayStudents.removeAt(i);
                        setState(() {});
                      }
                    },
                    businessParent: this.widget,
                    name: i18nBook.remedical.agree.t,
                  ),
                  FElevatedButton(
                    child: FText(i18nBook.user.refuse.t),
                    style: ButtonStyle(
                      backgroundColor:
                          MaterialStateProperty.all<Color>(Colors.red),
                    ),
                    onPressed: () async {
                      var result = await controller.refuseApply(
                        user.code!,
                        widget.userGroupCode!,
                      );
                      if (result) {
                        if (result) {
                          controller.state.applayStudents.removeAt(i);
                          setState(() {});
                        }
                      }
                    },
                    businessParent: this.widget,
                    name: i18nBook.user.refuse.t,
                  ),
                ],
              ),
            );
          } else {
            return FSizedBox(
              height: 50,
              child: FRow(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  FElevatedButton(
                    child: FText(i18nBook.common.remove.t),
                    onPressed: () async {
                      var result = await controller.removeStudent(
                        user.code!,
                        widget.userGroupCode!,
                      );
                      if (result) {
                        controller.state.students.removeAt(i);
                        setState(() {});
                      }
                    },
                    businessParent: widget,
                    name: '',
                  ),
                ],
              ),
            );
          }
        },
      ),
    ];
  }
}
