import 'dart:convert';

import 'package:initial_folder/base_service.dart';
import 'package:initial_folder/models/banners_model.dart';
import 'package:http/http.dart' as http;

class BannersService {
  Future<Map<String, dynamic>> getAllBanners() async {
    Uri url = Uri.parse('$baseUrl/mobile/banners');
    var header = {
      'Content-Type': 'application/json; charset=UTF-8',
      'Connection': 'Keep-Alive'
    };
    var response = await http.get(url, headers: header);
    if (response.statusCode == 200) {
      var data = jsonDecode(response.body)['data'][0];

      List<BannersModel> banners = [];
      print("Berhasil banner${response.body}");
      for (var item in data) {
        if (item['status'] == '1') {
          banners.add(BannersModel.fromJson(item));
        }
      }
      return {'status': response.statusCode, 'data': banners};
    } else if (response.statusCode == 404) {
      print("Banner kosong, saat ini running banner default. ${response.body}");
      return {'status': response.statusCode, 'data': []};
    } else {
      print("Gagal banner ${response.body}");
      throw Exception('Gagal ambil data');
    }
  }
}
