mirrored_repositories/Flutter-Open-Source-Apps/Flutter Open Source Apps/Intermediate/E-com app/lib/screens
import 'dart:convert';

import 'package:http/http.dart' as http;
import 'package:vishal_mega_mart_app/screens/constant.dart';
import 'package:vishal_mega_mart_app/screens/home.dart';
import 'package:vishal_mega_mart_app/screens/model/product.dart';
import 'package:vishal_mega_mart_app/screens/sharedpref.dart';

class ProductProvider {
  //Client client = Client();
  Future<ProductModel> product() async {
    // ignore: avoid_print
    print("======= API CALLING  =========",);
   final mytoken = await appPreferences.getStringPreference("my_token");
    String token = mytoken;
    print("===TOKEN IN API=========");

    print("===TOKEN IN API=====${token}====");
    
    var headers = {
      'Content-Type': 'application/json',
      'Authorization':
          'Bearer token',
          
          
    };
    var request = http.Request(
        'POST',
        Uri.parse(
            'https://www.vishalmegamart.com/s/-/dw/data/v20_10/product_search'));
    //var elemnt = Searchtext();
    var value = Searchtext.searchelemnt;
    print("========= VALUE======= ${value}+++++++++++");
    request.body = json.encode({
      "query": {
        "text_query": {
          "fields": ["name"],
          "search_phrase": value
        }
      },
      "expand": ["prices"],
      "select": "(**)"
    });
    request.headers.addAll(headers);

    http.StreamedResponse response = await request.send();

    final res = await response.stream.bytesToString();
   print("=======Product====== ${res}");
    if (response.statusCode == 200) {
      print("=======Product====== ${res}");
      // print("============= ${await response.stream.bytesToString()}");
    } else {
      print("======= Failed to load posts =========");
    }

    return ProductModel.fromJson(jsonDecode(res));

//     const url =
//         "https://www.vishalmegamart.com/s/-/dw/data/v20_10/product_search";
//     const token =
//         "eyJ0eXAiOiJKV1QiLCJraWQiOiJEMWhPUDdEODN4TjBqZWlqaTI3WWFvZFRjL0E9IiwiYWxnIjoiUlMyNTYifQ.eyJzdWIiOiI5ODA0ZjhiNC1lNDMxLTQ5NTAtYTc0My1iZGEyMmYwOWViYjgiLCJjdHMiOiJPQVVUSDJfU1RBVEVMRVNTX0dSQU5UIiwiYXVkaXRUcmFja2luZ0lkIjoiZjBkY2U0OTEtNjBjMi00NDlhLWJkMWY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//       'expand': 'prices',
//       'select': '(**)'
//     };

//     final uri = Uri.parse(url).replace(queryParameters: {
//       'fields': 'name',
//       'search_phrase': 'jeans',
//       'expand': 'prices',
//       'select': '(**)'
//     });

// // String urll = Uri.parse("https://www.vishalmegamart.com/s/-/dw/data/v20_10");
// // Uri urls = Uri.http(
// //     urll,
// //     '/product_search',
// //     {
// //       'mapData': jsonEncode(queryParams),
// //     },
// // );
// //   Uri url = Uri.http(
// //     'domain.com',
// //     '/endpoint',
// //     {
// //       'mapData': jsonEncode(queryParams),
// //     },
// // );
// //   {
// //       'mapData': jsonEncode(queryParams),
// //     },

//     var response = await client.post(uri, headers: {
//       'Content-Type': 'application/json',
//       'Accept': 'application/json',
//       'Authorization': 'Bearer $token',
//     });
//     final jsondata = jsonDecode(response.body.toString());
//     //return CategorySection.fromJson(jsonDecode(response.body));
//       print(
//         "\n\n=========PRODUCT====${response.statusCode}+++++++++++++++++++\n\n");
//     print(
//         "\n\n=========PRODUCT====${response.body.toString()}+++++++++++++++++++\n\n");
//     if (response.statusCode == 200) {
//       // If the call to the server was successful, parse the JSON
//       return ProductModel.fromJson(json.decode(response.body));
//     } else {
//       // If that call was not successful, throw an error.
//       throw Exception('=====Failed to load post');
//     }
  }
}
