<?php

namespace App\Http\Controllers;

use App\Models\Setting;
use App\Models\SchoolYear;
use App\Models\CuttingSchoolYear;
use App\Services\RepportService;
use App\Services\Chapter3Service;
use Barryvdh\DomPDF\Facade\Pdf;
use Illuminate\Http\Request;

class GlobalRapportController extends Controller
{
    protected $service, $service1;
    public function __construct(RepportService $service, Chapter3Service $service1) {
        $this->service = $service;
        $this->service1 = $service1;
    }
    /**
     * Display a listing of the resource.
     */
    public function index() {
        try {
            $year = SchoolYear::where('status', '1')->first();
            $cutting = CuttingSchoolYear::where('school_year_id', $year->id)->get();
            return view('pages.globals.index',[
                'year' => $year,
                'cutting' => $cutting
            ]);
        }
        catch (\Exception $e) {
            return back()->with([
                'str' => 'danger',
                'msg' => 'Une erreur est survenue.'
            ]);
        }
    }


    public function docPdf($str) {
        try {
            $school = Setting::where('school_id', auth()->user()->school_id)->first();
            $dts = CuttingSchoolYear::find($str);
            $name = $dts->cutting->libelle.'_'.$str;
            $cycle1 = $this->service1->cycle(auth()->user()->school_id, 'collège');
            $cycle2 = $this->service1->cycle(auth()->user()->school_id, 'lycée');
            $pdf = Pdf::loadView('docs.rapport', [
                'school' => $school,
                'cutting' => $dts,
                'cycle1' => $cycle1,
                'cycle2' => $cycle2
            ]);

            $pdf->setPaper('A3', 'portrait'); // ou 'A4', 'A3', etc.
            // $pdf->setPaper('A4', 'portrait'); // ou 'A4', 'A3', etc.
            return $pdf->stream('rapart_'.$name.'.pdf');
        }
        catch (\Exception $e) {
            return back()->with([
                'str' => 'danger',
                'msg' => 'Une erreur est survenue.'
            ]);
        }
    }

    /**
     * Show the form for creating a new resource.
     */
    public function dococument(Request $request) {
        try {
            $val = $request->validate([
                'cutting' => 'required|string',
                'school' => 'required|string',
                'docs' => 'required|array',
                'docs.*' => 'required|string',
                'nbre' => 'required|array',
                'nbre.*' => 'required|string',
                'text' => 'required|array',
                'text.*' => 'required|string',
            ]);
            $dts = $this->service->document($val);
            return back()->with([
                'str' => $dts ? 'success':'danger',
                'msg' => $dts ? 'Enregistrement effectué.':'Une erreur est survenue.',
                'chpt' => 'chapter1',
                'sub' => 'vie pedagogique',
            ]);
        }
        catch (\Exception $e) {
            return back()->with([
                'str' => 'danger',
                'msg' => 'Une erreur est survenue.',
                'chpt' => 'chapter1',
                'sub' => 'vie pedagogique',
            ]);
        }
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request) {
        try {
            $val = $request->validate([
                'cutting' => 'required|string',
                'school' => 'required|string',
                'text' => 'required|string'
            ]);
            $dts = $this->service->introductif($val);
            return back()->with([
                'str' => $dts ? 'success':'danger',
                'msg' => $dts ? 'Enregistrement effectué.':'Une erreur est survenue.'
            ]);
        }
        catch (\Exception $e) {
            return back()->with([
                'str' => 'danger',
                'msg' => 'Une erreur est survenue.'
            ]);
        }
    }

