import 'dart:convert';

import 'package:zhiying_base_widget/widgets/goods_details/footer/model/goods_details_footer_model.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

class GoodsDetailsFooterRepository {
  
  GoodsDetailsFooterModel _oldmodel;
  
  /// 初始化数据
  Future<GoodsDetailsFooterModel> fetchParentData(final Map<String, dynamic> model) async {
    try {
      GoodsDetailsFooterModel data =  GoodsDetailsFooterModel.fromJson(jsonDecode(model['data']));
      _oldmodel = data;
      return data;
    } catch (e, s) {
      Logger.error(e, s);
    }
    return null;
  }

  /// 点击收藏
  Future<GoodsDetailsFooterModel> fetchCollect(final Map<String, dynamic> model) async {
    try {
      if (!EmptyUtil.isEmpty(model)) {
        var result = await NetUtil.post('/api/v1/user/myfav', params: model, method: NetMethod.POST);
        if(NetUtil.isSuccess(result)){
          _oldmodel.isFav = '1';
          return _oldmodel;
        }
      }
    } catch (e, s) {
      Logger.error(e, s);
    }
    return null;
  }

  /// 取消收藏
  Future<GoodsDetailsFooterModel> fetchDeleteCollect(final Map<String, dynamic> model) async{
    try{
      if(!EmptyUtil.isEmpty(model)){
        var result = await NetUtil.post('/api/v1/user/myfav/delete', params: {'gids':[model['good_id']]}, method: NetMethod.POST);
        if(NetUtil.isSuccess(result)){
          _oldmodel.isFav = '0';
          return _oldmodel;
        }
      }
    }catch(e, s){
      Logger.error(e,s);
    }
    return null;
  }

  /// 跳转购买

  /// 分享
}
