<?php
error_reporting( E_ALL ^ E_NOTICE ^ E_STRICT ^ E_WARNING ^ E_DEPRECATED );
require_once dirname( __FILE__ ) . '/config.php';

/* grid - zapisanie elementu */
if ( $_POST['a'] == 'gsave' )
{
  $grid = $_SESSION[ 'g' . $_POST[ 'gtable' ] ];
  if ( is_a( $grid, 'grid' ) )
  {
    $values_tmp = json_decode( $_POST[ 'values' ], true );

    if ( is_array( $values_tmp ) ) foreach ( $values_tmp as $val )
      $values[ $val['name'] ] = $val['value'];

    if ( $grid -> saveElement( $values ) )
    {
      if ( $values[ $grid -> id ] )
        $msg = 'Wybrany element zotał zmodyfikowany.';
      else
        $msg = 'Nowy element został dodany.';
    }

    $response = array(
      'status' => 'ok',
      'output' => $grid -> drawResults(),
      'msg' => $msg
    );
  }
  else
    $response = array( 'status' => 'error' );
  echo json_encode( $response );
  \S::delete_dir( '../../temp/' );
  exit;
}

/* rysowanie grida - edycja */
if ( $_POST['a'] == 'gedit' )
{
  $grid = $_SESSION[ 'g' . $_POST[ 'gtable' ] ];
  if ( is_a( $grid, 'grid' ) )
  {
    $response = array(
      'status' => 'ok',
      'output' => $grid -> drawEdit( (int)$_POST[ 'gedit' ] )
    );
  }
  else
    $response = array( 'status' => 'error' );
  echo json_encode( $response );
  exit;
}

/* rysowanie grida */
if ( $_POST['a'] == 'gdraw' )
{
  $grid = $_SESSION[ 'g' . $_POST['gtable'] ];
  if ( is_a( $grid, 'grid' ) )
  {
    /* ukrywanie kolumn */
    if ( $_POST['column'] )
    {
      $grid -> hide_column( $_POST['column'], $_POST['hidden'] );
      $grid -> hidden_columns[ $_POST['column'] ] = filter_var( $_POST['hidden'], FILTER_VALIDATE_BOOLEAN );
    }

    /* zmiana liczby wyświetlanych elementów */
    if ( $_POST['glimit'] )
    {
      $grid -> limit = $_POST['glimit'];
      $grid -> save_limit( $_POST['glimit'] );
    }

    /* usuwanie wielu elementów */
    if ( $_POST['gdelete_multi'] )
    {
      if ( is_array( $_POST['gdelete_multi'] ) ) foreach ( $_POST['gdelete_multi'] as $id )
        $grid -> delete( $id );
      $msg = 'Wybrane elementy zostały usunięte.';
      \S::delete_dir( '../../temp/' );
    }

    /* usuwanie */
    if ( $_POST['gdelete'] )
    {
      if ( $grid -> delete( $_POST['gdelete'] ) )
        $msg = 'Wybrany element został usunięty.';
      \S::delete_dir( '../../temp/' );
    }

    /* wyszukiwanie */
    if ( $_POST['gsearch_column'] )
    {
      if ( $_POST['gsearch_value'] === 'null' || $_POST['gsearch_value'] === '' )
        unset( $grid -> filters[ $_POST['gsearch_column'] ] );
      else
      {
        $grid -> filters[ $_POST[ 'gsearch_column' ] ]['value']  = $_POST[ 'gsearch_value' ];
        $grid -> filters[ $_POST[ 'gsearch_column' ] ]['type']   = $_POST[ 'gsearch_type' ];
      }
      $grid -> save_filters();
      $grid -> set_cp( 1 );
    }

    /* sortowanie */
    if ( $_POST[ 'gsort_column' ] )
    {
      if ( $_POST[ 'gsort_type' ] == 'ASC' )
        $grid -> order = [ 'column' => $_POST[ 'gsort_column' ], 'type' => 'DESC' ];
      else
        $grid -> order = [ 'column' => $_POST[ 'gsort_column' ], 'type' => 'ASC' ];
      $grid -> save_order();
    }

    /* zmiana strony */
    if ( $_POST[ 'gpage'] )
      $grid -> set_cp( $_POST[ 'gpage'] );

    $response = array(
      'status' => 'ok',
      'msg' => $msg,
      'output' => $grid -> drawResults()
    );
  }
  else
    $response = array( 'status' => 'error' );
  echo json_encode( $response );
  exit;
}