<?php
class Fournisseursprojets extends CI_Controller {

    function __construct()
    {
        parent::__construct('fournisseursprojets');
        $this->load->helper('url');
        $this->load->library('pagination');
        $this->load->model('Fournisseursprojets_model');
        $this->lang->load('fournisseursprojets');
        $this->load->model('Tache_model');
        $this->load->library('table');
        $this->load->helper('form');
        $this->load->model('FxForm_model');

        $this->load->model('Config_model');
        $this->load->library('session');
    }
    public function index()
    {
        $this->load->view('fournisseursprojets/header' );
        $this->load->view('fournisseursprojets/list' );
        $this->load->view('fournisseursprojets/footer' );
    }
    public function form($mem_id,$error='')
    {
        $error = []; // initialise comme tableau
      //  $error['error']=array('type'=>'error','message'=>'test');
        $error['error']=$error;



        // prépare tout les info pour la forme
        $data=$this->Fournisseursprojets_model->mod_getForminfo($mem_id);
        $data['contacts']=$this->Fournisseursprojets_model->mod_listcontact($mem_id);

        $this->load->view('fournisseursprojets/header' );
        $this->load->view('fournisseursprojets/form',$data );
        $this->load->view('fournisseursprojets/footer',$error );
    }
    public function loadDatabad($record=0,$filtre1='',$filtre2='',$filtre3='') {

        $filtre1=($filtre1=='null'?'':$filtre1);
        $filtre2=($filtre2=='null'?'':$filtre2);
        $recordPerPage = 10;
        $config["cur_page"] = $record;
        if($record != 0){
            $record = ($record-1) * $recordPerPage;
        }

        $recordCount2 = $this->Fournisseursprojets_model->mod_getRecordCount();

        $recordCount = $this->Fournisseursprojets_model->mod_getRecordCountfilter($filtre1,$filtre2,$filtre3);
        $client_list = $this->Fournisseursprojets_model->mod_getRecords($record,$recordPerPage,$filtre1,$filtre2,$filtre3);
        $config['base_url'] = base_url().'index.php/Fournisseursprojets/loadData';
        $config['use_page_numbers'] = TRUE;
        $config['next_link'] = 'Next';
        $config['prev_link'] = 'Previous';
        $config['total_rows'] = $recordCount;
        $config['per_page'] = $recordPerPage;

        $choice = $config["total_rows"] / $config["per_page"];
        $config["uri_segment"] = 2;
        $config["num_links"] = 2;
        $config['full_tag_open'] = '<div ><ul class="pagination">';
        $config['full_tag_close'] = '</ul></div><!--pagination-->';

        $config['first_link'] = '&laquo; First';
        $config['first_tag_open'] = '<li class="prev page">';
        $config['first_tag_close'] = '</li>';

        $config['last_link'] = 'Last &raquo;';
        $config['last_tag_open'] = '<li class="next page">';
        $config['last_tag_close'] = '</li>';

        $config['next_link'] = 'Next &rarr;';
        $config['next_tag_open'] = '<li class="next page">';
        $config['next_tag_close'] = '</li>';

        $config['prev_link'] = '&larr; Previous';
        $config['prev_tag_open'] = '<li class="prev page">';
        $config['prev_tag_close'] = '</li>';

        $config['cur_tag_open'] = '<li class="active"><a href="">';
        $config['cur_tag_close'] = '</a></li>';

        $config['num_tag_open'] = '<li class="page">';
        $config['num_tag_close'] = '</li>';
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $data['recordCount']=$recordCount2;
        // creation du tableau


        // prepare header

        $mem_link_new=anchor('fournisseursprojets/form/0' , ' ', 'class="btn btn-primary btn-circle glyphicon glyphicon-pencil" ');
        $header=array($mem_link_new,
            'Compagnie',
            'Ville',
            'Type',


            '<i class="fa fa-fw fa-phone text-muted hidden-md hidden-sm hidden-xs"></i>Téléphone ');

        $nbitem=0;


        // filter
        global $mem_data;
           $data_row = array();
            $data_row[]=array('data'=>'Filtre', 'align' => 'left');
        // -------------------------------------------------------------------------
      //      $mem_champ='filtre_1';
     //       $mem_value=trim($filtre1);
     //       $this->FxForm_model->fxinputtxt($mem_champ,$mem_value,'','','','filtre_1');

    //    $mem_filtre=form_input($mem_data['input'][$mem_champ]);
  //      $data_row[]=array('data'=>$mem_filtre, 'align' => 'left');
        // -------------------------------------------------------------------------
        $mem_champ='filtre_2';
        $mem_value=trim($filtre2);
        $this->FxForm_model->fxinputtxt($mem_champ,$mem_value,'','','','filtre_2');
        $mem_filtre=form_input($mem_data['input'][$mem_champ]);
        $data_row[]=array('data'=>$mem_filtre, 'align' => 'left');

        // -------------------------------------------------------------------------
        $data_row[]=array('data'=>"", 'align' => 'left');

        // -------------------------------------------------------------------------
        $mem_champ='filtre_3';
        $mem_value=$filtre3;
        $mem_data['select'][$mem_champ]['choix']=$this->Config_model->conf_getGenresFournisseur('fr',true);
        // value multipli select
        //$mem_value = explode(',', $mem_value);
        $mem_filtre=form_dropdown($mem_champ,$mem_data['select'][$mem_champ]['choix'],$mem_value,'class="filtre_3"');

        $data_row[]=array('data'=>$mem_filtre, 'align' => 'left');

       // echo form_dropdown($mem_champ,$select[$mem_champ]['choix'],$select[$mem_champ]['value']);
        // -------------------------------------------------------------------------

        // -------------------------------------------------------------------------


// prépare les donnés du tableau







        $tabular_data[] = $data_row;




        foreach($client_list as $row)
        {   $nbitem++;
            // faire le link pour edit
            $mem_link_edit=anchor('fournisseursprojets/form/'.$row['com_id'], ' ', 'class="btn btn-primary btn-circle glyphicon glyphicon-edit" ');
            // <a href="javascript:void(0);" class="btn btn-primary btn-circle"><i ></i></a> ';
            $data_row = array();
            $data_row[] = array('data'=> $mem_link_edit, 'align' => 'left');
        //    $data_row[] = array('data'=>$row['com_num_client'], 'align' => 'left');
            $data_row[] = array('data'=>$row['com_compagnie'], 'align' => 'left');
            $data_row[] = array('data'=>$row['com_ville'], 'align' => 'left');
            $data_row[] = array('data'=>$row['list_type'], 'align' => 'left');
          //  $data_row[] = array('data'=>$row['rep_prenom']." ".$row['rep_nom'], 'align' => 'left');
            $data_row[] = array('data'=>$row['com_telephone'], 'align' => 'left');

            $tabular_data[] = $data_row;

        }


        $template = array(
            'table_open'            => '<table border="0" cellpadding="4" cellspacing="0" class="table table-striped table-bordered table-hover">',

            'thead_open'            => '<thead>',
            'thead_close'           => '</thead>',

            'heading_row_start'     => '<tr>',
            'heading_row_end'       => '</tr>',
            'heading_cell_start'    => '<th>',
            'heading_cell_end'      => '</th>',

            'tbody_open'            => '<tbody>',
            'tbody_close'           => '</tbody>',

            'row_start'             => '<tr>',
            'row_end'               => '</tr>',
            'cell_start'            => '<td>',
            'cell_end'              => '</td>',

            'row_alt_start'         => '<tr>',
            'row_alt_end'           => '</tr>',
            'cell_alt_start'        => '<td>',
            'cell_alt_end'          => '</td>',

            'table_close'           => '</table>'
        );

        $this->table->set_template($template);
        $this->table->set_heading($header);
        $this->table->set_heading($header);
        $tableau_sortie= $this->table->generate($tabular_data);



        $data['empData'] = $tableau_sortie;

        echo json_encode($data);
    }
    public function loadData_list($record=0,$filtre1='',$filtre2='',$filtre3='', $listactive , $model='liste') {

        $this->Config_model->conf_getFiltreData($this->session->id,'list_fournisseur_projets','mem_select_2',$filtre2,1,0);
        $this->Config_model->conf_getFiltreData($this->session->id,'list_fournisseur_projets','mem_select_3',$filtre3,1,0);
        $this->Config_model->conf_getFiltreData($this->session->id,'list_fournisseur_projets','mem_listactive',$listactive,1,0);



        $filtre1=urldecode($filtre1);
        $filtre2=urldecode($filtre2);
        $filtre3=urldecode($filtre3);

        $data=$this->Fournisseursprojets_model->mod_loadData_list($record,$filtre1,$filtre2,$filtre3,$listactive,$model);

        echo json_encode($data);
    }
    public function saveRecord($id)
    {

      if ($id==0){
          $id=$this->Fournisseursprojets_model->mod_createRecordbd($_POST,'com_');

        if ($id!=0)
           $error=array('type'=>'success','message'=>'Nouvelle fiche créée');
        else
           $error=array('type'=>'error','message'=>'test');
      } else {
          $ok=$this->Fournisseursprojets_model->mod_saveRecordbd($id,$_POST,'com_');
          if ($ok)
              $error=array('type'=>'success','message'=>'Fiche sauvegardé');
          else
              $error=array('type'=>'error','message'=>'test');



      }

        $this->form($id,$error);
    }

