import 'dart:async';

import 'package:zhiying_base_widget/pages/orders_page/models/order_filter_model.dart';
import 'package:zhiying_base_widget/pages/orders_page/models/order_model.dart';
import 'package:zhiying_comm/util/base_bloc.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

class OrderContentBloc extends BlocBase {
  List<OrderModel> _orders = List();

  StreamController<List<OrderModel>> _ordersController =
      StreamController<List<OrderModel>>();

  Stream<List<OrderModel>> get outData => _ordersController.stream;

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

  void loadData(OrderFilterModel filter) {
    Map<String, dynamic> params = filter.toJson();
    params.removeWhere((key, value) => value == null || value == '');
    NetUtil.request('/api/v1/order', method: NetMethod.POST, params: params,
        onCache: (data) {
      _loadData(data);
    }, onSuccess: (data) {
      _loadData(data);
    });
  }

  void _loadData(dynamic data) {
    List list = data ?? [];

    OrderModel model = OrderModel();
    model.itemId = '6918577752399759376';
    model.imgUrl =
        'https://t7.baidu.com/it/u=3616242789,1098670747&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg?sec=1603444938&t=04aeb23595ac79d419be19ab92bea668';
    model.title = '乐町百褶半身裙秋冬女2019新款复古麻花半身裙甜美百褶半身裙';
    model.price = '108';
    model.commission = '0';
    model.commissionRate = '9.2';
    _orders.add(model);

    _orders.addAll(list.map((item) {
      return OrderModel.fromJson(Map<String, dynamic>.from(item));
    }).toList());

    _ordersController.add(_orders);
  }
}
