<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Contracts\Encryption\DecryptException;
use App\Booking;
use App\Plot;
use App\Commission;
use App\Installment;
use EF;


class DashboardController extends Controller
{
	private $call_back = [];

	public function __construct()
    {
        $this->call_back = ['status' => 'false', 'view' => 'No Records'];
    }

    public function getBookingStats(Request $request)
    {
    	// PARAMETER FOR THIS FUNCTION
    	// 1. STATUS, 2. TITLE, 3. COLOR, 4. BRANCH

    	try
        {
        	$booking = new Booking;

        	$plot    = new Plot;

        	$form_collect = $request->input();

        	$branch_list = [decrypt($form_collect['branch'])];

        	$status_list = [$form_collect['status']];

        	if($request->ajax())
        	{
        	  	$count_booking    = $booking->getBookingsCountByStatusBranchwise($status_list,$branch_list);


        	  	$sold_count       = $plot->getInventoryCountByStatusBranchwise(["sold"],$branch_list);

        		$unsold_count     = $plot->getInventoryCountByStatusBranchwise(["unsold"],$branch_list);

        	    $percentage       = EF::calculatePercentage($count_booking,($sold_count + $unsold_count),100);

        	    $this->call_back['status'] = 'true';

        	    $this->call_back['view']   = view('components.dashboard.stats_card',[

        	    	'count'      => $count_booking,
        	    	'percentage' => $percentage,
        	    	'title'      => $form_collect['title'],
        	    	'color'      => $form_collect['color'],

        	    ])->render();
        	}	
		}
        catch (DecryptException $e)
        {

        }

        return $this->call_back;
    }

    // OTHER BOOKING MEAN RE-SALE, HOT ITEMS, BLOCKED
    public function getOtherBookingStats(Request $request)
    {
    	// PARAMETER FOR THIS FUNCTION
    	// 1. TYPE OF BOOKING, 2. TITLE, 3. COLOR, 4. BRANCH

    	try
        {
        	$booking = new Booking;

        	$plot    = new Plot;

        	$form_collect = $request->input();

        	$branch_list  = [decrypt($form_collect['branch'])];

        	$type_of_booking  = $form_collect['type_of_booking'];

        	if($request->ajax())
        	{
        	  	$approve_booking_count  = $booking->getBookingsCountByStatusBranchwise(['Approve'],$branch_list);

        		if($type_of_booking == "Hot Booking")
        		{
        			 $booking_count = @count($booking->getBookingByColumnBranchwise('is_hotitem',1,$branch_list));
        		}
        		else if($type_of_booking == "Blocked Booking")
        		{
        			 $booking_count = @count($booking->getBookingByColumnBranchwise('is_block',1,$branch_list));
        		}
        		else if($type_of_booking == "Resale Booking")
        		{
        			 $booking_count = @count($booking->getBookingByColumnBranchwise('is_resale',1,$branch_list));
        		}

        	    $percentage       = EF::calculatePercentage($booking_count,$approve_booking_count,100);

        	    $this->call_back['status'] = 'true';

        	    $this->call_back['view']   = view('components.dashboard.stats_card',[

        	    	'count'      => $booking_count,
        	    	'percentage' => $percentage,
        	    	'title'      => $form_collect['title'],
        	    	'color'      => $form_collect['color'],

        	    ])->render();
        	}	
		}
        catch (DecryptException $e)
        {

        }

        return $this->call_back;
    }

    public function getInventoryProgress(Request $request)
    {
		try
        {
        	if($request->ajax())
        	{
        		$plot = new Plot;

        		$form_collect = $request->input();

        		$branch_list  = [decrypt($form_collect['branch'])];
        		

        		$this->call_back['status'] = 'true';

        	    $this->call_back['view']   = $plot->getPlotListByGroupBranchwise('block_name',['sold'],$branch_list);

        	    
        	}	
		}
        catch (DecryptException $e)
        {

        }

        return $this->call_back;
    }

    public function getSalesPersonProgress(Request $request)
    {
    	try
        {
        	if($request->ajax())
        	{
        		$commission = new Commission;

        		$form_collect = $request->input();

        		$branch_list  = [decrypt($form_collect['branch'])];

        		$commission     = $commission->salesPersonChartBranchwise($branch_list);

				$sales_list 	= array();

        		$sales[0] 		= 'data1';

		        for($i = 0; $i<count($commission); $i++)
		        {
		            $sales_list[$i] = substr($commission[$i]['name'],0,7);

		            $sales[$i+1] = $commission[$i]['sales'];
		        }

		        $this->call_back['status'] = 'true';

				$this->call_back['view']   = ['sales' => $sales, 'sales_list' => $sales_list];
        	}	
		}
        catch (DecryptException $e)
        {

        }

        return $this->call_back;
    }

    public function getTotalOverDue(Request $request)
    {
    	try
        {
        	if($request->ajax())
        	{
                $installment = new Installment;

                $date           = Date('Y-m-d');
                $operator       = '<=';

                $form_collect = $request->input();

                $branch_list  = [decrypt($form_collect['branch'])];

                $this->call_back['status'] = 'true';

                $this->call_back['view'] = view('components.dashboard.total_overdue',[
                    'installment_overdues' => $installment->getOverDuebranchwise($date,$operator,$branch_list)
                ])->render();
        	}	
		}
        catch (DecryptException $e)
        {

        }

        return $this->call_back;
    }

    public function getFiveDayReminder(Request $request)
    {
    	try
        {
            if($request->ajax())
            {
                $installment = new Installment;

                $date           = Date('Y-m-d');

                $operator       = '<=';

                $form_collect = $request->input();

                $branch_list  = [decrypt($form_collect['branch'])];

                $end_date     = EF::getNumOfDays('5',$date,$flag = '1');


                $this->call_back['status'] = 'true';


                $this->call_back['view'] = view('components.dashboard.fiveday_reminder_overdue',[
                    'reminder_overdues' => $installment->getOverDueByIntervalBranchwise($date,$end_date,$branch_list)
                ])->render();
            }   
        }
        catch (DecryptException $e)
        {

        }

        return $this->call_back;
    }

    public function getBookingOverDues(Request $request)
    {
    	try
        {
            if($request->ajax())
            {
                $installment = new Installment;

                $date           = Date('Y-m-d');
                $operator       = '<=';

                $form_collect = $request->input();

                $branch_list  = [decrypt($form_collect['branch'])];

                $this->call_back['status'] = 'true';

                $this->call_back['view'] = view('components.dashboard.bookingwise_overdue',[
                    'booking_overdues' => $installment->getBookingWiseOverDueBranchwise($date,$operator,$branch_list)
                ])->render();
            }   
        }
        catch (DecryptException $e)
        {

        }

        return $this->call_back;
    }

}
