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/architecture/router/router.dart';
import 'package:flyinsono/pages/organization/org_personnel/controller.dart';
import 'package:flyinsono/pages/organization/org_personnel/state.dart';
import 'package:fis_theme/theme.dart';

class UserEditDialogPage extends FStatefulWidget {
  UserEditDialogPage({
    Key? key,
    this.rowData,
    this.onOkTap,
  }) : super(key: key);

  /// 用户编辑数据
  final UserDTO? rowData;

  /// 确定回调
  final ValueCallback<String>? onOkTap;

  @override
  FState<UserEditDialogPage> createState() => _UserEditDialogState();
}

class _UserEditDialogState extends FState<UserEditDialogPage> {
  @override
  FWidget build(BuildContext context) {
    return FSimpleDialog(
      backgroundColor: Colors.white,
      title: FText(
        i18nBook.common.edit.t,
        style: TextStyle(
          color: Colors.white,
          fontSize: 18,
        ),
      ),
      children: [
        FContainer(
          alignment: Alignment.center,
          margin: EdgeInsets.symmetric(vertical: 10, horizontal: 10),
          width: 600,
          child: FColumn(
            children: [
              FContainer(
                padding: EdgeInsets.symmetric(
                  horizontal: 40,
                ),
                child: _BuildUserEditDialog(
                  rowData: widget.rowData,
                ),
              ),
            ],
          ),
        ),
      ],
      okString: i18nBook.common.save4Btn.t,
      cancelString: i18nBook.common.cancel.t,
      isDefault: true,
      onOk: () {
        widget.onOkTap?.call(
          widget.rowData!.userCode!,
        );
      },
      onCancel: () => router.back(),
    );
  }
}

class _BuildUserEditDialog extends FisView<OrgPesonnelController> {
  _BuildUserEditDialog({
    Key? key,
    this.rowData,
  }) : super(key: key);

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

  @override
  FWidget build(BuildContext context) {
    /// 获取用户详情
    controller.getEditUserInfo(rowData);
    return FContainer(
      child: _buildUserEditBody(),
    );
  }

  ///编辑用户信息页面
  FWidget _buildUserEditBody() {
    return FColumn(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        FRow(
          children: [
            FText(
              i18nBook.organization.basicInfo.t,
              style: TextStyle(
                fontWeight: FontWeight.w600,
                fontSize: 17,
              ),
            ),
          ],
        ),
        const FSizedBox(
          height: 25,
        ),
        _buildUserEditBasicInformation(),
        const FSizedBox(
          height: 30,
        ),
        const FDivider(
          height: 1.0,
          indent: 1.0,
          color: Color.fromRGBO(241, 241, 241, 1),
        ),
        const FSizedBox(
          height: 30,
        ),
        _buildUserEditOperationLine(),
        const FSizedBox(
          height: 15,
        ),
      ],
    );
  }

  ///编辑用户基础信息
  FWidget _buildUserEditBasicInformation() {
    return FRow(
      children: [
        FSizedBox(
          width: 20,
        ),
        FCircleAvatar(
          radius: 40,
          child: (rowData!.headImageUrl.toString() == '')
              ? FAvatar(
                  imageProvider: AssetImage("assets/images/avatar_man.png"),
                  size: 80,
                )
              : FAvatar(
                  imageProvider: NetworkImage(
                    rowData!.headImageUrl.toString(),
                  ),
                  size: 80,
                  errorBuilder: (context, error, stackTrace) {
                    return FImage.asset(
                      'assets/images/avatar_man.png',
                      fit: BoxFit.cover,
                    );
                  },
                ),
        ),
        FSizedBox(
          width: 15,
        ),
        FColumn(
          children: [
            FContainer(
              width: 250,
              child: FFlex(
                direction: Axis.horizontal,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  FContainer(
                    width: 80,
                    child: FText(
                      i18nBook.common.fullName.t,
                      style: TextStyle(
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ),
                  FContainer(
                    child: FText(rowData!.fullName ?? ''),
                  )
                ],
              ),
            ),
            FSizedBox(
              height: 10,
            ),
            FContainer(
              width: 250,
              child: FFlex(
                direction: Axis.horizontal,
                children: [
                  FContainer(
                    width: 80,
                    child: FText(
                      i18nBook.common.mobileNumber.t,
                      style: TextStyle(
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ),
                  FContainer(
                    child: FText(rowData!.phone ?? ''),
                  ),
                ],
              ),
            ),
          ],
        ),
      ],
    );
  }

  ///用户编辑
  FWidget _buildUserEditOperationRow(
    String title,
    FWidget userEditOperation,
  ) {
    return FContainer(
      width: 500,
      child: FFlex(
        direction: Axis.horizontal,
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          FContainer(
            child: FRow(
              children: [
                FText(
                  title,
                  style: TextStyle(
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ],
            ),
          ),
          FContainer(
            width: 400,
            child: userEditOperation,
          ),
        ],
      ),
    );
  }

  ///用户编辑操作水平视图
  FWidget _buildUserEditOperationLine() {
    return FCenter(
      child: FColumn(
        children: [
          _buildUserEditOperationRow(
            i18nBook.organization.department.t,
            FSelect<OrgNameModel, String>(
              fontFamily: FTheme.ins.localeSetting.fontFamily,
              source: controller.allOrgList,
              hintText: i18nBook.organization.selectDepartmentTip.t,
              value: controller.userEditInfo?.organizationCode,
              height: 36,
              optionLabelExtractor: (data) => data.name,
              optionValueExtractor: (data) => data.code,
              onSelectChanged: (value, index) {
                controller.userEditInfo?.organizationCode = value;
              },
            ),
          ),
          const FSizedBox(
            height: 10,
          ),
          _buildUserEditOperationRow(
            i18nBook.organization.positionIdentity.t,
            FSelect<OrgNameModel, String>(
              fontFamily: FTheme.ins.localeSetting.fontFamily,
              source: controller.allProfessionalList,
              hintText: i18nBook.organization.selectWorkPostIdentify.t,
              value: controller.userEditInfo?.positionCodes,
              height: 36,
              optionLabelExtractor: (data) => data.name,
              optionValueExtractor: (data) => data.code,
              onSelectChanged: (value, index) {
                controller.userEditInfo?.positionCodes = value;
              },
            ),
          ),
          const FSizedBox(
            height: 10,
          ),
          _buildUserEditOperationRow(
            i18nBook.organization.workTitle.t,
            FSelect<OrgNameModel, String>(
              source: controller.allRanks,
              fontFamily: FTheme.ins.localeSetting.fontFamily,
              hintText: i18nBook.organization.workTitleSelectionTip.t,
              value: controller.userEditInfo?.rankCodes,
              height: 36,
              optionLabelExtractor: (data) => data.name,
              optionValueExtractor: (data) => data.code,
              onSelectChanged: (value, index) =>
                  controller.userEditInfo?.rankCodes = value,
            ),
          ),
        ],
      ),
    );
  }
}
