import 'package:fis_i18n/i18n.dart';
import 'package:fis_ui/base_define/page.dart';
import 'package:fis_ui/index.dart';
import 'package:fis_ui/interface/interactive_container.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/pages/user/user_device/controller.dart';
import 'package:flyinsono/pages/user/user_device/widgets/delete_user.dart';
import 'package:get/get.dart';

///选择设备分享用户
class SelectDeviceUserMember extends StatefulWidget implements FPage {
  SelectDeviceUserMember({
    Key? key,
    this.code = '',
    this.pageName = 'SelectDeviceUserMember',
  }) : super(key: key);
  final String? code;
  @override
  final String pageName;

  @override
  State<SelectDeviceUserMember> createState() => _SelectDeviceUserMemberState();
}

class _SelectDeviceUserMemberState extends State<SelectDeviceUserMember> {
  final userDeviceController = Get.put(DeviceController());

  @override
  FWidget build(BuildContext context) {
    userDeviceController.state.batchOperation = false;
    return FSafeArea(
      top: false,
      child: FScaffold(
        appBar: _buildSelectUserAppBar(),
        body: FMaterial(
          color: Colors.white,
          child: FSafeArea(
            child: FColumn(
              children: [
                FSizedBox(
                  height: 15,
                ),
                FExpanded(
                  child: FObx(
                    () {
                      final tableDataSource =
                          userDeviceController.state.tableDataSource;
                      return FListView.builder(
                        shrinkWrap: true,
                        itemCount: tableDataSource.length,
                        itemBuilder: (context, index) {
                          return _buildDeleteMember(context, index, widget);
                        },
                      );
                    },
                  ),
                ),
              ],
            ),
          ),
        ),
        bottomNavigationBar: FObx(
          () {
            if (userDeviceController.state.batchOperation) {
              return FBottomSubmitButton(
                businessParent: widget,
                name: "Device/SelectUser",
                title: i18nBook.common.remove.t,
                onPressed: () {
                  deleteList = [];
                  userDeviceController.deleteShareDeviceToUserAsync(widget);
                },
              );
            } else {
              return FText('');
            }
          },
        ),
      ),
    );
  }

  ///选择用户APPBar
  AppBar _buildSelectUserAppBar() {
    return FAppBar(
      title: FText(i18nBook.user.userRelated.t),
      elevation: 0,
      actions: [
        FGestureDetector(
          businessParent: widget,
          name: "userRelated",
          onTap: () {
            userDeviceController.state.batchOperation =
                !userDeviceController.state.batchOperation;
            deleteList = [];
          },
          child: FObx(
            () {
              var child;
              userDeviceController.state.batchOperation
                  ? child = FCenter(
                      child: FText(i18nBook.common.cancel.t),
                    )
                  : child = FCenter(
                      child: FText(i18nBook.user.batchOperation.t),
                    );
              return child;
            },
          ),
        ),
        FSizedBox(
          width: 10,
        )
      ],
      centerTitle: true,
      leading: FIconButton(
        name: "back",
        businessParent: widget,
        icon: FIcon(Icons.arrow_back_ios_new),
        onPressed: () {
          deleteList = [];
          Get.back();
        },
      ),
    );
  }

  ///移除选择的用户
  FWidget _buildDeleteMember(
      BuildContext context, int index, FInteractiveContainer businessParent) {
    FWidget image(String? isImage) {
      var child;
      isImage!.isEmpty
          ? child = QuickFWidget(FlutterLogo())
          : child = FImage.network(isImage);
      return child;
    }

    return DeleteMemberUser(
      image: FCircleAvatar(
        child: image(
          userDeviceController.state.tableDataSource
              .toList()[index]
              .headImageUrl,
        ),
      ),
      title: FText(
        userDeviceController.state.tableDataSource.toList()[index].displayName!,
        style: TextStyle(
          fontWeight: FontWeight.bold,
          fontSize: 15,
        ),
      ),
      listObject: userDeviceController.state.tableDataSource.toList()[index],
      positationalText: FText(
        userDeviceController.state.tableDataSource.toList()[index].phone!,
        style: TextStyle(
          color: Color.fromRGBO(143, 141, 140, 1),
          fontSize: 14,
        ),
      ),
      onClick: () {},
      businessParent: businessParent,
    );
  }
}
