import 'package:fis_i18n/i18n.dart';
import 'package:fis_jsonrpc/services/user.m.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';

///关联用户
class CorrelationUser extends FisView<DeviceController>
    implements FInteractiveContainer {
  CorrelationUser({this.pageName = 'CorrelationUser'});
  @override
  final String pageName;
  @override
  FWidget build(BuildContext context) {
    final columns = _buildHeadPanel();
    return FContainer(
      margin: EdgeInsets.symmetric(vertical: 10),
      child: FColumn(
        children: [
          _buildBottomList(),
          FExpanded(
            child: FObx(
              () {
                if (controller.state.terminals.length == 0)
                  return NoDataWidget();
                return FTable<ShareDeviceUserDTO>(
                  autoHeight: false,
                  noDataHintText: i18nBook.common.noData.t,
                  columns: columns,
                  source: controller.state.tableDataSource,
                  showSelect: true,
                  selecteds: [],
                  onRowSelected: (value, index, idxs) {
                    controller.getSelectedIdxs(idxs);
                  },
                  onAllRowsSelected: (value, idxs) {
                    controller.getSelectedIdxs(idxs);
                  },
                );
              },
            ),
          ),
        ],
      ),
    );
  }

  ///底部按钮集合
  FWidget _buildBottomList() {
    return FContainer(
      height: 54,
      child: FFlex(
        direction: Axis.horizontal,
        mainAxisAlignment: MainAxisAlignment.end,
        children: [
          FElevatedButton(
            businessParent: this,
            name: "CorrelationUser deleteShareDeviceToUser",
            onPressed: () {
              controller.deleteShareDeviceToUserAsync(this);
            },
            style: ButtonStyle(
              padding: MaterialStateProperty.all(
                EdgeInsets.symmetric(vertical: 15, horizontal: 25),
              ),
            ),
            child: FText(i18nBook.common.remove.t),
          ),
          FSizedBox(
            width: 20,
          ),
        ],
      ),
    );
  }

  ///带用户头像的用户集合
  List<FTableColumn<ShareDeviceUserDTO>> _buildHeadPanel() {
    return <FTableColumn<ShareDeviceUserDTO>>[
      FTableColumn<ShareDeviceUserDTO>(
          headerText: i18nBook.organization.avatar.t,
          render: (_, i) => FCenter(
              child: (_.headImageUrl.toString() == '')
                  ? FAvatar(
                      imageProvider: AssetImage("assets/images/avatar_man.png"),
                      size: 30,
                    )
                  : FAvatar(
                      imageProvider: NetworkImage(_.headImageUrl.toString()),
                      size: 30,
                      errorBuilder: (context, error, stackTrace) {
                        return FImage.asset(
                          'assets/images/avatar_man.png',
                          fit: BoxFit.cover,
                        );
                      },
                    ))

          // textAlign: TextAlign.right,
          ),
      FTableColumn<ShareDeviceUserDTO>(
        headerText: i18nBook.common.fullName.t,
        render: (rowData, index) => FCenter(
          child: FText(
            rowData.displayName.toString(),
            style: const TextStyle(
              fontSize: 16,
            ),
          ),
        ),
      ),
      FTableColumn<ShareDeviceUserDTO>(
        headerText: i18nBook.common.mobileNumber.t,
        // textFormatter: (rowData, index) => rowData.phone.toString(),
        render: (rowData, index) => FCenter(
          child: FText(
            rowData.phone.toString(),
            style: const TextStyle(
              fontSize: 16,
            ),
          ),
        ),
      ),
      FTableColumn<ShareDeviceUserDTO>(
        headerText: i18nBook.user.OrganizationLocated.t,
        render: (rowData, index) => FCenter(
          child: FText(
            rowData.rootOrganizationName.toString(),
            style: const TextStyle(
              fontSize: 16,
            ),
          ),
        ),
      ),
      FTableColumn<ShareDeviceUserDTO>(
        headerText: i18nBook.organization.workTitle.t,
        textFormatter: (rowData, index) {
          var ranks = rowData.rankNames;
          if (ranks != null && ranks.isNotEmpty) {
            return ranks.first;
          }
          return '';
        },
      ),
    ];
  }
}
