coupon_provider.dart 11.8 KB
import 'package:flutter/material.dart';
import 'package:one_key/constant/constant.dart';
import 'package:one_key/constant/error.dart';
import 'package:one_key/constant/state.dart';
import 'package:one_key/models/coupon_model.dart';
import 'package:one_key/net/dio_util.dart';
import 'package:one_key/net/http_url.dart';
import 'package:one_key/net/base_resp.dart';
import 'package:one_key/provider/user_model.dart';
import 'package:one_key/utils/utils.dart';
import 'package:provider/provider.dart';

class CouponProvider with ChangeNotifier {
  List<bool> swichStateData = []; //优惠券是否启用  true启用   false停用
  PageState pageState = PageState.Loading;
  PageState pageStateHistory = PageState.Loading;
  PageState pageStateSelect = PageState.Loading;
  bool hasModify = false;

  bool checkValue = true; //优惠券页面“全选”按钮状态 ----- 数据状态全部选中为true
  // List<Map<String, dynamic>> listData = []; //优惠券页面数据
  List<String> coupons = []; //选中的优惠券id集合
  // List<String> couponsShow = []; //邀请函显示数量 选中的优惠券id集合

  List<CouponSunModel> couponListHistory = []; //优惠券历史列表
  List<CouponSunModel> couponList = []; //优惠券列表
  List<CouponSunModel> couponListSelect = []; //优惠券选择列表

  CouponProvider() {
    // for (int i = 0; i < 20; i++) {
    //   swichStateData.add(true);
    //   listData.add({'checked': true, 'id': '${i}'});
    // }
  }

  //全选按钮执行“全选”
  addAll() async {
    //把数据状态全部改成true,即选中状态
    coupons = [];
    for (var i = 0; i < couponListSelect.length; i++) {
      couponListSelect[i].isCheck = true;
      coupons.add(couponListSelect[i].id);
    }
    checkValue = true;
    notifyListeners();
  }

  ///清楚选择
  clearSelect() {
    coupons = [];
    // couponsShow = [];
  }

  //默认选择前5个
  initCouponSelect(BuildContext context) {
    coupons = [];
    // couponsShow = [];
    for (var i = 0; i < couponListSelect.length; i++) {
      if (i < 5) {
        couponListSelect[i].isCheck = true;
        coupons.add(couponListSelect[i].id);
      }
    }
    // couponsShow.addAll(coupons);
    // coupons = [];
    notifyListeners();
  }

//全选按钮执行“全不选”
  cancelAll() async {
    //把数据状态全部改成false,即不选中状态
    for (var i = 0; i < couponListSelect.length; i++) {
      couponListSelect[i].isCheck = false;
    }
    coupons = [];
    checkValue = false;
    notifyListeners();
  }

//选中
  addData(int index) {
    couponListSelect[index].isCheck = true;
    coupons.add(couponListSelect[index].id);
    notifyListeners();
  }

//未选中
  cancelData(int index) {
    couponListSelect[index].isCheck = false;
    coupons.removeWhere((element) => element == couponListSelect[index].id);
    notifyListeners();
  }

  //获取全选按钮的状态  true  or  false
  getCheckValue() {
    bool temp = couponListSelect.any((value) {
      return value.isCheck == false;
    });
    if (temp == true) {
      checkValue = false;
    } else {
      checkValue = true;
    }
    notifyListeners();
  }

  ///客户经理创建活动
  Future<dynamic> createCustomerActivity(
      BuildContext context, Map<String, String> params,
      {isShowLoading = true}) async {
    BaseResp<dynamic> baseResp = await DioUtil()
        .request<dynamic>(context, Method.get, HttpUrl.create_customer_activity,
            data: params, showLoading: isShowLoading)
        .catchError((error) {
      print(error.toString());
      if (error is MyError) {
        // switch (error.type) {
        //   case 1:
        //     pageState = PageState.ConnectivityError;
        //     break;
        //   case 2:
        //     pageState = PageState.ConnectivityTimeOut;
        //     break;
        //   default:
        //     pageState = PageState.ConnectivityError;
        //     break;
        // }
      }
      // notifyListeners();
      return false;
    });
    if (baseResp.code == Constant.status_success) {
      if (baseResp.data.isEmpty) {
        // pageState = PageState.ConnectivityError;
        return false;
      } else {
        // pageState = PageState.Success;
        return true;
      }

      // notifyListeners();
      // return NotebookDetail.fromJson(baseResp.data);
    } else {
      // pageState = PageState.ConnectivityError;
      if (baseResp != null) {
        Utils.showToast(baseResp.msg ?? "");
      }
      return false;
    }
  }

