import 'dart:async';
import 'dart:convert' as convert;

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:zhiying_base_widget/dialog/tip_dialog/tip_dialog.dart';
import 'package:zhiying_base_widget/pages/withdraw_page/models/withdraw_data_model.dart';
import 'package:zhiying_base_widget/pages/withdraw_page/models/withdraw_model.dart';
import 'package:zhiying_comm/util/base_bloc.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

class WithdrawBloc extends BlocBase {
  WithdrawModel _model;

  StreamController<WithdrawModel> _dataController =
      StreamController<WithdrawModel>();

  Stream<WithdrawModel> get outData => _dataController.stream;

  WithdrawDataModel withdrawDataModel;

  Map<String,dynamic> aliPayModel;

  String skipIdentifier;

  @override
  void dispose() {
    _dataController.close();
    _dataController = null;
  }

  void loadData(String skipIdentifier) async {
    this.skipIdentifier = skipIdentifier;
    NetUtil.request('/api/v1/mod/${skipIdentifier.toString()}',
        method: NetMethod.GET, onCache: (data) {
      _loadData(data);
    }, onSuccess: (data) {
      _loadData(data);
    });
  }

  ///加载数据
  void loadWithdrawData() {
    NetUtil.request("/api/v1/user/cash_out", onSuccess: (data) {
      withdrawDataModel = WithdrawDataModel.fromJson(data);
      _dataController.add(_model);
    });
  }

  void _loadData(dynamic data) {
    try {
      Map<String, dynamic> json = Map<String, dynamic>.from(data);
      if (json == null || json.length == 0) {
        return;
      }
      List mods = json['mod_list'];
      if (mods.first != null) {
        json = Map<String, dynamic>.from(mods.first);
        String d = json['data'];
        if (json.containsKey('data')) {
          String d = json['data'];
          Map<String, dynamic> da =
              Map<String, dynamic>.from(convert.jsonDecode(d));
          _model = WithdrawModel.fromJson(da);
          _dataController.add(_model);
        }
      }
    } catch (err) {
      Logger.error(err);
    }
  }

  ///提交提现申请
  void submitApply(BuildContext context, String money) async {
    num amount = num.tryParse(money);
    if (amount != null && amount > 0) {
      var result = await showDialog(
          context: context,
          child: TipDialog(
            btnText: "确定",
            content: "确定要提现？",
          ));

      if (result == null || !result) {
        return;
      }

      NetUtil.request("/api/v1/user/cash_out",
          method: NetMethod.POST,
          params: {'amount': amount.toString()}, onSuccess: (data) {
        showDialog(
            context: context,
            child: TipDialog(
              content: "提现申请已成功提交，请耐心等候审核通过",
            ));
        loadData(skipIdentifier);
        loadWithdrawData();
      }, onError: (data) {
        showDialog(
            context: context,
            child: TipDialog(
              content: "提现申请提交失败！，请稍后再试",
            ));
      });
    } else {
      showDialog(
          context: context,
          child: TipDialog(
            content: "请输入正确的金额！",
          ));
    }
  }

  ///支付宝是否绑定请求
  getAlipayStatus(){
    NetUtil.request("/api/v1/settings/account/security",onSuccess: (data){
      aliPayModel=data;
      _dataController.add(_model);
    });
  }

}
