import 'package:fis_i18n/i18n.dart';
import 'package:fis_ui/base_define/page.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/architecture/router/index.dart';
import 'package:flyinsono/pages/remedical/consultation/widgets/muti_select.dart';
import 'package:flyinsono/pages/user/user_device/controller.dart';
import 'package:flyinsono/pages/user/user_device/state.dart';
import 'package:get/instance_manager.dart';
import 'package:fis_theme/theme.dart';

///查找设备页面
class FindTerminalPage extends FStatefulWidget implements FPage {
  FindTerminalPage({this.pageName = 'FindTerminalPage', Key? key})
      : super(key: key);
  @override
  final String pageName;
  @override
  FState<FindTerminalPage> createState() => _FindTerminalPageState();
}

class _FindTerminalPageState extends FState<FindTerminalPage> {
  final controller = Get.find<DeviceController>();
  final _labelStyle = TextStyle(fontSize: 16);

  @override
  void initState() {
    super.initState();
  }

  @override
  FWidget build(BuildContext context) {
    return FSimpleDialog(
      backgroundColor: Colors.white,
      title: FText(
        i18nBook.common.filter.t,
        style: TextStyle(
          color: Colors.white,
          fontSize: 18,
        ),
      ),
      okString: i18nBook.common.confirm.t,
      cancelString: i18nBook.common.cancel.t,
      children: [
        FContainer(
          alignment: Alignment.center,
          margin: EdgeInsets.symmetric(vertical: 30, horizontal: 40),
          width: 400,
          child: FColumn(
            children: [
              FObx(
                () => _buildSelectDeviceFilterRow(
                  title: i18nBook.user.deviceType.t,
                  selectSource: controller.state.deviceTypes,
                  value: controller.state.deviceType,
                  onSelectChanged: (value) {
                    controller.state.deviceType = value!;
                  },
                ),
              ),
              const FSizedBox(height: 20),
              FObx(
                () => _buildSelectDeviceFilterRow(
                  title: i18nBook.user.deviceModel.t,
                  selectSource: controller.state.deviceModels,
                  value: controller.state.deviceModel,
                  onSelectChanged: (value) {
                    controller.state.deviceModel = value;
                  },
                ),
              ),
              const FSizedBox(height: 20),
              _buildDeviceOperateInfo(),
              const FSizedBox(height: 20),
              FObx(() {
                if (controller.state.organizationLocatedList.length <= 1) {
                  return FSizedBox();
                } else {
                  return _buildMultipleSelectFilterRow(
                    title: i18nBook.user.OrganizationLocated.t,
                    selectSource: controller.state.organizationLocatedList,
                    value: controller.state.selectedOrganizationLocated,
                    onSelectChanged: (value) {},
                  );
                }
              })
            ],
          ),
        ),
      ],
      bottomButton: _buildTerminalBttomButton(),
    );
  }

  FWidget _buildSelectDeviceFilterRow({
    required String title,
    required List<FSelectModel> selectSource,
    required ValueCallback onSelectChanged,
    String? value,
  }) {
    return FRow(
      children: [
        FContainer(
          width: 80,
          child: FText.rich(
            TextSpan(
              children: [
                TextSpan(
                  text: title,
                  style: _labelStyle,
                ),
              ],
            ),
          ),
        ),
        const FSizedBox(width: 50),
        FExpanded(
          child: FSelect<FSelectModel, String>(
            source: selectSource,
            fontFamily: FTheme.ins.localeSetting.fontFamily,
            value: value,
            optionLabelExtractor: (data) => data.name,
            optionValueExtractor: (data) => data.code,
            height: 36,
            hintText: i18nBook.remedical.pleaseSelect.translate([title]),
            onSelectChanged: (value, index) {
              onSelectChanged.call(value);
            },
          ),
        )
      ],
    );
  }