  ///客户经理获取所创建活动列表
  Future<List<CouponSunModel>> getCustomerActivity(
      BuildContext context, Map<String, String> params,
      {isShowLoading = true}) async {
    if (isShowLoading) {
      pageState = PageState.Loading;
      notifyListeners();
    }
    BaseResp<dynamic> baseResp = await DioUtil()
        .request<dynamic>(context, Method.get, HttpUrl.get_customer_activity,
            data: params, showLoading: false)
        .catchError((error) {
      print(error.toString());
      if (error is MyError) {
        switch (error.type) {
          case 1:
            pageState = PageState.ConnectivityError;
            break;
          case 2:
            pageState = PageState.ConnectivityTimeOut;
            break;
          default:
            pageState = PageState.ConnectivityError;
            break;
        }
      }
      notifyListeners();
    });
    if (baseResp.code == Constant.status_success) {
      if (baseResp.data.isEmpty) {
        pageState = PageState.Empty;
      } else {
        pageState = PageState.Success;
      }
      couponList = CouponModel.fromJson(baseResp.responseData).data;
      notifyListeners();
    } else {
      pageState = PageState.ConnectivityError;
      if (baseResp != null) {
        Utils.showToast(baseResp.msg ?? "");
      }
    }
    return couponList;
  }

  ///客户经理获取所创建活动列表
  Future<List<CouponSunModel>> getCustomerHistoryActivity(
      BuildContext context, Map<String, String> params,
      {isShowLoading = true}) async {
    if (isShowLoading) {
      pageStateHistory = PageState.Loading;
      notifyListeners();
    }
    BaseResp<dynamic> baseResp = await DioUtil()
        .request<dynamic>(context, Method.get, HttpUrl.get_customer_activity,
            data: params, showLoading: false)
        .catchError((error) {
      print(error.toString());
      if (error is MyError) {
        switch (error.type) {
          case 1:
            pageStateHistory = PageState.ConnectivityError;
            break;
          case 2:
            pageStateHistory = PageState.ConnectivityTimeOut;
            break;
          default:
            pageStateHistory = PageState.ConnectivityError;
            break;
        }
      }
      notifyListeners();
    });
    if (baseResp.code == Constant.status_success) {
      if (baseResp.data.isEmpty) {
        pageStateHistory = PageState.Empty;
      } else {
        pageStateHistory = PageState.Success;
      }
      couponListHistory = CouponModel.fromJson(baseResp.responseData).data;
      notifyListeners();
    } else {
      pageStateHistory = PageState.ConnectivityError;
      if (baseResp != null) {
        Utils.showToast(baseResp.msg ?? "");
      }
    }
    return couponListHistory;
  }

