import 'package:fis_i18n/i18n.dart';
import 'package:fis_jsonrpc/rpc.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/pages/organization/org_personnel/controller.dart';
import 'package:get/get.dart';

///组织成员信息页面
class OrgMemberInfo extends FStatefulWidget {
  OrgMemberInfo({
    Key? key,
    this.orgMemberInfoImage,
    this.onClick,
    this.userItem,
  }) : super(key: key);

  /// 组织信息的图片组件
  final FWidget? orgMemberInfoImage;

  /// 点击事件
  final Function? onClick;

  /// 用户信息
  final UserDTO? userItem;

  @override
  FState<OrgMemberInfo> createState() => _OrgMemberInfoState();
}

class _OrgMemberInfoState extends FState<OrgMemberInfo> {
  final orgPesonnelController = Get.find<OrgPesonnelController>();

  @override
  FWidget build(BuildContext context) {
    /// 匹配手机端部门、岗位身份、职称 数据初始化
    orgPesonnelController.state.companyCode = widget.userItem!.organizationCode;
    orgPesonnelController.state.positionListCode =
        widget.userItem?.positionCodes?.length == 0
            ? ''
            : widget.userItem!.positionCodes?.toList()[0];
    orgPesonnelController.state.professionalListCode =
        widget.userItem!.rankCodes?.length == 0
            ? ''
            : widget.userItem!.rankCodes?.toList()[0];
    return FMaterial(
      color: Colors.white,
      child: FColumn(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          /// 基本信息
          _buildTitle(
            i18nBook.organization.basicInfo.t,
          ),
          _buildBasicInfoBody(),
          const FSizedBox(
            height: 5,
          ),
          // 组织架构信息
          _buildTitle(
            i18nBook.organization.orgStructureInfo.t,
          ),
          _buildOrgStructureInfoBody(),
        ],
      ),
    );
  }

  /// 标签
  ///
  /// [title] 基本信息或机构信息
  FWidget _buildTitle(String title) {
    return FContainer(
      margin: const EdgeInsets.only(
        top: 10,
        bottom: 15,
        left: 20,
        right: 20,
      ),
      child: FText(
        title,
        style: TextStyle(
          fontWeight: FontWeight.bold,
        ),
      ),
    );
  }

  /// 基本信息主体部分
  FWidget _buildBasicInfoBody() {
    return FContainer(
      height: 150,
      margin: EdgeInsets.symmetric(
        horizontal: 20,
      ),
      padding: EdgeInsets.symmetric(
        vertical: 20,
      ),
      decoration: BoxDecoration(
        border: Border.all(
          color: Color.fromRGBO(228, 228, 228, 1),
        ),
        borderRadius: BorderRadius.all(
          Radius.circular(
            4,
          ),
        ),
      ),
      child: FContainer(
        alignment: Alignment.topLeft,
        child: FColumn(
          children: [
            _buildFullNameItem(),
            const FSizedBox(
              height: 20,
            ),
            _buildPhoneView(),
          ],
        ),
      ),
    );
  }

  /// 组织架构信息主体部分
  FWidget _buildOrgStructureInfoBody() {
    return FContainer(
      height: 140,
      margin: EdgeInsets.symmetric(
        horizontal: 20,
      ),
      padding: EdgeInsets.symmetric(
        vertical: 20,
      ),
      decoration: BoxDecoration(
        border: Border.all(
          color: Color.fromRGBO(
            228,
            228,
            228,
            1,
          ),
        ),
        borderRadius: BorderRadius.all(
          Radius.circular(
            4,
          ),
        ),
      ),
      child: FContainer(
        alignment: Alignment.topLeft,
        child: FRow(
          children: [
            FExpanded(
              flex: 2,
              child: FColumn(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  FExpanded(
                    child: FContainer(
                      child: FAlign(
                        alignment: Alignment.centerRight,
                        child: FText(
                          i18nBook.organization.department.t,
                          style: TextStyle(
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                      ),
                    ),
                  ),
                  FExpanded(
                    child: FContainer(
                      child: FAlign(
                        alignment: Alignment.centerRight,
                        child: FText(
                          i18nBook.organization.positionIdentity.t,
                          style: TextStyle(
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                      ),
                    ),
                  ),
                  FExpanded(
                    child: FContainer(
                      child: FAlign(
                        alignment: Alignment.centerRight,
                        child: FText(
                          i18nBook.organization.workTitle.t,
                          style: TextStyle(
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
            const FSizedBox(
              width: 15,
            ),
            FExpanded(
              flex: 7,
              child: FColumn(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  FExpanded(
                    child: FMaterial(
                      color: Colors.white,
                      child: FInkWell(
                        onTap: () {
                          orgPesonnelController.getCompanyPersonnelList();
                        },
                        child: FContainer(
                          margin: EdgeInsets.symmetric(horizontal: 15),
                          child: FAlign(
                            alignment: Alignment.centerLeft,
                            child: FObx(
                              () => _buildIconRow(
                                orgPesonnelController.state.companyName,
                                Icons.chevron_right,
                              ),
                            ),
                          ),
                        ),
                      ),
                    ),
                  ),
                  FExpanded(
                    child: FMaterial(
                      color: Colors.white,
                      child: FInkWell(
                        onTap: () {
                          orgPesonnelController.getPositionList();
                        },
                        child: FContainer(
                          margin: EdgeInsets.symmetric(horizontal: 15),
                          child: FAlign(
                            alignment: Alignment.centerLeft,
                            child: FObx(
                              () => _buildIconRow(
                                orgPesonnelController.state.positionListName,
                                Icons.chevron_right,
                              ),
                            ),
                          ),
                        ),
                      ),
                    ),
                  ),
                  FExpanded(
                    child: FMaterial(
                      color: Colors.white,
                      child: FInkWell(
                        onTap: () {
                          orgPesonnelController.getProfessional();
                        },
                        child: FContainer(
                          margin: EdgeInsets.symmetric(
                            horizontal: 15,
                          ),
                          child: FAlign(
                            alignment: Alignment.centerLeft,
                            child: FObx(
                              () => _buildIconRow(
                                orgPesonnelController
                                    .state.professionalListName,
                                Icons.chevron_right,
                              ),
                            ),
                          ),
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }

  /// 构建icon
  FWidget _buildIconRow(String name, IconData iconData) {
    return FRow(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        FText(
          name,
        ),
        FIcon(
          iconData,
          color: Color.fromRGBO(
            137,
            137,
            137,
            1,
          ),
        ),
      ],
    );
  }

  ///名称信息视图
  FWidget _buildFullNameItem() {
    return FRow(
      children: [
        FRow(
          children: [
            FSizedBox(
              width: 30,
            ),
            widget.orgMemberInfoImage!,
            FSizedBox(
              width: 12,
            ),
            FText(
              widget.userItem?.displayName ?? '',
              style: TextStyle(
                fontWeight: FontWeight.bold,
                fontSize: 15,
              ),
            ),
          ],
        ),
      ],
    );
  }

  ///手机号视图
  FWidget _buildPhoneView() {
    return FContainer(
      alignment: Alignment.topLeft,
      height: 20,
      child: FRow(
        children: [
          FExpanded(
            flex: 2,
            child: FColumn(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                FExpanded(
                  child: FContainer(
                    child: FAlign(
                      alignment: Alignment.centerRight,
                      child: FText(
                        i18nBook.common.mobileNumber.t,
                        style: TextStyle(
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
          const FSizedBox(
            width: 30,
          ),
          FExpanded(
            flex: 7,
            child: FColumn(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                FExpanded(
                  child: FContainer(
                    child: FAlign(
                      alignment: Alignment.centerLeft,
                      child: FText(
                        widget.userItem?.phone ?? '',
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
