<?php

namespace App\Http\Controllers\BundlesControllers;
use App\Models\fcmodels\Ime_Trial;
use App\Http\Controllers\Controller;
use Maatwebsite\Excel\Concerns\FromCollection;
use EloquentBuilder;
use App\Exports\AirtimeHistoryPDF;
use Maatwebsite\Excel\Facades\Excel;   
use App\Exports\Data_Bundles_TopUpExport;

use App\Models\bundles\Data_Bundles_TopUp;
use App\Models\atmodels\User;
use App\Models\atmodels\DataBundlesCredits;
use Illuminate\Http\Request;
use Auth;
use PDF;
use Carbon\Carbon;

class BundlesReportController extends Controller
{
    //
    //customer dash
    public function dashboard()
    {
        //
        return view('bundleviews.customer-dashboard');
    }
 //export excel
 public function exportExcel()
 {

   // $query =Data_Bundles_TopUp::where('to_shortcode','20384');
   // $requests=EloquentBuilder::to($query,request()->all())->orderBy('ime_trials_id','desc')->get();

 // Get the current date and time
$current = Carbon::now();

// Create the file name using the current date and time
$fileName = 'datatopups.xlsx_' . $current->format('H-i-s_d-m-y') . '.xlsx';
// Use the Excel library to download the data
return Excel::download(new Data_Bundles_TopUpExport, $fileName);
     return new Data_Bundles_TopUpExport(request()->all());
 }

// Display user data in view
public function showHistory(){
 $customer_id=Auth::user()->userShopUser['customer_id'];
 $history = Airtime_sending_record::where('customer_id',$customer_id)->get();
 return view('atviews.history.pdf_view', compact('history'));
}

        // Generate PDF
        public function createPDF() {
        // retreive all records from db
        $customer_id=Auth::user()->userShopUser['customer_id'];
        //$history=EloquentBuilder::to($query,request()->all())->get();

        // share data to view
        //view()->share('history',$data);
        $shop_id=Auth::user()->shop_id;
        $customer_id=Auth::user()->userShopUser['customer_id'];
        $shopname=Auth::user()->userShopUser['customer_id'];
     //   $wallet = Currency_credit::where('shopid',$shop_id)->pluck('amount');
       // $warrantrecords = Ime_Trial::All();

        $query =Data_Bundles_TopUp::where('ShopID',$shop_id);
        $bundlerecords=EloquentBuilder::to($query,request()->all())->orderBy('qnty','desc')->get();

       // $warrantrecords=EloquentBuilder::to($historye,request()->all())->get();   
        $pdf = PDF::loadView('bundleviews.history.pdf_view', compact('bundlerecords'));


 // Get the current date and time
 $current = Carbon::now();

 // Create the file name using the current date and time
 $fileName = 'datatopups.pdf_' . $current->format('H-i-s_d-m-y') . '.pdf';

 // Use the Excel library to download the data
 return $pdf->download($fileName);
        // download PDF file with download method
        }
 
}
