/*
 * @Author: XianKaiQun
 * @LastEditors: XianKaiQun
 * @Date: 2020-10-26 11:07:19
 * @LastEditTime: 2020-10-26 11:11:43
 */
import 'package:flutter/material.dart';
import 'package:wisdom_cli/wisdom_cli.dart';

class WFSeasonPicker extends StatelessWidget {
  const WFSeasonPicker({
    Key? key,
    required this.title,
    this.isRequired = false,
    this.hintText,
    required this.onChanged,
    this.initialSeason,
    this.type,
    this.minuteInterval,
  }) : super(key: key);
  final Widget title;

  ///是否必须
  final bool? isRequired;
  final String? hintText;
  final dynamic initialSeason;
  final void Function(dynamic datetime, String? seasonStr) onChanged;
  final String? type;
  final int? minuteInterval;

  String getQuater(date) {
    List arr = date.split('-');
    String quater = '';
    switch (int.parse(arr[1])) {
      case 1:
        quater = "${arr[0]}年第1季度";
        break;
      case 4:
        quater = "${arr[0]}年第2季度";
        break;
      case 7:
        quater = "${arr[0]}年第3季度";
        break;
      case 10:
        quater = "${arr[0]}年第4季度";
        break;
      default:
    }
    return quater;
  }

  @override
  Widget build(BuildContext context) {
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;
    String? _initialDate = initialSeason;
    return WisFormLayoutComponent(
      title: title,
      isRequired: isRequired,
      axis: Axis.horizontal,
      child: StatefulBuilder(
        builder: (BuildContext context, setState) {
          return Row(
            children: [
              TextField(
                controller: TextEditingController(
                  text:
                      _initialDate != null ? '${getQuater(_initialDate)}' : '',
                ),
                readOnly: true,
                onTap: () async {
                  final res = await WPickerUtil.season(
                    context,
                    title: title,
                    initialDate: _initialDate != null
                        ? DateTime.parse('$_initialDate')
                        : null,
                  );
                  if (res != null) {
                    _initialDate = res.toFormat('{YYYY}-{MM}-{DD}');
                    setState(() {});
                    onChanged(_initialDate, getQuater(_initialDate));
                  }
                },
                style: TextStyle(
                  fontSize: 14.pt,
                  color: colorScheme.minor,
                ),
                decoration: InputDecoration(
                  fillColor: Colors.transparent,
                  filled: true,
                  hintText: hintText,
                  hintStyle: TextStyle(
                    fontSize: 14.pt,
                    color: colorScheme.placeholder,
                    fontWeight: FontWeight.w300,
                  ),
                ),
              ).asExpanded(),
              _initialDate != null
                  ? WFClose(
                      onTap: () {
                        _initialDate = null;
                        setState(() {});
                        onChanged(null, null);
                      },
                    )
                  : Icon(
                      FaIcons.angle_right,
                      size: 18.pt,
                      color: colorScheme.placeholder,
                    )
            ],
          );
        },
      ),
    );
  }
}
