import 'package:fis_ui/base_define/page.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

///提交按钮
class SubmitButton extends StatelessWidget implements FWidget {
  SubmitButton({
    required this.businessParent,
    required this.text,
    required this.description,
    this.loading = false,
    this.disabled = false,
    this.loadingText,
    this.onPressed,
  });

  final FPage businessParent;

  ///描述
  final String description;

  /// 提交按钮文本
  final String text;

  /// 是否加载
  final bool loading;

  /// 加载文案
  final String? loadingText;

  /// 是否禁用
  final bool disabled;

  /// 确认回调
  final VoidCallback? onPressed;

  @override
  FWidget build(BuildContext context) {
    final style = kIsMobile
        ? ElevatedButton.styleFrom(
            elevation: 0,
            minimumSize: Size.fromHeight(40.sp),
            padding: EdgeInsets.symmetric(vertical: 6 / Get.pixelRatio),
          )
        : ElevatedButton.styleFrom(
            elevation: 0,
            minimumSize: Size.fromHeight(54.sp),
            padding: EdgeInsets.symmetric(vertical: 16.sp),
          );
    final child = loading
        ? buildLoadingWidget()
        : FText(
            text,
            style: TextStyle(
              fontSize: (kIsMobile ? 18 : 22).sp,
            ),
          );
    final isDisable = disabled || loading;
    return FElevatedButton(
      onPressed: isDisable ? null : onPressed,
      child: child,
      style: style,
      businessParent: businessParent,
      name: description,
    );
  }

  FWidget buildLoadingWidget() {
    final loadingCircle = const FSizedBox(
      width: 16,
      height: 16,
      child: FCircularProgressIndicator(strokeWidth: 2),
    );
    if (loadingText == null || loadingText!.isEmpty) {
      return loadingCircle;
    }
    return FRow(
      mainAxisSize: MainAxisSize.min,
      children: [
        loadingCircle,
        const FSizedBox(width: 8),
        FText(
          loadingText!,
          style: TextStyle(
            fontSize: (kIsMobile ? 16 : 20).sp,
          ),
        ),
      ],
    );
  }
}

class FormSecondaryButton extends StatelessWidget implements FWidget {
  FormSecondaryButton({
    required this.businessParent,
    required this.text,
    required this.description,
    this.onPressed,
  });

  final FPage businessParent;

  /// 按钮文案
  final String text;

  ///描述
  final String description;

  /// 回调方法
  final VoidCallback? onPressed;

  @override
  FWidget build(BuildContext context) {
    final style = kIsMobile
        ? OutlinedButton.styleFrom(
            minimumSize: Size.fromHeight(40.sp),
            padding: EdgeInsets.symmetric(vertical: 6.sp),
          )
        : ElevatedButton.styleFrom(
            minimumSize: Size.fromHeight(54.sp),
            padding: EdgeInsets.symmetric(vertical: 16.sp),
          );
    return FOutlinedButton(
      onPressed: onPressed,
      child: FText(
        text,
        style: TextStyle(
          fontSize: (kIsMobile ? 18 : 22).sp,
        ),
      ),
      style: style,
      businessParent: businessParent,
      name: description,
    );
  }
}
