<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Storage;
use App\Mail\SendMail;
use App\Mail\SendMessageToEndUser;
use Mail;
use App\history;
use App\elink;
use App\form;
use App\implink;
use App\FaqCategory;
use App\PaymentAmount;
use App\notice;
use App\slider;
use App\gallery;
use App\admission;
use App\routine;
use App\result;
use App\contact;
use App\schedule;
use App\speech;
use App\User;
use Auth;
use App\Episode;
use App\Guest;
use App\JoinAsType;
use App\JoinAsDetail;
use App\Specialguest;
use App\Judge;
use App\Mark;
use App\Question;
use App\Quizepermission;
use App\Specialmoment;
use App\Audience;
use App\Winneraudience;
use App\Questiondistribution;
use App\Roundone;
use App\Answer;
use App\Roundthree;
use App\Roundfour;
use App\Roundfive;
use App\Roundsix;
use App\Event;
use App\Round;
use App\Reward;
use App\Certificate;
use App\Blogs;
use App\Winner;
use App\Session;
use App\Partners;
use App\Faqs;
use App\Teams;
use App\Mentor;
use App\Participant;
Use App\District;
use App\Division;
use App\Upazila;
use Illuminate\Support\Facades\Redirect;
//for BD date
use Jenssegers\Date\Date;

class AdminController extends Controller
{

    //public  view  part

    public function indexview()
    {
        Date::setLocale('bd');
        $date = Date::now()->format('l j F Y ');
        $sliders = slider::all();
        
        $events = Event::with('session')->get();
        
        $history = DB::table('historys')->first();
        $rounds = DB::table('rounds')->get();
        $rewards = DB::table('rewards')->get();
        $certificates = DB::table('certificates')->get();
        $video = DB::table('videos')->first();
        $contact = DB::table('contacts')->first();
        $schedule = DB::table('schedule')->first();
        $notices = notice::all();
        $forms = form::all();
        $elinks = elink::all();
        $implinks = implink::all();
        $galleries = DB::table('galleries')->inRandomOrder()->limit(12)->get();
        $speechs = DB::table('speechs')->limit(2)->get();

        return view('home')->with(['date' => $date, 'galleries' => $galleries, 'certificates' => $certificates, 'rewards' => $rewards, 'rounds' => $rounds, 'events' => $events, 'slider' => $sliders, 'contact' => $contact, 'schedule' => $schedule, 'history' => $history, 'speechs' => $speechs, 'video' => $video, 'notices' => $notices, 'forms' => $forms, 'elinks' => $elinks, 'implinks' => $implinks]);
    }

    public function participant()
    {

        Date::setLocale('bd');
        $date = Date::now()->format('l j F Y ');
        $sliders = slider::all();

        $history = DB::table('historys')->first();
        $video = DB::table('videos')->first();
        $contact = DB::table('contacts')->first();
        $schedule = DB::table('schedule')->first();
        $notices = notice::all();
        $forms = form::all();
        $elinks = elink::all();
        $implinks = implink::all();
        $galleries = DB::table('galleries')->inRandomOrder()->limit(4)->get();
        $speechs = DB::table('speechs')->limit(2)->get();

        $users = User::where('year', '=', 2019)
            ->where('role', '=', 1)
            ->where('permission', '=', 1)
            ->orderBy('class', 'ASC')
            ->paginate(25);
        //66 perosne
        //dd($users);

        $RoundOne = DB::table('marks')
            ->leftjoin('users', 'users.id', '=', 'marks.user_id')
            ->where('marks.round_id', '=', '0')
            ->where('users.year', '=', 2019)
            ->where('users.permission', '=', 1)
            ->where('users.selection', '=', 1)
            ->orderBy('marks.total', 'DESC')
            ->get();
        // 20 persons
        //dd($RoundOne);
        // $RoundOne = User::where('year', '=', 2019)
        // ->where('role', '=', 1)
        // ->where('permission', '=', 1)
        // ->where('selection', '=', 1)
        // ->get();
        //dd($RoundOne);

        $RoundTwo = DB::table('marks')
            ->leftjoin('users', 'users.id', '=', 'marks.user_id')
            ->where('marks.round_id', '=', '1')
            ->where('users.year', '=', 2019)
            ->where('users.permission', '=', 1)
            ->where('users.selection', '=', 1)
            ->where('users.round_1', '=', 1)
            ->orderBy('marks.total', 'DESC')
            ->get();
        //16 persons
        //dd($RoundTwo);
        //  $RoundTwo = User::where('year', '=', 2019)
        // ->where('role', '=', 1)
        // ->where('permission', '=', 1)
        // ->where('selection', '=', 1)
        //  ->where('round_1', '=', 1)
        // ->get();

        $RoundThree = DB::table('users')
            ->join('marks', 'marks.user_id', '=', 'users.id')
            ->select(
                'users.id', 'users.name', 'users.class', 'users.roll', 'users.picture', (DB::raw('SUM(total) as totalMark'))
            )
            ->where('marks.round_id', '=', '3')
            //->where('marks.marktype', '=', 'byplay')
            //->where('marks.marktype', '=', 'guest')
            ->groupBy('marks.user_id')
            //->orderBy('total', 'DESC')
            ->orderBy(DB::raw('SUM(total)'), 'desc')
            //->orderBy((DB::raw('SUM(total) as totalMark')), 'DESC')
            ->get();
        //dd($RoundThree);


        $Roundfour = DB::table('users')
            ->join('marks', 'marks.user_id', '=', 'users.id')
            ->select(
                'users.id', 'users.name', 'users.class', 'users.roll', 'users.picture', (DB::raw('SUM(total) as totalMark'))
            )
            ->where('marks.round_id', '=', '4')
            //->where('marks.marktype', '=', 'byplay')
            //->where('marks.marktype', '=', 'guest')
            ->groupBy('marks.user_id')
            //->orderBy('total', 'DESC')
            ->orderBy(DB::raw('SUM(total)'), 'desc')
            //->orderBy((DB::raw('SUM(total) as totalMark')), 'DESC')
            ->get();

        //dd($Roundfour);

        $Roundfive = DB::table('users')
            ->join('marks', 'marks.user_id', '=', 'users.id')
            ->select(
                'users.id', 'users.name', 'users.class', 'users.roll', 'users.picture', (DB::raw('SUM(total) as totalMark'))
            )
            ->where('marks.round_id', '=', '5')
            //->where('marks.marktype', '=', 'byplay')
            //->where('marks.marktype', '=', 'guest')
            ->groupBy('marks.user_id')
            //->orderBy('total', 'DESC')
            ->orderBy(DB::raw('SUM(total)'), 'desc')
            //->orderBy((DB::raw('SUM(total) as totalMark')), 'DESC')
            ->get();


        $Roundsix = DB::table('users')
            ->join('marks', 'marks.user_id', '=', 'users.id')
            ->select(
                'users.id', 'users.name', 'users.class', 'users.roll', 'users.picture', (DB::raw('SUM(total) as totalMark'))
            )
            ->where('marks.round_id', '=', '6')
            //->where('marks.marktype', '=', 'byplay')
            //->where('marks.marktype', '=', 'guest')
            ->groupBy('marks.user_id')
            //->orderBy('total', 'DESC')
            ->orderBy(DB::raw('SUM(total)'), 'desc')
            //->orderBy((DB::raw('SUM(total) as totalMark')), 'DESC')
            ->get();

        //dd($Roundsix);

        return view('participant', compact('Roundsix', 'Roundfive', 'Roundfour', 'RoundThree', 'RoundTwo', 'RoundOne', 'users', 'date', 'galleries', 'slider', 'contact', 'schedule', 'history', 'speechs', 'video', 'notices', 'forms', 'elinks', 'implinks'));
    }

    public function quizaccess(Request $request)
    {

        //dd($request);
        $quizaccess = new Roundone();
        $quizaccess->user_id = $request->input('user_id');
        $quizaccess->save();

        return Redirect::back();
    }

    public function quizepermission(Request $request, $id)
    {

        // dd($request);
        $permissionForround = Roundone::findOrFail($id);
        $permissionForround->type_one = $request->input('type_one');
        $permissionForround->update();

        return Redirect::back();
    }

    public function quizeroundoneimage(Request $request, $id)
    {

        $quizeroundoneimage = Roundone::findOrFail($id);
        $quizeroundoneimage->type_two = $request->input('type_two');
        $quizeroundoneimage->update();

        return Redirect::back();
    }

    public function quizeroundoneaudio(Request $request, $id)
    {

        $quizeroundoneaudio = Roundone::findOrFail($id);
        $quizeroundoneaudio->type_three = $request->input('type_three');
        $quizeroundoneaudio->update();
        return Redirect::back();
    }

    public function quizeroundonevideo(Request $request, $id)
    {

        $quizeroundonevideo = Roundone::findOrFail($id);
        $quizeroundonevideo->type_four = $request->input('type_four');
        $quizeroundonevideo->update();

        return Redirect::back();
    }

    public function quizeroundthreetext(Request $request, $id)
    {

        //dd($request);
        $permissionForround = Roundthree::find($id);
        $permissionForround->type_one = $request->input('type_one');
        $permissionForround->update();

        return Redirect::back();
    }

    public function quizeroundthreeimage(Request $request, $id)
    {

        // dd($request);
        $permissionForround = Roundthree::find($id);
        $permissionForround->type_two = $request->input('type_two');
        $permissionForround->update();

        return Redirect::back();
    }

    public function quizeroundfourtext(Request $request, $id)
    {

        //dd($request);
        $permissionForround = Roundfour::find($id);
        $permissionForround->type_one = $request->input('type_one');
        $permissionForround->update();

        return Redirect::back();
    }

    public function quizeroundfourimage(Request $request, $id)
    {

        //dd($request);
        $permissionForround = Roundfour::find($id);
        $permissionForround->type_two = $request->input('type_two');
        $permissionForround->update();

        return Redirect::back();
    }

    public function quizeroundfivetext(Request $request, $id)
    {

        //dd($request);
        $permissionForround = Roundfive::find($id);
        $permissionForround->type_one = $request->input('type_one');
        $permissionForround->update();

        return Redirect::back();
    }

    public function quizeroundfiveimage(Request $request, $id)
    {

        //dd($request);
        $permissionForround = Roundfive::find($id);
        $permissionForround->type_two = $request->input('type_two');
        $permissionForround->update();

        return Redirect::back();
    }

    public function quizeroundsixtext(Request $request, $id)
    {

        //dd($request);
        $permissionForround = Roundsix::find($id);
        $permissionForround->type_one = $request->input('type_one');
        $permissionForround->update();

        return Redirect::back();
    }

    public function quizeroundsiximage(Request $request, $id)
    {

        //dd($request);
        $permissionForround = Roundsix::find($id);
        $permissionForround->type_two = $request->input('type_two');
        $permissionForround->update();

        return Redirect::back();
    }

    public function episodecontrol()
    {


        // $users = User::where('year', '=', 2019)
        // ->where('role', '=', 1)
        // ->where('permission', '=', 1)
        // ->get();

        $users = User::where('year', '=', 2019)
            ->where('role', '=', 1)
            ->where('permission', '=', 1)
            //->where('selection', '=', 1)
            ->get();
        //dd($users);

        $RoundOne = User::where('year', '=', 2019)
            ->where('role', '=', 1)
            ->where('permission', '=', 1)
            ->where('selection', '=', 1)
            ->get();

        //  $RoundTwo = User::where('year', '=', 2019)
        // ->where('role', '=', 1)
        // ->where('permission', '=', 1)
        // ->where('selection', '=', 1)
        // ->where('round_1', '=', 1)
        // ->get();
        //dd($RoundTwo);
        $RoundFirst = DB::table('users')
            ->leftjoin('roundones', 'roundones.user_id', '=', 'users.id')
            //->leftjoin('users', 'followers.user_id', '=', 'users.id')
            ->where('users.permission', '=', 1)
            ->where('users.selection', '=', 1)
            ->where('users.round_1', '=', 1)
            ->select('users.id as user_id', 'roundones.type_one', 'roundones.type_two', 'roundones.type_three', 'roundones.type_four', 'roundones.type_five', 'roundones.id as id', 'users.name', 'users.class', 'users.roll', 'users.picture', 'users.qp_round_1')
            ->get();
        //dd($RoundFirst);


        $RoundThree = DB::table('users')
            ->leftjoin('roundthrees', 'roundthrees.user_id', '=', 'users.id')
            //->leftjoin('users', 'followers.user_id', '=', 'users.id')
            ->where('users.permission', '=', 1)
            //->where('users.selection', '=', 1)
            ->where('users.round_2', '=', 1)
            ->select('users.id as user_id', 'roundthrees.type_one', 'roundthrees.type_two', 'roundthrees.id as id', 'users.name', 'users.class', 'users.roll', 'users.picture', 'users.qp_round_1')
            ->get();

        //dd($RoundThree);


        $Roundfour = DB::table('users')
            ->leftjoin('roundfours', 'roundfours.user_id', '=', 'users.id')
            //->leftjoin('users', 'followers.user_id', '=', 'users.id')
            ->where('users.permission', '=', 1)
            //->where('users.selection', '=', 1)
            ->where('users.round_4', '=', 1)
            ->select('users.id as user_id', 'roundfours.type_one', 'roundfours.type_two', 'roundfours.id as id', 'users.name', 'users.class', 'users.roll', 'users.picture', 'users.qp_round_1')
            ->get();


        // $Roundfive = User::where('year', '=', 2019)
        // ->where('role', '=', 1)
        // ->where('permission', '=', 1)
        // ->where('selection', '=', 1)
        //  //->where('round_1', '=', 1)
        //  //->where('round_2', '=', 1)
        //  //->where('round_3', '=', 1)
        //  ->where('round_5', '=', 1)
        // ->get();


        $Roundfive = DB::table('users')
            ->leftjoin('roundfives', 'roundfives.user_id', '=', 'users.id')
            //->leftjoin('users', 'followers.user_id', '=', 'users.id')
            ->where('users.permission', '=', 1)
            //->where('users.selection', '=', 1)
            ->where('users.round_5', '=', 1)
            ->select('users.id as user_id', 'roundfives.type_one', 'roundfives.type_two', 'roundfives.id as id', 'users.name', 'users.class', 'users.roll', 'users.picture', 'users.qp_round_1')
            ->get();

        //dd($Roundfive);

        $Roundsix = DB::table('users')
            ->leftjoin('roundsixes', 'roundsixes.user_id', '=', 'users.id')
            //->leftjoin('users', 'followers.user_id', '=', 'users.id')
            ->where('users.permission', '=', 1)
            //->where('users.selection', '=', 1)
            ->where('users.round_6', '=', 1)
            ->select('users.id as user_id', 'roundsixes.type_one', 'roundsixes.type_two', 'roundsixes.id as id', 'users.name', 'users.class', 'users.roll', 'users.picture', 'users.qp_round_1')
            ->get();

        //dd($Roundsix);
        //  $Roundsix = User::where('year', '=', 2019)
        // ->where('role', '=', 1)
        // ->where('permission', '=', 1)
        // ->where('selection', '=', 1)
        //  ->where('round_1', '=', 1)
        //  ->where('round_2', '=', 1)
        //  ->where('round_3', '=', 1)
        //  ->where('round_4', '=', 1)
        //  ->where('round_5', '=', 1)
        // ->get();

        $RoundSeven = User::where('year', '=', 2019)
            ->where('role', '=', 1)
            ->where('permission', '=', 1)
            ->where('selection', '=', 1)
            ->where('round_1', '=', 1)
            ->where('round_2', '=', 1)
            ->where('round_3', '=', 1)
            ->where('round_4', '=', 1)
            ->where('round_5', '=', 1)
            ->where('round_6', '=', 1)
            ->get();

        return view('admin.episodecontrol', compact('RoundSeven', 'Roundsix', 'Roundfive', 'Roundfour', 'RoundThree', 'RoundFirst', 'RoundOne', 'users'));
    }

    public function episodecontrolcreate()
    {

        $users = User::where('year', '=', 2019)
            ->where('role', '=', 1)
            ->where('permission', '=', 1)
            ->where('selection', '=', 1)
            ->get();
        //dd($users);
        return view("admin/episodecontrolcreate", compact('users'));
    }

    public function episodecontrolstore(Request $request)
    {
        //dd($request);
        $epiosodecontroll = new Quizepermission();
        $epiosodecontroll->user_id = $request->input('user_id');
        $epiosodecontroll->name = $request->input('name');
        $epiosodecontroll->round_id = $request->input('round_id');
        $epiosodecontroll->question_type = $request->input('question_type');
        $epiosodecontroll->status = $request->input('status');
        $epiosodecontroll->session = $request->input('session');
        $epiosodecontroll->save();

        return redirect('admin/episodecontrol');
    }

    public function episodecontroledit($id)
    {

        // $users = User::where('year', '=', 2019)
        // ->where('role', '=', 1)
        // ->where('permission', '=', 1)
        // ->where('selection', '=', 1)
        // ->get();
        //dd($users);
        $quize = Quizepermission::findOrFail($id);
        return view("admin/episodecontroledit", compact('quize'));
    }

    public function episodecontrolupdate(Request $request, $id)
    {
        //dd($request);
        $epiosodecontroll = new Quizepermission();
        $epiosodecontroll->name = $request->input('name');
        $epiosodecontroll->round_id = $request->input('round_id');
        $epiosodecontroll->question_type = $request->input('question_type');
        $epiosodecontroll->status = $request->input('status');
        $epiosodecontroll->session = $request->input('session');
        $epiosodecontroll->save();

        return redirect('admin/episodecontrol');
    }

    public function quistions()
    {

        $quistions = Question::orderBy('round_id')->orderBy('question_type')->get();
        //dd($quistions);
        return view('admin.quistions', compact('quistions'));
    }

    public function quistionsprint()
    {

        $quistions = Question::orderBy('round_id')->orderBy('question_type')->get();
        //dd($quistions);
        return view('admin.quistionsprint', compact('quistions'));
    }

    public function quistioncreate()
    {

        return view('admin.questionsreate');
    }

