<?php

/**
 * Created by PhpStorm.
 * User: Utilisateur
 * Date: 2019-04-23
 * Time: 10:21
 */
class Produits_model extends CI_Model
{

    public function __construct()
    {
        $this->load->database();
        $this->load->helper('date');
        $this->load->model('FxForm_model');
        $this->load->model('Config_model');

    }

    public function mod_getRecords($rowno, $rowperpage, $filtre1 = "", $filtre2 = "", $filtre3 = "", $listactif = 1, $langue = 'fr')
    {
        // list la bb client
        $this->db->select('*,fou_genres.gen_nom_' . $langue . ' as type ');
        $this->db->from('pro_descriptions');

        $this->db->join('fou_genres', 'fou_genres.gen_id=pro_descriptions.des_ref_type', 'left');
        $this->db->where('des_actif', $listactif);
        if ($filtre1 != "")
            $this->db->where("des_nom_fr LIKE '%$filtre1%'");
        if ($filtre2 != "")
            $this->db->where("des_description_fr LIKE '%$filtre2%'");
        if ($filtre3 != "")
            $this->db->where("des_ref_type = " . $filtre3);


        $this->db->limit($rowperpage, $rowno);
        $this->db->order_by("des_nom_fr");

        $query = $this->db->get();


        return $query->result_array();
    }

    public function mod_getRecordCountfilter($filtre1 = "", $filtre2 = "", $filtre3 = "", $listactif)
    {

        $this->db->select('count(*) as allcount');
        $this->db->from('pro_descriptions');

        $this->db->where('des_actif', $listactif);

        if ($filtre1 != "")
            $this->db->where("des_nom_fr LIKE '%$filtre1%'");
        if ($filtre2 != "")
            $this->db->where("des_description_fr LIKE '%$filtre2%'");
        if ($filtre3 != "")
            $this->db->where("des_ref_type = " . $filtre3);


        $query = $this->db->get();
        $result = $query->result_array();
        return $result[0]['allcount'];


    }

    public function mod_getRecord($id, $langue = 'fr')
    {
        // trouve un enregiestrement
        $this->db->select('*');
        $this->db->from('pro_descriptions');
        $this->db->where('des_id=' . $id);
        $this->db->join('fou_genres', 'fou_genres.gen_id=pro_descriptions.des_ref_type', 'left');

        $this->db->limit(1);

        $query = $this->db->get();
        return $query->row();
    }

    public function mod_saveRecordbd($id, $data, $pref)
    {

        // creation de la chaine de save
        $mem_data = array();
        // ajoute les checkbox
       if (!isset($data['com_actif']))
            $data['com_actif'] = 0;


        foreach ($data as $key => $value) {
            if (substr($key, 0, strlen($pref)) == $pref) {
                $mem_data[$key] = $value;
            }

        }
        $mem_data['com_modif'] = date("Y-m-d H:i:s");
        $mem_where = "com_id =" . $id;

        $str = $this->db->update('cli_compte', $mem_data, $mem_where);
        $ok = true;

        return $ok;
    }

    public function mod_desactivateRecordbd($id, $statut)
    {

        // creation de la chaine de save
        $mem_data = array();

        $mem_data['des_actif'] = $statut;

        $mem_where = "des_id =" . $id;

        $str = $this->db->update('pro_descriptions', $mem_data, $mem_where);
        $ok = true;

        return $ok;
    }

    public function mod_createRecordbd($data, $pref, $id)
    {


        // creation de la chaine de save

        $mem_data = array();

        if (!isset($data['des_tps']))
            $data['des_tps']=0;
        if (!isset($data['des_tvq']))
            $data['des_tvq']=0;
        if (!isset($data['des_tvh']))
            $data['des_tvh']=0;
        if (!isset($data['des_opc']))
            $data['des_opc']=0;

        foreach ($data as $key => $value) {
            if (substr($key, 0, strlen($pref)) == $pref) {
                $mem_data[$key] = $value;
            }

        }



        if ($id == 0) {
            $mem_data['des_creation'] = date("Y-m-d H:i:s");
            $mem_data['des_actif'] = 1;
            $str = $this->db->insert('pro_descriptions', $mem_data);
        } else {
            $mem_where = "des_id =" . $id;

            $str = $this->db->update('pro_descriptions', $mem_data, $mem_where);

        }
        $ok = true;

        return $this->db->insert_id();
    }