    public function deletecontact($id,$con_id){
        $this->Fournisseursprojets_model->mod_deleteRecordbdcontact($id);
        $error=array('type'=>'success','message'=>'Contact effacé');
        $this->form($con_id,$error);
    }
public function test(){echo ('allo');

    //$mem_projets=$this->Fournisseursprojets_model->mod_listprojetsproduit(189);

    // <a href="javascript:void(0);" class="btn btn-primary btn-circle"><i ></i></a> ';
    $data_row = array();
    $data_row[] = array('data'=> 'ttt', 'align' => 'left');
    //    $data_row[] = array('data'=>$row['com_num_client'], 'align' => 'left');
    $data_row[] = array('data'=>'ttt', 'align' => 'left');
    $data_row[] = array('data'=>'ttt', 'align' => 'left');
    $data_row[] = array('data'=>'ttt', 'align' => 'left');
    //  $data_row[] = array('data'=>$row['rep_prenom']." ".$row['rep_nom'], 'align' => 'left');
    $data_row[] = array('data'=>'ttt', 'align' => 'left');
    $tabular_data[] = $data_row;
    // print_r( $row);

    $mem_projets=$this->Fournisseursprojets_model->mod_listprojetsproduit(189,1);
    //  $mem_projets=$this->mod_listprojetsproduit(74);

    // $projets=$this->Config_model->conf_getprojetsclients($row['com_id']);

    //   $mem_projets="";
    //  $mem_projets_client='';
    //  foreach($projets as $value){
    //      $mem_link_edit=anchor('projets/form/'.$value['proj_id'], ' ', 'class="btn btn-primary btn-circle glyphicon glyphicon-edit statut_'.$value['proj_etape'].'"' );
    //      $mem_projets=$mem_projets. $mem_link_edit;
    //      $mem_projets=$mem_projets.' '.$value['proj_numero'].' '.$value['proj_nom'].'<br>';
    //      $mem_projets_client=' Création de projet <br>';
    // }


    $data_row = array();
    $data_row[] = array('data'=>'' );
    $data_row[] = array('data'=>$mem_projets,'colspan' => 6 );

    $tabular_data[] = $data_row;

 print_r($mem_projets);


}
    public function saveRecordcontact($id)
    {

        $mem_con_id=0;
        if($_POST['button']=='new'){
            $mem_con_id=0;

        }else{
            $mem_con_id=$_POST['button'];

        }


        $new_post=array();

        foreach($_POST  as $key => $value ){
            $pieces = explode("/", $key);
            if(isset($pieces[1])){
                if ($pieces[0]== $mem_con_id){
                    $new_post[$pieces[1]]=$value;
                }
            }
        }
        if($_POST['button']=='new'){
            $this->Fournisseursprojets_model->mod_createRecordbdcontact($new_post,'con_',$id);

        }else{
            $this->Fournisseursprojets_model->mod_editRecordbdcontact($new_post,'con_',$mem_con_id);

        }

                $error=array('type'=>'success','message'=>'Nouvelle fiche créée');



        $this->form($id,$error);
    }
}