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

class Echipa extends BP_Controller
{

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


            $sql          = new TableSql( "SELECT t1.* FROM echipa t1 WHERE 1=1  " );

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

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

            $sql->addIfNeeded( $filters['NrOrdine'], '', " AND t1.NrOrdine LIKE '%%%s%%' ");
            $sql->addIfNeeded("ffff", "", " ORDER BY t1.Id DESC ");

        


            $sql->addLimitFromPagination($pg_info);


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

            //
            $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)
        {
            



        }

        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");
            $ret->Item           = new DbEchipaModel($id);
            


 $ret->Item->uploadPreview_Imagine = base_url().'./assets/images/echipa/'.$ret->Item->Imagine;

        }

        echo json_encode($ret);
    }


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

        if(!$ret->NotLogged)
        {
            $mode   = $this->input->post( "mode"   );
            $object = $this->input->post( "object" );
            $obj    = new DbEchipaModel();

            if( $mode == "add" ){
                if( array_key_exists('Id', $object) ) unset( $object['Id'] );
                $obj->constructFromArray($object);
                $obj->Insert(false, true);
            }else{
                $obj->constructFromArray($object);
                $obj->Update();
            }
        }

        echo  json_encode($ret);

    }

    



            public function upload_Imagine(){
                $this->load->helper('upload_info');
                $uh         = new UploadHelper();
                if( !is_dir('./assets/images/echipa/') ) mkdir('./assets/images/echipa/');
                $uh->upload( './assets/images/echipa/', 'file', 'jpg|png|jpeg|gif');
                if($uh->HasError)
                {
                    die($uh->Error);
                }else{
                    echo json_encode($uh);
                }
            }
        


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

        if(!$ret->NotLogged)
        {
            $id = $this->input->post( "id" );
            $a  = new DbEchipaModel($id);
            $a->Delete();
        }

        echo  json_encode($ret);
    }

}
