import 'package:flutter/material.dart';
import 'package:plugin_platform/engine/toast/toast_engine.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:shared/utils/log_utils.dart';
import 'package:shared/utils/util.dart';

import 'add_card_state.dart';

part 'add_card_view_model.g.dart';

@riverpod
class AddCardViewModel extends _$AddCardViewModel {
  @override
  AddCardState build() {
    return AddCardState();
  }

  //添加银行卡
  void addCard() {

    final FocusNode cardNameFocusNode = state.formData['card_name']!['focusNode'];
    final FocusNode cardNumFocusNode = state.formData['card_number']!['focusNode'];
    final FocusNode monthFocusNode = state.formData['month']!['focusNode'];
    final FocusNode yearFocusNode = state.formData['year']!['focusNode'];
    final FocusNode cycFocusNode = state.formData['cyc']!['focusNode'];

    final TextEditingController cardNameController = state.formData['card_name']!['controller'];
    final TextEditingController cardNumController = state.formData['card_number']!['controller'];
    final TextEditingController monthController = state.formData['month']!['controller'];
    final TextEditingController yearController = state.formData['year']!['controller'];
    final TextEditingController cycController = state.formData['cyc']!['controller'];

    cardNameFocusNode.unfocus();
    cardNumFocusNode.unfocus();
    monthFocusNode.unfocus();
    yearFocusNode.unfocus();
    cycFocusNode.unfocus();

    final cardName = cardNameController.text;
    final cardNum = cardNumController.text;
    final month = monthController.text;
    final year = yearController.text;
    final cyc = cycController.text;

    Log.d('当前待提交的 cardName:${cardName} cardNum:${cardNum} month:${month} year:${year} cyc:${cyc}');

    if (Utils.isEmpty(cardName)) {
      ToastEngine.show('Enter Name on Card');
      return;
    }

    if (Utils.isEmpty(cardNum)) {
      ToastEngine.show('Enter Card Number');
      return;
    }

    if (Utils.isEmpty(month)) {
      ToastEngine.show('Enter Month on Card');
      return;
    }

    if (Utils.isEmpty(year)) {
      ToastEngine.show('Enter Year on Card');
      return;
    }

    if (Utils.isEmpty(cyc)) {
      ToastEngine.show('Enter CYC on Card');
      return;
    }

    //执行密码登录
    ToastEngine.show('准备执行请求 cardName:${cardName} cardNum:${cardNum} month:${month} year:${year} cyc:${cyc}');

  }
}
