<?php
defined( 'BASEPATH' ) OR exit( 'No direct script access allowed' );

class Declaration_incident extends CI_Controller {

    public function index( $value = '' ) {
        # code...

        $fluxjson = file_get_contents( 'php://input' );
        //recuperation du flux json
        $this->Model->insert_last_id('json_declaration_sinistre', array( 'DATA_JSON'=>$fluxjson ) );
        //
        //$this->traitement();

    }

    public function traitement( $value = '' ) {

        $sql = 'SELECT `ID`,`DATA_JSON` FROM `json_declaration_sinistre` WHERE `TRAITER`=0';
        $DATA_JSON = $this->Model->getRequete( $sql );

        foreach ( $DATA_JSON as $key ) {
            $key[ 'DATA_JSON' ] = str_replace( '/', '_', $key[ 'DATA_JSON' ] );
            $mesdonne = json_decode( $key[ 'DATA_JSON' ] );

            // echo '<pre>';
            // print_r($mesdonne );
            // echo '</pre>';

            $ID_TYPE_SINISTRE = null;
            if ( isset( $mesdonne->identification_type_sinistre ) ) {

                $ID_TYPE_SINISTRE = $mesdonne->identification_type_sinistre;
            }

            $NOM_SOUSCRI = null;
            if ( isset( $mesdonne->identification_nom ) ) {

            $NOM_SOUSCRI = $mesdonne->identification_nom;
            }

            $PRENOM_SOUSCRI = null;
            if ( isset( $mesdonne->identification_prenom ) ) {

            $PRENOM_SOUSCRI = $mesdonne->identification_prenom;
            }

            $MATRICULE_SOUSCRI = null;
            if ( isset( $mesdonne->identification_matricule ) ) {

            $MATRICULE_SOUSCRI = $mesdonne->identification_matricule;
            }

            $POLICE_SOUSCRI = null;
            if ( isset( $mesdonne->identification_num_police ) ) {

            $POLICE_SOUSCRI = $mesdonne->identification_num_police;
            }

            $ID_SEXE = null;
            if ( isset( $mesdonne->identification_sexe_list ) ) {

            $ID_SEXE = $mesdonne->identification_sexe_list;
            }

            $DATE_EFFET = null;
            if ( isset( $mesdonne->identification_date_effet ) ) {

            $DATE_EFFET = $mesdonne->identification_date_effet;
            }

            $DATE_ECHEANCE = null;
            if ( isset( $mesdonne->identification_date_echeance ) ) {

            $DATE_ECHEANCE = $mesdonne->identification_date_echeance;
            }

            $NOM_BEN = null;
            if ( isset( $mesdonne->identification_nom_beneficiaire ) ) {

            $NOM_BEN = $mesdonne->identification_nom_beneficiaire;
            }

            $PRENOM_BEN = null;
            if ( isset( $mesdonne->identification_prenom_beneficiaire ) ) {

            $PRENOM_BEN = $mesdonne->identification_prenom_beneficiaire;
            }

            $CNI_BEN = null;
            if ( isset( $mesdonne->identification_cni_beneficiaire ) ) {

            $CNI_BEN = $mesdonne->identification_cni_beneficiaire;
            }

            $CODE_DECLARA = null;
            if ( isset( $mesdonne->code_declarant ) ) {

            $CODE_DECLARA = $mesdonne->code_declarant;
            }


            $ID_PROVINCE = null;
            if ( isset( $mesdonne->localite_province ) ) {

             $ID_PROVINCE = $mesdonne->localite_province;
            }

            $ID_COMMUNE = null;
            if ( isset( $mesdonne->localite_commune ) ) {

                $ID_COMMUNE = $mesdonne->localite_commune;
            }

            $ID_ZONE = null;
            if ( isset( $mesdonne->localite_zone ) ) {

                $ID_ZONE = $mesdonne->localite_zone;
            }
            $ID_COLLINE = null;
            if ( isset( $mesdonne->localite_colline ) ) {

                $ID_COLLINE = $mesdonne->localite_colline;
            }

            $LATITUDE = NULL;
            $LONGITUDE = NULL;

            if ( isset( $mesdonne->gps) ) {
                $coord = explode( ' ', $mesdonne->gps );
                
                            echo '<pre>';
            print_r($coord );
            echo '</pre>';
                $LATITUDE = $coord[ 0 ];
                $LONGITUDE = $coord[ 1 ];
            }


            $SOUSCOLINE = null;
            if ( isset( $mesdonne->localite_sous_colline ) ) {

                $SOUSCOLINE = $mesdonne->localite_sous_colline;
            }
            
            //NEW
            
            $TEL_BEN = null;
            if ( isset( $mesdonne->identification_tel_beneficiaire ) ) {

                $TEL_BEN = $mesdonne->identification_tel_beneficiaire;
            }
            
            $BANQUE_BEN = null;
            if ( isset( $mesdonne->identification_microfinance_beneficiaire ) ) {

            $BANQUE_BEN = $mesdonne->identification_microfinance_beneficiaire;
            }
            
            $COMPTE_BEN = null;
            if ( isset( $mesdonne->identification_compte_beneficiaire ) ) {

                $COMPTE_BEN = $mesdonne->identification_compte_beneficiaire;
            }
            
            
            if ( isset( $mesdonne->identification_banque_beneficiaire ) ) {

                $BANQUE_BEN = $mesdonne->identification_banque_beneficiaire;
            }
            
            $ID_MODE=null;
            if ( isset( $mesdonne->identification_moyen_paiement ) ) {

                $ID_MODE = $mesdonne->identification_moyen_paiement;
            }


            $incident = array(
                
                'ID_MODE'=>$ID_MODE,
                'ID_TYPE_SINISTRE' =>$ID_TYPE_SINISTRE,
                'NOM_SOUSCRI' =>$NOM_SOUSCRI,
                'PRENOM_SOUSCRI' =>$PRENOM_SOUSCRI,
                'MATRICULE_SOUSCRI' =>$MATRICULE_SOUSCRI,
                'POLICE_SOUSCRI' =>$POLICE_SOUSCRI,
                'ID_SEXE' =>$ID_SEXE,
                'DATE_EFFET' =>$DATE_EFFET,
                'DATE_ECHEANCE' =>$DATE_ECHEANCE,
                'NOM_BEN' =>$NOM_BEN,
                'LONGITUDE'=>$LONGITUDE,
                'LATITUDE'=>$LATITUDE,
                'PRENOM_BEN'=>$PRENOM_BEN,
                'CNI_BEN'=>$CNI_BEN,
                'ID_PROVINCE'=>$ID_PROVINCE,
                'ID_COMMUNE'=>$ID_COMMUNE,
                'ID_ZONE'=>$ID_ZONE,
                'ID_COLLINE'=>$ID_COLLINE,
                'SOUSCOLINE'=>$SOUSCOLINE,
                'CODE_DECLARA'=>$CODE_DECLARA,
                
                'TEL_BEN'=>$TEL_BEN,
                'BANQUE_BEN'=>$BANQUE_BEN,
                'COMPTE_BEN'=>$COMPTE_BEN,
                
            );

            $ID_DECL_SINISTRE = $this->Model->insert_last_id( 'odk_declaration_sinistre', $incident );

            //GESTION DES PREUVES
       
					$preuves = null;
					if (isset($mesdonne->preuve)) 
					{

						$preuves = $mesdonne->preuve;
					}

                    $i=0;

				foreach ($preuves as $preuveskey) 
					{ 
                        //$i++;
						$IMAGE = null;
						if (isset($preuveskey->preuve_images)) {
							$IMAGE = $preuveskey->preuve_images;
						}

                        	$ID_PREUVE = null;
						if (isset($preuveskey->preuve_preuve_list)) {
							$ID_PREUVE = $preuveskey->preuve_preuve_list;
						}

		
						$data_preuves = array(

							'ID_DECL_SINISTRE'=>$ID_DECL_SINISTRE,
							'IMAGE'=>$IMAGE,
							'ID_PREUVE'=>$ID_PREUVE,
						
						);

						$this->Model->create('odk_declaration_sinistre_preuve',$data_preuves);
						
			  if ( isset( $mesdonne->_attachments ) ) {
                # code...
                $image_url = $mesdonne->_attachments[$i++]->download_medium_url;
                $image_url = str_replace( 'https:__kc.kobotoolbox.org_media_medium', 'https://kc.kobotoolbox.org/media/medium', $image_url );
                	$image_url = str_replace(
					"https:__kc-eu.kobotoolbox.org_media_medium", 
					"https://kc-eu.kobotoolbox.org/media/medium", $image_url);
                $lienimageauserveur = '/home/uxmfao/public_html/documents.jagsolutions.bi/uploads/document_preuve/';
                
                // Create a stream context with basic authentication
				$context = stream_context_create([
					'http' => [
						'header' => 'Authorization: Basic ' . base64_encode('socar_sinistre:Socar@2025')
					]
				]);
                

                file_put_contents( $lienimageauserveur.$IMAGE, file_get_contents($image_url, false, $context));

            }
						
						
						

					}


            $this->Model->update( 'json_declaration_sinistre', array( 'ID'=>$key[ 'ID' ] ), array( 'TRAITER'=>1 ) );

        }
    }

}