<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Pdf_Sinistre extends CI_Controller {

    function __construct() {
        parent::__construct();
    }
    
    public function index($id){

      include 'pdfinclude/fpdf/mc_table.php';
      include 'pdfinclude/fpdf/pdf_config.php';
      header('Content-Type: text/html; charset=utf-8');

      $sinistre=$this->Model->getRequeteOne('SELECT * FROM `odk_declaration_sinistre` WHERE `ID_DECL_SINISTRE` = '.$id.'');
      $typesinistre=$this->Model->getRequeteOne('SELECT * FROM `odk_type_sinistre` WHERE `ID_TYPE_SINISTRE` = '.$sinistre['ID_TYPE_SINISTRE'].'');
      $sexesouscr=$this->Model->getRequeteOne('SELECT * FROM `rh_sexe` WHERE `ID_SEXE` = '.$sinistre['ID_SEXE'].'');
      $provinces=$this->Model->getRequeteOne('SELECT * FROM `syst_provinces` WHERE `PROVINCE_ID` = '.$sinistre['ID_PROVINCE'].'');
      $commune=$this->Model->getRequeteOne('SELECT * FROM `syst_communes` WHERE `COMMUNE_ID` = '.$sinistre['ID_COMMUNE'].'');
      $zone=$this->Model->getRequeteOne('SELECT * FROM `syst_zones` WHERE `ZONE_ID` = '.$sinistre['ID_ZONE'].'');
      $colline=$this->Model->getRequeteOne('SELECT * FROM `syst_collines` WHERE `COLLINE_ID` = '.$sinistre['ID_COLLINE'].'');
      $sinistre=$this->Model->getRequeteOne('SELECT * FROM `odk_declaration_sinistre` WHERE `ID_DECL_SINISTRE` = '.$id.'');

      $preuve=$this->Model->getRequete('SELECT * FROM `odk_declaration_sinistre_preuve` JOIN odk_preuve ON odk_preuve.ID_PREUVE = odk_declaration_sinistre_preuve.ID_PREUVE WHERE `ID_DECL_SINISTRE` = '.$id.'');


      

     



      
      // $info_conge=$this->Model->getRequeteOne('SELECT rh_responsabilite.DESCRIPTION AS FONCTION,ID_EXERCICE_SUIVANT, DATE_SYSTEME_DEMANDE, conge_credit.ID_EXERCICE AS ID_EXERCICE_CREDIT, conge_demande_conge.ID_EXERCICE AS ID_EXERCICE_DEMANDE,NB_JOURS_ACCORDE, ID_EXERCICE_PRECEDENT, DATE_DEBUT_CONGE_ACCORDE, DATE_FIN_CONGE_ACCORDE, organigramme_section.DESCRIPTION AS SECTION, organigramme_service.DESCRIPTION AS SERVICE, organigramme_direction.DESCRIPTION DIRECTION, rh_employe.NOM,  conge_demande_conge.ID_TYPE_CONGE, rh_employe.PRENOM, rh_employe.SIGNATURE, conge_credit.ID_CREDIT, conge_credit.NB_JOURS_CREDITE, conge_credit.NB_JOURS_RESTANT, config_exercice.DESCRIPTION AS EXERCICE, config_type_conge.NOM_CONGE FROM `conge_demande_conge` JOIN config_exercice ON config_exercice.ID_EXERCICE = conge_demande_conge.ID_EXERCICE LEFT JOIN conge_credit ON conge_credit.ID_EXERCICE = conge_demande_conge.ID_EXERCICE LEFT JOIN rh_employe ON rh_employe.ID_EMPLOYE = conge_demande_conge.ID_EMPLOYE LEFT JOIN organigramme_direction ON organigramme_direction.ID_DIRECTION = conge_demande_conge.ID_DIRECTION LEFT JOIN organigramme_service ON organigramme_service.ID_SERVICE = conge_demande_conge.ID_SERVICE LEFT JOIN organigramme_section ON organigramme_section.ID_SECTION = conge_demande_conge.ID_SECTION JOIN config_type_conge ON config_type_conge.ID_TYPE_CONGE = conge_demande_conge.ID_TYPE_CONGE LEFT JOIN rh_employe_affectation ON rh_employe_affectation.ID_EMPLOYE = rh_employe.ID_EMPLOYE JOIN rh_responsabilite ON rh_responsabilite.ID_RESPONSABILITE = rh_employe_affectation.ID_RESPONSABILITE WHERE 1 AND conge_demande_conge.ID_DEMANDE_CONGE = '.$id.'  AND conge_credit.ID_EMPLOYE = '.$infoconge['ID_EMPLOYE'].'');


      $bs=base_url().'conge/Demande_Conge_Det/detail/'.$id;
      $this->notifications->generateQrcode(utf8_decode($bs),'Conge_'.$id);
      $qrcode_name = 'Conge_'.$id.'.png';

      
     

      $urlpass = base_url();
      $elementspas = explode("/", $urlpass);
      $nouveau_urlpass = null ;
        $indicepass = sizeof($elementspas);
        for ($i = 0; $i < ($indicepass - 2); $i++) {
            $nouveau_urlpass .=$elementspas[$i] . '/';
        }

      // $logo_banque='dist/logo_pdf.png';
     

      $pdf = new PDF_CONFIG('P','mm','A4');
      $pdf->addPage();
      $pdf->Ln(20);
      $pdf->SetFont('Arial','B',15);

      // $pdf->SetX(10);
      $pdf->Cell(190,5,utf8_decode(strtoupper('Fiche de declaration de sinistre')),0,1,'C');
      
      $pdf->SetFont('Arial','',11);
      $pdf->Ln(4);
      $pdf->SetX(10);
      $pdf->Cell(55,6,utf8_decode('Type de sinistre'),0,0,'L');
      $pdf->Cell(100,6,utf8_decode(':'.$typesinistre['DESCRIPTION'].' '),0,1,'L');
      $pdf->Cell(55,6,utf8_decode('Nom du souscripteur'),0,0,'L');
      $pdf->Cell(100,6,utf8_decode(':'.$sinistre['NOM_SOUSCRI'].' '),0,1,'L');
      $pdf->Cell(55,6,utf8_decode('Prénom du souscripteur'),0,0,'L');
      $pdf->Cell(100,6,utf8_decode(':'.$sinistre['PRENOM_SOUSCRI'].' '),0,1,'L');
      $pdf->Cell(55,6,utf8_decode('Matricule'),0,0,'L');
      $pdf->Cell(100,6,utf8_decode(':'.$sinistre['MATRICULE_SOUSCRI'].' '),0,1,'L');
      $pdf->Cell(55,6,utf8_decode('Numéro de police'),0,0,'L');
      $pdf->Cell(100,6,utf8_decode(':'.$sinistre['POLICE_SOUSCRI'].' '),0,1,'L');
      $pdf->Cell(55,6,utf8_decode('Sexe du souscripteur'),0,0,'L');
      $pdf->Cell(100,6,utf8_decode(':'.$sexesouscr['DESCRIPTION'].' '),0,1,'L');
      $pdf->Cell(55,6,utf8_decode('Date effet'),0,0,'L');
      $pdf->Cell(100,6,utf8_decode(':'.$sinistre['DATE_EFFET'].' '),0,1,'L');
      $pdf->Cell(55,6,utf8_decode('Date echeance'),0,0,'L');
      $pdf->Cell(100,6,utf8_decode(':'.$sinistre['DATE_ECHEANCE'].' '),0,1,'L');
      $pdf->Cell(55,6,utf8_decode('Nom du bénéficiaire'),0,0,'L');
      $pdf->Cell(100,6,utf8_decode(':'.$sinistre['NOM_BEN'].' '),0,1,'L');
      $pdf->Cell(55,6,utf8_decode('Prénom du bénéficiaire'),0,0,'L');
      $pdf->Cell(100,6,utf8_decode(':'.$sinistre['PRENOM_BEN'].' '),0,1,'L');
      $pdf->Cell(55,6,utf8_decode('CNI du bénéficiaire'),0,0,'L');
      $pdf->Cell(100,6,utf8_decode(':'.$sinistre['CNI_BEN'].' '),0,1,'L');
      $pdf->Cell(55,6,utf8_decode('Province'),0,0,'L');
      $pdf->Cell(100,6,utf8_decode(':'.$provinces['PROVINCE_NAME'].' '),0,1,'L');
      $pdf->Cell(55,6,utf8_decode('Commune'),0,0,'L');
      $pdf->Cell(100,6,utf8_decode(':'.$commune['COMMUNE_NAME'].' '),0,1,'L');
      $pdf->Cell(55,6,utf8_decode('Zone'),0,0,'L');
      $pdf->Cell(100,6,utf8_decode(':'.$zone['ZONE_NAME'].' '),0,1,'L');
      $pdf->Cell(55,6,utf8_decode('Colline'),0,0,'L');
      $pdf->Cell(100,6,utf8_decode(':'.$colline['COLLINE_NAME'].' '),0,1,'L');
      $pdf->Cell(55,6,utf8_decode('Sous Colline'),0,0,'L');
      $pdf->Cell(100,6,utf8_decode(':'.$sinistre['SOUSCOLINE'].' '),0,1,'L');
      $pdf->Cell(55,6,utf8_decode('Code déclarant'),0,0,'L');
      $pdf->Cell(100,6,utf8_decode(':'.$sinistre['CODE_DECLARA'].' '),0,1,'L');
      $pdf->Cell(55,6,utf8_decode('Date déclaration'),0,0,'L');
      $dateInsertion = date('H:i d/m/Y', strtotime($sinistre['DATE_INSERTION']));
      $pdf->Cell(100, 6, utf8_decode(':' . $dateInsertion . ' '), 0, 1, 'L');
      
      $pdf->Ln(15);
      $pdf->Cell(190,5,utf8_decode(strtoupper('Preuve Prise sur les pages suivant')),0,1,'C');
      // $pdf->Cell(100, 6, utf8_decode('Preuve Prise '), 0, 1, 'L');

      // $pdf->Image(FCPATH . 'uploads/QRCODE/'.$qrcode_name,85,90,25,0);
      $largeur = 175; // Spécifiez la largeur souhaitée de l'image
      $hauteur = 0; // Spécifiez la hauteur souhaitée de l'image (0 pour garder les proportions)

      $espacementVertical = 20; // Spécifiez l'espacement vertical entre les images

      $y = 0; // Coordonnée verticale initiale
      $newhauteur = 200;
  
    foreach ($preuve as $imagepreuve) {

      // $pdf = new PDF_CONFIG('P','mm','A4');
      $pdf->AddPage(); // Ajoute une nouvelle page
      // $pdf->Ln(4);
      // $pdf->SetX(10);
      $imagePath = $nouveau_urlpass . 'uploads/document_preuve/' . $imagepreuve['IMAGE'];
     $imageSize = getimagesize($imagePath);
    


      // $pdf->addPage();
      $pdf->Ln(20);
      $pdf->SetFont('Arial','B',15);

      // $pdf->SetX(10);
      $pdf->Cell(190,5,utf8_decode(' '.$imagepreuve['DECRIPTION'].''),0,1,'C');


    $largeurs = $imageSize[0]; // Récupère la largeur de l'image

    // $largeur = $imageSize[0]; // Récupère la largeur de l'image
    $hauteurs = $imageSize[1]; // Récupère la hauteur de l'image

    
    $y += $hauteur + $espacementVertical; // Met à jour la coordonnée verticale pour la prochaine image
    $pdf->Image(''.$nouveau_urlpass.'uploads/document_preuve/'.$imagepreuve['IMAGE'].'', 20, 45, $largeur, $hauteur);
    $newhauteur += $newhauteur + $hauteurs;


      
        }
  

      $pdf->Output('pdf_sinistre_'.$id.'.pdf','I');

    }


    

    
}
