/*
 * @Author       : WuWei
 * @LastEditors  : WuWei
 * @Date         : 2021-04-08 17:23:50
 * @LastEditTime : 2023-05-16 10:13:13
 * @FilePath     : /wisdom-huiguanjia-app/wisdom_cli/lib/src/form_widget/components/input_money.dart
 * @Description  : Do not edit
 */

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:wisdom_cli/wisdom_cli.dart';

class WFInputMoney extends StatelessWidget {
  const WFInputMoney({
    Key? key,
    this.isRequired = false,
    this.title,
    this.type,
    this.hintText,
    this.maxLength,
    this.onValidator,
    this.onInput,
    this.initialValue,
  }) : super(key: key);

  ///是否必须
  final bool isRequired;

  ///标题文字
  final Widget? title;

  ///占位文字
  final String? hintText;

  ///最大长度（仅 `type==singleLine || type==multiLine` 时有效）,
  ///`singleLine` 默认64,`multiLine`,默认256
  final int? maxLength;

  ///输入框类型
  final InputType? type;

  final Function(dynamic)? onInput;

  ///校验
  final String Function(String?)? onValidator;

  final initialValue;
  @override
  Widget build(BuildContext context) {
    final theme = WTheme.of(context);
    final colorScheme = theme.colorScheme;
    return WisFormLayoutComponent(
      title: title,
      isRequired: isRequired,
      axis: type == InputType.multiLine ? Axis.vertical : Axis.horizontal,
      padding:
          type == InputType.multiLine ? EdgeInsets.only(bottom: 10.pt) : null,
      child: TextFormField(
        onChanged: (v) => onInput!(v),
        validator: (v) => onValidator!(v),
        initialValue: initialValue != null ? initialValue.toString() : null,
        keyboardType: TextInputType.numberWithOptions(decimal: true),
        inputFormatters: [
          _MoneyTextInputFormatter(),
        ],
        style: TextStyle(
          fontSize: 14.pt,
          color:
              type == InputType.money ? colorScheme.danger : colorScheme.minor,
        ),
        minLines: type == InputType.multiLine ? 3 : 1,
        maxLines: type == InputType.multiLine ? 3 : 1,
        maxLength: type == InputType.multiLine ? (maxLength ?? 256) : null,
        decoration: InputDecoration(
          contentPadding: type == InputType.multiLine
              ? EdgeInsets.fromLTRB(15.pt, 0, 0, 0)
              : null,
          fillColor: Colors.transparent,
          filled: true,
          hintText: hintText,
          hintStyle: TextStyle(
            fontSize: 14.pt,
            color: colorScheme.placeholder,
            fontWeight: FontWeight.w300,
          ),
        ),
      ),
    );
  }
}

///金额
class _MoneyTextInputFormatter extends TextInputFormatter {
  final regExp = RegExp('^[0-9]+\.?([0-9]{1,2})?\$');
  @override
  TextEditingValue formatEditUpdate(oldValue, newValue) {
    if (newValue.text == '')
      return TextEditingValue();

    ///打上0自动加上小数点
    else if (newValue.text == '0') {
      if (oldValue.text.length > newValue.text.length) {
        return TextEditingValue();
      } else {
        return TextEditingValue(
          text: '0.',
          selection: TextSelection(baseOffset: 2, extentOffset: 2),
        );
      }
    } else {
      return regExp.hasMatch(newValue.text) ? newValue : oldValue;
    }
  }
}
