<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models;
use Session;
use DB;
use Carbon\Carbon;
class DashboardController extends Controller
{
    public function index(){
    	 // dd('foo bar');

        $currentdate = date('m-d');
        $total_clients = Models\Client::count();
        $ussd_clients = Models\Client::where('services', 'LIKE', '%ussd%')->orwhere('services', 'LIKE', '%A2P%')->count();
        $airtime_clients = Models\Client::where('services', 'LIKE', '%airtime%')->count();
        // dd($airtime_clients);
        $sms_clients = Models\Client::where('services', 'LIKE', '%sms%')->count();
        $voice_clients = Models\Client::where('services', 'LIKE', '%ivr%')->count();
        $expiring_contracts = Models\Client::where('contract_auto_renew', '<>', 'YES')->where('contract_validity', '<>', null)->orwhere('contract_validity', '<>', '')->orderBy('contract_validity', 'ASC')->take(5)->get();
        $user_activities = Models\UserActivity::where('user_id', '>', '1')->with('userInfo')->orderBy('created_at', 'DESC')->take(5)->get();
        $recent_clients = Models\Client::with('auth_user_info')->orderBy('id', 'DESC')->take(5)->get();
        // $upcoming_birthdays = Models\StaffMember::whereDate('dob', '>=', $currentdate)->orderBy('dob', 'ASC')->take(5)->get();
        $upcoming_birthdays = Models\StaffMember::whereRaw("(month(dob) >= month(curdate())) AND day(dob) >= day(curdate())")->orderBy('dob', 'ASC')->take(5)->get();
        $upcoming_hodidays = Models\NationalHoliday::whereRaw("(month(event_date) >= month(curdate()))")->orderBy('event_date', 'ASC')->take(5)->get();

        // dd($upcoming_hodidays);
        // $recent_clients = Models\Client::orderBy('id', 'DESC')->take(5)->get();
        // dd($recent_clients);
        $data = [
            'page_title' => 'Dashboard',
            'sms' => $sms_clients,
            'ussd' => $ussd_clients,
            'voice' => $voice_clients,
            'airtime' => $airtime_clients,
            'total' => $total_clients,
            'recent_clients' => $recent_clients,
            'user_activities' => $user_activities,
            'expiring_contracts' => $expiring_contracts,
            'upcoming_birthdays' => $upcoming_birthdays,
            'upcoming_hodidays' => $upcoming_hodidays
        ];
    	return view('dashboard.index_two', $data);
    }

    public function getEvents(){
        $event_arr = [
            [
                "title" => 'Airtel MW Top Up',
                "start" => date("Y, m, d")
            ],
            [
                "title" => 'Airtel MW Top Down',
                "start" => date("Y, m, d")
            ],
        ];

        return response()->json($event_arr);

    }

    public function getQuotes(){
        // put numbers against the quotes
         // $daily_quotes = Models\Client::where('services', 'LIKE', '%ivr%')->count();

         $daily_quotes = DB::table('daily_quotes')
        ->where('id', '>=', DB::raw('(SELECT FLOOR(RAND() * (SELECT MAX(id) FROM daily_quotes)))'))
        // ->limit(5)
        ->first();
        return response()->json($daily_quotes);

    }
    public function getMnosContracts(){
        $threeMonthsLater = Carbon::now()->addMonths(3)->format('m-d');
        $threeMonthsEarlier = Carbon::now()->addMonths(3)->format('m-d');
        $currentMonth = Carbon::now()->format('m');
        $today = Carbon::now()->format('m-d');

        $expiring_three_months = Models\NetworkOps::where('contract_auto_renew', 'NO')->whereRaw("DATE_FORMAT(contract_validity, '%m-%d') = ?", [$threeMonthsLater])->get();
        #$network_arr = Models\NetworkOps::where('contract_auto_renew', 'NO')->whereRaw("DATE_FORMAT(contract_validity, '%m-%d') = ?", [$threeMonthsEarlier])->get();



        $expired_in_current_month = Models\NetworkOps::whereRaw("DATE_FORMAT(contract_validity, '%m-%d') < ?", [$today])->get();
        $expired_overall = Models\NetworkOps::whereDate('contract_validity', '<', Carbon::now())->get();
        
        // $network_arr = Models\NetworkOps::with('account_manager_info')
        //                 ->where('contract_auto_renew', 'NO')
        //                 ->get();
        
        $expiring_current_month = Models\NetworkOps::whereMonth('contract_validity', $currentMonth)->get();
        $data = [
            'expiring_three_months' => $expiring_three_months->count(),
            'expired_overall' => $expired_overall->count(),
            'expiring_current_month' => $expiring_current_month->count()
        ];
        return response()->json($data);
    }

    public function getClientContracts(){
        $threeMonthsLater = Carbon::now()->addMonths(3)->format('m-d');
        $threeMonthsEarlier = Carbon::now()->addMonths(3)->format('m-d');
        $currentMonth = Carbon::now()->format('m');
        $today = Carbon::now()->format('m-d');

        $expiring_three_months = Models\Client::where('contract_auto_renew', 'NO')->whereRaw("DATE_FORMAT(contract_validity, '%m-%d') = ?", [$threeMonthsLater])->get();
        #$network_arr = Models\NetworkOps::where('contract_auto_renew', 'NO')->whereRaw("DATE_FORMAT(contract_validity, '%m-%d') = ?", [$threeMonthsEarlier])->get();



        $expired_in_current_month = Models\Client::whereRaw("DATE_FORMAT(contract_validity, '%m-%d') < ?", [$today])->get();
        $expired_overall = Models\Client::whereDate('contract_validity', '<', Carbon::now())->get();
        
        // $network_arr = Models\NetworkOps::with('account_manager_info')
        //                 ->where('contract_auto_renew', 'NO')
        //                 ->get();
        


        $expiring_current_month = Models\Client::whereMonth('contract_validity', $currentMonth)->get();
        $data = [
            'expiring_three_months' => $expiring_three_months->count(),
            'expired_overall' => $expired_overall->count(),
            'expiring_current_month' => $expiring_current_month->count()
        ];
        return response()->json($data);
    }
}
