<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\User;
use App\Country;
use App\Models\MarketCountry;
use App\Models\ReferUser;
use App\StudentProposal;
use App\Models\University;
use Auth;
use Mail;
use Session;
use Hash;

class AdminController extends Controller
{

    public function __construct(){

        $this->middleware('admin');

    }


    public function dashboard()
    {
        $admin = Auth::guard('admin')->user();

        $register_active_teachers_list =User::where(
                'apply_status',1
            )->where(
                'sign_up',1
            )->where(
                'user_type',2
            )->orderBy(
                'registered_date','DESC'
            )->get();

        $pending_student_list =User::where(
            'user_type',1
        )->where(
            'sign_up',0
        )->where(
            'register_type',4
        )->orderBy(
            'id','DESC'
        )->get();

        $register_student_list =User::where(
                'user_type',1
            )->where(
                'sign_up',1
            )->orderBy(
                'id','DESC'
            )->get();

        $invited_registered_list =User::where(
            'user_type',2
        )->whereNotNull(
            'admin_invite_id'
        )->where(
            'register_type',1   //1=teacher invitation
        )->orderBy(
            'id','DESC'
        )->get();

        $landing_wishlist =User::where(
            'user_type',2
        )->where(
            'register_type',2  // 2=teacher landing
        )->orderBy(
            'id','DESC'
        )->get();


        $market_countries = MarketCountry::where(
                'status', 1
            )->select(
                'market_countries.*', \DB::raw('(SELECT name FROM countries WHERE market_countries.country_id = countries.id ) as sort')
            )->orderBy('sort')->get();

        $student_refer_friend_list =User::where(
            'register_type',6 // 6 = student refer a friend for a proposal 
        )->where(
            'user_type',1
        )->orderBy('id','DESC')->get();

        $application = StudentProposal::where(
            'proposal_submit_status',1
        )->orderBy('id','DESC')->get(); 

        $data =[
          'register_active_teachers_list'=>$register_active_teachers_list,
          'register_student_list'=>$register_student_list ,
          'market_countries'=>$market_countries,
          'refer_users'=>$student_refer_friend_list,
          'invited_registered_list'=>$invited_registered_list,
          'application'=>$application,
          'landing_wishlist'=>$landing_wishlist,
          'pending_student_list'=>$pending_student_list,
          'admin'=>$admin
        ];
        return view('admin.admin_profile',$data);
    }


    public function load_country_list($text){
        $selected = MarketCountry::pluck('country_id')->toArray();
        if($text == '0'){
            $countries = Country::where('status', 1)->paginate(10);
        }else{
            $countries = Country::where('status', 1)->where('name','like', '%'.$text.'%')->paginate(10);
        }

        return view('admin.loadCountry', compact('countries','selected'));
    }


    public function destination_wise_university(Request $request){

        $universities = University::where(
                'country_id',$request->country_id
            )->orderBy(
                'name'
            )->select(
                'id','name'
            )->get();

        return [
            'msg'=>'Destination wise university data',
            'data'=>[
                'universities'=>$universities
            ]
        ];
    }


}