    public function quistionstore(Request $request)
    {

        //$files = $request->file('qus_media');
        //dd($files);
        // dd($files[0]->getClientOriginalName());
//dd($request);    
        $this->validate($request, [
            'round_id' => 'required'
        ]);


//dd($request);
        $data = array_filter($request->all());
        $round_id = $data['round_id'];
        $question_type = $data['question_type'];
        $episode_name = $data['episode_name'];
        $question_name = $data['question_name'];
        $op_1 = $data['op_1'];
        $op_2 = $data['op_2'];
        $op_3 = $data['op_3'];
        $op_4 = $data['op_4'];
        $qus_media = $data['qus_media'];
        $link = $data['link'];
        $time = $data['time'];
        $right_answer = $data['right_answer'];
        $points = $data['points'];
        $status = $data['status'];


        foreach ($round_id as $key => $input) {
            $questions = new Question();

            $questions->round_id = $round_id[$key];
            $questions->question_type = $question_type[$key];
            $questions->episode_name = $episode_name[$key];
            $questions->question_name = $question_name[$key];
            $questions->op_1 = $op_1[$key];
            $questions->op_2 = $op_2[$key];
            $questions->op_3 = $op_3[$key];
            $questions->op_4 = $op_4[$key];
            $questions->qus_media = $qus_media[$key];
            // if($request->hasFile('qus_media')){
            // $originalFiles = $request->file('qus_media');
            // $image = $originalFiles[$key];
            // $filename = time().'.'.$image->getClientOriginalExtension();
            // $destinationPath = public_path('/images');
            // $image->move($destinationPath, $filename);
            // $questions->qus_media = $filename;
            //  }
            $questions->link = $link[$key];
            $questions->time = $time[$key];
            $questions->right_answer = $right_answer[$key];
            $questions->points = $points[$key];
            $questions->status = $status[$key];

            $questions->save();
        }

        return redirect('/admin/quistions')->with('updated');
    }

    public function quistionedit($id)
    {

        $quistion = Question::findOrFail($id);
        //dd($quistion);
        return view('admin.quistionedit', compact('quistion'));
    }

    public function quistionupdate(Request $request, $id)
    {


        // dd($request);
        $test = Question::find($id);
        $qs = $test->qus_media;
        //dd($qs);


        $question = Question::findOrFail($id);
        $question->round_id = $request->input('round_id');
        $question->question_type = $request->input('question_type');
        $question->episode_name = $request->input('episode_name');
        $question->question_name = $request->input('question_name');
        $question->op_1 = $request->input('op_1');
        $question->op_2 = $request->input('op_2');
        $question->op_3 = $request->input('op_3');
        $question->op_4 = $request->input('op_4');
        $question->qus_media = $request->input('qus_media');
        $question->option_num = $request->input('option_num');
        $question->na_answer = $request->input('na_answer');


        if ($request->hasFile('qus_media')) {
            $originalFiles = $request->file('qus_media');
            $image = $originalFiles;
            $filename = time() . '.' . $image->getClientOriginalExtension();
            $destinationPath = public_path('/images');
            $image->move($destinationPath, $filename);
            $question->qus_media = $filename;
        } else {
            $question->qus_media = $qs;
        }

        $question->link = $request->input('link');
        $question->time = $request->input('time');
        $question->right_answer = $request->input('right_answer');
        $question->points = $request->input('points');


        $question->update();

        return redirect('admin/quistions');
    }

    public function statusupdate(Request $request, $id)
    {


        // dd($request);

        $question = Question::findOrFail($id);

        $question->status = $request->input('status');
        $question->update();

        return redirect('admin/quistions');
    }

    public function quistiondelete($id)
    {
        //dd($id);
        $question = Question::findOrFail($id);
        $question->delete();

        return redirect('admin/quistions');
    }

    public function singlequistionscreate()
    {

        return view('admin.singlequistioncreate');
    }

    public function singlequistionsstore(Request $request)
    {
        $question = new Question();
        $question->round_id = $request->input('round_id');
        $question->question_type = $request->input('question_type');
        $question->episode_name = $request->input('episode_name');
        $question->question_name = $request->input('question_name');
        $question->op_1 = $request->input('op_1');
        $question->op_2 = $request->input('op_2');
        $question->op_3 = $request->input('op_3');
        $question->op_4 = $request->input('op_4');


        $question->option_num = $request->input('option_num');
        $question->na_answer = $request->input('na_answer');
        $question->qus_media = $request->input('qus_media');

        if ($request->hasFile('qus_media')) {
            $originalFiles = $request->file('qus_media');
            $image = $originalFiles;
            $filename = time() . '.' . $image->getClientOriginalExtension();
            $destinationPath = public_path('/images');
            $image->move($destinationPath, $filename);
            $question->qus_media = $filename;
        }

        $question->link = $request->input('link');
        $question->time = $request->input('time');
        $question->right_answer = $request->input('right_answer');
        $question->points = $request->input('points');
        $question->status = $request->input('status');

        $question->save();


        return redirect('admin/quistions');
    }

    public function studentoftheyear()
    {


        Date::setLocale('bd');
        $date = Date::now()->format('l j F Y ');
        $sliders = slider::all();

        $history = DB::table('historys')->first();
        $video = DB::table('videos')->first();
        $contact = DB::table('contacts')->first();
        $schedule = DB::table('schedule')->first();
        $notices = notice::all();
        $forms = form::all();
        $elinks = elink::all();
        $implinks = implink::all();
        $galleries = DB::table('galleries')->inRandomOrder()->limit(4)->get();
        $speechs = DB::table('speechs')->limit(2)->get();
        $users = User::where('year', '=', 2019)
            ->where('role', '=', 1)
            ->get();

        return view('studentoftheyear')->with(['users' => $users, 'date' => $date, 'galleries' => $galleries, 'slider' => $sliders, 'contact' => $contact, 'schedule' => $schedule, 'history' => $history, 'speechs' => $speechs, 'video' => $video, 'notices' => $notices, 'forms' => $forms, 'elinks' => $elinks, 'implinks' => $implinks]);
    }

    public function history()
    {
        Date::setLocale('bd');
        $date = Date::now()->format('l j F Y ');
        $forms = form::all();
        $elinks = elink::all();
        $implinks = implink::all();
        $notices = notice::all();
        $history = DB::table('historys')->first();
        $schedule = DB::table('schedule')->first();
        $galleries = DB::table('galleries')->inRandomOrder()->limit(4)->get();
        $contact = DB::table('contacts')->first();
        return view('history')->with(['schedule' => $schedule, 'galleries' => $galleries, 'date' => $date, 'contact' => $contact, 'history' => $history, 'notices' => $notices, 'forms' => $forms, 'elinks' => $elinks, 'implinks' => $implinks]);
    }

    public function notice()
    {
        Date::setLocale('bd');
        $date = Date::now()->format('l j F Y ');
        $forms = form::all();
        $elinks = elink::all();
        $implinks = implink::all();
        $notices = notice::all();
        $schedule = DB::table('schedule')->first();
        $galleries = DB::table('galleries')->inRandomOrder()->limit(4)->get();
        $history = DB::table('historys')->first();
        $contact = DB::table('contacts')->first();
        return view('notice')->with(['schedule' => $schedule, 'galleries' => $galleries, 'date' => $date, 'contact' => $contact, 'history' => $history, 'notices' => $notices, 'forms' => $forms, 'elinks' => $elinks, 'implinks' => $implinks]);
    }

    public function single_notice($id)
    {
        Date::setLocale('bd');
        $date = Date::now()->format('l j F Y ');
        $forms = form::all();
        $elinks = elink::all();
        $implinks = implink::all();
        $single_notice = DB::table('notices')->where('id', '=', $id)->get();
        $schedule = DB::table('schedule')->first();
        $notices = notice::all();
        $galleries = DB::table('galleries')->inRandomOrder()->limit(4)->get();
        $history = DB::table('historys')->first();
        $contact = DB::table('contacts')->first();

        return view('single-notice')->with(['schedule' => $schedule, 'galleries' => $galleries, 'date' => $date, 'contact' => $contact, 'history' => $history, 'notices' => $notices, 'single_notice' => $single_notice, 'forms' => $forms, 'elinks' => $elinks, 'implinks' => $implinks]);
    }
    
    
    
    
    
    public function about()
{
    $history = DB::table('historys')->first(); // use first() if you need only one row
    $contact = DB::table('contacts')->first();

    return view('about')->with([
        'history' => $history,
        'contact' => $contact,
    ]);
}

    
    public function registration()
    {
        $events = Event::with('session')->get();
        $amount = PaymentAmount::first();
        $class_array = array('GroupA(3-5)'=>'GroupA(3-5)','GroupA(6-8)'=>'GroupA(6-8)', 'GroupC(9-12)'=>'GroupC(9-12)','GroupC(University)'=>'GroupC(University)');
        // ,'৬ষ্ঠ' => '৬ষ্ঠ', '৭ম' => '৭ম', '৮ম' => '৮ম', '৯ম' => '৯ম', '১০ম' => '১০ম');
        $contact = DB::table('contacts')->first();
        return view('registration')->with(['contact' => $contact, 'events' => $events, 'classes' => $class_array, 'amount' => $amount]);
    }
    
    public function studentRegistration(Request $request){
        $validator = Validator::make($request->all(), [
            'source' => 'nullable|string|max:255',
            'event' => 'nullable|string|max:255',
            'name' => 'required|string|max:255',
            'email' => 'required|email|unique:participants,email',
            'mobile' => 'required|string|max:20',
            'institute' => 'nullable|string|max:255',
            'class' => 'nullable|string|max:100',
            'password' => 'required|confirmed|min:6',
            'bkash_payment' => 'nullable|boolean',
        ]);
        
         if ($validator->fails()) {
            return redirect()->back()->withErrors($validator)->withInput();
        }
        
        $user = User::where('email', $request->email)->first();
        
        if(!$user){
            User::create([
                'source' => $request->source,
                'event_id' => (int)$request->event,
                'name' => $request->name,
                'email' => $request->email,
                'mobile' => $request->mobile,
                'institute' => $request->institute,
                'class' => $request->class,
                'password' => Hash::make($request->password),
                'role' => 1,
                'bkash_payment' => $request->bkash_payment,
            ]);
            
            return redirect()->back()->with('success', 'Registration successful!');
        }else{
            return redirect()->back()->with('error', 'Email already registered.');
        }
    }
    
    
    public function teams(Request $request)
    {
        $teams = DB::table('teams')->get();
        // if (!$request->filled(['email', 'team_id', 'event_id', 'division_id', 'district_id'])) {
        //     $mentors = Mentor::with('join')->paginate(12);
        // } else {
            $mentors = Mentor::query()
                ->with('join')
                ->where(function ($q) use ($request) {
                    if ($request->email) {
                        $q->where('email', 'like', "%{$request->email}%");
                    }
                    if ($request->team_id) {
                        $q->where('join_as', $request->team_id);
                    }
                    if ($request->event_id) {
                        $q->where('event_id', $request->event_id);
                    }
                    if ($request->division_id) {
                        $q->where('division_id', $request->division_id);
                    }
                    if ($request->district_id) {
                        $q->where('district_id', $request->district_id);
                    }
                })
                ->paginate(12);
        // }


        $joinAsTypes = JoinAsType::get();
        $divisions = Division::get();
        $events = Event::with('session')->get();
        $contact = DB::table('contacts')->first();
        return view('teams')->with(['contact' => $contact, 'teams' => $teams, 'mentors' => $mentors, 'joinAsTypes' => $joinAsTypes, 'divisions' => $divisions, 'events' => $events]);
    }
    
    public function getTeamDistricts($divisionId)
    {
        $districts = District::where('division_id', $divisionId)->get();
        return response()->json($districts);
    }
    
    public function blogs()
    {
        $blogs = Blogs::orderBy('id', 'desc')->paginate(8);
        $contact = DB::table('contacts')->first();
        return view('blogs')->with(['contact' => $contact, 'blogs' => $blogs]);
    }
    
    public function blogdetails($id)
    {
        $blogs = DB::table('blogs')->get();
        $blog = DB::table('blogs')->where('id', $id)->first();
        $contact = DB::table('contacts')->first();
        return view('blogdetails')->with(['contact' => $contact, 'blogs' => $blogs, 'blog' => $blog]);
    }
    
    public function events()
    {
        $events = Event::with('session')
                ->whereHas('session', function($query) {
                    $query->where('status', 'active');
                })
                ->get();
        // DB::table('events')->get();
        $contact = DB::table('contacts')->first();
        return view('events')->with(['contact' => $contact, 'events' => $events]);
    }
    
    public function eventdetails($id)
    {
        $event = Event::with('session')
                ->whereHas('session', function($query) {
                    $query->where('status', 'active');
                })->where('id', $id)->first();
        $contact = DB::table('contacts')->first();
        return view('eventdetails')->with(['contact' => $contact, 'event' => $event]);
    }
    
    public function joinsoty()
    {
        $events = DB::table('events')->get();
        $divisions = Division::get();
        $contact = DB::table('contacts')->first();
        $joinAsTypes = JoinAsType::with('details')->get();
        // $joinAsDetails = JoinAsDetails::all();
        return view('joinsoty')->with(['contact' => $contact,  'events' => $events,  'divisions' => $divisions, 'joinAsTypes' => $joinAsTypes]);
    }
    
    public function winners()
    {
        $winners = Winner::with('session')->orderBy('id', 'desc')->get()->groupBy('session_id');
        $contact = DB::table('contacts')->first();
        return view('winners')->with(['contact' => $contact, 'winners' => $winners]);
    }
    
    public function joinmentor()
    {
        // $events = Event::with('session')->get();
        $events = Event::with('session')
                ->whereHas('session', function($query) {
                    $query->where('status', 'active');
                })
                ->get();
        $divisions = Division::get();
        $joinAsTypes = JoinAsType::where('status', 1)->get();
        $contact = DB::table('contacts')->first();
        return view('joinmentor')->with(['contact' => $contact,  'events' => $events,  'divisions' => $divisions, 'joinAsTypes' => $joinAsTypes]);
    }
    
    public function storeJoinmentor(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'join_as' => 'required',
            'event' => 'required',
            'profile_image' => 'nullable||mimes:jpg,jpeg,png|max:1999',
            'full_name' => 'required|string|max:255',
            'email' => 'required|email|unique:mentors,email',
            'whatsapp_number' => 'required',
            'password' => 'required|confirmed|min:6',
            'division' => 'required',
            'district' => 'required',
            'upazila' => 'required',
            'village_area' => 'required',
            'institute' => 'required',
            'class_level_designation' => 'required',
            'institute_mobile' => 'required',
            'agree_terms' => 'accepted',
        ]);
    
        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator)->withInput();
        }

        $profileImage = null;
        if ($request->hasFile('profile_image')) {
            $profileImage = $request->file('profile_image')->store('profiles', 'public');
        }
        
        Mentor::create([
            'join_as' => $request->join_as,
            'event_id' => $request->event,
            'profile_image' => $profileImage,
            'full_name' => strtoupper($request->full_name),
            'email' => $request->email,
            'whatsapp_number' => $request->whatsapp_number,
            'telegram_number' => $request->telegram_number,
            'password' => Hash::make($request->password),
            'division_id' => $request->division,
            'district_id' => $request->district,
            'upazila_id' => $request->upazila,
            'village_area' => $request->village_area,
            'institute' => $request->institute,
            'class_level_designation' => $request->class_level_designation,
            'group_department_subject' => $request->group_department_subject,
            'institute_mobile' => $request->institute_mobile,
            'institute_email' => $request->institute_email,
            'institute_eiin_no' => $request->institute_eiin_no,
            'agree_terms' => $request->has('agree_terms'),
        ]);

        return redirect()->back()->with('success', 'Mentor registration successful!');
    }
    
    public function partners()
    {
        $partners = DB::table('partners')->get();
        $contact = DB::table('contacts')->first();
        return view('partners')->with(['contact' => $contact, 'partners' => $partners]);
    }
    
    public function faq()
    {
        $faqCategories = FaqCategory::with('faqs')->where('status', 1)->get();
        $faqs = DB::table('faqs')->get();
        $contact = DB::table('contacts')->first();
        return view('faq')->with(['contact' => $contact, 'faqCategories' => $faqCategories, 'faqs' => $faqs]);
    }
    
    public function importantlinks()
    {
        Date::setLocale('bd');
        $date = Date::now()->format('l j F Y ');
        $forms = form::all();
        $elinks = elink::all();
        $implinks = implink::all();
        $notices = notice::all();
        $schedule = DB::table('schedule')->first();
        $galleries = DB::table('galleries')->inRandomOrder()->limit(4)->get();
        $history = DB::table('historys')->first();
        $contact = DB::table('contacts')->first();
        return view('important-links')->with(['schedule' => $schedule, 'galleries' => $galleries, 'date' => $date, 'contact' => $contact, 'history' => $history, 'notices' => $notices, 'forms' => $forms, 'elinks' => $elinks, 'implinks' => $implinks]);
    }

    public function eshebalinks()
    {
        Date::setLocale('bd');
        $date = Date::now()->format('l j F Y ');
        $forms = form::all();
        $elinks = elink::all();
        $implinks = implink::all();
        $notices = notice::all();
        $schedule = DB::table('schedule')->first();
        $galleries = DB::table('galleries')->inRandomOrder()->limit(4)->get();
        $history = DB::table('historys')->first();
        $contact = DB::table('contacts')->first();
        return view('esheba-links')->with(['schedule' => $schedule, 'galleries' => $galleries, 'date' => $date, 'contact' => $contact, 'history' => $history, 'notices' => $notices, 'forms' => $forms, 'elinks' => $elinks, 'implinks' => $implinks]);
    }

    public function importantforms()
    {
        Date::setLocale('bd');
        $date = Date::now()->format('l j F Y ');
        $forms = form::all();
        $elinks = elink::all();
        $implinks = implink::all();
        $notices = notice::all();
        $schedule = DB::table('schedule')->first();
        $galleries = DB::table('galleries')->inRandomOrder()->limit(4)->get();
        $history = DB::table('historys')->first();
        $contact = DB::table('contacts')->first();
        return view('important-forms')->with(['schedule' => $schedule, 'galleries' => $galleries, 'date' => $date, 'contact' => $contact, 'history' => $history, 'notices' => $notices, 'forms' => $forms, 'elinks' => $elinks, 'implinks' => $implinks]);
    }

    public function admission_info()
    {
        Date::setLocale('bd');
        $date = Date::now()->format('l j F Y ');
        $forms = form::all();
        $elinks = elink::all();
        $implinks = implink::all();
        $notices = notice::all();
        $schedule = DB::table('schedule')->first();
        $galleries = DB::table('galleries')->inRandomOrder()->limit(4)->get();
        $admission = DB::table('admission_infos')->first();
        $contact = DB::table('contacts')->first();
        //$request->session()->flash('success', 'Admission Information  Added successfully!');
        return view('admission')->with(['schedule' => $schedule, 'galleries' => $galleries, 'contact' => $contact, 'date' => $date, 'admission' => $admission, 'notices' => $notices, 'forms' => $forms, 'elinks' => $elinks, 'implinks' => $implinks]);
    }

    public function gallery_view()
    {
        Date::setLocale('bd');
        $date = Date::now()->format('l j F Y ');
        $forms = form::all();
        $elinks = elink::all();
        $implinks = implink::all();
        $notices = notice::all();
        $galleries = gallery::all();
        $schedule = DB::table('schedule')->first();
        $contact = DB::table('contacts')->first();
        return view('gallery')->with(['schedule' => $schedule, 'galleries' => $galleries, 'contact' => $contact, 'date' => $date, 'galleries' => $galleries, 'notices' => $notices, 'forms' => $forms, 'elinks' => $elinks, 'implinks' => $implinks]);
    }

    public function routine_show()
    {
        Date::setLocale('bd');
        $date = Date::now()->format('l j F Y ');
        $forms = form::all();
        $elinks = elink::all();
        $implinks = implink::all();
        $notices = notice::all();
        $schedule = DB::table('schedule')->first();
        $galleries = DB::table('galleries')->inRandomOrder()->limit(4)->get();
        $routines = routine::all();
        $contact = DB::table('contacts')->first();
        return view('routine')->with(['schedule' => $schedule, 'routines' => $routines, 'contact' => $contact, 'galleries' => $galleries, 'date' => $date, 'galleries' => $galleries, 'notices' => $notices, 'forms' => $forms, 'elinks' => $elinks, 'implinks' => $implinks]);
    }

    public function result_show()
    {
        Date::setLocale('bd');
        $date = Date::now()->format('l j F Y ');
        $forms = form::all();
        $elinks = elink::all();
        $implinks = implink::all();
        $notices = notice::all();
        $schedule = DB::table('schedule')->first();
        $galleries = DB::table('galleries')->inRandomOrder()->limit(4)->get();
        $results = result::all();
        $contact = DB::table('contacts')->first();
        return view('result')->with(['schedule' => $schedule, 'results' => $results, 'contact' => $contact, 'galleries' => $galleries, 'date' => $date, 'galleries' => $galleries, 'notices' => $notices, 'forms' => $forms, 'elinks' => $elinks, 'implinks' => $implinks]);
    }

    public function contact()
    {
        Date::setLocale('bd');
        $date = Date::now()->format('l j F Y ');
        $forms = form::all();
        $elinks = elink::all();
        $implinks = implink::all();
        $notices = notice::all();
        $schedule = DB::table('schedule')->first();
        $galleries = DB::table('galleries')->inRandomOrder()->limit(4)->get();
        $contact = DB::table('contacts')->first();
        return view('contact')->with(['schedule' => $schedule, 'galleries' => $galleries, 'contact' => $contact, 'date' => $date, 'notices' => $notices, 'forms' => $forms, 'elinks' => $elinks, 'implinks' => $implinks]);
    }