    public function mod_getRecordCount()
    {

        $this->db->select('count(*) as allcount');
        $this->db->from('pro_descriptions');
        $this->db->where('des_actif', 1);
        $query = $this->db->get();
        $result = $query->result_array();
        return $result[0]['allcount'];
    }

    public function mod_getforminfo($id)
    {
        global $mem_data, $validation;

        $this->load->model('Config_model');
        $this->load->model('FxForm_model');
        // vas chercher les info de la fiche

        if ($id != 0)
            $info = $this->mod_getRecord($id);

        // prepare info pour chaque champs utilisé
        // input
        $mem_data['mem_id'] = $id;
        $mem_champ = 'des_nom_fr';
        $mem_value = ($id == 0 ? '' : $info->$mem_champ);
        $this->FxForm_model->fxinputtxt($mem_champ, $mem_value, 'Nom Produit', '');
        $this->FxForm_model->fxvaliderequired($mem_champ);


        // -------------------------------------------------------------------------
        $mem_champ = 'des_nom_en';
        $mem_value = ($id == 0 ? '' : $info->$mem_champ);
        $this->FxForm_model->fxinputtxt($mem_champ, $mem_value, 'Nom Produit', '');
        $this->FxForm_model->fxvaliderequired($mem_champ);


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

        $mem_champ = 'des_description_fr';
        $mem_value = ($id == 0 ? '' : $info->$mem_champ);
        $this->FxForm_model->fxinputtxt($mem_champ, $mem_value, 'Description', '');
        //$this->FxForm_model->fxvaliderequired($mem_champ);
        // -------------------------------------------------------------------------

        $mem_champ = 'des_note';
        $mem_value = ($id == 0 ? '' : $info->$mem_champ);
        $this->FxForm_model->fxinputtxt($mem_champ, $mem_value, 'Note', '');
        // $this->FxForm_model->fxvaliderequired($mem_champ);
// -------------------------------------------------------------------------

        $mem_champ = 'des_description_en';
        $mem_value = ($id == 0 ? '' : $info->$mem_champ);
        $this->FxForm_model->fxinputtxt($mem_champ, $mem_value, 'Description', '');
        //$this->FxForm_model->fxvaliderequired($mem_champ);

        // -------------------------------------------------------------------------
        $mem_champ = 'des_ref_type';

        $mem_value = ($id == 0 ? '' : $info->$mem_champ);
        $mem_data['select'][$mem_champ]['choix'] = $this->Config_model->conf_getGenresFournisseur('fr', true, 1);
        // value multipli select
        // $mem_value = explode(',', $mem_value);

        $mem_data['select'][$mem_champ]['value'] = $mem_value;
        $mem_data['label'][$mem_champ] = 'Type';

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

        $mem_champ='des_tps';
        $mem_value=($id==0 ? true : ($info->$mem_champ==1 ? true:false));

        $mem_data['checkbox'][$mem_champ] = array(

            'name'  => $mem_champ,
            'value' =>  1,
            'checked' =>   $mem_value,
        );
        $mem_data['label'][$mem_champ] =  'TPS';
        $mem_data['icon'][$mem_champ] =  ' ';
        // -------------------------------------------------------------------------

        $mem_champ='des_tvq';
        $mem_value=($id==0 ? true : ($info->$mem_champ==1 ? true:false));

        $mem_data['checkbox'][$mem_champ] = array(

            'name'  => $mem_champ,
            'value' =>  1,
            'checked' =>   $mem_value,
        );
        $mem_data['label'][$mem_champ] =  'TVQ';
        $mem_data['icon'][$mem_champ] =  ' ';
        // -------------------------------------------------------------------------

        $mem_champ='des_tvh';
        $mem_value=($id==0 ? true : ($info->$mem_champ==1 ? true:false));

        $mem_data['checkbox'][$mem_champ] = array(

            'name'  => $mem_champ,
            'value' =>  1,
            'checked' =>   $mem_value,
        );
        $mem_data['label'][$mem_champ] =  'TVH';
        $mem_data['icon'][$mem_champ] =  ' ';
        // -------------------------------------------------------------------------

        $mem_champ='des_opc';
        $mem_value=($id==0 ? true : ($info->$mem_champ==1 ? true:false));

        $mem_data['checkbox'][$mem_champ] = array(

            'name'  => $mem_champ,
            'value' =>  1,
            'checked' =>   $mem_value,
        );
        $mem_data['label'][$mem_champ] =  'OPC';
        $mem_data['icon'][$mem_champ] =  ' ';

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

        $mem_data['validation'] = $this->FxForm_model->fxValidation($validation['rule'], $validation['message']);


        return $mem_data;
    }

