<?php

namespace App\Controllers;
use App\Models\ContributionModel;


class AssiseDaaraController extends BaseController
{
   public function acceuil(){
    return view('AssiseDaara/acceuil');
   }

   public function contribution(){
      return view ('AssiseDaara/contribution');
   }

   public function infos(){
      return view ('AssiseDaara/infos');
   }

   public function statistique(){
      $model = new ContributionModel();
      $data['totalContributions'] = $model->getTotalContributions();
      $data['totalFormulaire'] = $model->compteParCanal('formulaire');
      $data['totalWhatsapp']   = $model->compteParCanal('whatsapp');
      $data['totalFichiers'] = $model->compteFiles();
      $data['assises'] = $model->findAll();
      return view ('AssiseDaara/statistique',$data);
   }

    public function ajContribution()
    {
        $ContributionModel = new ContributionModel();

        //  Récupération des données
        $data = [
            'nom'      => $this->request->getPost('nom'),
            'telephone'    => $this->request->getPost('telephone'),
            'email'      => $this->request->getPost('email'),
            'region'      => $this->request->getPost('region'),
            'departement'      => $this->request->getPost('departement'),
            'thematique'      => $this->request->getPost('thematique'),
            'description'   => $this->request->getPost('description'),
            'canal'   => 'formulaire',
        ];

        //  Gestion image
        $file = $this->request->getFile('documents');

        if ($file && $file->isValid() && !$file->hasMoved()) {
            $newName = $file->getRandomName();
            $file->move('uploads/contributions', $newName);
            $data['file'] = $newName;
        }

        $ContributionModel->insert($data);

        return redirect()->to(base_url('AssiseDaara/contribution'))
                         ->with('success', 'Contribution ajouté avec succès');
    }

    public function jsonstatistique()
{
    $model = new ContributionModel();

    $whatsapp = $model->where('canal', 'whatsapp')->countAllResults();
    $formulaire = $model->where('canal', 'formulaire')->countAllResults();
    $total = $model->getTotalContributions();

    return $this->response->setJSON([
        'whatsapp'   => $whatsapp,
        'formulaire' => $formulaire
    ]);
}
    public function jsoncontribution(){
        $assises = $model->findAll();
        return $this->response->setJSON([
        'assises'   => $assises
    ]);
}



    


//     public function statistique(){
//   // Compter les contributions WhatsApp
//         $model=new ContributionModel();
//         $nbWhatsapp = $modelassise->where('canal', 'whatsapp')->countAllResults();
//         $nbformulaire = $model->where('canal', 'formulaire')->countAllResults();
//         $nbFichiers = $modelassise->where('fichier IS NOT NULL')->where('fichier !=', '')->countAllResults();
//         $assises = $modelassise->where('canal', 'formulaire')->orderBy('date_creation', 'DESC')->findAll();

//         $data['assises'] = $assises;
//         $data['nbFichiers'] = $nbFichiers;  
//         $data['total']= $modelassise->countAll();     
//         $data['assises']=$modelassise->findAll() ;
//         $data['nbWhatsapp'] = $nbWhatsapp;
//         $data['nbformulaire'] = $nbformulaire;
        
//         return view('assise/V_statas',$data);
//     }
//     public function statsSource()
//     {
//         $model = new M_assise();

//         // Comptage par source
//         $whatsapp   = $model->where('canal', 'whatsapp')->countAllResults();
//         $formulaire = $model->where('canal', 'formulaire')->countAllResults();

//         return $this->response->setJSON([
//             'whatsapp'   => $whatsapp,
//             'formulaire' => $formulaire
//         ]);
//     }
//   public function listSuggestions()
//     {
//         $model = new M_assise();

//         // Optionnel : trier par thématique
//         $suggestions = $model->orderBy('thematique', 'ASC')->findAll();

//         return $this->response->setJSON($suggestions);
//     }
}