<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Booking;
use App\Plot;
use App\Installment;
use App\Sms;
use App\Branch;
use App\Commission;
use EF;


class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index()
    {
        // $booking        = new Booking;
        // $plot           = new Plot;
        // $installment    = new Installment;
        $sms            = new Sms;
        // $commission     = new Commission;
        $branch         = new Branch;
      
        // $date           = Date('Y-m-d');
        // $operator       = '<=';
        $status         = ['1'];
        // $plot_status    = ['sold'];
      
        // $approvebookingscount       = $booking->getBookingsCountByStatus(["Approve"]);

        // $unapprovebookingscount     = $booking->getBookingsCountByStatus(["Unapprove"]);

        // $maturebookings             = $booking->getMatureAll();

        // $hotbookings                = @count($booking->getBookingByColumn('is_hotitem',1));

        // $resalebookings             = @count($booking->getBookingByColumn('is_resale',1));

        // $blockbookings             = @count($booking->getBookingByColumn('is_block',1));
                
        // $cancel_bookingscount     = $booking->getBookingsCountByStatus(["Cancel"]);

        // $sold_inventorycount      = $plot->getInventoryCountByStatus(["sold"]);

        // $unsold_inventorycount    = $plot->getInventoryCountByStatus(["unsold"]);

     

        // $total_inventory          = ($sold_inventorycount + $unsold_inventorycount);

        // $approvebookingcount_percentage  = EF::calculatePercentage($approvebookingscount , $total_inventory,100);

        //  $unapprovebookingcount_percentage  = EF::calculatePercentage($unapprovebookingscount , $total_inventory,100);


        // $mature_percentage        = EF::calculatePercentage($maturebookings['mature'] , $approvebookingscount,100);

        // $immature_percentage      = EF::calculatePercentage($maturebookings['immature'] , $approvebookingscount,100);

        // $cancel_percentage        = EF::calculatePercentage($cancel_bookingscount , $approvebookingscount,100);

        // $hot_percentage           = EF::calculatePercentage($hotbookings , $approvebookingscount,100);

        // $resale_percentage           = EF::calculatePercentage($resalebookings , $approvebookingscount,100);
        //  $block_percentage           = EF::calculatePercentage($blockbookings , $approvebookingscount,100);


        // $sold_percentage          = EF::calculatePercentage($sold_inventorycount , $total_inventory,100);
        // $unsold_percentage        = EF::calculatePercentage($unsold_inventorycount , $total_inventory ,100);

        // $installment_overdues     = $installment->getOverDue($date,$operator);   
        // $booking_overdues         = $installment->getBookingWiseOverDue($date,$operator);
        $sms_list                 = $sms->getSmsByStatus($status);

        // $blocks                   = $plot->getBlockByStatus($plot_status);

        // $commission               = $commission->salesPersonChart();

        $branches                 = $branch->getBranchList();
        
        // $sales_list = array();
        // $sales[0] = 'data1';


        // for($i = 0; $i<count($commission); $i++)
        // {
        //     $sales_list[$i] = $commission[$i]['name'];
        //     $sales[$i+1] = $commission[$i]['sales'];

        // }
       




            return view('index',
            [
                // 'approvebookingscount'                => $approvebookingscount,
                // 'approvebookingcount_percentage'      => $approvebookingcount_percentage,
                // 'unapprovebookingscount'              => $unapprovebookingscount,
                // 'unapprovebookingcount_percentage'    => $unapprovebookingcount_percentage,
                // 'cancel_bookingscount'                => $cancel_bookingscount,
                // 'cancel_percentage'                   => $cancel_percentage,
                // 'sold_inventorycount'                 => $sold_inventorycount,
                // 'sold_percentage'                     => $sold_percentage,
                // 'unsold_inventorycount'               => $unsold_inventorycount,
                // 'unsold_percentage'                   => $unsold_percentage,
                // 'mature_bookingcount'                 => $maturebookings['mature'],
                // 'mature_percentage'                   => $mature_percentage,
                // 'immature_bookingcount'               => $maturebookings['immature'],
                // 'immature_percentage'                 => $immature_percentage,
                // 'installment_overdues'                => $installment_overdues,
                // 'booking_overdues'                    => $booking_overdues,
                'sms_list'                            => $sms_list,
                // 'blocks'                              => $blocks,
                // 'sales_list'                          => $sales_list,
                // 'sales'                               => $sales,
                // 'inventory_progress'                  => $plot->getPlotListByGroup('type',['sold']),
                // 'hotbookings'                         => $hotbookings,
                // 'blockbookings'                       => $blockbookings,
                // 'resalebookings'                      => $resalebookings,
                // 'hot_percentage'                      => $hot_percentage,
                // 'resale_percentage'                   => $resale_percentage,
                // 'block_percentage'                    => $block_percentage,
                'branches'                            => $branches,

               

            ]
     );
    }

    public function showLogs()
    {

        $current_month = date('m-Y');
        $file_name = 'activity_logs/'.$current_month.'.json';    

        $file_list = \File::allFiles(\Storage::disk('local')->path('activity_logs'));

        $log_list = \Storage::disk('local')->exists($file_name) ? json_decode(\Storage::disk('local')->get($file_name)) : [];

      

        
        return view('estatech_logs',['logs_list' => $log_list, 'file_list' => $file_list]);
    }

    public function getDateWiseLogs($date,$type)
    {
        if(\Request::ajax())
        {
         
            $file_name = $type.'_logs/'.$date.'.json';

            $log_list = \Storage::disk('local')->exists($file_name) ? json_decode(\Storage::disk('local')->get($file_name)) : []; 

            $count = 0;
            $view = " ";
            if(@count($log_list) > 0)
            {
                foreach($log_list as $rows)
                {
                    $view.= '<tr><td>'.++$count.'</td>
                            <td>'.$rows->name.'</td>
                            <td>'.$rows->branch.'</td>
                            <td>'.\EF::dateFormat($rows->log_date).'</td>
                            <td>'.$rows->log_time.'</td>
                            <td>'.$rows->message.'</td></tr>

                    ';
                }
            }
            else
            {
                $view.= '<tr colspan="5"><td>Logs not found!</td></tr>';
            }

            return $view;

             
        }
        

        return redirect('home');
    }


}