    public function mod_loadData_list($record = 0, $filtre1 = '', $filtre2 = '', $filtre3 = '', $listactif = 1, $model_de_produits)
    {
        $filtre1 = ($filtre1 == 'null' ? '' : $filtre1);
        $filtre2 = ($filtre2 == 'null' ? '' : $filtre2);
        $filtre3 = ($filtre3 == 'null' ? '' : $filtre3);

        if ($model_de_produits == "liste")
                 $recordPerPage = 10;
        else
                 $recordPerPage = 5;
        $config["cur_page"] = $record;
        if ($record != 0) {
            $record = ($record - 1) * $recordPerPage;
        }

        $recordCount2 = $this->mod_getRecordCount();
        $recordCount = $this->mod_getRecordCountfilter($filtre1, $filtre2, $filtre3, $listactif);

        $client_list = $this->mod_getRecords($record, $recordPerPage, $filtre1, $filtre2, $filtre3, $listactif);

        $config['base_url'] = base_url() . 'index.php/Produits/loadData_list';
        $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;

// switch voir delete


// creation du tableau


// prepare header
        $mem_link_new = "";
        if ($model_de_produits == "liste")
            $mem_link_new = '<a  id="pk_1" data-id="0" class="btn btn-primary btn-circle glyphicon glyphicon-pencil"> </a>';
//$mem_link_new=anchor('Produits/form/0' , ' ', 'class="btn btn-primary btn-circle glyphicon glyphicon-pencil" ');
        $header = array($mem_link_new, 'Nom',
            'Description',
            'Type');
// prépare les donnés du tableau
        $nbitem = 0;

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

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

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

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

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

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


        $tabular_data[] = $data_row;
// prépare les donnés du tableau


        foreach ($client_list as $row) {
            $nbitem++;
            if($model_de_produits=="liste"){
            // faire le link pour delete
            if ($row['des_actif'] == 1) {
                // faire le link pour edit
                $mem_link_edit = '<a  id="pk_1" data-id="' . $row['des_id'] . '" class="btn btn-primary btn-circle glyphicon glyphicon-pencil"> </a>';

                $mem_link_delete = anchor('Produits/desactivate/' . $row['des_id'] . '/0', ' ', 'class="btn btn-danger btn-circle glyphicon glyphicon-trash  " ');
            }
            else
            {
                $mem_link_edit = '';
                $mem_link_delete = anchor('Produits/desactivate/' . $row['des_id'] . '/1', ' ', 'class="btn btn-success btn-circle glyphicon glyphicon-thumbs-up " ');
            }
            }else{
               // $mem_link_edit = anchor('Projets/choix_produit/' . $row['des_id'] , ' ', 'class="btn btn-success btn-circle glyphicon  glyphicon-plus-sign" ');

                $databtn = array(
                    'name'          => 'button',
                    'id'            => 'button',
                    'value'         => $row['des_id'],
                    'class'          => 'btn btn-success btn-circle glyphicon  glyphicon-plus-sign'
                );
                $mem_link_edit =form_button($databtn);

                $mem_link_delete = '';

            }
            //  $mem_link_edit='<a  id="pk_'.$row['des_id'].'" data-id="1" class="btn btn-primary btn-circle glyphicon glyphicon-edit"> </a>';
            // <a href="javascript:void(0);" class="btn btn-primary btn-circle"><i ></i></a> ';
            $data_row = array();
            $data_row[] = array('data' => $mem_link_edit . ' ' . $mem_link_delete, 'align' => 'left');
            $data_row[] = array('data' => $row['des_nom_fr'], 'align' => 'left');
            $data_row[] = array('data' => $row['des_description_fr'], 'align' => 'left');
            $data_row[] = array('data' => $row['type'], '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);
        $tableau_sortie = $this->table->generate($tabular_data);


        $data['list'] = $tableau_sortie;
        return $data;
    }


}