//public  view  part end ADMIN  PART START

    public function index()
    {

        if (Auth::user()->role) {

            $userId = Auth::user()->id;

            //dd($userId);
            $type_one = Roundone::where('user_id', $userId)
                ->where('type_one', 1)
                ->select('type_one')
                ->first();
            //dd($type_one);

            $type_two = Roundone::where('user_id', $userId)
                ->where('type_two', 1)
                ->select('type_two')
                ->first();
            //dd($type_two);

            $type_three = Roundthree::where('user_id', $userId)
                ->where('access', 1)
                ->get();
            //dd($type_three);

            $question_typetext_r3 = Roundthree::where('user_id', $userId)
                ->where('access', 1)
                ->where('type_one', 1)
                ->select('type_one')
                ->get();
            //$question_typetext_r3 = count($question_typetext_r3);

            $question_typeimage_r3 = Roundthree::where('user_id', $userId)
                ->where('access', 1)
                ->where('type_two', 2)
                ->select('type_two')
                ->get();


            $type_four = Roundfour::where('user_id', $userId)
                ->where('access', 1)
                ->get();
            //dd($type_four);

            $question_typetext_r4 = Roundfour::where('user_id', $userId)
                ->where('access', 1)
                ->where('type_one', 1)
                ->select('type_one')
                ->get();

            //dd($question_typetext_r4);
            $question_typeimage_r4 = Roundfour::where('user_id', $userId)
                ->where('access', 1)
                ->where('type_two', 2)
                ->select('type_two')
                ->get();
            //dd($question_typeimage_r4);
            // $question_typetext_r4 = $type_four->type_one;
            // $question_typeimage_r4 = $type_four->type_two;
            // $question_typeaudio_r4 = $type_four->type_three;
            // $question_typevideo_r4 = $type_four->type_four;

            $type_five = Roundfive::where('user_id', $userId)
                ->where('access', 1)
                ->get();

            //dd($type_five);

            $question_typetext_r5 = Roundfive::where('user_id', $userId)
                ->where('access', 1)
                ->where('type_one', 1)
                ->select('type_one')
                ->get();
            //dd($question_typetext_r5);

            $question_typeimage_r5 = Roundfive::where('user_id', $userId)
                ->where('access', 1)
                ->where('type_two', 2)
                ->select('type_two')
                ->get();

            //dd($question_typeimage_r5);


            $type_six = Roundsix::where('user_id', $userId)
                ->where('access', 1)
                ->get();

            //dd($type_six);

            $question_typetext_r6 = Roundsix::where('user_id', $userId)
                ->where('access', 1)
                ->where('type_one', 1)
                ->select('type_one')
                ->get();
            //dd($question_typetext_r6);

            $question_typeimage_r6 = Roundsix::where('user_id', $userId)
                ->where('access', 1)
                ->where('type_two', 2)
                ->select('type_two')
                ->get();
            //dd($question_typeimage_r6);


            if ($type_one) {
                $quize = Question:: where('round_id', 2)
                    ->where('status', 1)
                    ->where('question_type', 1)
                    ->inRandomOrder()->limit(1)
                    ->first();

                //dd($quize);
                //  $marks = Mark::where('user_id', $userId)
                // ->orderBy('id', 'desc')
                // ->first();

                $marks = DB::table('marks')
                    ->join('questions', 'questions.id', '=', 'marks.question_id')
                    ->where('marks.user_id', $userId)
                    ->orderBy('marks.id', 'desc')
                    //->select('marks.id')
                    ->first();
                //dd($marks);

                return view('admin.studentdashboard', compact('quize', 'marks'));
            } elseif ($type_two) {
                $quize = Question:: where('round_id', 2)
                    ->where('status', 1)
                    ->where('question_type', 2)
                    ->inRandomOrder()->limit(1)
                    ->first();

                //  $marks = Mark::where('user_id', $userId)
                // ->orderBy('id', 'desc')
                // ->first();


                $marks = DB::table('marks')
                    ->join('questions', 'questions.id', '=', 'marks.question_id')
                    ->where('marks.user_id', $userId)
                    ->orderBy('marks.id', 'desc')
                    //->select('marks.id')
                    ->first();

                //dd($marks);

                return view('admin.studentdashboard', compact('quize', 'marks'));
            } elseif (count($type_three) > 0) {

                if (count($question_typetext_r3)) {
                    $quize = Question:: where('round_id', 3)
                        ->where('status', 1)
                        ->where('question_type', 1)
                        //->Orwhere('question_type', 2)
                        // ->Orwhere('question_type', $question_typeaudio_r3)
                        // ->Orwhere('question_type', $question_typevideo_r3)
                        //ekhane ekt aquery bosate hobe
                        ->inRandomOrder()->limit(1)
                        ->first();
                    //dd($quize);
                } elseif (count($question_typeimage_r3)) {
                    $quize = Question:: where('round_id', 3)
                        ->where('status', 1)
                        ->where('question_type', 2)
                        //->Orwhere('question_type', 2)
                        // ->Orwhere('question_type', $question_typeaudio_r3)
                        // ->Orwhere('question_type', $question_typevideo_r3)
                        //ekhane ekt aquery bosate hobe
                        ->inRandomOrder()->limit(1)
                        ->first();
                    //dd($quize);
                } else {

                    //return 3;
                    return view('admin.studentdashboarderror');
                }

                // $quize =  Question:: where('round_id', 3)
                // ->where('status', 1)
                // ->where('question_type', 1)
                // //->Orwhere('question_type', 2)
                // // ->Orwhere('question_type', $question_typeaudio_r3)
                // // ->Orwhere('question_type', $question_typevideo_r3)
                // //ekhane ekt aquery bosate hobe
                // ->inRandomOrder()->limit(1)
                // ->first();
                // dd($quize);
                // $marks = Mark::where('user_id', $userId)
                // ->orderBy('id', 'desc')
                // ->first();

                $marks = DB::table('marks')
                    ->join('questions', 'questions.id', '=', 'marks.question_id')
                    ->where('marks.user_id', $userId)
                    ->orderBy('marks.id', 'desc')
                    ->select('questions.question_name', 'questions.op_1', 'questions.op_2', 'questions.op_3', 'questions.op_4', 'questions.right_answer', 'marks.your_ans')
                    ->first();
                //dd($marks);

                return view('admin.studentdashboard', compact('quize', 'marks'));
            } elseif (count($type_four) > 0) {

                //dd('hello i am here 4');
                if (count($question_typetext_r4)) {
                    $quize = Question:: where('round_id', 4)
                        ->where('status', 1)
                        ->where('question_type', 1)
                        //->Orwhere('question_type', 2)
                        // ->Orwhere('question_type', $question_typeaudio_r3)
                        // ->Orwhere('question_type', $question_typevideo_r3)
                        //ekhane ekt aquery bosate hobe
                        ->inRandomOrder()->limit(1)
                        ->first();
                    //dd($quize);
                } elseif (count($question_typeimage_r4)) {
                    $quize = Question:: where('round_id', 4)
                        ->where('status', 1)
                        ->where('question_type', 2)
                        //->Orwhere('question_type', 2)
                        // ->Orwhere('question_type', $question_typeaudio_r3)
                        // ->Orwhere('question_type', $question_typevideo_r3)
                        //ekhane ekt aquery bosate hobe
                        ->inRandomOrder()->limit(1)
                        ->first();
                    //dd($quize);
                } else {
                    // return 4;
                    return view('admin.studentdashboarderror');
                }

                $marks = DB::table('marks')
                    ->join('questions', 'questions.id', '=', 'marks.question_id')
                    ->where('marks.user_id', $userId)
                    ->orderBy('marks.id', 'desc')
                    ->select('questions.question_name', 'questions.op_1', 'questions.op_2', 'questions.op_3', 'questions.op_4', 'questions.right_answer', 'marks.your_ans')
                    ->first();
                //dd($quize);

                return view('admin.studentdashboard', compact('quize', 'marks'));
            } elseif (count($type_five) > 0) {

                //dd('hello i am here on 5');
                if (count($question_typetext_r5)) {
                    $quize = Question:: where('round_id', 5)
                        ->where('status', 1)
                        ->where('question_type', 1)
                        //->Orwhere('question_type', 2)
                        // ->Orwhere('question_type', $question_typeaudio_r3)
                        // ->Orwhere('question_type', $question_typevideo_r3)
                        //ekhane ekt aquery bosate hobe
                        ->inRandomOrder()->limit(1)
                        ->first();
                    //dd($quize);
                } elseif (count($question_typeimage_r5)) {
                    $quize = Question:: where('round_id', 5)
                        ->where('status', 1)
                        ->where('question_type', 2)
                        //->Orwhere('question_type', 2)
                        // ->Orwhere('question_type', $question_typeaudio_r3)
                        // ->Orwhere('question_type', $question_typevideo_r3)
                        //ekhane ekt aquery bosate hobe
                        ->inRandomOrder()->limit(1)
                        ->first();
                    //dd($quize);
                } else {
                    // return 5;
                    return view('admin.studentdashboarderror');
                }

                $marks = DB::table('marks')
                    ->join('questions', 'questions.id', '=', 'marks.question_id')
                    ->where('marks.user_id', $userId)
                    ->orderBy('marks.id', 'desc')
                    ->select('questions.question_name', 'questions.op_1', 'questions.op_2', 'questions.op_3', 'questions.op_4', 'questions.right_answer', 'marks.your_ans')
                    ->first();
                //dd($quize);

                return view('admin.studentdashboard', compact('quize', 'marks'));
            } elseif (count($type_six) > 0) {

                //dd('hello i am here on 5');
                if (count($question_typetext_r6)) {
                    $quize = Question:: where('round_id', 6)
                        ->where('status', 1)
                        ->where('question_type', 1)
                        //->Orwhere('question_type', 2)
                        // ->Orwhere('question_type', $question_typeaudio_r3)
                        // ->Orwhere('question_type', $question_typevideo_r3)
                        //ekhane ekt aquery bosate hobe
//                            ->inRandomOrder()->limit(1)
                        ->limit(1)
                        ->first();
                    //dd($quize);
                } elseif (count($question_typeimage_r6)) {
                    $quize = Question:: where('round_id', 6)
                        ->where('status', 1)
                        ->where('question_type', 2)
                        //->Orwhere('question_type', 2)
                        // ->Orwhere('question_type', $question_typeaudio_r3)
                        // ->Orwhere('question_type', $question_typevideo_r3)
                        //ekhane ekt aquery bosate hobe
//                            ->inRandomOrder()->limit(1)
                        ->limit(1)
                        ->first();
                    //dd($quize);
                } else {
                    // return 5;
                    return view('admin.studentdashboarderror');
                }

                $marks = DB::table('marks')
                    ->join('questions', 'questions.id', '=', 'marks.question_id')
                    ->where('marks.user_id', $userId)
                    ->orderBy('marks.id', 'desc')
                    ->select('questions.question_name', 'questions.op_1', 'questions.op_2', 'questions.op_3', 'questions.op_4', 'questions.right_answer', 'marks.your_ans')
                    ->first();
                //dd($quize);

                return view('admin.studentdashboard', compact('quize', 'marks'));
            } else {
                //$quize = null;

                return view('admin.studentdashboarderror');
            }
        } else {
            return view('admin.dashboard');
        }
    }

    public function qizeanswerstore(Request $request, $id)
    {

        //dd($request);
        $user_answer = $request->input('your_ans');
        $right_ans_from_qst = $request->input('right_answer');
        //dd($right_ans_from_qst);

        $question = Question::findOrFail($id);
        $question->status = $request->input('status');
        $question->update();

        $roundoneans = new Mark();
        $roundoneans->user_id = $request->input('user_id');
        $roundoneans->round_id = $request->input('round_id');
        $roundoneans->marktype = $request->input('marktype');
        $roundoneans->question_id = $request->input('question_id');
        $roundoneans->your_ans = $request->input('your_ans');
        if ($user_answer === $right_ans_from_qst) {
            $roundoneans->marks_1 = $request->input('marks_1');
        } else {
            $roundoneans->marks_1 = 0;
        }

        if ($user_answer === $right_ans_from_qst) {
            $roundoneans->total = $request->input('marks_1');
        } else {
            $roundoneans->total = 0;
        }


        $roundoneans->save();


        return Redirect::back();
    }

    public function editprofile($id)
    {


        $user = User::findOrFail($id);

        return view('admin.profile', compact('user'));
    }

    public function updateprofile(Request $request, $id)
    {
        $this->validate($request, [
            'picture' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
        ]);

        $clients = User::find($id);
        $clients->picture = $request->input('picture');

        if ($request->hasFile('picture')) {
            $image = $request->file('picture');
            $filename = time() . '.' . $image->getClientOriginalExtension();
            $destinationPath = public_path('/images');
            $image->move($destinationPath, $filename);
            $clients->picture = $filename;
        }

        $clients->update();

        return redirect('/admin')->with('image updated');
    }

    public function perticipentlist()
    {

        $users = User::where('role', '=', 1)
            //->where('year', '=', 2019)
            ->orderBy('class', '=', 'DESC')
            ->paginate(15);
            
        return view('admin.perticipentlist', compact('users'));
    }

    public function registeredstudent(Request $request)
    {
        $session_array = array('2019' => '2019', '2020' => '2020', '2021' => '2021');
        $class_array = array('৬ষ্ঠ' => '৬ষ্ঠ', '৭ম' => '৭ম', '৮ম' => '৮ম', '৯ম' => '৯ম', '১০ম' => '১০ম');
        $category_array = array('রাজ টেক্সটাইল মাধ্যমিক বিদ্যালয়' => 'রাজ টেক্সটাইল মাধ্যমিক বিদ্যালয়', 'বর্ণমালা ই-স্কুল' => 'বর্ণমালা ই-স্কুল');
        $round_array = array('selection' => 'বাছাই', '1' => 'এক', '2' => 'দুই', '3' => 'তিন', '4' => 'চার', '5' => 'পাঁচ', '6' => 'ফাইনাল');
        $users = User::with('event')->where('role', '=', 1)
            ->orderBy('id', '=', 'DESC');
        $session = $request->input('session');
        if ($session != '') {
            $users = $users->where('session', $session);
        }
        $class = $request->input('class');
        if ($class != '') {
            $users = $users->where('class', $class);
        }
        $category = $request->input('category');
        if ($category != '') {
            $users = $users->where('category', $category);
        }
        $category = $request->input('category');
        if ($category != '') {
            $users = $users->where('category', $category);
        }
        $round = $request->input('round');
        if ($round == 'selection') {
            $round_sub_title = 'রবাছাইকৃত শিক্ষার্থীদের তথ্য - 2020';
        } elseif ($round == '1') {
            $round_sub_title = 'প্রথম রাউন্ডের শিক্ষার্থীদের তথ্য - 2020';
        } elseif ($round == '2') {
            $round_sub_title = 'দ্বিতীয় রাউন্ডের শিক্ষার্থীদের তথ্য - 2020';
        } elseif ($round == '3') {
            $round_sub_title = 'তৃতীয় রাউন্ডের শিক্ষার্থীদের তথ্য - 2020';
        } elseif ($round == '4') {
            $round_sub_title = 'চতুর্থ রাউন্ডের শিক্ষার্থীদের তথ্য - 2020';
        } elseif ($round == '5') {
            $round_sub_title = 'পঞ্চম রাউন্ডের শিক্ষার্থীদের তথ্য - 2020';
        } elseif ($round == '6') {
            $round_sub_title = 'ফাইনাল রাউন্ডের শিক্ষার্থীদের তথ্য - 2020';
        } else {
            $round_sub_title = 'রেজিস্টারকৃত শিক্ষার্থীদের তথ্য - 2020';
        }
        if ($round != '') {
            if ($round == 'selection') {
                $round_filed = 'selection';
            } else {
                $round_filed = 'round_' . $round;
            }
            $users = $users->where($round_filed, 1);
        }
        $users = $users->paginate(15);

        return view('admin.registeredstudent', compact(
            'users',
            'session',
            'class',
            'category',
            'round',
            'round_sub_title',
            'session_array',
            'class_array',
            'category_array',
            'round_array'
        ));
    }

    public function permission(Request $request, $id)
    {
        //dd($request);
        $permission = User::findOrFail($id);
        $permission->permission = $request->input('permission');
        $permission->update();


        // $permissionForround = new Quizepermission();
        // $permissionForround->user_id = $request->input('user_id'); 
        // $permissionForround->round_id = $request->input('round_id'); 
        // $permissionForround->status = $request->input('status');    
        // $permissionForround->save(); 

        return Redirect::back();
    }

    public function roundone(Request $request, $id)
    {

        //dd($request);
        $roundone = User::findOrFail($id);
        $roundone->round_1 = $request->input('round_1');
        $roundone->update();

        return Redirect::back();
    }

    public function roundtwo(Request $request, $id)
    {

        //dd($request);

        $roundtwo = User::findOrFail($id);
        $roundtwo->round_2 = $request->input('round_2');
        $roundtwo->update();

        return Redirect::back();
    }

    public function roundthree(Request $request, $id)
    {

        $roundthree = User::findOrFail($id);
        $roundthree->round_3 = $request->input('round_3');
        $roundthree->update();

        return Redirect::back();
    }

    public function roundfour(Request $request, $id)
    {

        $roundfour = User::findOrFail($id);
        $roundfour->round_4 = $request->input('round_4');
        $roundfour->update();

        return Redirect::back();
    }

    public function roundfive(Request $request, $id)
    {

        $roundfive = User::findOrFail($id);
        $roundfive->round_5 = $request->input('round_5');
        $roundfive->update();

        return Redirect::back();
    }

    public function roundsix(Request $request, $id)
    {

        $roundsix = User::findOrFail($id);
        $roundsix->round_6 = $request->input('round_6');
        $roundsix->update();

        return Redirect::back();
    }

    public function roundseven(Request $request, $id)
    {

        $roundseven = User::findOrFail($id);
        $roundseven->round_7 = $request->input('round_7');
        $roundseven->update();

        return Redirect::back();
    }

    public function selection(Request $request, $user_id)
    {

        //dd($request);
        $select = User::findOrFail($user_id);
        $select->selection = $request->input('selection');
        $select->update();


        $permissionForround = new quizepermission();
        $permissionForround->user_id = $request->input('user_id');
        $permissionForround->round_id = $request->input('round_id');
        $permissionForround->status = $request->input('status');
        $permissionForround->save();


        return Redirect::back();
    }

    public function meritlist()
    {


        //   $selections =    User::where('year', '=', 2019)
        //     ->where('role', '=', 1)
        //     ->where('permission', '=', 1)
        //     ->orderBy('class', 'ASC')
        //     ->get();

        $selections = DB::table('marks')
            ->leftjoin('users', 'users.id', '=', 'marks.user_id')
            ->where('marks.round_id', '=', '0')
            //->where('users.year', '=', 2019)
            ->where('users.permission', '=', 1)
            //->where('users.selection', '=', 1)
            ->orderBy('marks.total', 'DESC')
            ->get();

        //dd($selections);

        $firstRounds = DB::table('marks')
            ->leftjoin('users', 'users.id', '=', 'marks.user_id')
            ->select('users.id as user_id', 'users.name', 'users.class', 'users.roll', 'users.picture', 'users.selection', 'users.round_1', 'users.round_2', 'marks.id as id', 'marks.marks_1', 'marks.marks_2', 'marks.marks_3', 'marks.marks_4', 'marks.total')
            ->where('marks.round_id', '=', '1')
            ->where('users.year', '=', 2019)
            ->where('users.permission', '=', 1)
            ->where('users.selection', '=', 1)
            ->orderBy('marks.total', 'DESC')
            ->get();
        //dd($firstRounds);  

        $secondRounds = DB::table('users')
            ->join('marks', 'marks.user_id', '=', 'users.id')
            ->select(
                'users.id as user_id', 'users.name', 'users.class', 'users.roll', 'users.picture', 'users.selection', 'users.round_1', 'users.round_2', 'marks.id as id', 'marks.marks_1', 'marks.marks_2', 'marks.marks_3', 'marks.marks_4', (DB::raw('SUM(total) as total'))
            )
            ->where('marks.round_id', '=', '2')
            //->where('marks.marktype', '=', 'byplay')
            //->where('marks.marktype', '=', 'guest')
            ->groupBy('marks.user_id')
            //->orderBy('total', 'DESC')
            ->orderBy(DB::raw('SUM(total)'), 'desc')
            //->orderBy((DB::raw('SUM(total) as totalMark')), 'DESC')
            ->get();

        $thirdRounds = DB::table('users')
            ->join('marks', 'marks.user_id', '=', 'users.id')
            ->select(
                'users.id as user_id', 'users.name', 'users.class', 'users.roll', 'users.picture', 'users.selection', 'users.round_1', 'users.round_3', 'marks.id as id', 'marks.marks_1', 'marks.marks_2', 'marks.marks_3', 'marks.marks_4', (DB::raw('SUM(total) as total'))
            )
            ->where('marks.round_id', '=', '3')
            //->where('marks.marktype', '=', 'byplay')
            //->where('marks.marktype', '=', 'guest')
            ->groupBy('marks.user_id')
            //->orderBy('total', 'DESC')
            ->orderBy(DB::raw('SUM(total)'), 'desc')
            //->orderBy((DB::raw('SUM(total) as totalMark')), 'DESC')
            ->get();

        //dd($thirdRounds);

        $fourthRounds = DB::table('users')
            ->join('marks', 'marks.user_id', '=', 'users.id')
            ->select(
                'users.id as user_id', 'users.name', 'users.class', 'users.roll', 'users.picture', 'users.selection', 'users.round_1', 'users.round_3', 'marks.id as id', 'marks.marks_1', 'marks.marks_2', 'marks.marks_3', 'marks.marks_4', (DB::raw('SUM(total) as total'))
            )
            ->where('marks.round_id', '=', '4')
            //->where('marks.marktype', '=', 'byplay')
            //->where('marks.marktype', '=', 'guest')
            ->groupBy('marks.user_id')
            //->orderBy('total', 'DESC')
            ->orderBy(DB::raw('SUM(total)'), 'desc')
            //->orderBy((DB::raw('SUM(total) as totalMark')), 'DESC')
            ->get();

        $fiftRounds = DB::table('users')
            ->join('marks', 'marks.user_id', '=', 'users.id')
            ->select(
                'users.id as user_id', 'users.name', 'users.class', 'users.roll', 'users.picture', 'users.selection', 'users.round_1', 'users.round_3', 'marks.id as id', 'marks.marks_1', 'marks.marks_2', 'marks.marks_3', 'marks.marks_4', (DB::raw('SUM(total) as total'))
            )
            ->where('marks.round_id', '=', '5')
            //->where('marks.marktype', '=', 'byplay')
            //->where('marks.marktype', '=', 'guest')
            ->groupBy('marks.user_id')
            //->orderBy('total', 'DESC')
            ->orderBy(DB::raw('SUM(total)'), 'desc')
            //->orderBy((DB::raw('SUM(total) as totalMark')), 'DESC')
            ->get();

        $sixRounds = DB::table('users')
            ->join('marks', 'marks.user_id', '=', 'users.id')
            ->select(
                'users.id as user_id', 'users.name', 'users.class', 'users.roll', 'users.picture', 'users.selection', 'users.round_1', 'users.round_3', 'marks.id as id', 'marks.marks_1', 'marks.marks_2', 'marks.marks_3', 'marks.marks_4', (DB::raw('SUM(total) as total'))
            )
            ->where('marks.round_id', '=', '6')
            //->where('marks.marktype', '=', 'byplay')
            //->where('marks.marktype', '=', 'guest')
            ->groupBy('marks.user_id')
            //->orderBy('total', 'DESC')
            ->orderBy(DB::raw('SUM(total)'), 'desc')
            //->orderBy((DB::raw('SUM(total) as totalMark')), 'DESC')
            ->get();

        //dd($sixRounds);


        return view('admin.meritlist', compact('sixRounds', 'fiftRounds', 'secondRounds', 'firstRounds', 'selections', 'users', 'thirdRounds', 'fourthRounds'));
    }

    public function marks()
    {


        //   $selections =    User::where('year', '=', 2019)
        //     ->where('role', '=', 1)
        //     ->where('permission', '=', 1)
        //     ->orderBy('class', 'ASC')
        //     ->get();

        $selections = DB::table('marks')
            ->leftjoin('users', 'users.id', '=', 'marks.user_id')
            ->select('users.id as user_id', 'users.name', 'users.class', 'users.permission', 'users.roll', 'users.picture', 'users.selection', 'users.round_1', 'users.round_2', 'marks.id as id', 'marks.marks_1', 'marks.marks_2', 'marks.marks_3', 'marks.marks_4', 'marks.total')
            ->where('marks.round_id', '=', '0')
            //->where('users.year', '=', 2019)
            ->where('users.permission', '=', 1)
            //->where('users.selection', '=', 1)
            ->orderBy('marks.total', 'DESC')
            ->get();

        //dd($selections);

        $firstRounds = DB::table('marks')
            ->leftjoin('users', 'users.id', '=', 'marks.user_id')
            ->select('users.id as user_id', 'users.name', 'users.class', 'users.roll', 'users.picture', 'users.selection', 'users.round_1', 'users.round_2', 'marks.id as id', 'marks.marks_1', 'marks.marks_2', 'marks.marks_3', 'marks.marks_4', 'marks.total')
            ->where('marks.round_id', '=', '1')
            ->where('users.year', '=', 2019)
            ->where('users.permission', '=', 1)
            ->where('users.selection', '=', 1)
            ->orderBy('marks.total', 'DESC')
            ->get();
        //dd($firstRounds);  

        $secondRounds = DB::table('marks')
            ->leftjoin('users', 'users.id', '=', 'marks.user_id')
            ->select('users.id as user_id', 'users.name', 'users.class', 'users.roll', 'users.picture', 'users.selection', 'users.round_1', 'users.round_2', 'marks.id as id', 'marks.marks_1', 'marks.marks_2', 'marks.marks_3', 'marks.marks_4', 'marks.total')
            ->where('marks.round_id', '=', '2')
            // ->where('users.year', '=', 2019)
            ->where('marks.marktype', '=', 'guest')
            ->groupBy('marks.user_id')
            ->orderBy(DB::raw('SUM(total)'), 'desc')
            ->get();


        $thirdRounds = DB::table('marks')
            ->leftjoin('users', 'users.id', '=', 'marks.user_id')
            ->select('users.id as user_id', 'users.name', 'users.class', 'users.roll', 'users.picture', 'users.selection', 'users.round_1', 'users.round_2', 'marks.id as id', 'marks.marks_1', 'marks.marks_2', 'marks.marks_3', 'marks.marks_4', 'marks.total')
            ->where('marks.round_id', '=', '3')
            // ->where('users.year', '=', 2019)
            ->where('marks.marktype', '=', 'guest')
            ->groupBy('marks.user_id')
            ->orderBy(DB::raw('SUM(total)'), 'desc')
            ->get();


        $fourthRounds = DB::table('marks')
            ->leftjoin('users', 'users.id', '=', 'marks.user_id')
            ->select('users.id as user_id', 'users.name', 'users.class', 'users.roll', 'users.picture', 'users.selection', 'users.round_1', 'users.round_2', 'marks.id as id', 'marks.marks_1', 'marks.marks_2', 'marks.marks_3', 'marks.marks_4', 'marks.total')
            ->where('marks.round_id', '=', '4')
            // ->where('users.year', '=', 2019)
            ->where('marks.marktype', '=', 'guest')
            ->groupBy('marks.user_id')
            ->orderBy(DB::raw('SUM(total)'), 'desc')
            ->get();


        $fifthRounds = DB::table('marks')
            ->leftjoin('users', 'users.id', '=', 'marks.user_id')
            ->select('users.id as user_id', 'users.name', 'users.class', 'users.roll', 'users.picture', 'users.selection', 'users.round_1', 'users.round_2', 'marks.id as id', 'marks.marks_1', 'marks.marks_2', 'marks.marks_3', 'marks.marks_4', 'marks.total')
            ->where('marks.round_id', '=', '5')
            // ->where('users.year', '=', 2019)
            ->where('marks.marktype', '=', 'guest')
            ->groupBy('marks.user_id')
            ->orderBy(DB::raw('SUM(total)'), 'desc')
            ->get();

        $sixRounds = DB::table('marks')
            ->leftjoin('users', 'users.id', '=', 'marks.user_id')
            ->select('users.id as user_id', 'users.name', 'users.class', 'users.roll', 'users.picture', 'users.selection', 'users.round_1', 'users.round_2', 'marks.id as id', 'marks.marks_1', 'marks.marks_2', 'marks.marks_3', 'marks.marks_4', 'marks.total')
            ->where('marks.round_id', '=', '6')
            // ->where('users.year', '=', 2019)
            ->where('marks.marktype', '=', 'guest')
            ->groupBy('marks.user_id')
            ->orderBy(DB::raw('SUM(total)'), 'desc')
            ->get();
        //dd($sixRounds);


        return view('admin.marks', compact('sixRounds', 'fifthRounds', 'secondRounds', 'firstRounds', 'selections', 'users', 'thirdRounds', 'fourthRounds'));
    }

    public function markcreate()
    {

        $users = User::where('role', '=', 1)
            ->where('permission', '=', 1)
            ->get();

        $firstRoundParticipents = User::where('role', '=', 1)
            ->where('permission', '=', 1)
            ->where('selection', '=', 1)
            ->get();

        $secondRoundParticipents = User::where('role', '=', 1)
            ->where('permission', '=', 1)
            ->where('selection', '=', 1)
            ->where('round_1', '=', 1)
            ->get();

        $thirdRoundParticipents = User::where('role', '=', 1)
            ->where('permission', '=', 1)
            ->where('selection', '=', 1)
            ->where('round_2', '=', 1)
            ->get();


        $fourthRoundParticipents = User::where('role', '=', 1)
            ->where('permission', '=', 1)
            ->where('selection', '=', 1)
            ->where('round_4', '=', 1)
            ->get();


        $fifththRoundParticipents = User::where('role', '=', 1)
            ->where('permission', '=', 1)
            ->where('selection', '=', 1)
            ->where('round_5', '=', 1)
            ->get();

        $sixthRoundParticipents = User::where('role', '=', 1)
            ->where('permission', '=', 1)
            ->where('selection', '=', 1)
            ->where('round_6', '=', 1)
            ->get();

        //dd($sixthRoundParticipents);
        return view('admin.markcreate', compact('users', 'sixthRoundParticipents', 'fifththRoundParticipents', 'firstRoundParticipents', 'secondRoundParticipents', 'thirdRoundParticipents', 'fourthRoundParticipents'));
    }

    public function markstore(Request $request)
    {

        //dd($request);  
        $this->validate($request, [
            'round_id' => 'required'
        ]);

        $data = array_filter($request->all());
        $user_id = $data['user_id'];
        $round_id = $data['round_id'];
        $marktype = $data['marktype'];
        $marks_1 = $data['marks_1'];
        $marks_2 = $data['marks_2'];
        $marks_3 = $data['marks_3'];
        $marks_4 = $data['marks_4'];
        $season = $data['season'];


        foreach ($user_id as $key => $input) {
            $marks = new Mark();
            $marks->user_id = $user_id[$key];
            $marks->round_id = $round_id[$key];
            $marks->marktype = $marktype[$key];
            $marks->marks_1 = $marks_1[$key];
            $marks->marks_2 = $marks_2[$key];
            $marks->marks_3 = $marks_3[$key];
            $marks->marks_4 = $marks_4[$key];
            $marks->total = ($marks_1[$key] + $marks_2[$key] + $marks_3[$key] + $marks_4[$key]) / 4;
            $marks->season = $season[$key];
            $marks->save();
        }


        return redirect('/admin/meritlist')->with('updated');
    }

    public function guestmarkstore(Request $request)
    {

        //dd($request);  
        $this->validate($request, [
            'round_id' => 'required'
        ]);

        $data = array_filter($request->all());
        $user_id = $data['user_id'];
        $round_id = $data['round_id'];
        $marktype = $data['marktype'];
        $marks_1 = $data['marks_1'];
        $marks_2 = $data['marks_2'];
        $marks_3 = $data['marks_3'];
        $marks_4 = $data['marks_4'];
        $season = $data['season'];


        foreach ($user_id as $key => $input) {
            $marks = new Mark();
            $marks->user_id = $user_id[$key];
            $marks->round_id = $round_id[$key];
            $marks->marktype = $marktype[$key];
            $marks->marks_1 = $marks_1[$key];
            $marks->marks_2 = $marks_2[$key];
            $marks->marks_3 = $marks_3[$key];
            $marks->marks_4 = $marks_4[$key];
//            $marks->total = ($marks_1[$key] + $marks_2[$key] + $marks_3[$key] + $marks_4[$key]) / 4;
            $marks->total = $marks_1[$key] + $marks_2[$key] + $marks_3[$key] + $marks_4[$key];
            $marks->season = $season[$key];
            $marks->save();
        }


        return redirect('/admin/meritlist')->with('updated');
    }

    public function marksedit($id)
    {

        // $marksUpdate = DB::table('marks')
        // ->leftjoin('users', 'users.id', '=', 'marks.user_id')
        // ->select('users.id as user_id', 'users.name', 'marks.id as id', 'marks.marks_1', 'marks.marks_2', 'marks.marks_3', 'marks.marks_3','marks.marks_4', 'marks.season','marks.round_id')
        // ->where('marks.round_id', '=', '0')
        // ->where('users.role', '=', 1)
        // //->where('marks.user_id', '=', $id)
        // ->first();

        $marksUpdate = Mark::findOrFail($id);
        $usrId = $marksUpdate->user_id;

        $user = DB::table('users')
            ->where('users.id', '=', $usrId)
            ->first();

        //dd($user);
        // $users        = User::where('role', '=', 1)->get(); 

        return view('admin.markedit', compact('marksUpdate', 'user'));
    }

    public function marksupdate(Request $request, $id)
    {


        //dd($id);
        $marksUpdate = Mark::find($id);
        $marksUpdate->user_id = $request->input('user_id');
        $marksUpdate->round_id = $request->input('round_id');
        $marksUpdate->marks_1 = $request->input('marks_1');
        $marksUpdate->marks_2 = $request->input('marks_2');
        $marksUpdate->marks_3 = $request->input('marks_3');
        $marksUpdate->marks_4 = $request->input('marks_4');
        $marksUpdate->total = ($request->input('marks_1') + $request->input('marks_2') + $request->input('marks_3') + $request->input('marks_4')) / 4;
        $marksUpdate->season = $request->input('season');

        $marksUpdate->update();

        return redirect('admin/marks');
    }

    public function marksdelete($id)
    {

        $marksdelete = Mark::findOrFail($id);

        $marksdelete->delete();

        return redirect('admin/meritlist');
    }

    public function perticipentProfile($id)
    {

        $user = User::findOrFail($id);

        return view('admin.perticipentprofile', compact('user'));
    }

    public function school_history()
    {

        $history = DB::table('historys')->first();
        return view('admin.history')->with(['history' => $history]);
    }

    public function updatehistory(Request $request)
    {
        $data = $request->aboutus;
        DB::table('historys')->where('id', 1)->update(['aboutus' => $data]);
        $history = DB::table('historys')->first();
        $request->session()->flash('success', 'School History  Updated successfully!');
        return redirect('admin/history')->with(['history' => $history]);
    }

    public function speech()
    {
        $speech = DB::table('speechs')->first();
        return view('admin.speech')->with(['speech' => $speech]);
    }

    public function updatespeech(Request $request) 
    { 
        $data = $request->speech; 
        // $data = $request->image; 
        DB::table('speechs')->where('id', 2)->update(['speech' => $data]); 
        $history = DB::table('speechs')->first(); $request->session()->flash('success', 'Updated successfully!'); 
        return redirect('admin/speech')->with(['speech' => $history]); 
        
    }
        


    //for  video
    public function videos()
    {
        $video = DB::table('videos')->first();

        return view('admin.video')->with(['video' => $video]);
    }

    public function update_video(Request $request)
    {
        $data = $request->url;
        DB::table('videos')->where('id', 1)->update(['url' => $data]);
        $request->session()->flash('success', 'Video Url  Updated successfully!');
        return redirect('admin/videos');
    }

    public function schedule()
    {
        $schedule = DB::table('schedule')->first();
        return view('admin.schedule')->with(['schedule' => $schedule]);
    }

    public function update_schedule(Request $request)
    {
        $saturday = $request->saturday;
        $sunday = $request->sunday;
        $monday = $request->monday;
        $tuesday = $request->tuesday;
        $wednesday = $request->wednesday;
        $thursday = $request->thursday;
        $friday = $request->friday;

        DB::table('schedule')->where('id', 1)->update(['saturday' => $saturday, 'sunday' => $sunday, 'monday' => $monday, 'tuesday' => $tuesday, 'wednesday' => $wednesday, 'thursday' => $thursday, 'friday' => $friday]);
        $request->session()->flash('success', 'Schedule  Updated successfully!');
        return redirect('admin/schedule');
    }

    public function admisssioninfo()
    {

        $description = DB::table('admission_infos')->first();

        return view('admin.admissioninfo')->with(['description' => $description]);
    }

    public function updateadmisssioninfo(Request $request)
    {
        $data = $request->description;
        DB::table('admission_infos')->where('id', 1)->update(['description' => $data]);

        $description = DB::table('admission_infos')->first();
        $request->session()->flash('success', 'Admission Information Updated successfully!');

        return redirect('admin/admissioninfo');
    }

    //elinks  code
    public function elinks()
    {
        return view('admin.elink');
    }

    public function create_elink(Request $request)
    {
        $title = $request->title;
        $url = $request->url;
        $elinks = new elink();
        $elinks->title = $title;
        $elinks->url = $url;
        $elinks->save();
        $request->session()->flash('success', 'New Elink Added successfully!');
        return redirect('admin/elinks');
    }

    public function elinklist()
    {
        $elinks = elink::all();
        return view('admin.elinkslist', ['elinks' => $elinks]);
    }

    //implinks  code
    public function implinks()
    {
        return view('admin.implink');
    }

    public function create_implink(Request $request)
    {
        $title = $request->title;
        $url = $request->url;
        $implinks = new implink();
        $implinks->title = $title;
        $implinks->url = $url;
        $implinks->save();
        $request->session()->flash('success', 'New Link Added successfully!');
        return redirect('admin/implinks');
    }

    public function implinklist()
    {
        $implinks = implink::all();
        return view('admin.implinkslist', ['implinks' => $implinks]);
    }

    //forms code

    public function forms()
    {
        return view('admin.form');
    }

    public function add_form(Request $request)
    {
        $title = $request->title;
        $image = $request->file('file');

        $name = time() . rand(1, 100000) . '.' . $image->getClientOriginalExtension();
        $image->move('formfiles', $name);


        // $name = time().'.'.$image->getClientOriginalExtension();
        // $destinationPath = public_path('/formfiles');
        // $image->move($destinationPath, $name);
        $forms = new form();
        $forms->title = $title;
        $forms->file = $name;
        $forms->save();
        $request->session()->flash('success', ' New Form  added successfully!');
        return redirect('admin/forms');
    }

    public function formlist()
    {

        $forms = form::all();
        return view('admin.formslist', ['forms' => $forms]);
    }

    //Notices code


    public function routines()
    {
        return view('admin.routine');
    }

    public function create_routine(Request $request)
    {
        $routines = new routine();
        $class = $request->class;
        $image = $request->file('file');
        if ($image != '') {
            //$name = time().'.'.$image->getClientOriginalExtension();
            //$destinationPath = public_path('/routinefiles');
            //$image->move($destinationPath, $name);
            $name = time() . rand(1, 100000) . '.' . $image->getClientOriginalExtension();
            $image->move('routinefiles', $name);

            $routines->file = $name;
        }
        $routines->class = $class;
        $routines->save();
        $request->session()->flash('success', 'New Routine  added successfully!');
        return redirect('admin/routines');
    }

    public function add_result()
    {
        return view('admin.result');
    }

    public function create_result(Request $request)
    {
        $results = new result();
        $class = $request->class;
        $image = $request->file('file');
        if ($image != '') {
            $name = time() . rand(1, 100000) . '.' . $image->getClientOriginalExtension();
            $image->move('resultfiles', $name);

            //$name = time().'.'.$image->getClientOriginalExtension();
            //$destinationPath = public_path('/resultfiles');
            //$image->move($destinationPath, $name);
            $results->file = $name;
        }
        $results->class = $class;
        $results->save();
        $request->session()->flash('success', ' Results  added successfully!');
        return redirect('admin/results');
    }

    public function resultlist()
    {
        $results = result::all();
        return view('admin.resultlist', ['results' => $results]);
    }

    public function notices()
    {
        return view('admin.notice');
    }

    public function create_notice(Request $request)
    {
        $notices = new notice();
        $title = $request->title;
        $description = $request->description;
        if (strlen($description) < 1) {
            $description = '';
        }
        $image = $request->file('file');

        if ($image != '') {
            $name = time() . rand(1, 100000) . '.' . $image->getClientOriginalExtension();
            $image->move('noticefiles', $name);


            //$name = time().'.'.$image->getClientOriginalExtension();
            //$destinationPath = public_path('/noticefiles');
            //$image->move($destinationPath, $name);
            $notices->file = $name;
        }

        $notices->title = $title;
        $notices->description = $description;
        $notices->save();
        $request->session()->flash('success', ' Notice  added successfully!');
        return redirect('admin/notices');
    }

    public function noticelist()
    {
        $notices = notice::all();
        return view('admin.noticelist', ['notices' => $notices]);
    }

    public function edit_notice($id)
    {
        $notice_byid = DB::table('notices')->where('id', '=', $id)->get();
        return view('admin.edit-notice', ['notice_byid' => $notice_byid]);
    }

    public function edit_slider($id)
    {
        $sliderby_ID = DB::table('sliders')->where('id', '=', $id)->get();
        return view('admin.edit-slider', ['sliderby_ID' => $sliderby_ID]);
    }

    public function update_slider(Request $request)
    {
        $slider = slider::find($request->id);
        $image = $request->file('file');
        if ($image != '') {
            $slider->caption_title = $request->caption_title;
            $slider->caption_description = $request->caption_description;
            $name = time() . rand(1, 100000) . '.' . $image->getClientOriginalExtension();
            $image->move('sliderphoto', $name);
            $slider->image = $name;
            $slider->save();
        } else {
            $slider->caption_title = $request->caption_title;
            $slider->caption_description = $request->caption_description;

            $slider->save();
        }

        $request->session()->flash('success', ' Slider has Updated successfully!');
        return redirect('admin/sliderlist');
    }

    public function update_notice(Request $request)
    {
        $notice = notice::find($request->id);
        $image = $request->file('file');
        if ($image != '') {
            $notice->title = $request->title;
            $notice->description = $request->description;


            $name = time() . rand(1, 100000) . '.' . $image->getClientOriginalExtension();
            $image->move('noticefiles', $name);
            $notice->file = $name;
            $notice->save();
        } else {
            $notice->title = $request->title;
            $notice->description = $request->description;

            $notice->save();
        }

        $request->session()->flash('success', ' Notice has Updated successfully!');
        return redirect('admin/notices');
    }

    public function delete_notice($id)
    {
        $notice = notice::find($id);
        $notice->delete();
        //session()->flash('warning', 'Notice  has deleted successfully!');
        return redirect('admin/noticelist');
    }

    //gallery code

    public function gallerys()
    {
        return view('admin.gallery');
    }

    public function create_gallery(Request $request)
    {
        $gallery_name = $request->gallery_name;
        $images = array();

        if ($files = $request->file('file')) {
            foreach ($files as $file) {
                $name = time() . rand(1, 100000) . '.' . $file->getClientOriginalExtension();
                $file->move('galleryphoto', $name);

                $images[] = $name;
                $gallery = new gallery();
                $gallery->gallery_name = $gallery_name;
                $gallery->image = $name;
                $gallery->save();
                return redirect('admin/gallerys');
            }
        }
    }

    public function gallerylist()
    {
        $gallerys = gallery::all();
        return view('admin.gallerylist', ['gallerys' => $gallerys]);
    }

    //slider code

    public function sliders()
    {
        return view('admin.slider');
    }

    public function create_slider(Request $request)
    {
        $caption_title = $request->caption_title;
        $caption_description = $request->caption_description;
        $image = $request->file('image');

        $name = time() . rand(1, 100000) . '.' . $image->getClientOriginalExtension();
        $image->move('sliderphoto', $name);

        //$name = time().'.'.$image->getClientOriginalExtension();
        //$destinationPath = public_path('/sliderphoto');
        //$image->move($destinationPath, $name);
        $sliders = new slider();
        $sliders->caption_title = $caption_title;
        $sliders->caption_description = $caption_description;
        $sliders->image = $name;
        $sliders->save();
        $request->session()->flash('success', ' New Slider Added successfully!');
        return redirect('admin/sliders');
    }

    public function sliderlist()
    {
        $sliders = slider::all();
        return view('admin.sliderlist', ['sliders' => $sliders]);
    }

    //contact  info


    public function contact_info()
    {
        $contact = DB::table('contacts')->first();
        return view('admin.contact')->with(['contact' => $contact]);
    }

    public function update_contact(Request $request)
    {


        $phone = new contact();
        $phone = $request->phone;
        $phone_two = $request->phone_two;
        $email = $request->email;
        $website = $request->website;
        $address = $request->address;
        $facebook = $request->facebook;
        $twitter = $request->twitter;
        $ytube = $request->ytube;
        DB::table('contacts')->where('id', 1)->update(['phone' => $phone, 'phone_two' => $phone_two, 'email' => $email, 'website' => $website, 'address' => $address, 'facebook' => $facebook, 'twitter' => $twitter, 'ytube' => $ytube,]);
        $contact = DB::table('contacts')->first();

        $request->session()->flash('success', ' Contact  Information Updated successfully!');
        return redirect('admin/contact')->with(['contact' => $contact]);
    }

    public function delete_gallery($id)
    {
        $gallery = gallery::find($id);
        $gallery->delete();
        return redirect('admin/gallerylist');
    }

    public function delete_slider($id)
    {
        $slider = slider::find($id);
        $slider->delete();
        return redirect('admin/sliderlist');
    }

    public function delete_form($id)
    {
        $form = form::find($id);
        $form->delete();
        return redirect('admin/formlist');
    }

    public function delete_result($id)
    {
        $result = result::find($id);
        $result->delete();
        return redirect('admin/resultlist');
    }

    public function delete_implink($id)
    {
        $implink = implink::find($id);
        $implink->delete();
        return redirect('admin/implinklist');
    }

    public function delete_elink($id)
    {
        $elink = elink::find($id);
        $elink->delete();
        return redirect('admin/elinklist');
    }

    //Student of the year all Logic
    public function allepisode()
    {

        $allepisodes = Episode::orderBy('round', 'ASC')->get();
        //dd($allepisodes);

        return view('admin/episode', compact('allepisodes'));
    }

    public function episodecreate()
    {

        return view('admin/episodecreate');
    }

    public function episodestore(Request $request)
    {

        $this->validate($request, [
            'round' => 'required'
        ]);


        $episodes = new Episode();
        $episodes->round = $request->input('round');
        $episodes->section_one = $request->input('section_one');
        $episodes->section_two = $request->input('section_two');
        $episodes->number = $request->input('number');
        $episodes->image_episode = $request->input('image_episode');

        if ($request->hasFile('image_episode')) {
            $image = $request->file('image_episode');
            $filename = time() . '.' . $image->getClientOriginalExtension();
            $upload_path = public_path('/images');
            $success = $image->move($upload_path, $filename);
            $episodes->image_episode = $filename;
        }

        $episodes->save();

        return redirect('admin/allepisode');
    }

    public function episodeshow($id)
    {

        $episode = Episode::findOrFail($id);

        $episodeId = $episode->round;
        //dd($episodeId);
        $guests = Guest::where('round_id', $episodeId)->get();
        $specialguests = Specialguest::where('round_id', $episodeId)->get();
        $judges = Judge::where('round_id', $episodeId)->get();

        if ($episodeId == 0) {

            $users = DB::table('users')
                ->where('users.permission', '=', '1')
                ->orderBy('id', 'ASC')
                ->get();

            $compitionMarks = DB::table('users')
                ->join('marks', 'marks.user_id', '=', 'users.id')
                ->select('marks.id as id', 'users.id as user_id', 'users.permission', 'users.group_name', 'users.selection', 'users.name', 'users.class', 'users.roll', 'marks.total as totalMark', 'users.picture', 'marks.marks_1', 'marks.marks_2', 'marks.marks_3', 'marks.marks_4', 'marks.season', 'marks.round_id')
                ->where('marks.round_id', '=', '0')
                ->where('users.permission', '=', '1')
                ->orderBy('total', 'DESC')
                ->get();

            $episodeMarks = DB::table('users')
                ->join('marks', 'marks.user_id', '=', 'users.id')
                ->select('marks.id as id', 'users.id as user_id', 'users.permission', 'users.group_name', 'users.selection', 'users.name', 'users.class', 'users.roll', 'marks.total as totalMark', 'users.picture', 'marks.marks_1', 'marks.marks_2', 'marks.marks_3', 'marks.marks_4', 'marks.season', 'marks.round_id')
                ->where('marks.round_id', '=', '0')
                ->where('users.selection', '=', '0')
                ->orderBy('total', 'DESC')
                ->get();


            //dd($compitionMarks);
            // $compitionMarks = DB::table('users')
            // ->join('marks', 'marks.user_id', '=', 'users.id')
            // ->select(
            // 'users.name', 'users.class', 'users.roll', 'users.picture',
            // (DB::raw('SUM(total) as totalMark'))
            // )
            // ->where('marks.round_id', '=', '0')
            // //->where('marks.marktype', '=', 'byplay')
            // //->groupBy('marks.user_id')
            // ->orderBy('total', 'DESC')
            // ->get();

            $guestmarks = DB::table('users')
                ->join('marks', 'marks.user_id', '=', 'users.id')
                ->select(
                    'users.name', 'users.class', 'users.roll', 'users.picture', (DB::raw('SUM(total) as totalMark'))
                )
                ->where('marks.round_id', '=', '0')
                ->where('marks.marktype', '=', 'guest')
                ->groupBy('marks.user_id')
                ->orderBy('total', 'DESC')
                ->get();

            //dd($episodeMarks);

            $studentOftheDay = DB::table('users')
                ->join('marks', 'marks.user_id', '=', 'users.id')
                ->select('marks.id as id', 'users.id as user_id', 'users.permission', 'users.selection', 'users.name', 'users.class', 'users.roll', 'marks.total as totalMark', 'users.picture', 'marks.marks_1', 'marks.marks_2', 'marks.marks_3', 'marks.marks_4', 'marks.season', 'marks.round_id')
                ->where('marks.round_id', '=', '0')
                ->where('users.selection', '=', '1')
                ->orderBy('total', 'DESC')
                ->first();

            $audiences = DB::table('audiences')
                ->where('round_id', '=', '0')
                ->get();

            $winners = DB::table('winneraudiences')
                ->where('round_id', '=', '0')
                ->get();

            $marksDists = DB::table('questiondistributions')
                ->where('round_id', '=', '0')
                ->get();
            $audiencesQuestions = Audience::where('round_id', '=', 0)->get();
            $specialmeoemnts = Specialmoment::where('round_id', '=', 0)->get();
        } else if ($episodeId == 1) {
            // $users = DB::table('users')
            // ->join('marks', 'marks.user_id', '=', 'users.id')
            // ->select('marks.id as id', 'users.id as user_id', 'users.permission', 'users.selection', 
            // 'users.name', 'users.class', 'users.roll', 'marks.total', 'users.picture','marks.marks_1', 
            // 'marks.marks_2', 'marks.marks_3', 'marks.marks_4', 'marks.season','marks.round_id')
            // ->where('marks.round_id', '=', '1')
            // ->where('users.selection', '=', '1')
            // ->orderBy('total', 'DESC')
            // ->get(); 

            $users = DB::table('users')
                ->where('users.selection', '=', '1')
                ->orderBy('id', 'ASC')
                ->get();

            $episodeMarks = DB::table('users')
                ->join('marks', 'marks.user_id', '=', 'users.id')
                ->select('marks.id as id', 'users.id as user_id', 'users.permission', 'users.group_name', 'users.selection', 'users.name', 'users.class', 'users.roll', 'marks.total as totalMark', 'users.picture', 'marks.marks_1', 'marks.marks_2', 'marks.marks_3', 'marks.marks_4', 'marks.season', 'marks.round_id')
                ->where('marks.round_id', '=', '1')
                ->where('users.selection', '=', '1')
                ->orderBy('total', 'DESC')
                ->get();

            //dd($users);
            // $users = User::where('selection', '=', 1)->get();
            //dd($users);

            $studentOftheDay = DB::table('users')
                ->join('marks', 'marks.user_id', '=', 'users.id')
                ->select('marks.id as id', 'users.id as user_id', 'users.permission', 'users.selection', 'users.name', 'users.class', 'users.roll', 'marks.total as totalMark', 'users.picture', 'marks.marks_1', 'marks.marks_2', 'marks.marks_3', 'marks.marks_4', 'marks.season', 'marks.round_id')
                ->where('marks.round_id', '=', '1')
                ->where('users.selection', '=', '1')
                ->orderBy('total', 'DESC')
                ->first();

            //dd($studentOftheDay);
            $marksDists = DB::table('questiondistributions')
                ->where('round_id', '=', '1')
                ->get();

            $audiences = DB::table('audiences')
                ->where('round_id', '=', '1')
                ->get();

            $winners = DB::table('winneraudiences')
                ->where('round_id', '=', '0')
                ->get();
            // dd($audiences);

            $compitionMarks = DB::table('users')
                ->join('marks', 'marks.user_id', '=', 'users.id')
                ->select(
                    'users.name', 'users.class', 'users.roll', 'users.picture', (DB::raw('SUM(total) as totalMark'))
                )
                ->where('marks.round_id', '=', '1')
                ->where('marks.marktype', '=', 'byplay')
                ->groupBy('marks.user_id')
                ->orderBy('total', 'DESC')
                ->get();

            $guestmarks = DB::table('users')
                ->join('marks', 'marks.user_id', '=', 'users.id')
                ->select(
                    'users.name', 'users.class', 'users.roll', 'users.picture', (DB::raw('SUM(total) as totalMark'))
                )
                ->where('marks.round_id', '=', '1')
                ->where('marks.marktype', '=', 'guest')
                ->groupBy('marks.user_id')
                ->orderBy('total', 'DESC')
                ->get();

            $specialmeoemnts = Specialmoment::where('round_id', '=', 1)->get();

            $audiencesQuestions = Audience::where('round_id', '=', 0)->get();
        } else if ($episodeId == 2) {
            // $users = DB::table('users')
            // ->join('marks', 'marks.user_id', '=', 'users.id')
            // ->select('marks.id as id', 'users.id as user_id', 'users.permission', 'users.selection', 
            // 'users.name', 'users.class', 'users.roll', 'marks.total', 'users.picture','marks.marks_1', 
            // 'marks.marks_2', 'marks.marks_3', 'marks.marks_4', 'marks.season','marks.round_id')
            // ->where('marks.round_id', '=', '2')
            // ->where('users.round_1', '=', '1')
            // ->orderBy('total', 'DESC')
            // ->get(); 

            $users = DB::table('users')
                ->where('users.round_1', '=', '1')
                ->orderBy('id', 'ASC')
                ->get();

            $episodeMarks = DB::table('users')
                ->join('marks', 'marks.user_id', '=', 'users.id')
                ->select(
                    'users.id', 'users.name', 'users.class', 'users.roll', 'users.picture', (DB::raw('SUM(total) as totalMark'))
                )
                ->where('marks.round_id', '=', '2')
                //->where('marks.marktype', '=', 'byplay')
                //->where('marks.marktype', '=', 'guest')
                ->groupBy('marks.user_id')
                //->orderBy('total', 'DESC')
                ->orderBy(DB::raw('SUM(total)'), 'desc')
                //->orderBy((DB::raw('SUM(total) as totalMark')), 'DESC')
                ->get();
            //dd($episodeMarks);

            $compitionMarks = DB::table('users')
                ->join('marks', 'marks.user_id', '=', 'users.id')
                ->select(
                    'users.name', 'users.class', 'users.roll', 'users.picture', (DB::raw('SUM(total) as totalMark'))
                )
                ->where('marks.round_id', '=', '2')
                ->where('marks.marktype', '=', 'byplay')
                ->groupBy('marks.user_id')
                ->orderBy('total', 'DESC')
                ->get();


            $guestmarks = DB::table('users')
                ->join('marks', 'marks.user_id', '=', 'users.id')
                ->select(
                    'users.name', 'users.class', 'users.roll', 'users.picture', (DB::raw('SUM(total) as totalMark'))
                )
                ->where('marks.round_id', '=', '2')
                ->where('marks.marktype', '=', 'guest')
                ->groupBy('marks.user_id')
                ->orderBy('total', 'DESC')
                ->get();

            //dd($guestmarks);

            $marksDists = DB::table('questiondistributions')
                ->where('round_id', '=', '2')
                ->get();

            $winners = DB::table('winneraudiences')
                ->where('round_id', '=', '1')
                ->get();

            $audiences = DB::table('audiences')
                ->where('round_id', '=', '2')
                ->get();

            //  $studentOftheDay = DB::table('users')
            // ->join('marks', 'marks.user_id', '=', 'users.id')
            // ->select('marks.id as id', 'users.id as user_id', 'users.permission', 'users.selection', 
            // 'users.name', 'users.class', 'users.roll', 'marks.total', 'users.picture','marks.marks_1', 
            // 'marks.marks_2', 'marks.marks_3', 'marks.marks_4', 'marks.season','marks.round_id')
            // ->where('marks.round_id', '=', '2')
            // ->where('marks.marktype', '=', 'byplay')
            // ->where('marks.marktype', '=', 'guest')
            // //->where('users.selection', '=', '1')
            // ->orderBy(DB::raw('SUM(total)'), 'desc')
            // ->first(); 

            $studentOftheDay = DB::table('users')
                ->join('marks', 'marks.user_id', '=', 'users.id')
                ->select(
                    'users.id', 'users.name', 'users.class', 'users.roll', 'users.picture', (DB::raw('SUM(total) as totalMark'))
                )
                ->where('marks.round_id', '=', '2')
                //->where('marks.marktype', '=', 'byplay')
                //->where('marks.marktype', '=', 'guest')
                ->groupBy('marks.user_id')
                //->orderBy('total', 'DESC')
                ->orderBy(DB::raw('SUM(total)'), 'desc')
                //->orderBy((DB::raw('SUM(total) as totalMark')), 'DESC')
                ->first();


            //$users = User::where('round_1', '=', 1)->get(); 
            $specialmeoemnts = Specialmoment::where('round_id', '=', 2)->get();

            $audiencesQuestions = Audience::where('round_id', '=', 1)->get();
        } else if ($episodeId == 3) {
            //$users = User::where('round_2', '=', 1)->get(); 
            $audiencesQuestions = Audience::where('round_id', '=', 2)->get();

            // $users = DB::table('users')
            // ->join('marks', 'marks.user_id', '=', 'users.id')
            // ->select('marks.id as id', 'users.id as user_id', 'users.permission', 'users.selection', 'users.name', 'users.class', 'users.roll', 'marks.total', 'users.picture','marks.marks_1', 'marks.marks_2', 'marks.marks_3', 'marks.marks_4', 'marks.season','marks.round_id')
            // ->where('marks.round_id', '=', '2')
            // ->where('users.round_2', '=', '1')
            // ->orderBy('total', 'DESC')
            // ->get(); 
            // $episodeMarks = DB::table('users')
            // ->join('marks', 'marks.user_id', '=', 'users.id')
            // ->select('marks.id as id', 'users.id as user_id', 'users.permission', 'users.group_name','users.selection', 'users.name', 'users.class', 'users.roll', 'marks.total', 'users.picture','marks.marks_1', 'marks.marks_2', 'marks.marks_3', 'marks.marks_4', 'marks.season','marks.round_id')
            // ->where('marks.round_id', '=', '3')
            // ->where('users.round_2', '=', '1')
            // ->orderBy('total', 'DESC')
            // ->get();
            $guestmarks = DB::table('users')
                ->join('marks', 'marks.user_id', '=', 'users.id')
                ->select(
                    'users.name', 'users.class', 'users.roll', 'users.picture', (DB::raw('SUM(total) as totalMark'))
                )
                ->where('marks.round_id', '=', '3')
                ->where('marks.marktype', '=', 'guest')
                ->groupBy('marks.user_id')
                ->orderBy('total', 'DESC')
                ->get();

            //dd($guestmarks);

            $compitionMarks = DB::table('users')
                ->join('marks', 'marks.user_id', '=', 'users.id')
                ->select(
                    'users.name', 'users.class', 'users.roll', 'users.picture', (DB::raw('SUM(total) as totalMark'))
                )
                ->where('marks.round_id', '=', '3')
                ->where('marks.marktype', '=', 'byplay')
                ->groupBy('marks.user_id')
                ->orderBy('total', 'DESC')
                ->get();
            //dd($compitionMarks);


            $users = DB::table('users')
                ->where('users.round_2', '=', '1')
                ->orderBy('id', 'ASC')
                ->get();

            //dd($users);

            $marksDists = DB::table('questiondistributions')
                ->where('round_id', '=', '3')
                ->get();

            $winners = DB::table('winneraudiences')
                ->where('round_id', '=', '3')
                ->get();
            $audiences = DB::table('audiences')
                ->where('round_id', '=', '3')
                ->get();
            $episodeMarks = DB::table('users')
                ->join('marks', 'marks.user_id', '=', 'users.id')
                ->select(
                    'users.id', 'users.name', 'users.class', 'users.roll', 'users.picture', (DB::raw('SUM(total) as totalMark'))
                )
                ->where('marks.round_id', '=', '3')
                //->where('marks.marktype', '=', 'byplay')
                //->where('marks.marktype', '=', 'guest')
                ->groupBy('marks.user_id')
                //->orderBy('total', 'DESC')
                ->orderBy(DB::raw('SUM(total)'), 'desc')
                //->orderBy((DB::raw('SUM(total) as totalMark')), 'DESC')
                ->get();

            //dd($episodeMarks);

            $studentOftheDay = DB::table('users')
                ->join('marks', 'marks.user_id', '=', 'users.id')
                ->select(
                    'users.id', 'users.name', 'users.class', 'users.roll', 'users.picture', (DB::raw('SUM(total) as totalMark'))
                )
                ->where('marks.round_id', '=', '3')
                //->where('marks.marktype', '=', 'byplay')
                //->where('marks.marktype', '=', 'guest')
                ->groupBy('marks.user_id')
                //->orderBy('total', 'DESC')
                ->orderBy(DB::raw('SUM(total)'), 'desc')
                //->orderBy((DB::raw('SUM(total) as totalMark')), 'DESC')
                ->first();
            //dd($studentOftheDay);
            $specialmeoemnts = Specialmoment::where('round_id', '=', 3)->get();
        } else if ($episodeId == 4) {


            $users = DB::table('users')
                ->where('users.round_4', '=', '1')
                ->orderBy('id', 'ASC')
                ->get();
            //dd($users);
            $audiencesQuestions = Audience::where('round_id', '=', 3)->get();

            $marksDists = DB::table('questiondistributions')
                ->where('round_id', '=', '4')
                ->get();

            $winners = DB::table('winneraudiences')
                ->where('round_id', '=', '3')
                ->get();

            $audiences = DB::table('audiences')
                ->where('round_id', '=', '4')
                ->get();

            $episodeMarks = DB::table('users')
                ->join('marks', 'marks.user_id', '=', 'users.id')
                ->select(
                    'users.id', 'users.name', 'users.class', 'users.roll', 'users.picture', (DB::raw('SUM(total) as totalMark'))
                )
                ->where('marks.round_id', '=', '4')
                //->where('marks.marktype', '=', 'byplay')
                //->where('marks.marktype', '=', 'guest')
                ->groupBy('marks.user_id')
                //->orderBy('total', 'DESC')
                ->orderBy(DB::raw('SUM(total)'), 'desc')
                //->orderBy((DB::raw('SUM(total) as totalMark')), 'DESC')
                ->get();
            //dd($episodeMarks);

            $studentOftheDay = DB::table('users')
                ->join('marks', 'marks.user_id', '=', 'users.id')
                ->select(
                    'users.id', 'users.name', 'users.class', 'users.roll', 'users.picture', (DB::raw('SUM(total) as totalMark'))
                )
                ->where('marks.round_id', '=', '4')
                //->where('marks.marktype', '=', 'byplay')
                //->where('marks.marktype', '=', 'guest')
                ->groupBy('marks.user_id')
                //->orderBy('total', 'DESC')
                ->orderBy(DB::raw('SUM(total)'), 'desc')
                //->orderBy((DB::raw('SUM(total) as totalMark')), 'DESC')
                ->first();

            //dd($studentOftheDay);

            $compitionMarks = DB::table('users')
                ->join('marks', 'marks.user_id', '=', 'users.id')
                ->select(
                    'users.name', 'users.class', 'users.roll', 'users.picture', (DB::raw('SUM(total) as totalMark'))
                )
                ->where('marks.round_id', '=', '4')
                ->where('marks.marktype', '=', 'byplay')
                ->groupBy('marks.user_id')
                ->orderBy('total', 'DESC')
                ->get();

            $guestmarks = DB::table('users')
                ->join('marks', 'marks.user_id', '=', 'users.id')
                ->select(
                    'users.name', 'users.class', 'users.roll', 'users.picture', (DB::raw('SUM(total) as totalMark'))
                )
                ->where('marks.round_id', '=', '4')
                ->where('marks.marktype', '=', 'guest')
                ->groupBy('marks.user_id')
                ->orderBy('total', 'DESC')
                ->get();

            $specialmeoemnts = Specialmoment::where('round_id', '=', 4)->get();
        } else if ($episodeId == 5) {
            // $users = DB::table('users')
            // ->join('marks', 'marks.user_id', '=', 'users.id')
            // ->select('marks.id as id', 'users.id as user_id', 'users.permission', 'users.selection', 'users.name', 'users.class', 'users.roll', 'marks.total', 'users.picture','marks.marks_1', 'marks.marks_2', 'marks.marks_3', 'marks.marks_4', 'marks.season','marks.round_id')
            // ->where('marks.round_id', '=', '4')
            // ->where('users.round_4', '=', '1')
            // ->orderBy('total', 'DESC')
            // ->get(); 

            $episodeMarks = DB::table('users')
                ->join('marks', 'marks.user_id', '=', 'users.id')
                ->select(
                    'users.id', 'users.name', 'users.class', 'users.roll', 'users.picture', (DB::raw('SUM(total) as totalMark'))
                )
                ->where('marks.round_id', '=', '5')
                //->where('marks.marktype', '=', 'byplay')
                //->where('marks.marktype', '=', 'guest')
                ->groupBy('marks.user_id')
                //->orderBy('total', 'DESC')
                ->orderBy(DB::raw('SUM(total)'), 'desc')
                //->orderBy((DB::raw('SUM(total) as totalMark')), 'DESC')
                ->get();
            //dd($episodeMarks);

            $users = DB::table('users')
                ->where('users.round_5', '=', '1')
                ->orderBy('id', 'ASC')
                ->get();

            //dd($users);
            $audiencesQuestions = Audience::where('round_id', '=', 4)->get();

            $marksDists = DB::table('questiondistributions')
                ->where('round_id', '=', '5')
                ->get();

            $winners = DB::table('winneraudiences')
                ->where('round_id', '=', '5')
                ->get();

            $audiences = DB::table('audiences')
                ->where('round_id', '=', '5')
                ->get();

            $studentOftheDay = DB::table('users')
                ->join('marks', 'marks.user_id', '=', 'users.id')
                ->select(
                    'users.id', 'users.name', 'users.class', 'users.roll', 'users.picture', (DB::raw('SUM(total) as totalMark'))
                )
                ->where('marks.round_id', '=', '5')
                //->where('marks.marktype', '=', 'byplay')
                //->where('marks.marktype', '=', 'guest')
                ->groupBy('marks.user_id')
                //->orderBy('total', 'DESC')
                ->orderBy(DB::raw('SUM(total)'), 'desc')
                //->orderBy((DB::raw('SUM(total) as totalMark')), 'DESC')
                ->first();

            $compitionMarks = DB::table('users')
                ->join('marks', 'marks.user_id', '=', 'users.id')
                ->select(
                    'users.name', 'users.class', 'users.roll', 'users.picture', (DB::raw('SUM(total) as totalMark'))
                )
                ->where('marks.round_id', '=', '5')
                ->where('marks.marktype', '=', 'byplay')
                ->groupBy('marks.user_id')
                ->orderBy('total', 'DESC')
                ->get();

            $guestmarks = DB::table('users')
                ->join('marks', 'marks.user_id', '=', 'users.id')
                ->select(
                    'users.name', 'users.class', 'users.roll', 'users.picture', (DB::raw('SUM(total) as totalMark'))
                )
                ->where('marks.round_id', '=', '5')
                ->where('marks.marktype', '=', 'guest')
                ->groupBy('marks.user_id')
                ->orderBy('total', 'DESC')
                ->get();

            //$users = User::where('round_4', '=', 1)->get(); 
            $specialmeoemnts = Specialmoment::where('round_id', '=', 5)->get();
        } else {

            $users = DB::table('users')
                ->where('users.round_6', '=', '1')
                ->orderBy('id', 'ASC')
                ->get();
            //dd($users);
            $audiencesQuestions = Audience::where('round_id', '=', 5)->get();

            $marksDists = DB::table('questiondistributions')
                ->where('round_id', '=', '6')
                ->get();

            $winners = DB::table('winneraudiences')
                ->where('round_id', '=', '6')
                ->get();

            $audiences = DB::table('audiences')
                ->where('round_id', '=', '6')
                ->get();

            $episodeMarks = DB::table('users')
                ->join('marks', 'marks.user_id', '=', 'users.id')
                ->select(
                    'users.id', 'users.name', 'users.class', 'users.roll', 'users.picture', (DB::raw('SUM(total) as totalMark'))
                )
                ->where('marks.round_id', '=', '6')
                //->where('marks.marktype', '=', 'byplay')
                //->where('marks.marktype', '=', 'guest')
                ->groupBy('marks.user_id')
                //->orderBy('total', 'DESC')
                ->orderBy(DB::raw('SUM(total)'), 'desc')
                //->orderBy((DB::raw('SUM(total) as totalMark')), 'DESC')
                ->get();
            //dd($episodeMarks);

            $studentOftheDay = DB::table('users')
                ->join('marks', 'marks.user_id', '=', 'users.id')
                ->select(
                    'users.id', 'users.name', 'users.class', 'users.roll', 'users.picture', (DB::raw('SUM(total) as totalMark'))
                )
                ->where('marks.round_id', '=', '6')
                //->where('marks.marktype', '=', 'byplay')
                //->where('marks.marktype', '=', 'guest')
                ->groupBy('marks.user_id')
                //->orderBy('total', 'DESC')
                ->orderBy(DB::raw('SUM(total)'), 'desc')
                //->orderBy((DB::raw('SUM(total) as totalMark')), 'DESC')
                ->first();

            //dd($studentOftheDay);

            $compitionMarks = DB::table('users')
                ->join('marks', 'marks.user_id', '=', 'users.id')
                ->select(
                    'users.name', 'users.class', 'users.roll', 'users.picture', (DB::raw('SUM(total) as totalMark'))
                )
                ->where('marks.round_id', '=', '6')
                ->where('marks.marktype', '=', 'byplay')
                ->groupBy('marks.user_id')
                ->orderBy('total', 'DESC')
                ->get();

            //dd($compitionMarks);
            $guestmarks = DB::table('users')
                ->join('marks', 'marks.user_id', '=', 'users.id')
                ->select(
                    'users.name', 'users.class', 'users.roll', 'users.picture', (DB::raw('SUM(total) as totalMark'))
                )
                ->where('marks.round_id', '=', '6')
                ->where('marks.marktype', '=', 'guest')
                ->groupBy('marks.user_id')
                ->orderBy('total', 'DESC')
                ->get();

            // dd($guestmarks);
            $specialmeoemnts = Specialmoment::where('round_id', '=', 6)->get();
        }
        // dd($users);
        return view('admin.episodeshow', compact('guestmarks', 'compitionMarks', 'episodeMarks', 'audiencesQuestions', 'marksDists', 'winners', 'studentOftheDay', 'episode', 'guests', 'specialguests', 'judges', 'users', 'specialmeoemnts', 'audiences'));
    }

    public function episodeedit($id)
    {

        $episode = Episode::findOrFail($id);

        return view('admin/episodeedit', compact('episode'));
    }

    public function episodeupdate(Request $request, $id)
    {


        $episode = Episode::findOrFail($id);
        $episode->round = $request->input('round');
        $episode->section_one = $request->input('section_one');
        $episode->section_two = $request->input('section_two');
        $episode->number = $request->input('number');
        $episode->image_episode = $request->input('image_episode');

        if ($request->hasFile('image_episode')) {
            $image = $request->file('image_episode');
            $filename = time() . '.' . $image->getClientOriginalExtension();
            $upload_path = public_path('/images');
            $success = $image->move($upload_path, $filename);
            $episode->image_episode = $filename;
        }

        $episode->update();

        return redirect('admin/allepisode');
    }

