<?php
error_reporting( E_ALL ^ E_NOTICE ^ E_STRICT ^ E_WARNING ^ E_DEPRECATED );

function __autoload_my_classes( $classname )
{
  $q = explode( '\\', $classname );
  $c = array_pop( $q );
  $base_path = 'autoload/' . implode( '/', $q ) . '/';
  $f = $base_path . $c . '.php';

  if ( !file_exists( $f ) )
    $f = $base_path . 'class.' . $c . '.php';

  if ( file_exists( $f ) )
    require_once( $f );
}
spl_autoload_register( '__autoload_my_classes' );
date_default_timezone_set( 'Europe/Warsaw' );

require_once 'config.php';
require_once 'libraries/medoo/medoo.php';
require_once 'libraries/grid/config.php';
require_once 'libraries/phpmailer/class.phpmailer.php';
require_once 'libraries/phpmailer/class.smtp.php';
require_once 'libraries/rb.php';

session_start();

$mdb = new medoo( [
    'database_type' => 'mysql',
    'database_name' => $database['name'],
    'server'        => $database['host'],
    'username'      => $database['user'],
    'password'      => $database['password'],
    'charset'       => 'utf8'
  ] );

\R::setup( 'mysql:host=' . $database['host'] . ';dbname=' . $database['name'], $database['user'], $database['password'] );
\R::ext( 'xdispense', function( $type )
{
  return R::getRedBean() -> dispense( $type );
} );

// dodawanie domeny przez API
if ( \S::get( 'action' ) == 'domain_tester_add' )
{
  if ( !$mdb -> count( 'domain_tester', [ 'AND' => [ 'url' => \S::get( 'domain' ), 'hidden' => 0 ] ] ) )
  {
    $mdb -> insert( 'domain_tester', [
      'url' => \S::get( 'domain' )
    ] );
  }

  echo json_encode( ['result' => 'ok'] );
  exit;
}

// Open Page Rank - pobieranie domeny
if ( \S::get( 'action' ) == 'domain_opr_check' )
{
  $mdb -> delete( 'domain_tester', [ 'url' => '' ] );

  $domain = $mdb -> get( 'domain_tester', '*', [ 'opr' => null, 'ORDER' => [ 'date_add' => 'DESC' ] ] );
  if ( $domain )
  {
    $result['id'] = $domain['id'];
    $result['url'] = $domain['url'];
    $result['domains_left'] = $mdb -> count( 'domain_tester', [ 'opr' => null ] );
    $result['result'] = 'ok';

    echo json_encode( $result );
  }
  else
    echo json_encode( ['result' => 'bad'] );
  exit;
}

// Open Page Rank - zapis
if ( \S::get( 'action' ) == 'domain_opr_save' )
{
  $mdb -> update( 'domain_tester', [
    'opr' => str_replace( ',', '.', \S::get( 'page_rank' ) ),
    'opr_date' => date( 'Y-m-d H:i:s' )
  ], [
    'id' => \S::get( 'domain_id' )
  ] );

  echo json_encode( ['result' => 'ok'] );
  exit;
}

if ( \S::get( 'action' ) == 'get_finances_categories' )
{
  $rows = $mdb -> select( 'finance_categories', '*', [ 'parent_id' => null, 'ORDER' => [ 'name' => 'ASC' ] ] );
  foreach ( $rows as $row )
  {
    $categories[] = [
      'id' => $row['id'],
      'name' => $row['name'],
      'subcategories' => $mdb -> select( 'finance_categories', '*', [ 'parent_id' => $row['id'], 'ORDER' => [ 'name' => 'ASC' ] ] )
    ];
  }
  echo json_encode( $categories );
  exit;
}

if ( \S::get( 'action' ) == 'add_finance_operation' )
{
  if ( !\S::get( 'operation_date' ) || !\S::get( 'net_value' ) || !\S::get( 'product_name' ) || !\S::get( 'category_id' ) )
  {
    echo json_encode( [ 'result' => 'error' ] );
    exit;
  }

  $recipient_name = \S::get( 'recipient_name' );
  if ( $recipient_name ) {
    $recipient_name = strip_tags( $recipient_name );
    $recipient_name = preg_replace( '/\s+/u', ' ', $recipient_name );
    $recipient_name = trim( $recipient_name );
  }

  $client_id = null;
  if ( $recipient_name ) {
    $client_id = $mdb -> get( 'crm_client', 'id', [ 'firm_name' => $recipient_name ] );
  }

  $data = [
    'date' => \S::get( 'operation_date' ),
    'amount' => str_replace( ',', '.', \S::get( 'net_value' ) ),
    'description' => \S::get( 'product_name' ),
    'category_id' => \S::get( 'category_id' ),
    'client_id' => $client_id,
  ];

  if ( $mdb -> insert( 'finance_operations', $data ) ) {
    $operation_id = $mdb -> id();
    echo json_encode( [ 'result' => 'ok', 'operation_id' => $operation_id ] );
  } else {
    echo json_encode( [ 'result' => 'error' ] );
  }

  exit;
}
