<? global $config, $user;?>
<div class="row">
  <div class="col-xs-12 col-md-8">
    <div class="panel panel-success panel-border top">
      <div class="panel-heading">
        <span class="panel-title">Informacje: <a href="http://<?= $this -> site['url'];?>" target="_blank" class="text-primary"><?= $this -> site['url'];?></a></span>
      </div>
      <div class="panel-body">
        <div class="mb10">
          <?= \Html::button(
                      array(
                        'class' => 'btn-sm btn-default',
                        'title' => 'Poprzednia strona',
                        'url'   => '/ranker_clients/main_view/id=' . $this -> prev_site_id,
                        'icon'  => 'fa-backward'
                      )
                  );?>
          <?= \Html::button(
                      array(
                        'class' => 'btn-sm btn-default',
                        'title' => 'Następna strona',
                        'url'   => '/ranker_clients/main_view/id=' . $this -> next_site_id,
                        'icon'  => 'fa-forward'
                      )
                  );?>
          <select name="page" class="form-control ranker-select" id="page" onChange="var id = $(this).val(); document.location.href='/ranker_clients/main_view/id=' + id;" style='max-width: 250px;'>
            <?
            if ( is_array ( $this -> sites ) ) foreach ( $this -> sites as $site )
            {
              if ( $this -> site['id'] == $site['id'] )
                $name = $site['name'];
              ?>
                <option value="<?= $site['id'];?>" <? if ( $this -> site['id'] == $site['id'] ) echo 'selected="selected"';?>>
                  <?= $site['name'];?>
                </option>
              <?
            }
            ?>
          </select>
          <? if ( $this -> site['subscription'] > 0 ) echo 'Abonament: <b>' . \S::number( $this -> site['subscription'] ) . ' zł</b>';?>
        </div>
      </div>
    </div>
  </div>
</div>
<div class="panel panel-primary panel-border top mbn">
  <div class="panel-heading">
    <span class="panel-title">Lista fraz</span>
    <div class="pull-right">
       <a href="/ranker_clients/main_view/id=<?= $this -> site['id'];?>&year=<?= date( 'Y', strtotime( '-1 months', strtotime( $this -> year . '-' . $this -> month . '-01' ) ) );?>&month=<?= date( 'm', strtotime( '-1 months', strtotime( $this -> year . '-' . $this -> month . '-01' ) ) );?>" class="btn btn-sm btn-default" title="Poprzedni m-c">
        <i class="fa fa-backward"></i>
      </a>
      <a href="/ranker_clients/main_view/id=<?= $this -> site['id'];?>&year=<?= date( 'Y', strtotime( '+1 months', strtotime( $this -> year . '-' . $this -> month . '-01' ) ) );?>&month=<?= date( 'm', strtotime( '+1 months', strtotime( $this -> year . '-' . $this -> month . '-01' ) ) );?>" class="btn btn-sm btn-default" title="Następny m-c">
        <i class="fa fa-forward"></i>
      </a>
      <select name="month_global" class="form-control ranker-select" id="month_global" onChange="document.location.href='/ranker_clients/main_view/id=<?= $this -> site['id'];?>&year=' + $( '#year_global' ).val() + '&month=' + $( this ).val();">
        <option value="01" <? if ( $this -> month == '01' ) echo 'selected="selected"'; ?>>Styczeń</option>
        <option value="02" <? if ( $this -> month == '02' ) echo 'selected="selected"'; ?>>Luty</option>
        <option value="03" <? if ( $this -> month == '03' ) echo 'selected="selected"'; ?>>Marzec</option>
        <option value="04" <? if ( $this -> month == '04' ) echo 'selected="selected"'; ?>>Kwiecień</option>
        <option value="05" <? if ( $this -> month == '05' ) echo 'selected="selected"'; ?>>Maj</option>
        <option value="06" <? if ( $this -> month == '06' ) echo 'selected="selected"'; ?>>Czerwiec</option>
        <option value="07" <? if ( $this -> month == '07' ) echo 'selected="selected"'; ?>>Lipiec</option>
        <option value="08" <? if ( $this -> month == '08' ) echo 'selected="selected"'; ?>>Sierpień</option>
        <option value="09" <? if ( $this -> month == '09' ) echo 'selected="selected"'; ?>>Wrzesień</option>
        <option value="10" <? if ( $this -> month == '10' ) echo 'selected="selected"'; ?>>Październik</option>
        <option value="11" <? if ( $this -> month == '11' ) echo 'selected="selected"'; ?>>Listopad</option>
        <option value="12" <? if ( $this -> month == '12' ) echo 'selected="selected"'; ?>>Grudzień</option>
      </select>
      <select name="year_global" id="year_global" class="form-control ranker-select" onChange="document.location.href='/ranker_clients/main_view/id=<?= $this -> site['id'];?>&year=' + $( this ).val() + '&month=' + $( '#month_global' ).val();">
        <? for ( $i = date( 'Y') - 2; $i <= date( 'Y') + 2; $i++ ):?>
          <option value="<?= $i;?>" <? if ( $i == $this -> year ) echo 'selected="selected"'; ?>><?=$i;?></option>
        <? endfor;?>
      </select>
    </div>
  </div>
