<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models;
use Session;
use Illuminate\Support\Arr;
use App\Jobs\SendNewUssdClientEmail;
use App\Jobs\SendUssdClientActiveEmail;
use App\Jobs\SendNewNotesEmailAlert;
use App\Http\Requests;
use Carbon\Carbon;



class ReportsController extends Controller{
    public function overview(){
         $document_arr = \DB::table('general_documents')
            ->select(\DB::raw('count(*) as docs_count, category'))
            ->groupBy('category')
            ->get();

        $client_docs = \DB::table('client_files')
            ->join('clients', 'clients.id', '=', 'client_files.client_id')
            ->select(\DB::raw('count(*) as docs_count, clients.name, client_files.client_id'))
            ->groupBy('clients.name', 'client_files.client_id')
            ->get();

        $mno_docs = \DB::table('mno_files')
            ->join('network_operators', 'network_operators.id', '=', 'mno_files.mno_id')
            ->select(\DB::raw('count(*) as docs_count, network_operators.name, mno_files.mno_id'))
            ->groupBy('network_operators.name', 'mno_files.mno_id')
            ->get();

        
        $data = [
            'page_title' => 'Report Overview',
            // 'vas_clients' => $vas_clients,
            // 'domain_clients' => $domain_clients,
            // 'recent_clients' => $recent_clients,
            // 'user_activities' => $user_activities,
            'current_user' => session('current_user'),
            // 'client_docs' => $client_docs,
            'mno_docs' => $mno_docs
        ];
        return view('reports.overview', $data);
    }
    public function getRecentClients(){
        $data = [
            'page_title' => 'Recent Clients',
            'current_user' => session('current_user')
        ];
        return view('reports.recent_clients', $data);

    }
    public function getRecentClientsJson(){
        // $period = $request->period;
        // $clients = Models\Client::where()->get();
        /*
        $clients = \DB::table('clients')
                ->whereRaw('week(created_at) = WEEK(NOW())')
                ->with('auth_user_info')
                ->get();
        */
        $clients = \DB::table('clients')
            ->join('auth_users AS aumngr', 'aumngr.id', '=', 'clients.auth_user_id')
            ->select('clients.id','clients.name', 'clients.status', 'clients.country', 'aumngr.name As accountMgr', 'clients.created_at')
            ->whereRaw("week(clients.created_at) = WEEK(NOW())")
            ->orderBy('name', 'ASC')->paginate(50);

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

    }


    public function getUserActivities(){
         #$user_activities = Models\UserActivity::where('user_id', '>', '1')->with('userInfo')->orderBy('created_at', 'DESC')->take(5)->get();
        $data = [
            'page_title' => 'User Activities',
            'current_user' => session('current_user')
        ];
        return view('reports.user-activities', $data);
    }

    public function getUserActivitiesJson(Request $request){
        #$user_activities = Models\UserActivity::where('user_id', '>', '1')->with('userInfo')->orderBy('created_at', 'DESC')->take(5)->get();
        $user_activities = \DB::table('user_activities')
            ->join('auth_users', 'auth_users.id', '=', 'user_activities.user_id')
            ->select('user_activities.id','auth_users.name', 'user_activities.content', 'user_activities.ip_address', 'user_activities.created_at',
              'user_activities.updated_at')
            ->whereRaw("user_activities.type = 'staff'")
            ->orderBy('user_activities.created_at', 'DESC')
            ->get();


        if($request->has('keyword')){
            $keyword = $request->keyword;
            $user_activities = \DB::table('user_activities')
             ->join('auth_users', 'auth_users.id', '=', 'user_activities.user_id')
            ->select('user_activities.id','auth_users.name', 'user_activities.content', 'user_activities.ip_address', 'user_activities.created_at', 'user_activities.updated_at')
            ->whereRaw("user_activities.name like '%$keyword%' or user_activities.ip_address like '%$keyword%' OR user_activities.created_at like '%$keyword%' OR user_activities.updated_at like '%$keyword%'")
            ->orderBy('user_activities.created_at', 'DESC')
            //->get();
            ->paginate(50);
        }
        return response()->json($user_activities);
    }

    public function getMnosContracts(){
         #$user_activities = Models\UserActivity::where('user_id', '>', '1')->with('userInfo')->orderBy('created_at', 'DESC')->take(5)->get();
        $data = [
            'page_title' => 'Expired MNO Contracts',
            'current_user' => session('current_user')
        ];
        return view('reports.mno-expired-contracts', $data);
    }

