import 'package:intl/intl.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../../../Helper/Constants/Apiconstants.dart';
import '../../../Model/Response/Orders/Getorderresponse.dart';
import '../../Provider/Orders/Ordersprovider.dart';

class OrdersRepository {

  int? tenantId;
  int? locationid;

  OrdersProvider ordersProvider = OrdersProvider();

  Future<GetOrders?> getOrders(String status) async {

    SharedPreferences prefs = await SharedPreferences.getInstance();
    tenantId = prefs.getInt('tenantId');
    locationid = prefs.getInt('locationId');
    var fromDate = DateFormat("yyyy-MM-dd").format(DateTime.now());
    var toDate = DateFormat("yyyy-MM-dd").format(DateTime.now());

    print('ddd $status');

    // var fromDateTm = DateFormat("yyyy-MM-dd").format(DateTime.now());
    // var toDateTm = DateFormat("yyyy-MM-dd").format(DateTime.now().add(Duration(days: 1)));

    var result = await ordersProvider.getOrders(
        '${ApiConstants.getOrders}/?tenantid=$tenantId&status=$status&fromdate=$fromDate&todate=$toDate&locationid=$locationid');
    return result;
  }

  Future<GetOrders?> getCurrentOrders() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    tenantId = prefs.getInt('tenantId');
    var fromDate = DateFormat("yyyy-MM-dd").format(DateTime.now());
    var toDate = DateFormat("yyyy-MM-dd").format(DateTime.now());
    var result = await ordersProvider.getOrders('${ApiConstants.getOrders}/?tenantid=$tenantId&status=ongoing&fromdate=$fromDate&todate=$toDate&locationid=$locationid');
    return result;
  }

  Future<GetOrders?> getCompletedOrders() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    tenantId = prefs.getInt('tenantId');
    var fromDate = DateFormat("yyyy-MM-dd").format(DateTime.now());
    var toDate = DateFormat("yyyy-MM-dd").format(DateTime.now());
    var result = await ordersProvider.getOrders('${ApiConstants.getOrders}/?tenantid=$tenantId&status=delivered&fromdate=$fromDate&todate=$toDate&locationid=$locationid');
    return result;
  }

  Future<GetOrders?> getCancelledOrder() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    tenantId = prefs.getInt('tenantId');
    var fromDate = DateFormat("yyyy-MM-dd").format(DateTime.now());
    var toDate = DateFormat("yyyy-MM-dd").format(DateTime.now());
    var result = await ordersProvider.getOrders('${ApiConstants.getOrders}/?tenantid=$tenantId&status=cancelled&fromdate=$fromDate&todate=$toDate&locationid=$locationid');
    return result;
  }


}