import 'package:fis_i18n/i18n.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/pages/components/area_select_tree.dart';
import 'package:flyinsono/pages/organization/org_info/state.dart';
import 'package:flyinsono/pages/organization/org_info/widget/higher_org.dart';
import 'package:fis_ui/base_define/page.dart';
import 'package:fis_theme/theme.dart';
import 'package:get/get.dart';

import '../controller.dart';

class BaseOrgInfos extends FisView<OrgInfoController> implements FPage {
  @override
  FWidget build(BuildContext context) {
    return FColumn(
      children: _buildBaseInfos(),
    );
  }

  @override
  String get pageName => 'BaseOrgInfos';

  /// 合成组件
  FWidget _buildFormItem({required FWidget title, required FWidget widge}) {
    return FFlex(
      direction: Axis.horizontal,
      children: [
        title,
        widge,
      ],
    );
  }

  /// 构建输入名称
  ///
  /// [title] 输入名称
  ///
  /// [bottom] 底部距离
  FWidget _buildTitle({required String title, double? bottom = 6}) {
    return FContainer(
      /// TODO:[Gavin] 多语言-UI-整合
      width: i18nBook.isCurrentChinese ? 100 : 200,
      margin: EdgeInsets.only(
        bottom: bottom!,
      ),
      child: FText(
        title,
        style: TextStyle(
          fontSize: 16,
        ),
      ),
    );
  }

  /// 构建输入主体
  ///
  /// [widget] 主体组件
  ///
  /// [height] 具体多高
  FWidget _buildContext({required FWidget widget, required double height}) {
    return FContainer(
      height: height,

      /// TODO:[Gavin] 多语言-UI-整合
      width: i18nBook.isCurrentChinese ? 600 : 700,
      child: FCenter(
        child: widget,
      ),
    );
  }

  /// 机构Logo
  FWidget _buildLogo() {
    return FStack(
      children: [
        FMaterial(
          color: Colors.white,
          child: FInk(
            child: FInkWell(
              onTap: () => controller.uploadImage(),
              child: FContainer(
                decoration: BoxDecoration(
                  border: Border.all(
                    color: Color.fromRGBO(141, 141, 141, 1),
                    width: 0.5,
                  ),
                  borderRadius: BorderRadius.circular(8),
                ),
                child: FCenter(
                  child: FIcon(
                    Icons.add,
                    color: Color.fromRGBO(
                      151,
                      151,
                      151,
                      1,
                    ),
                  ),
                ),
              ),
            ),
          ),
        ),
        FObx(
          () {
            if (controller.state.loadding!) {
              return _buildLoaddingImage();
            } else {
              if (controller.state.logoUrl.isNotEmpty) {
                return _buildOrgLogo();
              } else {
                return FContainer();
              }
            }
          },
        ),
      ],
    );
  }

  /// 加载图片
  FWidget _buildLoaddingImage() {
    return FCenter(
      child: FContainer(
        padding: EdgeInsets.all(15),
        width: 55,
        height: 55,
        child: FCircularProgressIndicator(
          valueColor: AlwaysStoppedAnimation(
            Colors.blueAccent,
          ),
        ),
      ),
    );
  }

  /// 机构Logo图片
  FWidget _buildOrgLogo() {
    return FMaterial(
      child: FInkWell(
        onTap: () => controller.uploadImage(),
        child: FContainer(
          padding: EdgeInsets.all(5),
          decoration: BoxDecoration(
            border: Border.all(
              color: Color.fromRGBO(
                141,
                141,
                141,
                1,
              ),
              width: 0.5,
            ),
            borderRadius: BorderRadius.circular(
              8,
            ),
          ),
          child: FImage(
            height: 60,
            width: 60,
            repeat: ImageRepeat.repeat,
            fit: BoxFit.fill,
            image: NetworkImage(
              controller.state.logoUrl,
            ),
          ),
        ),
      ),
    );
  }

  /// 机构名称
  FWidget _buildOrgName() {
    return FRow(
      children: [
        FBorderInput(
          hintSize: 16,
          contentSize: 16,
          maxLength: 20,
          borderColor: Color.fromRGBO(234, 234, 234, 1),
          height: 38,
          hintText: i18nBook.organization.orgNameInputTip.t,
          onChanged: (value) => controller.state.organizationName = value,
          controller: controller.orgName,
        ),
      ],
    );
  }

