<?php
  namespace App\Services;

  use App\Models\User;
  use App\Models\Deces;
  use App\Models\Maladie;
  use App\Models\Abandon;
  use App\Models\Grossesse;
  use App\Models\ReportQuarter;
  use App\Models\ConseilSchool;
  use App\Models\ActivitVieScolaire;
  use App\Models\ActivitParaScolaire;
  use App\Services\RepportService;
  use App\Services\Chapter1Service;
  class Chapter3Service 
  {
    protected $service, $svrs;
    public function __construct(RepportService $service, Chapter1Service $svrs) {
      $this->service = $service;
      $this->svrs = $svrs;
    }


    public function cycle($school, $str) {
      return $this->svrs->verifyCle($school, $str);
    }


    public function viescolaire($data) {
      $report = $this->verify($data['school'], $data['cutting']);
      if($report){
        $this->saveVieScloaire($data['tab1'], $data['tab2'], $data['tab3'], $data['tab4'], $data['tab5'], $data['tab6'], $report);
      }
      $exist = $report ? ActivitVieScolaire::where('report_quarter_id', $report)->count():false;
      return $exist ? true:false;
    }


    public function paraScolaire($data, $str) {
      $report = $this->verify($data['school'], $data['cutting']);
      if($report) {
        $this->saveParaScolaire($data['tab1'], $data['tab2'], $data['tab3'], $data['tab4'], $data['tab5'], $report, $str);
      }
      $exist = $report ? ActivitParaScolaire::where('report_quarter_id', $report)->where('type', $str)->count():false;
      return $exist ? true:false;
    }


    public function conseilSchool($data, $str) {
      $report = $this->verify($data['school'], $data['cutting']);
      if($report) {
        $this->saveConseil($data['tab'], $report, $str);
      }
      $exist = $report ? ConseilSchool::where('report_quarter_id', $report)->where('type', $str)->count():false;
      return $exist ? true:false;
    }


    public function abdonSchool($data) {
      $report = $this->verify($data['school'], $data['cutting']);
      if($report){
        $this->saveAbdon($data['tab1'], $data['tab2'], $data['tab3'], $report);
      }
      $exist = $report ? Abandon::where('report_quarter_id', $report)->count():false;
      return $exist ? true:false;
    }


    public function grsseSchool($data) {
      $report = $this->verify($data['school'], $data['cutting']);
      if($report){
        $this->saveGrsse($data['tab1'], $data['tab2'], $data['tab3'], $data['tab4'], $data['tab5'], $report);
      }
      $exist = $report ? Grossesse::where('report_quarter_id', $report)->count():false;
      return $exist ? true:false;
    }


    public function maladSchool($data) {
      $report = $this->verify($data['school'], $data['cutting']);
      if($report){
        $this->saveMalad($data['tab1'], $data['tab2'], $data['tab3'], $data['tab4'], $data['tab5'], $report);
      }
      $exist = $report ? Maladie::where('report_quarter_id', $report)->count():false;
      return $exist ? true:false;
    }


    public function dcsSchool($data) {
      $report = $this->verify($data['school'], $data['cutting']);
      if($report){
        $this->saveDcs($data['tab1'], $data['tab2'], $data['tab3'], $data['tab4'], $report);
      }
      $exist = $report ? Deces::where('report_quarter_id', $report)->count():false;
      return $exist ? true:false;
    }


    public function getstudent($id) {
      return $this->service->getstudent($id) ?? [];
    }


    public function getUsers($school) {
      $users = User::where('school_id', $school)->where('status', '1')->orderBy('first_name')->orderBy('last_name')->get();
      return $users;
    }


    public function getEducats($school) {
      $users = User::where('school_id', $school)->where('status', '1')->where('role_id', '5')->orderBy('first_name')->orderBy('last_name')->get();
      return $users;
    }


    private function verify($school, $cutting) {
      $exist = ReportQuarter::where('school_id', $school)->where('cutting_school_year_id', $cutting)->first();
      if(!$exist) {
        $exist = ReportQuarter::create([
          'school_id' => $school,
          'cutting_school_year_id' => $cutting
        ]);
      }
      return $exist ? $exist->id:null;
    }

    private function saveVieScloaire($dts1, $dts2, $dts3, $dts4, $dts5, $dts6, $report) {
      $i = 0;
      while($i < count($dts1) ) {
        $data = ActivitVieScolaire::where('report_quarter_id', $report)->where('order', $i+1)->first();
        if($data){
          $data->update([
            'cantin' => $dts3[$i],
            'projet' => $dts6[$i],
            'activits' => $dts4[$i],
            'infirmerie' => $dts1[$i],
            'bibliothek' => $dts2[$i],
            'cooperativ' => $dts5[$i],
          ]);
        }
        else{
          ActivitVieScolaire::create([
            'order' => $i+1,
            'cantin' => $dts3[$i],
            'projet' => $dts6[$i],
            'activits' => $dts4[$i],
            'infirmerie' => $dts1[$i],
            'bibliothek' => $dts2[$i],
            'cooperativ' => $dts5[$i],
            'report_quarter_id' => $report,
          ]);
        }
        $i++;
      }
    }

    private function saveParaScolaire($dts1, $dts2, $dts3, $dts4, $dts5, $report, $str) {
      $i = 0;
      while($i < count($dts1) ) {
        $data = ActivitParaScolaire::where('report_quarter_id', $report)->where('type', $str)->where('order', $i+1)->first();
        if($data){
          $data->update([
            'activit' => $dts1[$i],
            'date_dbt' => $dts2[$i],
            'date_fin' => $dts3[$i],
            'user_id' => $dts5[$i],
            'inscription_id' => $dts4[$i],
          ]);
        }
        else{
          ActivitParaScolaire::create([
            'type' => $str,
            'order' => $i+1,
            'activit' => $dts1[$i],
            'date_dbt' => $dts2[$i],
            'date_fin' => $dts3[$i],
            'user_id' => $dts5[$i],
            'inscription_id' => $dts4[$i],
            'report_quarter_id' => $report,
          ]);
        }
        $i++;
      }
    }

    private function saveConseil($dts, $report, $str) {
      $i = 0;
      while($i < count($dts) ) {
        $data = ConseilSchool::where('report_quarter_id', $report)->where('type', $str)->where('order', $i+1)->first();
        if($data){
          $data->update([
            'user_id' => $dts[$i]
          ]);
        }
        else{
          ConseilSchool::create([
            'type' => $str,
            'order' => $i+1,
            'user_id' => $dts[$i],
            'report_quarter_id' => $report,
          ]);
        }
        $i++;
      }
    }

    private function saveAbdon($dts1, $dts2, $dts3, $report) {
      $i = 0;
      while($i < count($dts1) ) {
        $data = Abandon::where('report_quarter_id', $report)->where('order', $i+1)->first();
        if($data){
          $data->update([
            'motif' => $dts3[$i],
            'classe_id' => $dts1[$i],
            'inscription_id' => $dts2[$i]
          ]);
        }
        else{
          Abandon::create([
            'order' => $i+1,
            'motif' => $dts3[$i],
            'classe_id' => $dts1[$i],
            'inscription_id' => $dts2[$i],
            'report_quarter_id' => $report,
          ]);
        }
        $i++;
      }
    }

    private function saveGrsse($dts1, $dts2, $dts3, $dts4, $dts5, $report) {
      $i = 0;
      while($i < count($dts1) ) {
        $data = Grossesse::where('report_quarter_id', $report)->where('order', $i+1)->first();
        if($data){
          $data->update([
            'qualite' => $dts5[$i],
            'classe_id' => $dts1[$i],
            'date_depot' => $dts3[$i],
            'date_accouch' => $dts4[$i],
            'inscription_id' => $dts2[$i]
          ]);
        }
        else{
          Grossesse::create([
            'order' => $i+1,
            'qualite' => $dts5[$i],
            'classe_id' => $dts1[$i],
            'date_depot' => $dts3[$i],
            'date_accouch' => $dts4[$i],
            'inscription_id' => $dts2[$i],
            'report_quarter_id' => $report
          ]);
        }
        $i++;
      }
    }

    private function saveMalad($dts1, $dts2, $dts3, $dts4, $dts5, $report) {
      $i = 0;
      while($i < count($dts1) ) {
        $data = Maladie::where('report_quarter_id', $report)->where('order', $i+1)->first();
        if($data){
          $data->update([
            'dure' => $dts5[$i],
            'date' => $dts3[$i],
            'maladie' => $dts4[$i],
            'classe_id' => $dts1[$i],
            'inscription_id' => $dts2[$i]
          ]);
        }
        else{
          Maladie::create([
            'order' => $i+1,
            'dure' => $dts5[$i],
            'date' => $dts3[$i],
            'maladie' => $dts4[$i],
            'classe_id' => $dts1[$i],
            'inscription_id' => $dts2[$i],
            'report_quarter_id' => $report
          ]);
        }
        $i++;
      }
    }

    private function saveDcs($dts1, $dts2, $dts3, $dts4, $report) {
      $i = 0;
      while($i < count($dts1) ) {
        $data = Deces::where('report_quarter_id', $report)->where('order', $i+1)->first();
        if($data){
          $data->update([
            'date' => $dts3[$i],
            'cause' => $dts4[$i],
            'classe_id' => $dts1[$i],
            'inscription_id' => $dts2[$i]
          ]);
        }
        else{
          Deces::create([
            'order' => $i+1,
            'date' => $dts3[$i],
            'cause' => $dts4[$i],
            'classe_id' => $dts1[$i],
            'inscription_id' => $dts2[$i],
            'report_quarter_id' => $report
          ]);
        }
        $i++;
      }
    }
  }