</div>
<div class="positions">
  <div class="left-column">
    <div class="row mn">
      <div class="col-xs-12 phrase-info"></div>
    </div>
    <? if ( is_array( $this -> phrases ) ): foreach ( $this -> phrases as $phrase ):?>
      <?
        $phrase_class = '';

        if ( new DateTime( $phrase['date_end'] ) < new DateTime( date( 'Y-m-d' ) ) or new DateTime( $this -> site['date_end'] ) < new DateTime( date( 'Y-m-d' ) ) ) 
          $phrase_class = 'text-danger';

        if ( $phrase['date_start'] and new DateTime( $phrase['date_start'] ) > new DateTime( date( 'Y-m-d' ) ) ) 
          $phrase_class = 'text-danger';
          
        $pos = $phrase['positions'][ date( 'Y-m-d' ) ]['position'];

        if ( $pos <= 10 )
          $start = 0;
        else
        {
          $start = 10 * floor( $pos / 10 );
          if ( $pos%10 == 0 )
            $start = $start - 10;
        }
      ?>
      <div class="row mn">
        <div class="col-xs-12 phrase-info">
          <a href="http://www.google.pl/search?q=<?= urlencode( $phrase['phrase'] );?>&num=10&ie=utf8&hl=pl&start=<?= $start;?>" target="_blank" title="<?= $phrase['phrase'];?>" class="phrase <?= $phrase_class;?>">
            <?= $phrase['phrase'];?>
          </a>
        </div>
      </div>
    <? endforeach; endif;?>
  </div>
  <div class="right-column">
    <div class="table-responsive" style="padding-left: 0;">
      <?
        $start  = date( 'Y-m-d', mktime( 0, 0, 0, $this -> month, 1, $this -> year ) );
        $days   = date( 't', strtotime( $start ) );
      ?>
      <table class="table table-bordered table-striped table-condensed table-hover">
        <thead>
          <tr>
            <th class="text-center text-muted" site-id="<?= $this -> site['id'];?>" date="<?= date( 'Y-m-t', strtotime( '-1 month', strtotime( $this -> year . '-' . $this -> month ) ) );?>">
              <?= date( 't', strtotime( '-1 month', strtotime( $this -> year . '-' . $this -> month ) ) );?>
            </th>
            <? for ( $i = 1; $i <= $days; $i++ ):?>
              <th class="text-center pointer table-date <? if ( $this -> comments[ date( 'Y-m-d', strtotime( $this -> year . '-' . $this -> month . '-' . $i ) ) ] ) echo "event-exists";?>" site-id="<?= $this -> site['id'];?>" date="<?= date( 'Y-m-d', strtotime( $this -> year . '-' . $this -> month . '-' . $i ) );?>">
                <?= $i;?>
              </th>
            <? endfor;?>
          </tr>
        </thead>
        <tbody>
          <? if ( is_array( $this -> phrases ) ):?>
            <? foreach ( $this -> phrases as $phrase ):?>
              <tr>
                <?
                  $date_tmp = date( 'Y-m-t', strtotime( '-1 month', strtotime( $this -> year . '-' . $this -> month ) ) );

                  $pos = $phrase['positions'][ $date_tmp ]['position'];
                  $map = $phrase['positions'][ $date_tmp ]['map'];
                  $url = $phrase['positions'][ $date_tmp ]['url'];

                  $class  = 'position';
                  $text   = '';
                  $end    = '';

                  if ( $pos == 1 )
                    $class = 'pos_1';
                  if ( $pos == 2 || $pos == 3 )
                    $class = 'pos_2';
                  if ( $pos >= 4 && $pos <= 6 )
                    $class = 'pos_4';
                  if ( $pos >= 7 && $pos <= 10 )
                    $class = 'pos_7';
                  if ( $pos >= 11 && $pos <= 20 )
                    $class = 'pos_11';
                  if ( $pos == 0 || $pos >= 21 )
                    $class = 'pos_21';

                  if ( new DateTime( $phrase['date_end'] ) < new DateTime( date( 'Y-m-d' ) ) or new DateTime( $this -> site['date_end'] ) < new DateTime( date( 'Y-m-d' ) ) ) 
                    $class = 'end position';

                  if ( $phrase['date_start'] and new DateTime( $phrase['date_start'] ) > new DateTime( date( 'Y-m-d' ) ) ) 
                    $class = 'end position';

                  if ( $pos != '' )
                    $pos = $pos;
                  else
                    $pos = '-';
                ?>
                <td class="position text-center small <?= $class;?>" id="<?= $phrase['id'] . '-' .  $date_tmp;?>" phrase-id="<?= $phrase['id'];?>" date="<?= $date_tmp;?>" title="<?= $url;?>">
                  <span><?= empty( $pos ) ? '-' : $pos;?></span>
                </td>
              <? for ( $i = 1; $i <= $days; $i++ ):
                  $i < 10 ? $d = '0' . $i : $d = $i;  

                  $pos = $phrase['positions'][ $this -> year . '-' . $this -> month . '-' . $d ]['position'];
                  $map = $phrase['positions'][ $this -> year . '-' . $this -> month . '-' . $d ]['map'];
                  $url = $phrase['positions'][ $this -> year . '-' . $this -> month . '-' . $d ]['url'];

                  $class  = 'position';
                  $text   = '';
                  $end    = '';

                  if ( $pos == 1 )
                    $class = 'pos_1';
                  if ( $pos == 2 || $pos == 3 )
                    $class = 'pos_2';
                  if ( $pos >= 4 && $pos <= 6 )
                    $class = 'pos_4';
                  if ( $pos >= 7 && $pos <= 10 )
                    $class = 'pos_7';
                  if ( $pos >= 11 && $pos <= 20 )
                    $class = 'pos_11';
                  if ( $pos == 0 || $pos >= 21 )
                    $class = 'pos_21';

                  if ( new DateTime( $phrase['date_end'] ) < new DateTime( date( 'Y-m-d' ) ) or new DateTime( $this -> site['date_end'] ) < new DateTime( date( 'Y-m-d' ) ) ) 
                    $class = 'end position';

                  if ( $phrase['date_start'] and new DateTime( $phrase['date_start'] ) > new DateTime( date( 'Y-m-d' ) ) ) 
                    $class = 'end position';
                  ?>
                  <td class="position text-center small <? if ( $pos != '' ):?>pointer<? endif;?> <?= $class;?>" id="<?= $phrase['id'] . '-' .  $this -> year . '-' . $this -> month . '-' . $d;?>" phrase="<?= $phrase['id'];?>" date="<?= $this -> year . '-' . $this -> month . '-' . $d;?>" title="<?= $url;?>">
                    <span <? if ( $pos == '' and $this -> year . '-' . $this -> month . '-' . $d == date( 'Y-m-d' ) and $class != 'end position' ) echo 'class="spin"';?>>
                      <?= $pos;?>
                      <? 
                        if ( $pos == '' and $this -> year . '-' . $this -> month . '-' . $d == date( 'Y-m-d' ) and $class != 'end position' )
                          echo '<i class="fa fa-refresh"></i>';
                      ?>
                    </span>
                  </td>
                <? endfor;?>
              </tr>
            <? endforeach;?>
          <? endif;?>
        </tbody>
      </table>
    </div>
  </div>
