<?php

namespace App\Http\Controllers;

use App\Models\Category;
use App\Models\Product;
use App\Models\OrderDetails;
use App\Models\Orders;
use App\Models\User;
use App\Models\PaymentDetails;
// use Barryvdh\DomPDF\Facade as PDF;
use PDF;
use File;
use Excel;
use Mail;
use App\Exports\ExportTask;
use App\Exports\ExportRecurringTask;
use App\Models\TransactionHistory;
use Illuminate\Http\Request;

class OrderController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }
    
    public function index()
    {
        $orders = Orders::latest()->get();
        return view('admin.Order.index', compact('orders'));
        
    }
    
    public function OrderDetails($id)
    {
          
        $orders_details = Orders::where('id', $id)->first();
        return view('admin.Order.details', compact('orders_details'));
        
    }
    
    public function OrderShippingStatus(Request $request)
    {
        // dd($request->all());
        
        $orders_details = Orders::find($request->id);
        $orders_details->status = $request->status;
        
        if($orders_details->save())
        {
            $order_detail = Orders::where('id', $orders_details->id)->first();
        
            $data['name'] = $order_detail->name;
            $data['email'] = $order_detail->email;
            $data['order_details'] = $order_detail;
    
            try{
                Mail::send('email.order_status', $data, function($message)use($data) {
                    $message->to($data['email'], $data['name'])
                        ->from("noreplywebbitech@gmail.com", 'no-reply')
                    ->subject('Order Details from Amirdham');
                });
            }
            catch(\Exception $e){
                // dd($e);
            }
            
        }
        
    }
    
    public function orderReport()
    {
        $reports = Orders::get();
        return view('admin.Reports.order_reports', compact('reports'));
    }
    
    public function userReport()
    {
        $reports = User::where('user_type', 'user')->get();
        return view('admin.Reports.user_report', compact('reports'));
    }
    
    public function UserReportDownload(Request $request)
    {
        
        $test2 = explode(' - ',$request->data_range);
        
        $form_date = (new \Carbon\Carbon($test2[0]))->format('Y-m-d') ?? '';
        $to_date   = (new \Carbon\Carbon($test2[1]))->format('Y-m-d') ?? '';
        
        $data2 = User::where('user_type', 'user')->with(['payment_details', 'transaction_history'])
                  ->with(['payment_details' => function ($q) use ($form_date,$to_date)
                  {
                      $q->whereDate('created_at', '>=', $form_date);
                      $q->whereDate('created_at', '<=', $to_date);
                  }])
                  ->with(['transaction_history' => function ($query) use ($form_date,$to_date)
                  {
                      $query->whereDate('created_at', '>=', $form_date);
                      $query->whereDate('created_at', '<=', $to_date);
                  }])
            ->get();
        
        if($data2)
        {
          $data2 = $data2->toArray();
        }
        
        // dd($data2);
        if (count($data2) > 0) {
            
            $data['form_date'] = $form_date;
            $data['to_date'] = $to_date;
            
            return Excel::download(new ExportTask($data), 'User Repors.xlsx');
        }
        else
        {   
            return redirect()->back()->with('error', 'No Data Available');
        }
    }
    
    public function OrderReportDownload(Request $request)
    {
        
        $test2 = explode(' - ',$request->data_range);
        
        $form_date = (new \Carbon\Carbon($test2[0]))->format('Y-m-d') ?? '';
        $to_date   = (new \Carbon\Carbon($test2[1]))->format('Y-m-d') ?? '';
        
        $data2 = Orders::whereDate('created_at', '>=', $form_date)
            ->whereDate('created_at', '<=', $to_date)
            ->get();
        
        // dd($data2);
        if (count($data2) > 0) {
            
            $data['form_date'] = $form_date;
            $data['to_date'] = $to_date;
            
            return Excel::download(new ExportRecurringTask($data), 'Order Repors.xlsx');
        }
        else
        {   
            return redirect()->back()->with('error', 'No Data Available');
        }
    }
    
    public function invoice($id)
    {
        
        $order_details = Orders::where('id', $id)->first();
        // if ($order_details) {
        //     $order_details = $order_details->toArray();
        // }
        
        $product_description = str_replace('#', '', $order_details->order_id);
        PDF::setOptions(['dpi' => 150, 'defaultFont' => 'sans-serif']);

        $pdf = PDF::loadView('invoice', compact('order_details'));
        $pdfname = $product_description.'.pdf';
        $path = 'public/invoice/';
        $pdf->save($path.''. $pdfname);
        // file_put_contents('public/invoice/'.$product_description.'.pdf', $pdf->output() );
        
        return $pdf->download($pdfname);

    }
    
}


?>