  ///客户经理获取所创建活动列表(选择优惠券)
  Future<List<CouponSunModel>> getCustomerSelectActivity(
      BuildContext context, Map<String, String> params,
      {isShowLoading = true}) async {
    if (isShowLoading) {
      pageStateSelect = PageState.Loading;
      notifyListeners();
    }
    BaseResp<dynamic> baseResp = await DioUtil()
        .request<dynamic>(context, Method.get, HttpUrl.get_customer_activity,
            data: params, showLoading: false)
        .catchError((error) {
      print(error.toString());
      if (error is MyError) {
        switch (error.type) {
          case 1:
            pageStateSelect = PageState.ConnectivityError;
            break;
          case 2:
            pageStateSelect = PageState.ConnectivityTimeOut;
            break;
          default:
            pageStateSelect = PageState.ConnectivityError;
            break;
        }
      }
      notifyListeners();
    });
    if (baseResp.code == Constant.status_success) {
      if (baseResp.data.isEmpty) {
        pageStateSelect = PageState.Empty;
      } else {
        pageStateSelect = PageState.Success;
      }
      couponListSelect = CouponModel.fromJson(baseResp.responseData).data;
      if (hasModify) {
        //已经绑定
        List<CouponSunModel> temp = [];
        for (int i = 0; i < couponListSelect.length; i++) {
          for (int j = 0; j < coupons.length; j++) {
            if (couponListSelect[i].id == coupons[j]) {
              temp.add(couponListSelect[i]);
            }
          }
        }
        couponListSelect = temp;
      } else {
        for (int i = 0; i < couponListSelect.length; i++) {
          for (int j = 0; j < coupons.length; j++) {
            if (couponListSelect[i].id == coupons[j]) {
              couponListSelect[i].isCheck = true;
            }
          }
        }
      }

      getCheckValue();
      notifyListeners();
    } else {
      pageStateSelect = PageState.ConnectivityError;
      if (baseResp != null) {
        Utils.showToast(baseResp.msg ?? "");
      }
    }
    return couponListSelect;
  }

  ///客户经理修改活动
  Future<dynamic> setCustomerActivity(
      BuildContext context, Map<String, String> params,
      {isShowLoading = true}) async {
    BaseResp<dynamic> baseResp = await DioUtil()
        .request<dynamic>(context, Method.get, HttpUrl.set_customer_activity,
            data: params, showLoading: isShowLoading)
        .catchError((error) {
      print(error.toString());
      if (error is MyError) {}
      return false;
    });
    if (baseResp.code == Constant.status_success) {
      if (int.parse(baseResp.data.toString()) == 1) {
        return true;
      } else {
        // Utils.showToast(baseResp.msg ?? "修改失败");
        return false;
      }
    } else {
      if (baseResp != null) {
        // Utils.showToast(baseResp.msg ?? "");
      }
      return false;
    }
  }

  ///设置预定绑定活动ID
  Future<dynamic> setActivityAsReserve(
      BuildContext context, Map<String, String> params,
      {isShowLoading = true}) async {
    BaseResp<dynamic> baseResp = await DioUtil()
        .request<dynamic>(context, Method.get, HttpUrl.set_activity_as_reserve,
            data: params, showLoading: isShowLoading)
        .catchError((error) {
      print(error.toString());
      if (error is MyError) {}
      return false;
    });
    if (baseResp.code == Constant.status_success) {
      if (baseResp.data != null) {
        // coupons = baseResp.data['aid'].split('');
        return true;
      } else {
        // Utils.showToast(baseResp.msg ?? "修改失败");
        return false;
      }
    } else {
      if (baseResp != null) {
        // Utils.showToast(baseResp.msg ?? "");
      }
      return false;
    }
  }

  ///获取预定绑定活动
  Future<bool> getActivityByRid(
      BuildContext context, Map<String, String> params,
      {isShowLoading = true}) async {
    BaseResp<dynamic> baseResp = await DioUtil()
        .request<dynamic>(context, Method.get, HttpUrl.get_activity_by_rid,
            data: params, showLoading: isShowLoading)
        .catchError((error) {
      print(error.toString());
      if (error is MyError) {}
    });
    if (baseResp.code == Constant.status_success) {
      if (baseResp.data == null) {
        hasModify = false;
      } else {
        couponListSelect = CouponModel.fromJson(baseResp.responseData).data;
        for (int i = 0; i < couponListSelect.length; i++) {
          coupons.add(couponListSelect[i].id);
          // couponsShow.add(couponTemp[i].id);
        }
        hasModify = true;
        notifyListeners();
      }
    } else {
      if (baseResp != null) {
        Utils.showToast(baseResp.msg ?? "");
      }
      return false;
    }
  }
}