/*
 * @Author       : WuWei
 * @LastEditors  : WuWei
 * @Date         : 2022-03-03 10:21:36
 * @LastEditTime : 2023-05-25 15:50:56
 * @Description  : Do not edit
 */
import 'package:flutter/material.dart';
import 'package:wisdom_cli/wisdom_cli.dart';

class WFCascadePicker extends StatelessWidget {
  const WFCascadePicker({
    Key? key,
    this.isRequired,
    this.hintText,
    this.title,
    required this.onChanged,
    this.initialValue,
    this.enableValue,
    this.label,
    this.value,
    this.axis,
    this.isSearch = false,
    this.titleWidth,
    this.isMulti,
    this.dataList,
    this.maxLength,
  }) : super(key: key);

  /// 是否可搜索
  final bool? isSearch;

  ///布局
  final Axis? axis;
  final double? titleWidth;

  ///是否必须
  final bool? isRequired;
  final String? hintText;
  final Widget? title;
  final List? initialValue;
  final List? enableValue;
  final void Function(List values) onChanged;

  /// 多选项对应的名称 { 'name': '标题名称', id: '999999'} 这里的[name]就是对应[label] 默认是'label'
  final String? label;

  /// 多选项对应的值 { 'name': '标题名称', id: '999999'} 这里的[id]就是对应[value] 默认是'value'
  final String? value;

  final bool? isMulti;

  /// 多少层级
  final int? maxLength;

  /// 选择数据列表
  final List? dataList;
  @override
  Widget build(BuildContext context) {
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;
    var _label = label ?? 'label';
    var _value = value ?? 'value';
    List<SinglePickerEntity> _initialValue =
        List<SinglePickerEntity>.from((initialValue ?? []).map(
      (item) => SinglePickerEntity(
        name: item.name,
        unique: item.unique,
        child: item.child ?? null,
      ),
    ));

    List<SinglePickerEntity> _list =
        List<SinglePickerEntity>.from((dataList ?? []).map(
      (item) => SinglePickerEntity(
        name: item[_label],
        unique: item[_value],
        child: item['child'] ?? null,
      ),
    ));

    _findValue() {
      return _initialValue.length > 0 ? _initialValue.last.name : null;
    }

    return WisFormLayoutComponent(
      title: title,
      titleWidth: titleWidth,
      isRequired: isRequired,
      axis: axis ?? Axis.horizontal,
      child: StatefulBuilder(
        builder: (BuildContext context, setState) {
          return Wisdom.row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            padding: EdgeInsets.only(left: 10.pt),
            children: [
              _initialValue.length > 0
                  ? TextField(
                      controller: TextEditingController(
                        text: (_initialValue.length > 0) ? _findValue() : ' ',
                      ),
                      readOnly: true,
                      onTap: () async {
                        final res =
                            await WPickerUtil.cascade<SinglePickerEntity>(
                          context,
                          title: title,
                          initialValue: _initialValue,
                          isMulti: isMulti!,
                          isSearch: isSearch,
                          data: _list,
                          maxLength: maxLength!,
                          label: 'name',
                          value: 'id',
                        );
                        if (res != null) {
                          _initialValue = res;
                          onChanged(res);
                        }
                      },
                      style: TextStyle(
                        fontSize: 14.pt,
                        color: colorScheme.minor,
                      ),
                      minLines: 1,
                      maxLines: 999,
                      decoration: InputDecoration(
                        fillColor: Colors.transparent,
                        filled: true,
                        hintText: hintText,
                        hintStyle: TextStyle(
                          fontSize: 14.pt,
                          color: colorScheme.placeholder,
                          fontWeight: FontWeight.w300,
                        ),
                      ),
                    ).asExpanded()
                  : Wisdom(
                      onTap: () async {
                        final res =
                            await WPickerUtil.cascade<SinglePickerEntity>(
                          context,
                          title: title,
                          initialValue: _initialValue,
                          isMulti: isMulti ?? false,
                          data: _list,
                          maxLength: maxLength ?? 256,
                          isSearch: isSearch,
                          label: 'name',
                          value: 'id',
                        );
                        if (res != null) {
                          _initialValue = res;
                          onChanged(res);
                        }
                      },
                      child: WisText(
                        hintText ?? '请选择',
                        color: colorScheme.placeholder,
                        weight: FontWeight.w300,
                        size: 14.pt,
                      ),
                    ).asExpanded(),
              _initialValue.length != 0
                  ? WFClose(
                      onTap: () {
                        _initialValue = [];
                        setState(() {});
                        onChanged([]);
                      },
                    )
                  : Icon(
                      FaIcons.angle_right,
                      size: 18.pt,
                      color: colorScheme.placeholder,
                    )
            ],
          );
        },
      ),
    );
  }
}