</div>
<!-- stawki -->
<? if ( is_array( $this -> phrases ) ):?>
  <div class="row">
    <?
      $suma = 0;
      foreach ( $this -> phrases as $phrase )
      {
        $discount = '';
        
        if ( $this -> site['discount'] )
          $discount = $this -> site['discount'];

        if ( $phrase['discount'] )
          $discount = $phrase['discount'];
        
        if ( is_array( $phrase['prices'] ) ) 
        {
          ?>
          <div class="col-xs-12 col-sm-6 col-md-4 col-lg-3">
            <div class="panel panel-primary">
              <div class="panel-heading small"><?= $phrase['phrase'];?> <? if ( $discount > 0 ):?><span class="fr">(rabat <?= $discount;?>%)</span><? endif;?></div>
              <div class="panel-body p0">
                <?
                  if ( is_array( $phrase['prices'] ) ) 
                  {
                    $sum = 0;
                    ?>
                    <table class="table table-condensed table-bordered table-striped">
                      <thead>
                        <tr>
                          <td class="text-right">Przedział</td>
                          <td class="text-right">Stawka</td>
                          <td class="text-right">Ilość dni</td>
                          <td class="text-right">Należność</td>
                        </tr>
                      </thead>
                      <tbody>
                        <?
                        foreach ( $phrase['prices'] as $price )
                        {
                          $price_c  = \S::number( $price['price'] - \S::number( $price['price'] * $discount / 100 ) );                    
                          ?>
                          <tr <? if ( !$price['count'] ):?> style="background: #efefef;"<? endif;?>>
                            <td class="text-right">
                              <?= $price['start'];?> - <?= $price['end'];?>
                            </td>
                            <td class="text-right">
                              <?= \S::number_display( $price_c );?>
                            </td>
                            <td class="text-right">
                              <?= $price['count'];?>
                            </td>
                            <td class="text-right">
                              <?= \S::number_display( $price_c * $price['count'] );?>
                            </td>
                          </tr>
                          <?
                          $sum  = $sum + $price_c * $price['count'];
                          $suma = $suma + $price_c * $price['count'];
                        }
                        ?>
                        <tr>
                          <td colspan="3" class="text-right">Suma:</td>
                          <td class="alert-dark dark text-right"><?= \S::number_display( $sum );?></td>
                        </tr>
                      </tbody>
                    </table>
                    <?
                  }
                ?>
              </div>
            </div>
          </div>
          <?
        }
      }
    ?>
  </div>
  <div class="row">
    <? if ( $suma ):?>
      <div class="col-md-3">
        <div class="panel panel-dark">
          <div class="panel-heading">
            Razem netto: <?= \S::number_display( $suma );?>
          </div>
        </div>
      </div>
      <div class="col-md-3">
        <div class="panel panel-dark">
          <div class="panel-heading">
            Razem brutto: <?= \S::number_display( $suma * $config['profit']['vat'] );?>
          </div>
        </div>
      </div>
    <? endif;?>
  </div>
<? endif;?>