import 'package:cashier_reserve/common/request/request_manager.dart';
import 'package:cashier_reserve/data_model/reserve/reserve_log_model.dart';
import 'package:cashier_reserve/data_model/reserve/table_area_model.dart';
import 'package:cashier_reserve/data_model/reserve/table_model.dart';

/// ReserveModel 台桌预定相关请求
class ReserveModel {
  /// getShopTableAreaList 获取店铺桌台区域列表
  static Future<List<TableAreaModel?>> getShopTableAreaList() async {
    final r = await RequestManager.get("/api/booking/shop-table/area");
    if (r == null) {
      return [];
    }
    List<TableAreaModel?> list = [];
    for (var item in r as List) {
      list.add(TableAreaModel.fromJson(item));
    }
    return list;
  }

  /// getAreaTableList 获取区域桌台列表
  static Future<List<TableModel?>> getAreaTableList(num areaId, String date, String type) async {
    Map<String, dynamic> paramData = {
      "bookingDate": date,
      "bookingType": type,
    };
    if (areaId > 0) {
      paramData["areaId"] = areaId;
    }
    final r = await RequestManager.get("/api/booking/shop-table/list", params: paramData);
    if (r == null) {
      return [];
    }
    List<TableModel?> list = [];
    for (var item in r as List) {
      list.add(TableModel.fromJson(item));
    }
    return list;
  }

  /// commitReserveInfo 提交预定信息
  static Future<void> commitReserveInfo(Map<String, dynamic> params) async {
    await RequestManager.post("/api/booking/shop-table", params);
  }

  static Future<Map<String, ReserveLogModel?>> getUserReserveLog(List<String> phoneNos) async {
    final r = await RequestManager.post("/api/booking/shop-table/summary", {
      "phoneNos": phoneNos,
    });

    Map<String, ReserveLogModel?> map = {};
    if (r == null) {
      return map;
    }

    for (var item in r.entries) {
      map[item.key] = ReserveLogModel.fromJson(item.value);
    }

    return map;
  }

  static Future<String> getReserveSms() async {
    final r = await RequestManager.get("/api/booking/shop-table/sms");
    return r.toString();
  }

  static Future<void> updateReserveStatus(num reserveId, num status) async {
    await RequestManager.post("/api/booking/shop-table/mark-status", {
      "id": reserveId,
      "status": status,
    });
  }
}