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

class Formular 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 formular 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['Telefon'], '', " AND t1.Telefon LIKE '%%%s%%' ");
        

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

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

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

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

            $sql->addIfNeeded( $filters['Status'], '', " AND t1.Status = '%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 DbFormularModel($id);
            








        }

        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 DbFormularModel();

            if( $mode == "add" ){
                if( array_key_exists('Id', $object) ) unset( $object['Id'] );
                $obj->constructFromArray($object);
                date_default_timezone_set('Europe/Bucharest');
                $obj->DataCreare = $now = date("Y-m-d H:i:s");
                $obj->Insert(false, true);
            }else{
                $obj->constructFromArray($object);
                $obj->Update();
            }
        }

        echo  json_encode($ret);

    }

    









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

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

        echo  json_encode($ret);
    }

}
