<?php
namespace controls;

class RankerReseller {
  
  public function getReportsContent()
  {
    $rw = \S::get( 'rw' );
    
    if ( $rw == 'create-report' )
      \factory\RankerReseller::createPdfReport( \S::get( 'sites' ), \S::get( 'report-form' ), \S::get( 'date-from' ), \S::get( 'date-to' ) );
        
    return \view\RankerReseller::drawReportsForm();
  }
  
  public function getSummary()
  {
    return \view\RankerReseller::drawSummary( \S::get( 'month' ), \S::get( 'year' ) );      
  }
  
  public function getSitesContent()
  {
    global $user;
    
    $rw = \S::get( 'rw' );
    
    if ( $rw == 'save_costs' && \factory\RankerReseller::saveCosts(
            \S::get( 'reseller_id' ),
            \S::get( 'phrase_id' ),
            \S::get( 'from' ),
            \S::get( 'to' ),
            \S::get( 'price' )
          )
        )
      \S::alert( 'Koszty frazy zostały zmodyfikowane.' );
    
    if ( $rw == 'save_phrase' && \factory\RankerReseller::savePhrase( 
            \S::get( 'reseller_id' ),
            \S::get( 'phrase_id' ),
            \S::get( 'discount' )
          )
        )
      \S::alert( 'Fraza została zmodyfikowana.' );
    
    if ( $rw == 'edit_phrase' )
      return \view\RankerReseller::phraseEdit( \S::get( 'id' ), \S::get( 'phrase_id' ), $user['id'] );
    
    if ( $rw == 'edit_costs_reseller' )
      return \view\RankerReseller::editCosts( \S::get( 'id' ), \S::get( 'phrase_id' ), $user['id'] );
    
    return \view\RankerReseller::drawSitesList( \S::get( 'id' ), \S::get( 'month' ), \S::get( 'year' ) );
  }

  public function getClientsContent()
  {
    global $user;
    
    $rw = \S::get( 'rw' );
    
    if ( $rw == 'add_new' )
    {
      if ( \factory\RankerReseller::addClient( \S::get( 'login' ), \S::get( 'password' ), \S::get( 'sites' ), \S::get( 'enabled' ), \S::get( 'reseller_id' ) ) ) 
        \S::alert( 'Klient został dodany.' );
      header( 'Location: /?p=reseller_clients' );
      exit;
    }
    
    if ( $rw == 'save' )
    {
      if ( \factory\RankerReseller::saveClient( \S::get( 'client_id' ), \S::get( 'login' ), \S::get( 'password' ), \S::get( 'sites' ), \S::get( 'enabled' ), \S::get( 'reseller_id' ) ) )
        \S::alert( 'Klient został zmodyfikowany.' );
      header( 'Location: /?p=reseller_clients' );
      exit;
    }
    
    if ( $rw == 'del' )
    {
      if ( \factory\RankerReseller::deleteClient( \S::get( 'id' ), $user['id'] ) )
        \S::alert( 'Klient został usunięty.' );
      header( 'Location: /?p=reseller_clients' );
      exit;
    }
    
    if ( $rw == 'add' )
      return \view\RankerReseller::drawClientEdit();
    
    if ( $rw == 'edit' )
      return \view\RankerReseller::drawClientEdit( \S::get( 'id' ) );
    
    return \view\RankerReseller::drawClientList();
  }
}
?>