  /// 上级机构
  FWidget _buildHigherOrg() {
    return FRow(
      children: [
        FObx(
          () {
            return FBorderInput(
              hintSize: 16,
              contentSize: 16,
              maxLength: 20,
              borderColor: Color.fromRGBO(234, 234, 234, 1),
              height: 38,
              hintText: i18nBook.organization.orgNameInputTip.t,
              readOnly: true,
              mouseCursor: SystemMouseCursors.click,
              // onChanged: (value) => controller.state.organizationName = value,
              controller: TextEditingController(
                text: controller.state.parentOrganizationName,
              ),
              suffixIcon:
                  controller.state.parentOrganizationName?.isEmpty ?? true
                      ? null
                      : FIconButton(
                          icon: FIcon(Icons.close),
                          onPressed: () {
                            controller.state.parentCode = '';
                            controller.state.parentOrganizationName = '';
                          },
                          businessParent: this,
                          name: 'Clear orgInfos',
                        ),
              onTap: () {
                Get.dialog(
                  HigherOrgDialog(
                    higherOrgCode: controller.state.parentCode,
                  ),
                );
              },
            );
          },
        )
      ],
    );
  }

  /// 服务对象
  FWidget _buildType() {
    final select = FObx(() {
      return FSelect<OrgModel, String>(
        source: controller.state.orgType,
        hintText: i18nBook.organization.serveObj.t,
        fontFamily: FTheme.ins.localeSetting.fontFamily,
        value: controller.state.patientType,
        height: 36,
        width: 188,
        optionLabelExtractor: (data) => data.name,
        optionValueExtractor: (data) => data.code,
        onSelectChanged: (value, index) => controller.state.patientType = value,
      );
    });
    return select;
  }

  /// 所在地区
  FWidget _buildArea() {
    return FRow(
      children: [
        FObx(
          () {
            return AreaSelectTree(
              fontSize: 16,
              left: 10.0,
              right: 10.0,
              bottom: 5.0,
              top: 2,
              boxDecoration: BoxDecoration(
                border: Border.all(
                  color: Color.fromRGBO(
                    235,
                    235,
                    235,
                    1,
                  ),
                  width: 1,
                ), //边框
                borderRadius: BorderRadius.all(
                  Radius.circular(
                    4.0,
                  ),
                ),
              ),
              onIsChinaAreaChanged: (val) => controller.state.isChinaArea = val,
              onCityChanged: (val) => controller.state.cityCode = val,
              onProvinceChanged: (val) => controller.state.provinceCode = val,
              onCountryChanged: (val) => controller.state.countryCode = val,
              initCountry: controller.state.countryCode,
              initProvince: controller.state.provinceCode,
              initcity: controller.state.cityCode,
            );
          },
        ),
      ],
    );
  }

  ///机构基本信息
  List<FWidget> _buildBaseInfos() {
    final infos = [
      FContainer(
        /// TODO:[Gavin] 多语言-UI-整合
        width: i18nBook.isCurrentChinese ? 700 : 900,
        alignment: Alignment.centerLeft,
        child: FText(
          i18nBook.homePage.organizationInfo.t,
          style: TextStyle(
            fontSize: 20,
            fontWeight: FontWeight.bold,
          ),
        ),
      ),
      const FSizedBox(height: 20),
      _buildFormItem(
        title: _buildTitle(title: i18nBook.organization.orgLogo.t),
        widge: _buildContext(
          widget: FRow(
            children: [
              FContainer(
                width: 60,
                height: 60,
                child: _buildLogo(),
              ),
            ],
          ),
          height: 80.0,
        ),
      ),
      _buildFormItem(
        title: _buildTitle(
          title: i18nBook.organization.orgName.t,
        ),
        widge: _buildContext(
          widget: FRow(
            children: [
              FContainer(
                width: 400,
                child: _buildOrgName(),
              ),
            ],
          ),
          height: 60.0,
        ),
      ),
      _buildFormItem(
        title: _buildTitle(
          title: i18nBook.auth.hospitalCode.t,
        ),
        widge: _buildContext(
          height: 60.0,
          widget: FObx(
            () => FRow(
              children: [
                FText(controller.state.orgShortCode.isEmpty
                    ? '  ${i18nBook.realTimeConsultation.noAssistant.t}'
                    : "  ${controller.state.orgShortCode}"),
                FExpanded(
                  child: FSizedBox(),
                ),
              ],
            ),
          ),
        ),
      ),
      _buildFormItem(
        title: _buildTitle(
          title: i18nBook.organization.upperOrg.t,
        ),
        widge: _buildContext(
          widget: FRow(
            children: [
              FContainer(
                width: 400,
                child: _buildHigherOrg(),
              ),
            ],
          ),
          height: 60.0,
        ),
      ),
      _buildFormItem(
        title: _buildTitle(
          title: i18nBook.organization.locatedArea.t,
          bottom: 0.0,
        ),
        widge: _buildContext(
          widget: _buildArea(),
          height: 60.0,
        ),
      ),
      _buildFormItem(
        title: _buildTitle(
          title: i18nBook.organization.serveObj.t,
          bottom: 0.0,
        ),
        widge: _buildContext(
          widget: FRow(
            children: [
              FContainer(
                width: 400,
                child: FRow(
                  children: [
                    _buildType(),
                  ],
                ),
              ),
            ],
          ),
          height: 60.0,
        ),
      ),
    ];
    return infos;
  }
}
