<?php 
 /**
  * 
  */
 class Client extends CI_Controller
 {
  
  function __construct()
  {
    parent::__construct();
    $this->load->library('Mylibrary');
    $this->ci = & get_instance();
    $this->ci->load->library("user_agent");
    // $this->Is_Connected();
    // $this->verification();

  }

  public function Is_Connected()
  {

   if (empty($this->session->userdata('SOCAR_ID_USER')))
   {
     redirect(base_url('Login/'));
   }
 }

 


 public function index()
 {
  $title = "Enregistrement Client";

  
        // $data['typebout']=$this->Model->getList('syst_type_bouteille',array());
  $data['typeclient']=$this->Model->getList('client_type',array());
  $data['title'] = $title;
  $data['provinces'] = $this->Model->getRequete("SELECT `PROVINCE_ID`, `PROVINCE_NAME` FROM `provinces` ORDER BY PROVINCE_NAME ASC");
        // if ($nrequette['RANGE_FROM'] == NULL) {
  $crequette= $this->Model->getRequeteOne("SELECT COUNT(ID_CLIENT) AS NOMBRE FROM `client_client`");
  $number1 = $crequette['NOMBRE'] + 1;
  $formatted1 = str_pad($number1, 4, '0', STR_PAD_LEFT);
  $data['NUMERO_CLIENT'] = 'C-'.$formatted1;
  
        //  }
        //  else{
        //   $datas['NUM_ASSURANCE'] = $nrequette['NEW_NUMBER'];
        //   // $PLAFOND =;
        //  }
  $this->load->view("Client_Add_View",$data);
}

function get_commune()
{
  $province_perso_ref = $this->input->post('province_perso_ref');

  $query = $this->Model->getRequete("SELECT `commune_id`, `commune_name` FROM `communes` WHERE province_id=" . $province_perso_ref . " ORDER BY commune_name ASC ");
  // print_r($query);exit();
  $select = "<option value=''>-</option>";
  foreach ($query as $key) {
      # code...
    $select .= "<option value='" . $key['commune_id'] . "'>" . $key['commune_name'] . "</option>";
  }

  echo $select;
}



function get_zones()
{
  $commune_perso_ref = $this->input->post('commune_perso_ref');

  $query = $this->Model->getRequete("SELECT `ZONE_ID`, `ZONE_NAME`, `COMMUNE_ID` FROM `zones` WHERE COMMUNE_ID=" . $commune_perso_ref . " ORDER BY ZONE_NAME ASC");

  $select = "<option value=''>-</option>";
  foreach ($query as $key) {
      # code...
    $select .= "<option value='" . $key['ZONE_ID'] . "'>" . $key['ZONE_NAME'] . "</option>";
  }

  echo $select;
}

function get_collines()
{
  $zone_perso_ref = $this->input->post('zone_perso_ref');

  $query = $this->Model->getRequete("SELECT `colline_id`, `colline`, `zone_id` FROM `collines` WHERE zone_id=" . $zone_perso_ref . " AND statut=1 ORDER BY colline ASC");

  $select = "<option value=''>-</option>";
  foreach ($query as $key) {
      # code...
    $select .= "<option value='" . $key['colline_id'] . "'>" . $key['colline'] . "</option>";
  }

  echo $select;
}


public function add()
{
  
  $NUMERO_CLIENT=$this->input->post('NUMERO_CLIENT');
  $ID_CLIENT_TYPE=$this->input->post('ID_CLIENT_TYPE');
  $NOM=$this->input->post('NOM');
  $PRENOM=$this->input->post('PRENOM');
  $TELEPHONE=$this->input->post('TELEPHONE');
  $MAIL=$this->input->post('MAIL');
  $RC=$this->input->post('RC');
  $NIF=$this->input->post('NIF');
  $ADRESSE=$this->input->post('ADRESSE');
  $ID_STANDARD=$this->input->post('ID_STANDARD');

  $PROVINCE_ID=$this->input->post('province_perso_ref');
  $COMMUNE_ID=$this->input->post('commune_perso_ref');
  $ZONE_ID=$this->input->post('zone_perso_ref');
  $COLLINE_ID=$this->input->post('colline_perso_ref');
  
  
  
  $dataclient = array(          
    'NUMERO_CLIENT'=>$NUMERO_CLIENT,  
    'ID_CLIENT_TYPE'=>$ID_CLIENT_TYPE,  
    'NOM'=>$NOM,  
    'PRENOM'=>$PRENOM,  
    'TELEPHONE'=>$TELEPHONE,  
    'MAIL'=>$MAIL,  
    'RC'=>$RC,  
    'NIF'=>$NIF,  
    'ADRESSE'=>$ADRESSE,
    'ID_STANDARD'=>$ID_STANDARD,

    'PROVINCE_ID'=>$PROVINCE_ID,  
    'COMMUNE_ID'=>$COMMUNE_ID,  
    'ZONE_ID'=>$ZONE_ID,
    'COLLINE_ID'=>$COLLINE_ID,
    
  );
  $this->form_validation->set_rules('NUMERO_CLIENT', 'Numero', 'required');
  $this->form_validation->set_rules('ID_CLIENT_TYPE', 'Type Client', 'required');
  $this->form_validation->set_rules('NOM', 'Nom', 'required');
  $this->form_validation->set_rules('TELEPHONE', 'Nom', 'required');
  $this->form_validation->set_rules('ADRESSE', 'Nom', 'required');
  
  
    //SI Valide
  if ($this->form_validation->run() == FALSE){        
    
    $title = "Enregistrement Client";

    
    $data['typeclient']=$this->Model->getList('client_type',array());
    $data['title'] = $title;

    $crequette= $this->Model->getRequeteOne("SELECT COUNT(ID_CLIENT) AS NOMBRE FROM `client_client`");
    $number1 = $crequette['NOMBRE'] + 1;
    $formatted1 = str_pad($number1, 4, '0', STR_PAD_LEFT);
    $data['NUMERO_CLIENT'] = 'C-'.$formatted1;
    

    $message = "<div class='alert alert-danger' id='message'>
    Donne manquantes, Processus échoué.
    <button type='button' class='close' data-dismiss='alert'>&times;</button>
    </div>";

    
    $this->session->set_flashdata(array('message'=>$message));
    $this->load->view("Client_Add_View",$data);

  }else{
        // prod_production

    $pproduct = $this->Model->insert_last_id('client_client',$dataclient);
    
    $message = "<div class='alert alert-success'  id='message'>
    Enregistrement fait avec succès.
    <button type='button' class='close' data-dismiss='alert'>&times;</button>
    </div>";       
    $this->session->set_flashdata(array('message'=>$message));
    redirect(base_url('donnee/Client'));
  }
}



public function listing()
{
  
  $data['title'] = "Client";
  $this->load->view('Client_List_View',$data);

}



public function index_update($id)
{
  $title = "Modification Client";
  $ID_CLIENT =  $id;
  
  $data['typeclient']=$this->Model->getList('client_type',array());
  $data['title'] = $title;
  $data['provinces'] = $this->Model->getRequete("SELECT `PROVINCE_ID`, `PROVINCE_NAME` FROM `provinces` ORDER BY PROVINCE_NAME ASC");
  $data['communes'] = $this->Model->getRequete("SELECT `commune_id`, `commune_name` FROM `communes` ORDER BY commune_name ASC");

  $data['zones'] = $this->Model->getRequete("SELECT `ZONE_ID`, `ZONE_NAME` FROM `zones` ORDER BY ZONE_NAME ASC");

  $data['collines'] = $this->Model->getRequete("SELECT `colline_id`, `colline`, `zone_id`,`statut` FROM `collines` WHERE statut=1 ORDER BY colline ASC");


  $dataclient= $this->Model->getRequeteOne("SELECT * FROM `client_client` WHERE ID_CLIENT = ".$ID_CLIENT."");
  
  $data['NUMERO_CLIENT'] = $dataclient['NUMERO_CLIENT'];
  $data['dataclient'] = $dataclient;

  $this->load->view("Client_Update_View",$data);
}




public function update()
{
  
  $NUMERO_CLIENT=$this->input->post('NUMERO_CLIENT');
  $ID_CLIENT_TYPE=$this->input->post('ID_CLIENT_TYPE');
  $NOM=$this->input->post('NOM');
  $PRENOM=$this->input->post('PRENOM');
  $TELEPHONE=$this->input->post('TELEPHONE');
  $MAIL=$this->input->post('MAIL');
  $RC=$this->input->post('RC');
  $NIF=$this->input->post('NIF');
  $ADRESSE=$this->input->post('ADRESSE');
  $ID_CLIENT=$this->input->post('ID_CLIENT');

  
  $PROVINCE_ID=$this->input->post('province_perso_ref');
  $COMMUNE_ID=$this->input->post('commune_perso_ref');
  $ZONE_ID=$this->input->post('zone_perso_ref');
  $COLLINE_ID=$this->input->post('colline_perso_ref');
  
  $dataclient = array(          
    'NUMERO_CLIENT'=>$NUMERO_CLIENT,  
    'ID_CLIENT_TYPE'=>$ID_CLIENT_TYPE,  
    'NOM'=>$NOM,  
    'PRENOM'=>$PRENOM,  
    'TELEPHONE'=>$TELEPHONE,  
    'MAIL'=>$MAIL,  
    'RC'=>$RC,  
    'NIF'=>$NIF,  
    'ADRESSE'=>$ADRESSE,
    'PROVINCE_ID'=>$PROVINCE_ID,  
    'COMMUNE_ID'=>$COMMUNE_ID,  
    'ZONE_ID'=>$ZONE_ID,
    'COLLINE_ID'=>$COLLINE_ID,
  );
  $this->form_validation->set_rules('NUMERO_CLIENT', 'Numero', 'required');
  $this->form_validation->set_rules('ID_CLIENT_TYPE', 'Type Client', 'required');
  $this->form_validation->set_rules('NOM', 'Nom', 'required');
  $this->form_validation->set_rules('TELEPHONE', 'Nom', 'required');
  $this->form_validation->set_rules('ADRESSE', 'Nom', 'required');
  
  
    //SI Valide
  if ($this->form_validation->run() == FALSE){        
    
    $title = "Modification Client";
    
    $data['typeclient']=$this->Model->getList('client_type',array());
    $data['title'] = $title;

    $dataclient= $this->Model->getRequeteOne("SELECT * FROM `client_client` WHERE ID_CLIENT = ".$ID_CLIENT."");
    
    $data['NUMERO_CLIENT'] = $dataclient['NUMERO_CLIENT'];
    $data['dataclient'] = $dataclient;

    
    

    $message = "<div class='alert alert-danger' id='message'>
    Donne manquantes, Processus échoué.
    <button type='button' class='close' data-dismiss='alert'>&times;</button>
    </div>";

    
    $this->session->set_flashdata(array('message'=>$message));
    $this->load->view("Client_Update_View",$data);

  }else{
        // prod_production

    $pproduct = $this->Model->update('client_client',array('ID_CLIENT'=>$ID_CLIENT),$dataclient);
    
    $message = "<div class='alert alert-success'  id='message'>
    Enregistrement fait avec succès.
    <button type='button' class='close' data-dismiss='alert'>&times;</button>
    </div>";       
    $this->session->set_flashdata(array('message'=>$message));
    redirect(base_url('donnee/Client/listing'));
  }
}



public function desactiver($id)
{
  
  $ID_CLIENT=$id;
  $dataclient = array('ID_STATUS'=>2);
  $pproduct = $this->Model->update('client_client',array('ID_CLIENT'=>$ID_CLIENT),$dataclient);
  
  $message = "<div class='alert alert-success'  id='message'>
  Enregistrement fait avec succès.
  <button type='button' class='close' data-dismiss='alert'>&times;</button>
  </div>";       
  $this->session->set_flashdata(array('message'=>$message));
  redirect(base_url('donnee/Client/listing'));
  
}

public function reactiver($id)
{
  
  $ID_CLIENT=$id;
  $dataclient = array('ID_STATUS'=>1);
  $pproduct = $this->Model->update('client_client',array('ID_CLIENT'=>$ID_CLIENT),$dataclient);
  
  $message = "<div class='alert alert-success'  id='message'>
  Enregistrement fait avec succès.
  <button type='button' class='close' data-dismiss='alert'>&times;</button>
  </div>";       
  $this->session->set_flashdata(array('message'=>$message));
  redirect(base_url('donnee/Client/listing'));
  
}

}                        