  FWidget _buildMultipleSelectFilterRow({
    required String title,
    required List<FMutiSelectModel> selectSource,
    required ValueCallback onSelectChanged,
    List<String>? value,
  }) {
    return FRow(
      children: [
        FContainer(
          width: 80,
          child: FText.rich(
            TextSpan(
              children: [
                TextSpan(
                  text: title,
                  style: _labelStyle,
                ),
              ],
            ),
          ),
        ),
        const FSizedBox(width: 50),
        FExpanded(
          child: FMutiSelect<FMutiSelectModel, String>(
            source: selectSource,
            value: value,
            optionLabelExtractor: (data) => data.name,
            optionValueExtractor: (data) => data.code,
            height: 36,
            hintText: i18nBook.remedical.pleaseSelect.translate([title]),
            onSelectChanged: (value, index) {
              onSelectChanged.call(value);
            },
          ),
        )
      ],
    );
  }

  ///筛选条件
  FWidget _buildDeviceOperateInfo() {
    return FRow(
      children: [
        FContainer(
          width: 80,
          child: FText.rich(
            TextSpan(
              children: [
                TextSpan(
                  text: i18nBook.common.state.t,
                  style: _labelStyle,
                ),
              ],
            ),
          ),
        ),
        const FSizedBox(width: 50),
        FRadio(
          value: 0,
          groupValue: controller.state.deviceStatus.index,
          onChanged: (v) {
            setState(() {
              controller.state.deviceStatus = OnlineStatus.All;
            });
          },
        ),
        FGestureDetector(
          name: "OnlineStatus.All",
          businessParent: this.widget,
          onTap: () {
            setState(() {
              controller.state.deviceStatus = OnlineStatus.All;
            });
          },
          child: FMouseRegion(
            cursor: SystemMouseCursors.click,
            child: FText(i18nBook.common.all.t),
          ),
        ),
        const FSizedBox(width: 15),
        FRadio(
          value: 1,
          groupValue: controller.state.deviceStatus.index,
          onChanged: (v) {
            setState(() {
              controller.state.deviceStatus = OnlineStatus.Online;
            });
          },
        ),
        FGestureDetector(
          businessParent: this.widget,
          name: "OnlineStatus.Online",
          onTap: () {
            setState(() {
              controller.state.deviceStatus = OnlineStatus.Online;
            });
          },
          child: FMouseRegion(
            cursor: SystemMouseCursors.click,
            child: FText(i18nBook.user.online.t),
          ),
        ),
        const FSizedBox(width: 15),
        FRadio(
          value: 2,
          groupValue: controller.state.deviceStatus.index,
          onChanged: (v) {
            setState(() {
              controller.state.deviceStatus = OnlineStatus.OffLine;
            });
          },
        ),
        FGestureDetector(
          businessParent: this.widget,
          name: "OnlineStatus.OffLine",
          onTap: () {
            setState(() {
              controller.state.deviceStatus = OnlineStatus.OffLine;
            });
          },
          child: FMouseRegion(
            cursor: SystemMouseCursors.click,
            child: FText(i18nBook.user.offline.t),
          ),
        ),
      ],
    );
  }

  ///底部操作按钮
  FWidget _buildTerminalBttomButton() {
    return FContainer(
      alignment: Alignment.center,
      padding: EdgeInsets.symmetric(vertical: 15),
      child: FFlex(
        direction: Axis.horizontal,
        // mainAxisAlignment: MainAxisAlignment.center,
        mainAxisSize: MainAxisSize.min,
        children: [
          FElevatedButton(
            businessParent: this.widget,
            name: "findTerminalByParameters",
            onPressed: () {
              controller.findTerminalByParameters();
            },
            style: ButtonStyle(
              padding: MaterialStateProperty.all(
                EdgeInsets.symmetric(
                  vertical: 15,
                  horizontal: 25,
                ),
              ),
            ),
            child: FText(i18nBook.common.confirm.t),
          ),
          FSizedBox(
            width: 20,
          ),
          FOutlinedButton(
            businessParent: this.widget,
            name: "back",
            onPressed: () => router.back(),
            style: ButtonStyle(
              backgroundColor: MaterialStateProperty.all(
                Colors.white,
              ),
              padding: MaterialStateProperty.all(
                EdgeInsets.symmetric(vertical: 15, horizontal: 25),
              ),
            ),
            child: FText(
              i18nBook.common.cancel.t,
              style: TextStyle(
                color: Color.fromRGBO(61, 146, 252, 1),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
