<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Order;
use App\CompanyDetails;
use Carbon\Carbon;


class DboyReportController extends Controller
{
    public function index() {
        $date = date("Y-m-d");
        $Results=Order::where('created_at','LIKE','%'.$date.'%')->get();
        return view('DboyReport.index',compact('Results'));
    }

    public function detailsview(Order $Order) {
        
        return view('DboyReport.deatilsView', compact('Order')); 
    }

    public function searchResults(Request $request) {


        $status_id   = $request->status_id;
        $datesort    = $request->datesort;
        $txtSDate    = $request->txtSDate;
        $txtSDate1   = $request->txtSDate1;
        $nmbYear     = $request->nmbYear;


        $DateS = new Carbon ($request->txtSDate);
        $DateE = new Carbon ($request->txtSDate1);
        //dd($request);
        if($request->status_id) {
            if($request->datesort){
                if($request->datesort == 'day'){
                    $Results = Order::where('status_id',$request->status_id)->where('created_at','LIKE','%'.$request->txtSDate.'%')->get(); 
                    
                }
                elseif($request->datesort == 'month') {
                    $Results = Order::where('status_id',$request->status_id)->where('created_at','LIKE','%'.$request->txtSDate.'%')->get();
                }
                elseif($request->datesort == 'year') {
                    $Results = Order::where('status_id',$request->status_id)->where('created_at','LIKE','%'.$request->nmbYear.'%')->get();
                }
                elseif($request->datesort == '2') {
                    $Results = Order::whereBetween('created_at', [$DateS->format('Y-m-d')." 00:00:00", $DateE->format('Y-m-d')." 23:59:59"])->where('status_id',$request->status_id)->get();
                } 
                    if($request->print == 1) {
                        return $this->printingDetails($Results,$status_id,$datesort,$txtSDate,$txtSDate1,$nmbYear);
                    }
                    else {
                         return view('DboyReport.searchResults', compact('Results','status_id','datesort','txtSDate','txtSDate1','nmbYear'));
                    }
                
            }
            else {
                $Results = Order::where('status_id',$request->status_id)->get();
                
                if($request->print == 1) {
                    return $this->printingDetails($Results,$status_id,$datesort,$txtSDate,$txtSDate1,$nmbYear);
                }
                else {
                    return view('DboyReport.searchResults', compact('Results','status_id','datesort','txtSDate','txtSDate1','nmbYear'));
                }
            }
        }
        elseif($request->datesort) {
            if($request->datesort == 'day'){
                $Results = Order::where('created_at','LIKE','%'.$request->txtSDate.'%')->get(); 
            }
            elseif($request->datesort == 'month') {
                $Results = Order::where('created_at','LIKE','%'.$request->txtSDate.'%')->get();
            }
            elseif($request->datesort == 'year') {
                $Results = Order::where('created_at','LIKE','%'.$request->nmbYear.'%')->get();
            }
            elseif($request->datesort == '2') {
                $Results = Order::whereBetween('created_at', [$DateS->format('Y-m-d')." 00:00:00", $DateE->format('Y-m-d')." 23:59:59"])->get();
            }
                if($request->print == 1) {
                    return $this->printingDetails($Results,$status_id,$datesort,$txtSDate,$txtSDate1,$nmbYear);
                }
                else {
                    return view('DboyReport.searchResults', compact('Results','status_id','datesort','txtSDate','txtSDate1','nmbYear'));
                }
        }
        else {
            return redirect()->back();
        }    
        
    }

    public function printingDetails($values,$status_id=0,$datesort=0,$txtSDate=0,$txtSDate1=0,$nmbYear=0){
        $Results = $values;
        $status_id = $status_id;
        $datesort = $datesort;
        $txtSDate = $txtSDate;
        $txtSDate1 = $txtSDate1;
        $nmbYear = $nmbYear;
        $today = date('Y-m-d H:i:s');
        $Items = CompanyDetails::all();
        return view('DboyReport.Printview',compact('Items','Results','status_id','datesort','txtSDate','txtSDate1','nmbYear','today'));
    }

    public function singlePrint(Request $request) {

        $Order=Order::find($request->id);
        $CompanyDetails = CompanyDetails::all();

        return view('DboyReport.singlePrint',compact('CompanyDetails','Order'));
    }

    public function detailReport() {
        $Orders = Order::all();
        return view('DboyReport.detailReport',compact('Orders'));
    }

    public function ReportSelectGroups(Request $request){ 

        $q = $request->searchitems;
        //$Orders = Order::where('status_id','LIKE','%'.$q.'%')->get();
        $Orders = Order::where('status_id',$q)->get();
        
        return view('DboyReport.selectResults', compact('Orders')); 
      
    }

    public function detailPrint(Request $request) {
        
        $Orders = Order::where('status_id',$request->selectgrp)->get();
        $CompanyDetails = CompanyDetails::all();

        return view('DboyReport.detailPrint',compact('CompanyDetails','Orders'));
    }
}