    /**
     * Display the specified resource.
     */
    public function show($str) {
        try {
            $school = auth()->user()->school_id;
            $cutting = CuttingSchoolYear::find($str);
            $data = $this->service->getData($school);
            $class = $this->service->getClass($school, $str);
            $educs = $this->service1->getEducats($school);
            $datas = $this->service1->getUsers($school);
            $cycle1 = $this->service1->cycle($school, 'collège');
            $cycle2 = $this->service1->cycle($school, 'lycée');
            return view('pages.globals.detail', [
                'cutting' => $cutting,
                'matters' => $data[1],
                'users' => $data[0],
                'classes' => $class,
                'educs' => $educs,
                'data' => $datas,
                'cycle1' => $cycle1,
                'cycle2' => $cycle2
            ]);
        }
        catch (\Exception $e) {
            return back()->with([
                'str' => 'danger',
                'msg' => 'Une erreur est survenue.'
            ]);
        }
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function responsible(Request $request) {
        try {
            $vals = $request->validate([
                'cutting' => 'required|string',
                'school' => 'required|string',
                'matters' => 'required|array',
                'matters.*' => 'required|string',
                'users' => 'required|array',
                'users.*' => 'required|string',
            ]);
            $dts = $this->service->responsable($vals);
            return back()->with([
                'str' => $dts ? 'success':'danger',
                'msg' => $dts ? 'Enregistrement effectué.':'Une erreur est survenue.',
                'chpt' => 'chapter1',
                'sub' => 'vie pedagogique',
            ]);
        }
        catch (\Exception $e) {
            return back()->with([
                'str' => 'danger',
                'msg' => 'Une erreur est survenue.',
                'chpt' => 'chapter1',
                'sub' => 'vie pedagogique',
            ]);
        }
    }

    /**
     * Update the specified resource in storage.
     */
    public function activite(Request $request, $str) {
        try {
            $vals = $request->validate([
                'cutting' => 'required|string',
                'school' => 'required|string',
                'activite' => 'required|array',
                'activite.*' => 'required|string',
                'theme' => 'required|array',
                'theme.*' => 'required|string',
                'select' => 'required|array',
                'select.*' => 'required|string',
                'time' => 'required|array',
                'time.*' => 'required|string',
                'date' => 'required|array',
                'date.*' => 'required|date',
                'content' => 'required|array',
                'content.*' => 'required|string',
            ]);
            $dts = ($str == 1) ? 
            $this->service->upActivite($vals):
            $this->service->CeActivite($vals);
            return back()->with([
                'str' => $dts ? 'success':'danger',
                'msg' => $dts ? 'Enregistrement effectué.':'Une erreur est survenue.',
                'chpt' => 'chapter1',
                'sub' => 'vie pedagogique',
            ]);
        }
        catch (\Exception $e) {
            return back()->with([
                'str' => 'danger',
                'msg' => 'Une erreur est survenue.',
                'chpt' => 'chapter1',
                'sub' => 'vie pedagogique',
            ]);
        }
    }

    /**
     * Remove the specified resource from storage.
     */
    public function visity(Request $request, $str) {
        try {
            $vals = $request->validate([
                'cutting' => 'required|string',
                'school' => 'required|string',
                'text' => 'required|array',
                'text.*' => 'required|string',
                'user' => 'required|array',
                'user.*' => 'required|string',
                'matter' => 'required|array',
                'matter.*' => 'required|string',
                'time' => 'required|array',
                'time.*' => 'required|string',
                'date' => 'required|array',
                'date.*' => 'required|date',
                'classe' => 'required|array',
                'classe.*' => 'required|string',
            ]);

            $dts = ($str == 1) ? 
            $this->service->directeur($vals):
            $this->service->animateur($vals);
            return back()->with([
                'str' => $dts ? 'success':'danger',
                'msg' => $dts ? 'Enregistrement effectué.':'Une erreur est survenue.',
                'chpt' => 'chapter1',
                'sub' => 'vie pedagogique',
            ]);
        }
        catch (\Exception $e) {
            return back()->with([
                'str' => 'danger',
                'msg' => 'Une erreur est survenue.',
                'chpt' => 'chapter1',
                'sub' => 'vie pedagogique',
            ]);
        }
    }
}
