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

class Clienti extends BP_Controller
{

    public function index()
    {
        $ret            = $this->verify_login();
        if(!$ret->NotLogged)
        // if(1==1)
        {
            $filters      = $this->input->post("Filters");
            $order_by     = $this->input->post("OrderBy");

            $pg_info      = new PaginationInfo( $this->input->post("PaginationInfo") );


            $sql          = new TableSql( "SELECT t1.*, t2.Nume AS NumeUtilizator FROM clienti t1 LEFT JOIN utilizatori t2 ON t1.IdUserCreare=t2.Id WHERE t1.Id!=0 ");

            
            $sql->addIfNeeded( $filters['Nume'], '', " AND t1.Nume LIKE '%%%s%%' ");
        

            $sql->addIfNeeded( $filters['Prenume'], '', " AND t1.Prenume LIKE '%%%s%%' ");
        

            $sql->addIfNeeded( $filters['Email'], '', " AND t1.Email LIKE '%%%s%%' ");
        

            $sql->addIfNeeded( $filters['Telefon'], '', " AND t1.Telefon LIKE '%%%s%%' ");
        

            $sql->addIfNeeded( $filters['Cnp'], '', " AND t1.Cnp LIKE '%%%s%%' ");
        

            $sql->addIfNeeded( $filters['TipPersoana'], '-1', " AND t1.TipPersoana = '%s' ");

            $sql->addIfNeeded( $filters['Observatii'], '', " AND t1.Observatii LIKE '%%%s%%' ");

            $sql->addIfNeeded( $filters['IdUserCreare'], '-1', " AND t1.IdUserCreare = '%s' ");


            if(isset($filters['IntervalData'][0]))
                $sql->addIfNeeded( $filters['IntervalData'][0], '', " AND DATE(t1.DataCreare) >= '%s' ");
            if(isset($filters['IntervalData'][1]))
                $sql->addIfNeeded( $filters['IntervalData'][1], '', " AND DATE(t1.DataCreare) <= '%s' ");
         

        
            


            $sql->addLimitFromPagination($pg_info);


            $result = $sql->getResult();
            $rows   = $result->Rows;
            // print_r($result);

            $ret->query_meu=$this->db->last_query();


            $pg_info->RowCount = $result->FullRowsCount;
            $ret->Results        = $rows;
            $ret->PaginationInfo = $pg_info;
        }

        echo  json_encode($ret);
    }

    public function get_info()
    {
        $ret            = $this->verify_login();
        if(!$ret->NotLogged)
        {
            // aici populezi selectul din filtre si trimiti toti utioizatorii

            $ret->rights_groups = $this->db->query('SELECT Nume,Id Name FROM rights_groups')->result();

        }

        echo json_encode($ret);
    }


    public function get_info_for_dialog()
    {
        $ret            = $this->verify_login();
        if(!$ret->NotLogged)
        {

        }

        echo json_encode($ret);
    }

    public function get_info_item_dialog()
    {
        $ret            = $this->verify_login();
        if(!$ret->NotLogged)
        {
            $id                  = $this->input->post("id");
            $user = $this->getUserFromToken();
            $ret->Item           = new DbClientiModel($id);
            if($ret->Item->IdUserCreare>0)
            $ret->Item->NumeUser = new DbUtilizatoriModel($ret->Item->IdUserCreare);
            $ret->Item->Telefon='bbb';

                  
        }

        echo json_encode($ret);
    }
    // public function filip(){
    //     echo "<pre>";
    //     $var=new stdclass();
    //     var_dump($var);
    //     $var->nume="Filip";
    //     $var->varsta=22;
    //     $var->oras="Bucuresti";
    //     $var->telefon="0737291791";

        
    //     $var->getInfo=function(){
    //         return "Nume:" . $this->nume . ", Oras:". $this->oras . ", Telefon:". $this->telefon;    
    //     };
    //     echo $var->getInfo();

        
    // }

    public function save()
    {
        $ret            = $this->verify_login();

        if(!$ret->NotLogged)
        {
            
            $user = $this->getUserFromToken();
            $mode   = $this->input->post( "mode"   );
            $object = $this->input->post( "object" );
            $obj    = new DbClientiModel();
            // $nume   = new DbUtilizatoriModel();
            
                     
            
            if( $mode == "add" ){
                if( array_key_exists('Id', $object) ) unset( $object['Id'] );
                $obj->constructFromArray($object);
                $obj->DataCreare=date('Y-m-d');
                $obj->IdUserCreare=$user->Id;
                $obj->Insert(false, true);
                
                
                
            }else{
                
                $obj->constructFromArray($object);
                $obj->Telefon='bbb';
                $obj->Update();
                

            }
        }

        echo  json_encode($ret);

    }


    public function delete_item()
    {
        $ret            = $this->verify_login();

        if(!$ret->NotLogged)
        {
            $id = $this->input->post( "id" );
            $ret->clienti= $this->db->query('SELECT COUNT(*) AS NumarClienti FROM clienti')->result();
            // var_dump($ret->clienti[0]->NumarClienti);
            // print_r($ret->clienti['NumarClienti']);
            
            if ($ret->clienti[0]->NumarClienti <= 10) {
                $ret->success = false;
                $ret->message = "Nu puteti sterge un client cand exista 10 sau mai putin clienti in total.";
            } else {
                $id = new DbClientiModel($id);
                $id->Delete();
                $ret->message = "Clientul a fost sters cu succes.";

            }
            
        }

        echo  json_encode($ret);
    }

}
