<?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;

        // 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($prov){
  //   $comm = $this->Model->getList('syst_communes',array('PROVINCE_ID'=>$prov));
  //   $html = '<option value="" > --- </option>';
  //   foreach ($comm as $comm) {
  //     $html .= '<option value="'.$comm['COMMUNE_ID'].'">'.$comm['COMMUNE_NAME'].'</option>';
  //   }
  //   echo $html;

  // }


  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');
    
    
    
    
     $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,
        );
     $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;

          $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');
    
     $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,
        );
     $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'));
     
  }

 }                        