    public function getMnosContractsJson(Request $request){
       
        $today = Carbon::now()->format('m-d');
        $expired_contracts = \DB::table('network_operators')
            ->join('auth_users', 'auth_users.id', '=', 'network_operators.account_manager_id')
            ->select('network_operators.id','auth_users.name AS accountManager', 'network_operators.name','network_operators.contact_person', 'network_operators.contact_person_email', 'network_operators.country', 'network_operators.contract_validity', 'network_operators.created_at')
            ->whereRaw("DATE_FORMAT(contract_validity, '%m-%d') < ?", [$today])
            ->orderBy('network_operators.name', 'DESC')
            ->get();

        if($request->has('keyword')){
            $keyword = $request->keyword;
            $expired_contracts = \DB::table('network_operators')
            ->join('auth_users', 'auth_users.id', '=', 'network_operators.account_manager_id')
            ->select('network_operators.id','auth_users.name AS accountManager', 'network_operators.name','network_operators.contact_person', 'network_operators.contact_person_email', 'network_operators.country', 'network_operators.contract_validity', 'network_operators.created_at')
            ->whereRaw("DATE_FORMAT(contract_validity, '%m-%d') < ?", [$today])
            ->whereRaw("network_operators.name like '%$keyword%' or network_operators.contact_person like '%$keyword%' OR network_operators.created_at like '%$keyword%' OR network_operators.contract_validity like '%$keyword%' OR network_operators.contact_person_email like '%$keyword%'")
            ->orderBy('network_operators.name', 'DESC')
            ->get();
        }
        return response()->json($expired_contracts);
    }
    
    public function getClientContracts(){
         #$user_activities = Models\UserActivity::where('user_id', '>', '1')->with('userInfo')->orderBy('created_at', 'DESC')->take(5)->get();
        $data = [
            'page_title' => 'Expirted Client Contracts',
            'current_user' => session('current_user')
        ];
        return view('reports.clients-expired-contracts', $data);
    }

    public function getClientContractsJson(Request $request){
         $today = Carbon::now()->format('m-d');
        $expired_contracts = \DB::table('clients')
            ->join('auth_users', 'auth_users.id', '=', 'clients.auth_user_id')
            ->select('clients.id','auth_users.name AS accountManager', 'clients.name','clients.contact_person', 'clients.email', 'clients.country', 'clients.contract_validity', 'clients.created_at')
            ->whereRaw("DATE_FORMAT(contract_validity, '%m-%d') < ?", [$today])
            ->orderBy('clients.name', 'DESC')
            ->get();

        if($request->has('keyword')){
            $keyword = $request->keyword;
            $expired_contracts = \DB::table('clients')
            ->join('auth_users', 'auth_users.id', '=', 'clients.auth_user_id')
            ->select('clients.id','auth_users.name AS accountManager', 'clients.name','clients.contact_person', 'clients.email', 'clients.country', 'clients.contract_validity', 'clients.created_at')
            ->whereRaw("DATE_FORMAT(contract_validity, '%m-%d') < ?", [$today])
            ->whereRaw("clients.name like '%$keyword%' or clients.contact_person like '%$keyword%' OR clients.created_at like '%$keyword%' OR clients.contract_validity like '%$keyword%' OR clients.contact_person_email like '%$keyword%'")
            ->orderBy('clients.name', 'DESC')
            ->get();
        }
        return response()->json($expired_contracts);
    }

    public function getAmNewClients(){
        // $new_clients = Models\Client::where()
    }

    public function getAmNewNotes(){
        
    }
    public function getClientsByService(){
        $data = [
            'page_title' => 'Clients By Service',
            'current_user' => session('current_user')
        ];
        return view('reports.clients-by-service', $data);
    }
    public function getClientsByServiceJson(){
        $keyword = "USSD";
        $clients = \DB::table('clients')
            ->join('auth_users AS aumngr', 'aumngr.id', '=', 'clients.auth_user_id')
            ->select('clients.name', 'clients.services', 'clients.status', 'clients.country', 'aumngr.name As accountMgr', 'clients.created_at')
            ->whereRaw("services like '%$keyword%'")
            ->orderBy('name', 'ASC')->get();

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

    }
    public function name(){
       
       $list = ['first', 'second', 'third'];
       // return $list[2];
       print($list[2]); 
    }
}
