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/common/page_helper.dart';
import 'package:flyinsono/pages/organization/org_info/state.dart';
import 'package:flyinsono/pages/remedical/vocabulary_entry/controller.dart';
import 'package:fis_theme/theme.dart';

///词条分类词典对话框
class EntryClassifcationDialog extends FStatefulWidget {
  EntryClassifcationDialog({
    Key? key,
    this.onOkTap,
    this.thesaurusInfoKey,
    this.onCancelTap,
  }) : super(key: key);
  ValueCallback<bool>? onOkTap;
  VoidCallback? onCancelTap;
  String? thesaurusInfoKey;

  @override
  FState<EntryClassifcationDialog> createState() =>
      _EntryClassifcationDialogState();
}

///词条分类词典对话框状态组件
class _EntryClassifcationDialogState extends FState<EntryClassifcationDialog> {
  @override
  FWidget build(BuildContext context) {
    return FSimpleDialog(
      title: FText(
        widget.thesaurusInfoKey != null
            ? i18nBook.remedical.updateVocCat.t
            : i18nBook.remedical.createVocCat.t,
        style: TextStyle(
          color: Colors.white,
          fontSize: 18,
        ),
      ),
      isDefault: true,
      okString: i18nBook.common.save4Btn.t,
      cancelString: i18nBook.common.cancel.t,
      onOk: () => widget.onOkTap?.call(widget.thesaurusInfoKey != null),
      onCancel: () => widget.onCancelTap?.call(),
      insetPadding: EdgeInsets.symmetric(vertical: 100),
      children: [
        FContainer(
          width: 580,
          height: 250,
          padding: EdgeInsets.only(
            left: 25,
            right: 25,
          ),
          child: _EntryClassifcation(
            thesaurusInfoKey: widget.thesaurusInfoKey,
          ),
        ),
      ],
    );
  }
}

///词条分类词典对话框框内容页
class _EntryClassifcation extends FisView<VocabularyEntryController> {
  _EntryClassifcation({this.thesaurusInfoKey});
  String? thesaurusInfoKey;

  @override
  FWidget build(BuildContext context) {
    return FColumn(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        _EntryClassifcationItem(
          isRequire: true,
          inputWidget: FRow(
            children: [
              FExpanded(
                child: FContainer(
                  height: 45,
                  // color: Colors.red,
                  child: FRow(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      FSizedBox(
                        width: 25,
                      ),
                      FBorderInput(
                        hintSize: 16,
                        contentSize: 16,
                        maxLength: PageHelper.maxInputLength_general(),
                        borderColor: Color(0xffdcdfe6),
                        height: 40,
                        hintText: i18nBook.remedical.inputCatName.t,
                        controller: TextEditingController(
                          text: controller
                              .state.createClassification['thesaurusItemName'],
                        ),
                        onChanged: (value) {
                          controller.state
                                  .createClassification['thesaurusItemName'] =
                              value.toString().trim();
                        },
                      ),
                      FSizedBox(
                        width: 10,
                      ),
                    ],
                  ),
                ),
              ),
            ],
          ),
          title: i18nBook.remedical.categoryName.t,
        ),
        FSizedBox(
          height: 20,
        ),
        _EntryClassifcationItem(
          inputWidget: FRow(
            children: [
              FExpanded(
                child: FContainer(
                  height: 45,
                  // color: Colors.red,
                  child: FRow(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      FSizedBox(
                        width: 25,
                      ),
                      FExpanded(
                        child: FSelect<OrgModel, String>(
                          fontFamily: FTheme.ins.localeSetting.fontFamily,
                          source: thesaurusInfoKey != null
                              ? controller.state.editClassification
                              : controller.state.classification,
                          hintText: i18nBook.remedical.selectUpperCat.t,
                          value: controller
                                  .state.createClassification['parentItemCode']
                                  .toString()
                                  .isEmpty
                              ? null
                              : controller
                                  .state.createClassification['parentItemCode'],

                          clearable: true,
                          // isExpanded: true,
                          height: 36,
                          optionLabelExtractor: (data) => data.name,
                          optionValueExtractor: (data) => data.code,
                          onSelectChanged: (value, index) {
                            controller.state
                                .createClassification['parentItemCode'] = value;
                          },
                        ),
                      ),
                      FSizedBox(
                        width: 10,
                      ),
                    ],
                  ),
                ),
              ),
            ],
          ),
          title: i18nBook.remedical.parentClass.t,
        )
      ],
    );
  }
}

///词条分类词典对话框框内容页条目
class _EntryClassifcationItem extends FisView<VocabularyEntryController> {
  _EntryClassifcationItem({
    required this.title,
    required this.inputWidget,
    this.isRequire = false,
  });
  String title;
  FWidget inputWidget;
  bool? isRequire;
  final TextStyle labelStyle = TextStyle(fontSize: 16);
  @override
  FWidget build(BuildContext context) {
    return FRow(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        FContainer(
          margin: EdgeInsets.symmetric(
            horizontal: 15,
          ),
          width: 80,
          child: FText.rich(
            TextSpan(
              children: [
                TextSpan(
                  text: title,
                  style: labelStyle,
                ),
                isRequire!
                    ? TextSpan(
                        text: ' *',
                        style: labelStyle.copyWith(color: Colors.red),
                      )
                    : TextSpan(),
              ],
            ),
          ),
        ),
        FExpanded(
          child: FContainer(
            margin: EdgeInsets.symmetric(
              horizontal: 15,
            ),
            child: inputWidget,
          ),
        )
      ],
    );
  }
}
