<?php
namespace view;

class RankerReseller {
  
  public function drawReportsForm()
  {    
    global $user;
    
    $tpl = new \Savant3;
    $tpl -> _sites    = \factory\RankerReseller::getSites( $user['id'] );      
    return $tpl -> fetch( 'reseller/reports' );
  }
  
  public function drawSummary( $month = '', $year = '' )
  {
    global $user;
    
    if ( $month ) \S::set_session( 'drawSitesList:month', $month );    
    if ( $year ) \S::set_session( 'drawSitesList:year', $year );
    
    $month  = \S::get_session( 'drawSitesList:month' );
    $year   = \S::get_session( 'drawSitesList:year' );
    
    if ( !$month ) $month = date( 'm' );
    if ( !$year ) $year = date( 'Y' );
    
    $tpl = new \Savant3;
    $tpl -> _sites      = \factory\RankerReseller::getSitesSummary( $month, $year, $user['id'], $user['reseller_id'] );
    $tpl -> _sites_res  = \factory\RankerReseller::getSitesSummary( $month, $year, $user['id'], $user['id'] );
    $tpl -> _month    = $month;
    $tpl -> _year     = $year;
    return $tpl -> fetch( 'reseller/ranker-summary' );
  }
  
  public function phraseEdit( $site_id, $phrase_id = '', $reseller_id )
  {    
    $tpl = new \Savant3;
    $tpl -> _site_id      = $site_id;
    $tpl -> _reseller_id  = $reseller_id;
    $tpl -> _phrase       = \factory\Ranker::getPhrase( $phrase_id, $reseller_id);
    return $tpl -> fetch( 'reseller/phrase-edit' );
  }
  
  public function editCosts( $site_id, $phrase_id, $reseller_id )
  {
    $tpl = new \Savant3;
    $tpl -> _site_id      = $site_id;
    $tpl -> _reseller_id  = $reseller_id;
    $tpl -> _phrase       = \factory\Ranker::getPhrase( $phrase_id );
    $tpl -> _prices       = \factory\Ranker::getPhrasePrices( $phrase_id, $reseller_id );
    return $tpl -> fetch( 'reseller/costs-edit' );    
  }

  public function drawSitesList( $id, $month = '', $year = '' )
  {    
    global $user;
    
    if ( $id ) \S::set_session( 'drawSitesList:id', $id );
    if ( $month ) \S::set_session( 'drawSitesList:month', $month );    
    if ( $year ) \S::set_session( 'drawSitesList:year', $year );
    
    $month  = \S::get_session( 'drawSitesList:month' );
    $year   = \S::get_session( 'drawSitesList:year' );
    $id     = \S::get_session( 'drawSitesList:id' );
    
    if ( !$month ) $month = date( 'm' );
    if ( !$year ) $year = date( 'Y' );
    
    $site = \factory\RankerClients::getClientSite( $id, $user['id'] );
    
    $tpl = new \Savant3;
    $tpl -> _sites            = \factory\RankerClients::getClientSites( $user['id'] );
    $tpl -> _phrases          = \factory\Ranker::getSitePhrases( $site['id'], $month, $year, false, $user['reseller_id'], $user['login'] );
    $tpl -> _phrases_reseller = \factory\Ranker::getSitePhrases( $site['id'], $month, $year, false, $user['id'], $user['login'] );
    $tpl -> _site             = $site;
    $tpl -> _month            = $month;
    $tpl -> _year             = $year;
    $tpl -> _next_site_id     = \factory\RankerClients::getNextClientSiteId( $site['id'], $user['id'] );
    $tpl -> _prev_site_id     = \factory\RankerClients::getPrevClientSiteId( $site['id'], $user['id'] );
    return $tpl -> fetch( 'reseller/site-list' );
  }
  
  public function drawClientEdit( $id = '' )
  { 
    global $user;
    
    $tpl = new \Savant3;
    $tpl -> _reseller_id  = $user['id'];
    $tpl -> _client       = \factory\Ranker::getClient( $id );
    $tpl -> _sites        = \factory\RankerClients::getClientSites( $user['id'] );
    return $tpl -> fetch( 'reseller/client-edit' );
  }
  
  public static function drawClientList()
  {
    $tpl = new \Tpl;
    return $tpl -> render( 'reseller/client-list' );
  }
}
?>