//Guest

    public function guest()
    {

        $guests = Guest::all();

        return view('admin.guestlist', compact('guests'));
    }

    public function guestcreate()
    {

        return view('admin.guestcreate');
    }

    public function gueststore(Request $request)
    {

        $this->validate($request, [
            'round_id' => 'required'
        ]);

        $guests = new Guest();
        $guests->round_id = $request->input('round_id');
        $guests->guest_name = $request->input('guest_name');
        $guests->guest_designation = $request->input('guest_designation');
        $guests->guest_address = $request->input('guest_address');
        $guests->guest_image = $request->input('guest_image');

        if ($request->hasFile('guest_image')) {
            $image = $request->file('guest_image');
            $filename = time() . '.' . $image->getClientOriginalExtension();
            $upload_path = public_path('/images');
            $success = $image->move($upload_path, $filename);
            $guests->guest_image = $filename;
        }

        $guests->save();

        return redirect('admin/allepisode');
    }

    public function guestedit($id)
    {

        $guestedit = Guest::find($id);

        return view('admin/guestedit', compact('guestedit'));
    }

    public function guestupdate(Request $request, $id)
    {


        $guests = Guest::findOrFail($id);
        $guests->round_id = $request->input('round_id');
        $guests->guest_name = $request->input('guest_name');
        $guests->guest_designation = $request->input('guest_designation');
        $guests->guest_address = $request->input('guest_address');
        $guests->guest_image = $request->input('guest_image');

        if ($request->hasFile('guest_image')) {
            $image = $request->file('guest_image');
            $filename = time() . '.' . $image->getClientOriginalExtension();
            $upload_path = public_path('/images');
            $success = $image->move($upload_path, $filename);
            $guests->guest_image = $filename;
        }

        $guests->update();

        return redirect('admin/guest');
    }

    public function guestdelete($id)
    {

        $guestedit = Guest::find($id);
        $guestedit->delete();

        return redirect('admin/guest');
    }

    public function specialguest()
    {

        $specialguests = Specialguest::all();
        return view('admin.specialguest', compact('specialguests'));
    }

    public function specialguestcretae()
    {

        return view('admin.specialguestcretae');
    }

    public function specialgueststore(Request $request)
    {

        $this->validate($request, [
            'round_id' => 'required'
        ]);

        $special_guests = new Specialguest();
        $special_guests->round_id = $request->input('round_id');
        $special_guests->specialguest_name = $request->input('specialguest_name');
        $special_guests->specialguest_designation = $request->input('specialguest_designation');
        $special_guests->specialguest_address = $request->input('specialguest_address');
        $special_guests->specialguest_image = $request->input('specialguest_image');

        if ($request->hasFile('specialguest_image')) {
            $image = $request->file('specialguest_image');
            $filename = time() . '.' . $image->getClientOriginalExtension();
            $upload_path = public_path('/images');
            $success = $image->move($upload_path, $filename);
            $special_guests->specialguest_image = $filename;
        }

        $special_guests->save();

        return redirect('admin/specialguest');
    }

    public function specialguestedit($id)
    {
        $specialguestEdit = Specialguest::findOrFail($id);

        return view('admin.specialguestedit', compact('specialguestEdit'));
    }

    public function specialguestupdate(Request $request, $id)
    {

        $this->validate($request, [
            'round_id' => 'required'
        ]);

        $special_guests = Specialguest::find($id);
        $special_guests->round_id = $request->input('round_id');
        $special_guests->specialguest_name = $request->input('specialguest_name');
        $special_guests->specialguest_designation = $request->input('specialguest_designation');
        $special_guests->specialguest_address = $request->input('specialguest_address');
        $special_guests->specialguest_image = $request->input('specialguest_image');

        if ($request->hasFile('specialguest_image')) {
            $image = $request->file('specialguest_image');
            $filename = time() . '.' . $image->getClientOriginalExtension();
            $upload_path = public_path('/images');
            $success = $image->move($upload_path, $filename);
            $special_guests->specialguest_image = $filename;
        }

        $special_guests->update();

        return redirect('admin/specialguest');
    }

    public function specialguestdelete($id)
    {
        $specialguestdelete = Specialguest::find($id);

        $specialguestdelete->delete();

        return redirect('admin/specialguest');
    }

    public function judge()
    {

        $judges = Judge::all();

        return view('admin.judges', compact('judges'));
    }

    public function judgecretae()
    {

        return view('admin.judgecretae');
    }

    public function whysoty()
    {

        Date::setLocale('bd');
        $date = Date::now()->format('l j F Y ');
        $forms = form::all();
        $elinks = elink::all();
        $implinks = implink::all();
        $notices = notice::all();
        $history = DB::table('historys')->first();
        $description = DB::table('admission_infos')->first();
        $schedule = DB::table('schedule')->first();
        $galleries = DB::table('galleries')->inRandomOrder()->limit(4)->get();
        $contact = DB::table('contacts')->first();
        return view('whysoty')->with(['schedule' => $schedule, 'galleries' => $galleries, 'date' => $date, 'contact' => $contact, 'history' => $history, 'description' => $description, 'notices' => $notices, 'forms' => $forms, 'elinks' => $elinks, 'implinks' => $implinks]);
    }

    public function judgetstore(Request $request)
    {
        //dd($request);
        $this->validate($request, [
            'round_id' => 'required'
        ]);

        $judges = new Judge();
        $judges->round_id = $request->input('round_id');
        $judges->judge_name = $request->input('judge_name');
        $judges->judge_designation = $request->input('judge_designation');
        $judges->judge_address = $request->input('judge_address');
        $judges->judge_image = $request->input('judge_image');

        if ($request->hasFile('judge_image')) {
            $image = $request->file('judge_image');
            $filename = time() . '.' . $image->getClientOriginalExtension();
            $upload_path = public_path('/images');
            $success = $image->move($upload_path, $filename);
            $judges->judge_image = $filename;
        }

        $judges->save();

        return redirect('admin/judge');
    }

    public function judgeedit($id)
    {

        $judgeedit = Judge::find($id);
        //dd($judgeedit);
        return view('admin.judgeedit', compact('judgeedit'));
        //return view('admin.specialguestedit', compact('specialguestEdit'));
    }

    public function judgeupdate(Request $request, $id)
    {
        //dd($request);
        $this->validate($request, [
            'round_id' => 'required'
        ]);

        $judges = Judge::findOrFail($id);
        $judges->round_id = $request->input('round_id');
        $judges->judge_name = $request->input('judge_name');
        $judges->judge_designation = $request->input('judge_designation');
        $judges->judge_address = $request->input('judge_address');
        $judges->judge_image = $request->input('judge_image');

        if ($request->hasFile('judge_image')) {
            $image = $request->file('judge_image');
            $filename = time() . '.' . $image->getClientOriginalExtension();
            $upload_path = public_path('/images');
            $success = $image->move($upload_path, $filename);
            $judges->judge_image = $filename;
        }

        $judges->update();

        return redirect('admin/judge');
    }

    public function judgedelete($id)
    {

        $judgeDelete = Judge::find($id);

        $judgeDelete->delete();

        return redirect('admin/judge');
    }

    public function specialmoment()
    {

        $specialmoments = Specialmoment::all();
        return view('admin/specialmoment', compact('specialmoments'));
    }

    public function specialmomentcreate()
    {

        return view('admin/specialmomentcreate');
    }

    public function specialmomentstore(Request $request)
    {
        //dd($request);
        $this->validate($request, [
            'round_id' => 'required'
        ]);

        $judges = new Specialmoment();
        $judges->round_id = $request->input('round_id');
        $judges->captions = $request->input('captions');
        $judges->picture = $request->input('picture');

        if ($request->hasFile('picture')) {
            $image = $request->file('picture');
            $filename = time() . '.' . $image->getClientOriginalExtension();
            $upload_path = public_path('/images');
            $success = $image->move($upload_path, $filename);
            $judges->picture = $filename;
        }

        $judges->save();

        return redirect('admin/specialmoment');
    }

    public function specialmomentdelete($id)
    {

        $judgeDelete = Specialmoment::find($id);

        $judgeDelete->delete();

        return redirect('admin/specialmoment');
    }

    public function audiencequestion()
    {

        $audiences = Audience::all();
        return view('admin/audiences', compact('audiences'));
    }

    public function audiencequestioncreate()
    {

        return view('admin/audiencequestioncreate');
    }

    public function audiencequestionstore(Request $request)
    {
        //dd($request);
        $this->validate($request, [
            'round_id' => 'required'
        ]);

        $audience = new Audience();
        $audience->round_id = $request->input('round_id');
        $audience->question = $request->input('question');
        $audience->right_answer = $request->input('right_answer');
        $audience->picture = $request->input('picture');

        if ($request->hasFile('picture')) {
            $image = $request->file('picture');
            $filename = time() . '.' . $image->getClientOriginalExtension();
            $upload_path = public_path('/images');
            $success = $image->move($upload_path, $filename);
            $audience->picture = $filename;
        }

        $audience->save();

        return redirect('admin/audiencequestion');
    }

    public function audiencequestiondelete($id)
    {

        $audience = Audience::find($id);

        $audience->delete();

        return redirect('admin/audiencequestion');
    }

    public function winneraudience()
    {

        $winners = Winneraudience::all();
        return view('admin/winneraudience', compact('winners'));
    }

    public function winneraudiencecreate()
    {

        return view('admin/winneraudiencecreate');
    }

    public function winneraudiencestore(Request $request)
    {
        //dd($request);
        $this->validate($request, [
            'round_id' => 'required'
        ]);

        $winner = new Winneraudience();
        $winner->round_id = $request->input('round_id');
        $winner->winner_name = $request->input('winner_name');
        $winner->winner_class = $request->input('winner_class');
        $winner->winner_roll = $request->input('winner_roll');
        $winner->picture = $request->input('picture');

        if ($request->hasFile('picture')) {
            $image = $request->file('picture');
            $filename = time() . '.' . $image->getClientOriginalExtension();
            $upload_path = public_path('/images');
            $success = $image->move($upload_path, $filename);
            $winner->picture = $filename;
        }

        $winner->save();

        return redirect('admin/winneraudience/create');
    }

    public function winneraudiencedelete($id)
    {

        $audience = Winneraudience::find($id);

        $audience->delete();

        return redirect('admin/winneraudience');
    }

    public function qd()
    {

        $questiondistributions = Questiondistribution::all();
        return view('admin/qd', compact('questiondistributions'));
    }

    public function qdCreate()
    {

        return view('admin/qdcreate');
    }

    public function qdstore(Request $request)
    {


        $this->validate($request, [
            'round_id' => 'required'
        ]);

        $data = array_filter($request->all());
        $round_id = $data['round_id'];
        $question_type = $data['question_type'];
        $number = $data['number'];
        $marks = $data['marks'];

        foreach ($round_id as $key => $input) {
            $questions = new Questiondistribution();

            $questions->round_id = $round_id[$key];
            $questions->question_type = $question_type[$key];
            $questions->number = $number[$key];
            $questions->marks = $marks[$key];

            $questions->save();
        }

        return redirect('/admin/qd')->with('updated');
    }

    public function qddelete($id)
    {

        $audience = Questiondistribution::find($id);

        $audience->delete();

        return redirect('admin/qd');
    }
    
    
    // About us 
    public function aboutCreate()
    {
        return view('admin.about');
    }
    
    public function aboutStore(Request $request) {
        $about = new About();
        $about->description = $request->description;
        
        $about->save();
        
        return redirect('/admin/about')->with('stored');
    }
    
    
    // New development
    
    public function eventList()
    {
        $events = Event::with('session')->paginate(10);
        return view('admin.events_list', compact('events'));
    }
    
    public function eventCreate()
    {
        $sessions = Session::where('status', 'active')->get();
        
        return view('admin.event_create', compact('sessions'));
    }
    
    public function eventStore(Request $request) {
        $event = new Event();
        $event->title = $request->title;
        $event->event_category = $request->event_category;
        $event->start_date = $request->start_date;
        $event->end_date = $request->end_date;
        $event->location = $request->location;
        $event->descriptions = $request->descriptions;
        
        
        if ($request->hasFile('image')) {
            $originalFiles = $request->file('image');
            $image = $originalFiles;
            $filename = time() . '.' . $image->getClientOriginalExtension();
            $destinationPath = public_path('/images');
            $image->move($destinationPath, $filename);
            $event->image = $filename;
        }
        
        $event->save();
        
        return redirect('/admin/event/list')->with('stored');
    }
    
    public function eventEdit($id)
    {
        $eventedit = Event::find($id);
        
        $sessions = Session::where('status', 'active')->get();
        
        return view('admin.event_update', compact('eventedit', 'sessions'));
    }
    
    public function eventUpdate(Request $request, $id) {
        
        $event = Event::findOrFail($id);
        $event->title = $request->title;
        $event->event_category = $request->event_category;
        $event->start_date = $request->start_date;
        $event->end_date = $request->end_date;
        $event->location = $request->location;
        $event->descriptions = $request->descriptions;
        
        if ($request->hasFile('image')) {
            $originalFiles = $request->file('image');
            $image = $originalFiles;
            $filename = time() . '.' . $image->getClientOriginalExtension();
            $destinationPath = public_path('/images');
            $image->move($destinationPath, $filename);
            $event->image = $filename;
        }
        
        $event->update();
        
        return redirect('/admin/event/list')->with('stored');
    }
    
    public function eventDelete($id) {
        $event = Event::findOrFail($id);
        $event->delete();

        return redirect('admin/event/list');
    }
    
    
    //Blogs
    public function blogsList()
    {
        $blogs = Blogs::paginate(10);
        return view('admin.blogs_list', compact('blogs'));
    }
    
    public function blogsCreate()
    {
        return view('admin.blogs_create');
    }
    
    public function blogsStore(Request $request) {
        $blogs = new Blogs();
        $blogs->title = $request->title;
        $blogs->tags = $request->tags;
        $blogs->post_date = $request->post_date;
        $blogs->description = $request->description;
        $blogs->author = $request->author;
        
        if ($request->hasFile('image')) {
            $originalFiles = $request->file('image');
            $image = $originalFiles;
            $filename = time() . '.' . $image->getClientOriginalExtension();
            $destinationPath = public_path('/images');
            $image->move($destinationPath, $filename);
            $blogs->image = $filename;
        }
        
        $blogs->save();
        
        return redirect('/admin/blogs/list')->with('stored');
    }
    
    public function blogsEdit($id)
    {
        $blogsedit = Blogs::find($id);
        return view('admin.blogs_update', compact('blogsedit'));
    }
    
    public function blogsUpdate(Request $request, $id) {
        $blogs = Blogs::findOrFail($id);
        $blogs->title = $request->title;
        $blogs->tags = $request->tags;
        $blogs->post_date = $request->post_date;
        $blogs->description = $request->description;
        $blogs->author = $request->author;
        
        if ($request->hasFile('image')) {
            $originalFiles = $request->file('image');
            $image = $originalFiles;
            $filename = time() . '.' . $image->getClientOriginalExtension();
            $destinationPath = public_path('/images');
            $image->move($destinationPath, $filename);
            $blogs->image = $filename;
        }
        
        $blogs->update();
        
        return redirect('/admin/blogs/list')->with('stored');
    }
    
    public function blogsDelete($id) {
        $blogs = Blogs::find($id);
        $blogs->delete();

        return redirect('admin/blogs/list');
    }
    
    
    //Partners
    public function partnersList()
    {
        $partners = Partners::all();
        return view('admin.partners_list', compact('partners'));
    }
    
    public function partnersCreate()
    {
        return view('admin.partners_create');
    }
    
    public function partnersStore(Request $request) {
        $partners = new Partners();
        $partners->title = $request->title;
        $partners->category = $request->category;
        
        if ($request->hasFile('image')) {
            $originalFiles = $request->file('image');
            $image = $originalFiles;
            $filename = time() . '.' . $image->getClientOriginalExtension();
            $destinationPath = public_path('/images');
            $image->move($destinationPath, $filename);
            $partners->image = $filename;
        }
        
        $partners->save();
        
        return redirect('/admin/partners/list')->with('stored');
    }
    
    public function partnersEdit($id)
    {
        $partnersedit = Partners::find($id);
        return view('admin.partners_update', compact('partnersedit'));
    }
    
    public function partnersUpdate(Request $request, $id) {
        $partners = Partners::findOrFail($id);
        $partners->title = $request->title;
        $partners->category = $request->category;
        
        if ($request->hasFile('image')) {
            $originalFiles = $request->file('image');
            $image = $originalFiles;
            $filename = time() . '.' . $image->getClientOriginalExtension();
            $destinationPath = public_path('/images');
            $image->move($destinationPath, $filename);
            $partners->image = $filename;
        }
        
        $partners->update();
        
        return redirect('/admin/partners/list')->with('stored');
    }
    
    public function partnersDelete($id) {
        $partners = Partners::find($id);
        $partners->delete();

        return redirect('admin/partners/list');
    }

    //Faq Category
    public function faqCatList()
    {
        $faqCats = FaqCategory::all();
        return view('admin.faq_Cat_list', compact('faqCats'));
    }
    
    public function faqCatCreate()
    {
        return view('admin.faqCatCreate');
    }
    
    public function faqCatStore(Request $request) {
        $faqCats = new FaqCategory();
        $faqCats->name = $request->name;
        $faqCats->status = $request->status;

        $faqCats->save();
    
        return redirect('/admin/faqcat/list')->with('stored');
    }
    
    public function faqCatEdit($id)
    {
        $faqCatedit = FaqCategory::find($id);
        return view('admin.faq_cat_update', compact('faqCatedit'));
    }
    
    public function faqCatUpdate(Request $request, $id) {
        
        $faqCat = FaqCategory::findOrFail($id);
        $faqCat->name = $request->name;
        $faqCat->status = $request->status;

        $faqCat->update();
    
        return redirect('/admin/faqcat/list')->with('stored');
    }
    
    public function faqCatDelete($id) {
        $faqCat = FaqCategory::find($id);
        $faqCat->delete();

        return redirect('admin/faqcat/list');
    }

    //Faqs
    public function faqsList()
    {
        $faqs = Faqs::with('faqCategory')->get();
        return view('admin.faq_list', compact('faqs'));
    }
    
    public function faqsCreate()
    {
        $faqCategories = FaqCategory::where('status', 1)->get();
        return view('admin.faq_create', compact('faqCategories'));
    }
    
    public function faqsStore(Request $request) {
        $faqs = new Faqs();
        $faqs->question = $request->question;
        $faqs->answer = $request->answer;
        $faqs->category_id = $request->category;
        
        $faqs->save();
    
        return redirect('/admin/faqs/list')->with('stored');
    }
    
    public function faqsEdit($id)
    {
        $faqsedit = Faqs::find($id);
        $faqCategories = FaqCategory::where('status', 1)->get();
        return view('admin.faq_update', compact('faqsedit', 'faqCategories'));
    }
    
    public function faqsUpdate(Request $request, $id) {
        
        $faqs = Faqs::findOrFail($id);
        $faqs->question = $request->question;
        $faqs->answer = $request->answer;
        $faqs->category_id = $request->category;
        
        $faqs->update();
    
        return redirect('/admin/faqs/list')->with('stored');
    }
    
    public function faqsDelete($id) {
        $faqs = Faqs::find($id);
        $faqs->delete();

        return redirect('admin/faqs/list');
    }
    
    //Teams
    public function teamsList()
    {
        $teams = Teams::all();
        return view('admin.teams_list', compact('teams'));
    }
    
    public function teamsCreate()
    {
        return view('admin.teams_create');
    }
    
    public function teamsStore(Request $request) {
        $teams = new Teams();
        $teams->name = $request->name;
        $teams->organization_name = $request->organization_name;
        $teams->designation = $request->designation;
        $teams->status = $request->status;
        $teams->address = $request->address;
        
        
        if ($request->hasFile('image')) {
            $originalFiles = $request->file('image');
            $image = $originalFiles;
            $filename = time() . '.' . $image->getClientOriginalExtension();
            $destinationPath = public_path('/images');
            $image->move($destinationPath, $filename);
            $teams->image = $filename;
        }
        
        $teams->save();
    
        return redirect('/admin/teams/list')->with('stored');
    }
    
    public function teamsEdit($id)
    {
        $teamsedit = Teams::find($id);
        return view('admin.teams_update', compact('teamsedit'));
    }
    
    public function teamsUpdate(Request $request, $id) {
        $teams = Teams::findOrFail($id);
        $teams->name = $request->name;
        $teams->organization_name = $request->organization_name;
        $teams->designation = $request->designation;
        $teams->status = $request->status;
        $teams->address = $request->address;
        
        
        if ($request->hasFile('image')) {
            $originalFiles = $request->file('image');
            $image = $originalFiles;
            $filename = time() . '.' . $image->getClientOriginalExtension();
            $destinationPath = public_path('/images');
            $image->move($destinationPath, $filename);
            $teams->image = $filename;
        }
        
        $teams->update();
    
        return redirect('/admin/teams/list')->with('stored');
    }
    
    public function teamsDelete($id) {
        $teams = Teams::find($id);
        $teams->delete();

        return redirect('admin/teams/list');
    }
    
    // Round
    public function roundList()
    {
        $rounds = Round::paginate(10);
        return view('admin.rounds_list', compact('rounds'));
    }
    
    public function roundDetails($id)
    {
        $rounds = DB::table('rounds')->get();
        $round = DB::table('rounds')->where('id', $id)->first();
        //dd( $round);
        $contact = DB::table('contacts')->first();
        return view('/rounddetails')->with(['contact' => $contact, 'rounds' => $rounds, 'round' => $round]);
    }
    
    public function roundCreate()
    {
        return view('admin.round_create');
    }
    
    public function roundStore(Request $request) {
        $round = new Round();
        $round->title = $request->title;
        $round->titlebn = $request->titlebn;
        $round->syllabus = $request->syllabus;
        $round->status = $request->status;
        
        if ($request->hasFile('image')) {
            $originalFiles = $request->file('image');
            $image = $originalFiles;
            $filename = time() . '.' . $image->getClientOriginalExtension();
            $destinationPath = public_path('/images');
            $image->move($destinationPath, $filename);
            $round->image = $filename;
        }
        
        $round->save();
        
        return redirect('/admin/round/list')->with('stored');
    }
    
    public function roundEdit($id)
    {
        $roundedit = Round::find($id);
        
        return view('admin.round_update', compact('roundedit'));
    }
    
    public function roundUpdate(Request $request, $id) {
        
        $round = Round::findOrFail($id);
        $round->title = $request->title;
        $round->titlebn = $request->titlebn;
        $round->syllabus = $request->syllabus;
        $round->status = $request->status;
        
        if ($request->hasFile('image')) {
            $originalFiles = $request->file('image');
            $image = $originalFiles;
            $filename = time() . '.' . $image->getClientOriginalExtension();
            $destinationPath = public_path('/images');
            $image->move($destinationPath, $filename);
            $round->image = $filename;
        }
        
        $round->update();
        
        return redirect('/admin/round/list')->with('stored');
    }
    
    public function roundDelete($id) {
        $round = Round::findOrFail($id);
        $round->delete();

        return redirect('admin/round/list');
    }
    
    
    
    // Reward
    public function rewardList()
    {
        $rewards = Reward::all();
        return view('admin.rewards_list', compact('rewards'));
    }
    
    public function rewardCreate()
    {
        return view('admin.reward_create');
    }
    
    public function rewardStore(Request $request) {
        $reward = new Reward();
        $reward->title = $request->title;
        $reward->price = $request->price;
        $reward->status = $request->status;
        
        if ($request->hasFile('image')) {
            $originalFiles = $request->file('image');
            $image = $originalFiles;
            $filename = time() . '.' . $image->getClientOriginalExtension();
            $destinationPath = public_path('/images');
            $image->move($destinationPath, $filename);
            $reward->image = $filename;
        }
        
        $reward->save();
        
        return redirect('/admin/reward/list')->with('stored');
    }
    
    public function rewardEdit($id)
    {
        $rewardedit = Reward::find($id);
        
        return view('admin.reward_update', compact('rewardedit'));
    }
    
    public function rewardUpdate(Request $request, $id) {
        
        $reward = Reward::find($id);
        $reward->title = $request->title;
        $reward->price = $request->price;
        $reward->status = $request->status;
        
        if ($request->hasFile('image')) {
            $originalFiles = $request->file('image');
            $image = $originalFiles;
            $filename = time() . '.' . $image->getClientOriginalExtension();
            $destinationPath = public_path('/images');
            $image->move($destinationPath, $filename);
            $reward->image = $filename;
        }
        
        $reward->update();
        
        return redirect('/admin/reward/list')->with('stored');
    }
    
    public function rewardDelete($id) {
        $reward = Reward::findOrFail($id);
        $reward->delete();

        return redirect('admin/reward/list');
    }
    
    
    // Certificate
    
    public function certificateList()
    {
        $certificates = Certificate::all();
        return view('admin.certificates_list', compact('certificates'));
    }
    
    public function certificateCreate()
    {
        return view('admin.certificate_create');
    }
    
    public function certificateStore(Request $request) {
        $certificate = new Certificate();
        $certificate->title = $request->title;
        $certificate->subtitle = $request->subtitle;
        
        if ($request->hasFile('image')) {
            $originalFiles = $request->file('image');
            $image = $originalFiles;
            $filename = time() . '.' . $image->getClientOriginalExtension();
            $destinationPath = public_path('/images');
            $image->move($destinationPath, $filename);
            $certificate->image = $filename;
        }
        
        $certificate->save();
        
        return redirect('/admin/certificate/list')->with('stored');
    }
    
    public function certificateEdit($id)
    {
        $certificateedit = Certificate::find($id);
        
        return view('admin.certificate_update', compact('certificateedit'));
    }
    
    public function certificateUpdate(Request $request, $id) {
        
        $certificate = Certificate::find($id);
        $certificate->title = $request->title;
        $certificate->subtitle = $request->subtitle;
        
        if ($request->hasFile('image')) {
            $originalFiles = $request->file('image');
            $image = $originalFiles;
            $filename = time() . '.' . $image->getClientOriginalExtension();
            $destinationPath = public_path('/images');
            $image->move($destinationPath, $filename);
            $certificate->image = $filename;
        }
        
        $certificate->update();
        
        return redirect('/admin/certificate/list')->with('stored');
    }
    
    public function certificateDelete($id) {
        $certificate = Certificate::findOrFail($id);
        $certificate->delete();

        return redirect('admin/certificate/list');
    }
    
    
    // Winner
    
    public function winnerList()
    {
        $winners = Winner::with('session')->paginate(10);
        return view('admin.winners_list', compact('winners'));
    }
    
    public function winnerCreate()
    {
        $sessions = Session::orderBy('id', "DESC")->where('status', 'active')->get();
        return view('admin.winner_create', compact('sessions'));
    }
    
    public function winnerStore(Request $request) {
        $winner = new Winner();
        $winner->title = Session::where('id', $request->title)->first()->title;
        $winner->session_id = $request->title;
        $winner->categories = $request->categories;
        $winner->winnername = $request->winnername;
        $winner->institutename = $request->institutename;
        $winner->address = $request->address;
        $winner->mobile = $request->mobile;
        $winner->email = $request->email;
        $winner->status = $request->status;
        
        if ($request->hasFile('image')) {
            $originalFiles = $request->file('image');
            $image = $originalFiles;
            $filename = time() . '.' . $image->getClientOriginalExtension();
            $destinationPath = public_path('/images');
            $image->move($destinationPath, $filename);
            $winner->image = $filename;
        }
        
        $winner->save();
        
        return redirect('/admin/winner/list')->with('stored');
    }
    
    public function winnerEdit($id)
    {
        $winneredit = Winner::find($id);
        $sessions = Session::orderBy('id', "DESC")->get();
        return view('admin.winner_update', compact('winneredit', 'sessions'));
    }
    
    public function winnerUpdate(Request $request, $id) {
        
        $winner = Winner::find($id);
        // dd(Session::where('id', $request->title)->first()->title);
        $winner->title = Session::where('id', $request->title)->first()->title;
        $winner->session_id = $request->title;
        $winner->categories = $request->categories;
        $winner->winnername = $request->winnername;
        $winner->institutename = $request->institutename;
        $winner->address = $request->address;
        $winner->mobile = $request->mobile;
        $winner->email = $request->email;
        $winner->status = $request->status;
        
        if ($request->hasFile('image')) {
            $originalFiles = $request->file('image');
            $image = $originalFiles;
            $filename = time() . '.' . $image->getClientOriginalExtension();
            $destinationPath = public_path('/images');
            $image->move($destinationPath, $filename);
            $winner->image = $filename;
        }
        
        $winner->update();
        
        return redirect('/admin/winner/list')->with('stored');
    }
    
    public function winnerDelete($id) {
        $winner = Winner::findOrFail($id);
        $winner->delete();

        return redirect('admin/winner/list');
    }
    
    
    // Session
    
    public function sessionList()
    {
        $sessions = Session::all();
        return view('admin.sessions_list', compact('sessions'));
    }
    
    public function sessionCreate()
    {
        return view('admin.session_create');
    }
    
    public function sessionStore(Request $request) {
        $session = new Session();
        $session->title = $request->title;
        $session->status = $request->status;
        
        $session->save();
        
        return redirect('/admin/session/list')->with('stored');
    }
    
    public function sessionEdit($id)
    {
        $sessionedit = Session::find($id);
        
        return view('admin.session_update', compact('sessionedit'));
    }
    
    public function sessionUpdate(Request $request, $id) {
        
        $session = Session::find($id);
        $session->title = $request->title;
        $session->status = $request->status;
        
        $session->update();
        
        return redirect('/admin/session/list')->with('stored');
    }
    
    public function sessionDelete($id) {
        $session = Session::findOrFail($id);
        $session->delete();

        return redirect('admin/session/list');
    }
    
    // Join As Type
    
    public function joinAsList()
    {
        $joinAsTypes = JoinAsType::all();
        return view('admin.join_as_list', compact('joinAsTypes'));
    }
    
    public function joinAsCreate()
    {
        return view('admin.join_as_create');
    }
    
    public function joinAsStore(Request $request) {
        $joinAsType = new JoinAsType();
        $joinAsType->name = $request->name;
        $joinAsType->status = $request->status;
        
        $joinAsType->save();
        
        return redirect('/admin/join-as-list')->with('stored');
    }
    
    public function joinAsEdit($id)
    {
        $joinAsedit = JoinAsType::find($id);
        
        return view('admin.join_as_update', compact('joinAsedit'));
    }
    
    public function joinAsUpdate(Request $request, $id) {
        
        $joinAsType = JoinAsType::find($id);
        $joinAsType->name = $request->name;
        $joinAsType->status = $request->status;
        
        $joinAsType->update();
        
        return redirect('/admin/join-as-list')->with('stored');
    }
    
    public function joinAsDelete($id) {
        $joinAsType = JoinAsType::findOrFail($id);
        $joinAsType->delete();

        return redirect('admin/join-as-list');
    }
    
    public function joinAsDetailList()
    {
        $JoinAsDetails = JoinAsDetail::with('joinType')->get();
        return view('admin.join_as_detail_list', compact('JoinAsDetails'));
    }
    
    public function joinAsDetailCreate()
    {
        $joinAsTypes = JoinAsType::get();
        return view('admin.join_as_detail_create', compact('joinAsTypes'));
    }
    
    public function joinAsDetailStore(Request $request) {
        $joinAsDetails = new JoinAsDetail();
        $joinAsDetails->title = $request->title;
        $joinAsDetails->join_as_type_id = $request->join_as_type_id;
        $joinAsDetails->description = $request->description;
        $joinAsDetails->save();
        
        return redirect('/admin/joinasdetail/list')->with('stored');
    }
    
    public function joinAsDetailEdit($id)
    {
        $joinAsDetailEdit = JoinAsDetail::find($id);
        $joinAsTypes = JoinAsType::get();
        return view('admin.join_as_detail_update', compact('joinAsDetailEdit','joinAsTypes'));
    }
    
    public function joinAsDetailUpdate(Request $request, $id) {
        $joinAsDetail = JoinAsDetail::findOrFail($id);
        $joinAsDetail->title = $request->title;
        $joinAsDetail->join_as_type_id = $request->join_as_type_id;
        $joinAsDetail->description = $request->description;
        $joinAsDetail->update();
        
        return redirect('/admin/joinasdetail/list')->with('stored');
    }
    
    public function joinAsDetailDelete($id) {
        $joinAsDetail = JoinAsDetail::find($id);
        $joinAsDetail->delete();

        return redirect('admin/joinasdetail/list');
    }
    
    //participants
    public function participants()
    {

        return view('admin.participants');
    }
    
    public function participant_create()
    {

        return view('admin.participant_create');
    }
    
    public function participant_update()
    {

        return view('admin.participant_update');
    }
    
    //winners
    public function winners_list()
    {
        
        return view('admin.winners_list');
    }
    
    //joinsoty
    
    public function joinsoty_list()
    {
        $mentors = Mentor::with('event', 'division', 'district', 'upazila')->paginate(5);
        return view('admin.joinsoty_list', compact('mentors'));
    }
    
    public function soty_edit($id){
        $joinAsTypes = JoinAsType::where('status', 1)->get();
        
        $mentor = Mentor::findOrFail($id);
        $events = Event::with('session')->get();
        $divisions = Division::all();
        $districts = District::where('division_id', $mentor->division_id)->get();
        $upazilas = Upazila::where('district_id', $mentor->district_id)->get();
    
        return view('admin.soty_edit', compact('mentor', 'events', 'joinAsTypes', 'divisions', 'districts', 'upazilas'));
    }
    
    public function updateJoinmentor(Request $request, $id)
    {
        $mentor = Mentor::findOrFail($id);
    
        $validator = Validator::make($request->all(), [
            'join_as' => 'required',
            'event' => 'required',
            'profile_image' => 'nullable|mimes:jpg,jpeg,png|max:1999',
            'full_name' => 'required|string|max:255',
            'email' => 'required|email|unique:mentors,email,' . $mentor->id,
            'whatsapp_number' => 'required',
            'division' => 'required',
            'district' => 'required',
            'upazila' => 'required',
            'village_area' => 'required',
            'institute' => 'required',
            'class_level_designation' => 'required',
            'institute_mobile' => 'required',
            'agree_terms' => 'accepted',
        ]);
    
        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator)->withInput();
        }
    
        $profileImage = $mentor->profile_image;
        if ($request->hasFile('profile_image')) {
            if ($mentor->profile_image && Storage::disk('public')->exists($mentor->profile_image)) {
                Storage::disk('public')->delete($mentor->profile_image);
            }
            $profileImage = $request->file('profile_image')->store('profiles', 'public');
        }
    
        $mentor->update([
            'join_as' => $request->join_as,
            'event_id' => $request->event,
            'profile_image' => $profileImage,
            'full_name' => strtoupper($request->full_name),
            'email' => $request->email,
            'whatsapp_number' => $request->whatsapp_number,
            'telegram_number' => $request->telegram_number,
            'password' => $request->password
                ? Hash::make($request->password)
                : $mentor->password,
            'division_id' => $request->division,
            'district_id' => $request->district,
            'upazila_id' => $request->upazila,
            'village_area' => $request->village_area,
            'institute' => $request->institute,
            'class_level_designation' => $request->class_level_designation,
            'group_department_subject' => $request->group_department_subject,
            'institute_mobile' => $request->institute_mobile,
            'institute_email' => $request->institute_email,
            'institute_eiin_no' => $request->institute_eiin_no,
            'agree_terms' => $request->has('agree_terms'),
        ]);
    
        return redirect()->back()->with('success', 'Mentor information updated successfully!');
    }
    
    public function destroyJoinmentor($id)
    {
        $mentor = Mentor::findOrFail($id);
    
        if ($mentor->profile_image && Storage::disk('public')->exists($mentor->profile_image)) {
            Storage::disk('public')->delete($mentor->profile_image);
        }
    
        $mentor->delete();
    
        return redirect()->back()->with('success', 'Mentor deleted successfully!');
    }



    public function getDistricts($divisionId)
    {
        $districts = District::where('division_id', $divisionId)->get();
        return response()->json(['districts' => $districts]);
    }

    public function getUpazilas($districtId)
    {
        $upazilas = Upazila::where('district_id', $districtId)->get();
        return response()->json(['upazilas' => $upazilas]);
    }
    
    public function mailform(){
        return view('mail');
    }
    public function maildata(Request $request){
        $email = $request->email;
        $name = $request->contact_person;
        $mess = $request->message;
        $senderMessage = "thanks for your message , we will reply you in later";
        $mailData = [
        'url' => 'https://sandroft.com/',
        ];
        $send_mail = "sahincseiu@gmail.com";
        Mail::to($send_mail)->send(new SendMail($name, $email, $mess));
        
        Mail::to( $email)->send(new
        SendMessageToEndUser($name,$senderMessage,$mailData));
        return redirect()->back()->with("Mail Send Successfully");
    }
    
    // Bkash amount set
    public function paymentAmount()
    {
        $amounts = PaymentAmount::get();
        return view('admin.bkashAmountList', compact('amounts'));
    }
    
    public function paymentAmountEdit($id)
    {
        $bkashAmount = PaymentAmount::find($id);

        return view('admin.bkashAmount_update', compact('bkashAmount'));
    }
    
    public function paymentAmountUpdate(Request $request, $id) {
        $bkashAmount = PaymentAmount::findOrFail($id);
        $bkashAmount->amount = $request->amount;
        $bkashAmount->update();
        
        return redirect('/admin/bkash-amount/list')->with('stored');
    }
    
}
