<? global $config, $user;?>
<div class="panel panel-success panel-border top">
  <div class="panel-heading" style="height: auto;">
    <div class="widget-menu ml10">
      <div class="row" style="color: #666666;">
        <div class="col-sm-2 col-xs-6 text-right">Netto dzisiaj:</div>
        <div class="col-sm-6 col-xs-6 text-left"><span id="day-profit"><?= $this -> day_profit;?></span> <a href="#" id="refresh-earnings" class="btn btn-primary btn-xs ml10">odśwież</a></div>
        <div class="col-sm-2 col-xs-6 text-right">Netto ogółem:</div>
        <div class="col-sm-2 col-xs-6 text-left"><span id="month-profit"><?= $this -> month_profit;?></span></div>
      </div>
    </div>
  </div>
</div>
<div class="panel panel-info panel-border top">
  <div class="panel-heading" style="height: auto; padding-bottom: 4px;">
    <div class="row" style="color: #666666;">
      <div class="col-xs-12 col-sm-6 col-md-6 col-lg-5 text-centred-xs text-centred-sm">
        <div class="btn-group site-buttons">
          <?= \Html::button(
                      array(
                        'class' => 'btn-sm btn-default',
                        'title' => 'Poprzednia strona',
                        'url'   => '/ranker/main_view/id=' . $this -> prev_site_id,
                        'icon'  => 'fa-backward'
                      )
                  );?>
          <?= \Html::button(
                      array(
                        'class' => 'btn-sm btn-default',
                        'title' => 'Następna strona',
                        'url'   => '/ranker/main_view/id=' . $this -> next_site_id,
                        'icon'  => 'fa-forward'
                      )
                  );?>
          <?= \Html::button(
                      array(
                        'class' => 'btn-sm btn-default',
                        'title' => 'Dodaj stronę',
                        'url'   => '/ranker/site_edit/',
                        'icon'  => 'fa-plus'
                      )
                  );?>
          <?= \Html::button(
                      array(
                        'class' => 'btn-sm btn-default',
                        'title' => 'Edytuj stronę',
                        'url'   => '/ranker/site_edit/id=' . $this -> site['id'],
                        'icon'  => 'fa-file-text'
                      )
                  );?>
          <?= \Html::button(
                      array(
                        'class' => 'btn-sm btn-default site-delete',
                        'title' => 'Usuń grupę',
                        'icon'  => 'fa-trash fa-lg'
                      )
                  );?>
          <?= \Html::button(
                      array(
                        'class' => 'btn-sm btn-success',
                        'title' => 'Dodaj frazy',
                        'url'   => '/?p=ranker_sites&id=' . $this -> site['id'] . '&rw=add_phrase',
                        'icon'  => 'fa-plus'
                      )
                  );?>
          <?= \Html::button(
                      array(
                        'class' => 'btn-sm btn-default site-refresh',
                        'title' => 'Odśwież wszystkie pozycje',
                        'icon'  => 'fa-refresh',
                        'attr'  => [ 'site-id' => $this -> site['id'] ]
                      )
                  );?>
          <? if ( $this -> site['date_confirm'] < date( 'Y-m-d' ) and $this -> site['need_confirm'] ):?>
            <?= \Html::button(
                        array(
                          'class' => 'btn-sm btn-success site-confirm',
                          'title' => 'Potwierdź wyniki',
                          'icon'  => 'fa-check',
                          'attr'  => [ 'site-id' => $this -> site['id'] ]
                        )
                    );?>
          <? endif;?>
        </div>
        <select name="page" class="form-control ranker-select" id="page" onChange="var id = $(this).val(); document.location.href='/ranker/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>
      </div>
      <div class="col-xs-12 col-sm-4 col-md-4 col-lg-2 text-centred-xs hidden-md hidden-xs hidden-sm">
        <? if ( $this -> site['subscription'] > 0 ) echo 'Ab: <b>' . \S::number( $this -> site['subscription'] ) . ' zł</b>';?>
      </div>
      <div class="col-xs-12 col-sm-4 col-md-4 col-lg-2 text-centred-xs hidden-md hidden-xs hidden-sm">
        <a href="http://<?= $this -> site['url'];?>" target="_blank"><?= $this -> site['url'];?></a>
      </div>
      <div class="col-xs-12 col-sm-6 col-md-6 col-lg-3 text-centred-xs text-centred-sm">
        <a href="/ranker/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-arrow-left"></i>
        </a>
        <a href="/ranker/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-arrow-right"></i>
        </a>
        <select name="month_global" class="form-control ranker-select" id="month_global" onChange="document.location.href='/ranker/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/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>
<? $last_logged = \factory\Ranker::last_login_client( $this -> site['id'] );?>
<? if ( $last_logged ):?>
  <div class="panel panel-danger panel-border top" id="client-last-logged">
    <div class="panel-heading">
      <span class="panel-title">Ostatnie logowanie klientów:</span>
    </div>
    <div class="panel-body">
      <?= $last_logged;?>
    </div>
  </div>
<? endif;?>
<div class="panel hidden-sm hidden-md hidden-lg">
  <div class="panel-body p0">
    <div class="row">
      <?
        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';

          if ( strpos( $phrase['phrase'], '* ' ) !== false )
            $phrase_class = 'text-dark';

          $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="col-xs-12">
            <div style="<? if ( $phrase['color'] ) echo 'border-top: 5px solid ' . $phrase['color'];?>">
              <div class="text-left p5" >
                <a href="http://www.google.pl/search?q=<?= urlencode( $phrase['phrase'] );?>&num=10&ie=utf8&hl=pl&start=<?= $start;?><? if ( $phrase['localization'] ) echo '&near=' . urlencode( $phrase['localization'] );?>" target="_blank" title="<?= $phrase['phrase'];?>" class="phrase <?= $phrase_class;?>">
                  <?= $phrase['phrase'];?>
                </a>
                <div class="page-options fr">
                  <a href="#" class="tip a-icon refresh refresh-<?= $this -> site['id'];?>" site-id="<?= $this -> site['id'];?>" phrase-id="<?= $phrase['id'];?>" title="Wymuś ponowne sprawdzenie pozycji">
                    <i class="fa fa-refresh text-dark" style="font-size: 15px; margin: 0 3px;"></i>
                  </a>
                  <a href="/?p=ranker_sites&rw=edit_phrase&phrase_id=<?= $phrase['id'];?>&id=<?= $this -> site['id'];?>" class="tip a-icon" title="Edytuj wybraną frazę">
                    <i class="fa fa-file-text text-dark" style="font-size: 14px; margin: 0 3px; position: relative; top: -1px;"></i>
                  </a>
                  <a href="#" class="tip a-icon delete" site-id="<?= $this -> site['id'];?>" phrase-id="<?= $phrase['id'];?>" title="Usuń wybraną pozycji">
                    <i class="fa fa-trash text-dark" style="font-size: 16px; margin: 0 3px;"></i>
                  </a>
                  <a href="/?p=ranker_sites&rw=edit_costs&phrase_id=<?= $phrase['id'];?>&id=<?= $this -> site['id'];?>" class="tip a-icon" title="Płatności za frazę">
                    <i class="fa fa-list text-dark" style="font-size: 15px; margin: 0 3px;"></i>
                  </a>
                </div>
                <a href="<?= $phrase['last_url'];?>" class="text-muted small clearfix" target="_blank" title="<?= $phrase['last_url'];?>">
                  <?= $phrase['last_url'];?>
                </a>
              </div>
            </div>
          </div>
          <div class="col-xs-12">
            <div class="table-responsive" <? if ( $z++ < 1 ):?>id="positions-xs"<? endif;?>>
              <?
              $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-positions">
                <thead>
                  <tr>
                    <th class="text-center" <? if ( $z < 2 ):?>id="date-xs-<?= date( 'Y-m-d', strtotime( '-1 month', strtotime( $this -> year . '-' . $this -> month ) ) );?>"<? endif;?>><?= date( 't', strtotime( '-1 month', strtotime( $this -> year . '-' . $this -> month ) ) );?></th>
                    <? for ( $i = 1; $i <= $days; $i++ ):?>
                      <th class="text-center" <? if ( $z < 2 ):?>id="date-xs-<?= date( 'Y-m-d', strtotime( $this -> year . '-' . $this -> month . '-' . $i ) );?>"<? endif;?>><?= $i;?></th>
                    <? endfor;?>
                  </tr>
                </thead>
                <tbody>
                  <tr style="height: 45px;">
                    <?
                      $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="m-<?= $phrase['id'] . '-' .  $date_tmp;?>" phrase="<?= $phrase['id'];?>" date="<?= $date_tmp;?>" <? if ( $map ):?> style="box-shadow: inset 0 0 10px rgba( 0, 0, 0, 0.4 );"<? endif;?> title="<?= $url;?>">
                      <span><?= $pos;?></span>
                      <input type="text" value="<?= $pos;?>" class="hide new-pos" />
                    </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';

                        if ( $pos != '' )
                          $pos = $pos;
                        else
                          $pos = '-';
                        ?>
                        <td class="position text-center small <?= $class;?>" id="m-<?= $phrase['id'] . '-' .  $this -> year . '-' . $this -> month . '-' . $d;?>" phrase="<?= $phrase['id'];?>" date="<?= $this -> year . '-' . $this -> month . '-' . $d;?>" <? if ( $map ):?> style="box-shadow: inset 2px 2px 10px rgba( 0, 0, 0, 0.5 );"<? endif;?> title="<?= $url;?>">
                          <span><?= $pos;?></span>
                          <input type="text" class="form-control hide new-pos text-center" value="<?= $pos;?>" style="width: 40px; margin: auto;" />
                        </td>
                    <? endfor;?>
                  </tr>
                </tbody>
              </table>
            </div>
          </div>
          <?
        }
      ?>
    </div>
  </div>
</div>
<div class="panel hidden-xs">
  <div class="panel-body p0">
    <div class="row">
      <div class="col-sm-5 col-md-4 col-lg-3">
        <table class="table table-bordered table-striped table-condensed" id="table-phrases">
          <thead>
            <tr>
              <th class="title text-center">P</th>
              <th class="title text-right">Fraza</th>
            </tr>
          </thead>
          <tbody>
            <?
            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;
              }
              ?>
              <tr style="height: 45px;">
                <td>
                  <div class="text-right" >
                    <a href="http://www.google.pl/search?q=<?= urlencode( $phrase['phrase'] );?>&num=10&ie=utf8&hl=pl&start=<?= $start;?><? if ( $phrase['localization'] ) echo '&near=' . urlencode( $phrase['localization'] );?>" target="_blank" title="<?= $phrase['phrase'];?>" class="phrase <?= $phrase_class;?>">
                      <?= $phrase['phrase'];?>
                    </a>
                    <div class="page-options">
                      <a class="tip a-icon refresh pointer refresh-<?= $this -> site['id'];?>" site-id="<?= $this -> site['id'];?>" phrase-id="<?= $phrase['id'];?>" title="Wymuś ponowne sprawdzenie pozycji">
                        <i class="fa fa-refresh text-dark" style="font-size: 15px; margin: 0 3px;"></i>
                      </a>
                      <a href="/?p=ranker_sites&rw=edit_phrase&phrase_id=<?= $phrase['id'];?>&id=<?= $this -> site['id'];?>" class="tip a-icon" title="Edytuj wybraną frazę">
                        <i class="fa fa-file-text text-dark" style="font-size: 14px; margin: 0 3px; position: relative; top: -1px;"></i>
                      </a>
                      <a class="tip a-icon delete pointer" site-id="<?= $this -> site['id'];?>" phrase-id="<?= $phrase['id'];?>" title="Usuń wybraną frazę">
                        <i class="fa fa-trash text-dark" style="font-size: 16px; margin: 0 3px;"></i>
                      </a>
                      <a href="/?p=ranker_sites&rw=edit_costs&phrase_id=<?= $phrase['id'];?>&id=<?= $this -> site['id'];?>" class="tip a-icon" title="Płatności za frazę">
                        <i class="fa fa-list text-dark" style="font-size: 15px; margin: 0 3px;"></i>
                      </a>
                      <? if ( $phrase['localization'] ):?>
                      <a href="http://www.google.pl/search?q=<?= urlencode( $phrase['phrase'] );?>&num=10&ie=utf8&hl=pl&start=<?= $start;?><? if ( $phrase['localization'] ) echo '&uule=' . \factory\Ranker::uule( $phrase['localization'] );?>" target="_blank" title="<?= $phrase['phrase'];?>" class="<?= $phrase_class;?>">
                          <i class="fa fa-map-marker text-danger" style="font-size: 16px; margin: 0 3px;"></i>
                        </a>
                      <? endif;?>
                    </div>
                    <div class="phrase_url clearfix small" id="phrase_url_<?= $phrase['id'];?>">
                      <a href="<?= $phrase['last_url'];?>" class="text-muted" target="_blank" title="<?= $phrase['last_url'];?>">
                        <?= $phrase['last_url'];?>
                      </a>
                    </div>
                  </div>
                </td>
              </tr>
              <?
            }
            ?>
          </tbody>
        </table>
      </div>
      <div class="col-sm-7 col-md-8 col-lg-9">
        <div class="table-responsive" id="positions-not-xs">
          <?
          $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 table-positions">
            <thead>
              <tr>
                <th class="text-center event <? if ( $this -> comments[ date( 'Y-m-t', strtotime( '-1 month', strtotime( $this -> year . '-' . $this -> month ) ) ) ] ) echo "event-exists";?>" date="<?= date( 'Y-m-t', strtotime( '-1 month', strtotime( $this -> year . '-' . $this -> month ) ) );?>" id="date-<?= date( 'Y-m-t', strtotime( '-1 month', strtotime( $this -> year . '-' . $this -> month ) ) );?>">
                  <?= date( 't', strtotime( '-1 month', strtotime( $this -> year . '-' . $this -> month ) ) );?>
                  <div class="comment-info" id="comment-info-<?= date( 'Y-m-d', strtotime( $this -> year . '-' . $this -> month . '-' . $i ) );?>">
                    <form method="POST" action="#" class="form-horizontal">
                      <div class="row">
                        <div class="col col-xs-12">
                          <div class="form-group ">
                            <div class="col-lg-12">
                              <textarea class="comment form-control" name="comment" rows="6"><?= $this -> comments[ date( 'Y-m-d', strtotime( $this -> year . '-' . $this -> month . '-' . $i ) ) ];?></textarea>
                            </div>
                          </div>
                          <div class="form-group">
                            <div class="col-xs-12 text-left">
                              <?= \Html::button(
                                          array(
                                            'class' => 'btn btn-sm btn-success',
                                            'text'  => 'Zatwierdź',
                                            'js'    => 'save_comment_info( $( this ).parents( "div.comment-info" ).find( ".comment" ).val(), "' . date( 'Y-m-t', strtotime( '-1 month', strtotime( $this -> year . '-' . $this -> month ) ) ) . '", ' . $this -> site['id'] . ' );',
                                            'icon'  => 'fa-check-circle mr5'
                                          )
                                      );?>
                              <?= \Html::button(
                                          array(
                                            'class' => 'btn btn-sm btn-dark',
                                            'text'  => 'Zamknij',
                                            'js'    => '$( "#overlay" ).hide(); $( this ).parents( "div.comment-info" ).hide();',
                                            'icon'  => 'fa-times mr5'
                                          )
                                      );?>
                              <?= \Html::button(
                                          array(
                                            'class' => 'btn btn-sm btn-danger pull-right',
                                            'text'  => 'Usun',
                                            'js'    => 'delete_comment_info( "' . date( 'Y-m-t', strtotime( '-1 month', strtotime( $this -> year . '-' . $this -> month ) ) ) . '", ' . $this -> site['id'] . ' );',
                                            'icon'  => 'fa-trash mr5'
                                          )
                                      );?>
                            </div>
                          </div>
                        </div>
                      </div>
                    </form>
                  </div>
                </th>
                <? for ( $i = 1; $i <= $days; $i++ ):?>
                <th class="text-center event <? if ( $this -> comments[ date( 'Y-m-d', strtotime( $this -> year . '-' . $this -> month . '-' . $i ) ) ] ) echo "event-exists";?>" date="<?= date( 'Y-m-d', strtotime( $this -> year . '-' . $this -> month . '-' . $i ) );?>" id="date-<?= date( 'Y-m-d', strtotime( $this -> year . '-' . $this -> month . '-' . $i ) );?>">
                  <?= $i;?>
                  <div class="comment-info" id="comment-info-<?= date( 'Y-m-d', strtotime( $this -> year . '-' . $this -> month . '-' . $i ) );?>">
                    <form method="POST" action="#" class="form-horizontal">
                      <div class="row">
                        <div class="col col-xs-12">
                          <div class="form-group ">
                            <div class="col-lg-12">
                              <textarea class="comment form-control" name="comment" rows="6"><?= $this -> comments[ date( 'Y-m-d', strtotime( $this -> year . '-' . $this -> month . '-' . $i ) ) ];?></textarea>
                            </div>
                          </div>
                          <div class="form-group">
                            <div class="col-xs-12 text-left">
                              <?= \Html::button(
                                          array(
                                            'class' => 'btn btn-sm btn-success',
                                            'text'  => 'Zatwierdź',
                                            'js'    => 'save_comment_info( $( this ).parents( "div.comment-info" ).find( ".comment" ).val(), "' . date( 'Y-m-d', strtotime( $this -> year . '-' . $this -> month . '-' . $i ) ) . '", ' . $this -> site['id'] . ' );',
                                            'icon'  => 'fa-check-circle mr5'
                                          )
                                      );?>
                              <?= \Html::button(
                                          array(
                                            'class' => 'btn btn-sm btn-dark',
                                            'text'  => 'Zamknij',
                                            'js'    => '$( "#overlay" ).hide(); $( this ).parents( "div.comment-info" ).hide();',
                                            'icon'  => 'fa-times mr5'
                                          )
                                      );?>
                              <?= \Html::button(
                                          array(
                                            'class' => 'btn btn-sm btn-danger pull-right',
                                            'text'  => 'Usun',
                                            'js'    => 'delete_comment_info( "' . date( 'Y-m-d', strtotime( $this -> year . '-' . $this -> month . '-' . $i ) ) . '", ' . $this -> site['id'] . ' );',
                                            'icon'  => 'fa-trash mr5'
                                          )
                                      );?>
                            </div>
                          </div>
                        </div>
                      </div>
                    </form>
                  </div>
                </th>
                <? endfor;?>
              </tr>
            </thead>
            <tbody>
              <? if ( is_array( $this -> phrases ) ):?>
                <? foreach ( $this -> phrases as $phrase ):?>
                  <tr style="height: 45px;">
                    <?
                      $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="<?= $phrase['id'];?>" date="<?= $date_tmp;?>" <? if ( $map ):?> style="box-shadow: inset 0 0 10px rgba( 0, 0, 0, 0.4 );"<? endif;?> title="<?= $url;?>">
                      <span><?= $pos;?></span>
                      <input type="text" value="<?= $pos;?>" class="hide new-pos" />
                    </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';

                      if ( $pos != '' )
                        $pos = $pos;
                      else
                        $pos = '-';
                      ?>
                      <td class="position text-center small <?= $class;?> <? if ( $pos == '-' and $this -> year . '-' . $this -> month . '-' . $d == date( 'Y-m-d' ) and $class != 'end position' ) echo ' to-check';?>" id="<?= $phrase['id'] . '-' .  $this -> year . '-' . $this -> month . '-' . $d;?>" phrase="<?= $phrase['id'];?>" date="<?= $this -> year . '-' . $this -> month . '-' . $d;?>" <? if ( $map ):?> style="box-shadow: inset 2px 2px 10px rgba( 0, 0, 0, 0.5 );"<? endif;?> title="<?= $url;?>">
                        <span><?= $pos;?></span>
                        <input type="text" class="form-control hide new-pos text-center" value="<?= $pos;?>" style="width: 40px; margin: auto;" />
                        <? if ( $pos == '-' and $this -> year . '-' . $this -> month . '-' . $d == date( 'Y-m-d' ) ):?>
                          <script type="text/javascript">
                            $( function() {
                              ajax_check_phrase_position( <?= $phrase['id'];?> );
                            });
                          </script>
                        <? endif;?>
                      </td>
                  <? endfor;?>
                </tr>
                <? endforeach;?>
              <? endif;?>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>
</div>

<!-- komentarz -->
<? if ( $this -> site['comments'] ):?>
  <div class="panel panel-danger panel-border top">
    <div class="panel-body">
      <?= $this -> site['comments'];?>
    </div>
  </div>
<? endif;?>

<!-- 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;?>
<div style="margin-bottom: 211px;"></div>
<link rel='stylesheet' type='text/css' href='/libraries/grid/plugins/impromptu/jquery-impromptu.css'>
<link rel="stylesheet" type="text/css" href="/libraries/framework/vendor/plugins/c3charts/c3.min.css">
<script type='text/javascript' src='/libraries/grid/plugins/impromptu/jquery-impromptu.js'></script>
<script type="text/javascript" src="/libraries/framework/vendor/plugins/c3charts/d3.min.js"></script>
<script type="text/javascript" src="/libraries/framework/vendor/plugins/c3charts/c3.min.js"></script>
<script type="text/javascript">
  var timer;
  var D3Charts = function ()
  {
    var runD3Plugins = function () { };

    var runD3Charts = function ()
    {
      var Colors = [ '#4a89dc', '#ff9800', '#f6bb42', '#967adc', '#e9573f', '#37bc9b', '#70ca63' ];
      var chart1 = c3.generate(
      {
        bindto: '#swl-follow-chart',
        color: {
          pattern: Colors
        },
        data: {
          columns: [
            [ 'nofollow', <?= $this -> swl_follow['nofollow'];?> ],
            [ 'dofollow', <?= $this -> swl_follow['dofollow'];?> ]
          ],
          type : 'pie'
        },
        legend: {
          position: 'right'
        }
      });
      var chart2 = c3.generate(
      {
        bindto: '#swl-types-chart',
        color: {
          pattern: Colors
        },
        data: {
          columns: [
            [ 'obrazki', <?= $this -> swl_types['img'];?> ],
            [ 'tekst', <?= $this -> swl_types['text'];?> ]
          ],
          type : 'pie'
        },
        legend: {
          position: 'right'
        }
      });
      var chart3 = c3.generate(
      {
        bindto: '#swl-anchor-chart',
        color: {
          pattern: Colors
        },
        data: {
          columns: [
            <? if ( is_array( $this -> swl_anchors ) ): foreach ( $this -> swl_anchors as $anchor ):?>
              [ '<?= mb_substr( $anchor['anchor'], 0, 30, 'UTF-8' );?><? if ( strlen( $anchor['anchor'] ) > 30 ) echo '...';?>', <?= $anchor['swl_count'];?> ]<? if ( $anchor != end( $this -> swl_anchors ) ):?>,<? endif;?>
            <? endforeach; endif;?>
          ],
          type : 'pie'
        },
        legend: {
          position: 'right'
        }
      });
      var chart4 = c3.generate(
      {
        bindto: '#swl-brand-chart',
        color: {
          pattern: Colors
        },
        data: {
          columns: [
            [ 'brand', <?= $this -> swl_brand['brand'];?> ],
            [ 'anchor', <?= $this -> swl_brand['anchor'];?> ]
          ],
          type : 'pie'
        },
        legend: {
          position: 'right'
        }
      });
    };
    return {
      init: function () {
        runD3Plugins();
        runD3Charts();
      }
    };
  }();

  D3Charts.init();

  function delete_comment_info( date, site_id )
  {
    $( '.comment-info' ).hide();
    $( '#overlay' ).hide();

    $.alert(
    {
      title: 'Potwierdź',
      content: 'Na pewno chcesz usunąć wybraną frazę?',
      type: 'orange',
      closeIcon: true,
      closeIconClass: 'fa fa-close',
      typeAnimated: true,
      animation: 'opacity',
      buttons:
      {
        confirm:
        {
          text: 'Zatwierdź',
          btnClass: 'btn-blue',
          keys: ['enter'],
          action: function()
          {
            jQuery.ajax(
            {
              type: 'POST',
              cache: false,
              url: '/ajax.php',
              data:
              {
                date: date,
                site_id: site_id,
                a: 'delete_site_comment_info'
              },
              beforeSend: function()
              {
                jQuery( '#overlay' ).show();
              },
              success: function( data )
              {
                $( '#comment-info-' + date ).parents( 'th.event' ).removeClass( 'event-exists' );
                $( '.comment-info' ).hide();
                $( '#overlay' ).hide();
              }
            });
          }
        },
        cancel:
        {
          text: 'Anuluj',
          action: function() {}
        }
      }
    });
  }

  function save_comment_info( comment, date, site_id )
  {
    jQuery.ajax(
    {
      type: 'POST',
      cache: false,
      url: '/ajax.php',
      data:
      {
        comment: comment,
        date: date,
        site_id: site_id,
        a: 'change_site_comment_info'
      },
      beforeSend: function()
      {
        jQuery( '#overlay' ).show();
      },
      success: function( data )
      {
        $( '#comment-info-' + date ).parents( 'th.event' ).addClass( 'event-exists' );
        $( '.comment-info' ).hide();
        $( '#overlay' ).hide();
      }
    });
  }

  $( document ).ready( function()
  {
    /* odświeżenie wszystkich pozycji */
    $( 'body' ).on( 'click', '.site-refresh', function()
    {
      $.alert(
      {
        title: 'Potwierdź',
        content: 'Na pewno chcesz odświerzyć wszystkie pozycje?',
        type: 'orange',
        closeIcon: true,
        closeIconClass: 'fa fa-close',
        typeAnimated: true,
        animation: 'opacity',
        buttons:
        {
          confirm:
          {
            text: 'Zatwierdź',
            btnClass: 'btn-blue',
            keys: ['enter'],
            action: function()
            {
              $( '#table-phrases .fa-refresh' ).each( function()
              {
                $( this ).trigger( 'dblclick' );
              });
            }
          },
          cancel:
          {
            text: 'Anuluj',
            action: function() {}
          }
        }
      });
    });

    <? if ( is_array( $this -> phrases ) and !empty( $this -> phrases ) ):?>
      if ( $( '#positions-not-xs' ).hasScrollBar() === true ) {
        $( '#positions-not-xs' ).animate( { scrollLeft: $( '#date-<?= date( 'Y-m-d' );?>' ).position().left - 50 }, 500);
      }

      if ( $( '#positions-xs' ).hasScrollBar() === true ) {
        $( '.hidden-sm .table-responsive' ).animate( { scrollLeft: $( '#date-xs-<?= date( 'Y-m-d' );?>' ).position().left - 50 }, 500);
      }
    <? endif;?>

    $( 'body' ).on( 'click', '.event', function(e) {
      if ( $( e.target ).is( 'th' ) ) {
        var date = $( this ).attr( 'date' );
        $( '#overlay' ).show();
        $( '#comment-info-' + date ).show();
      }
    });

    $( 'body' ).on( 'dblclick', 'td.position', function() {
      var id = $( this ).attr( 'id' );
      $( 'td#' + id ).children( 'span' ).addClass( 'hide' );
      $( 'td#' + id ).children( 'input' ).removeClass( 'hide' ).focus().select();
    });

    $( 'body' ).on( 'click', '.download-info', function( e )  {
      if ( $( e.target ).is( 'td' ) || $( e.target ).is( 'div' ) ) {
        $( '.swl-info' ).hide();
        $( '#overlay' ).show();
        $( this ).children( 'div.swl-info' ).show();
      }
    });

    $( 'body' ).on( 'blur', '.new-pos', function() {
      var new_class = '';
      var input     = $( this );
      var date      = $( this ).parent( 'td' ).attr( 'date' );
      var position  = $( this ).val();
      var phrase_id = $( this ).parent( 'td' ).attr( 'phrase' );

      jQuery.ajax({
        type: 'POST',
        cache: false,
        url: '/ajax.php',
        data: {
          date: date,
          phrase_id: phrase_id,
          position: position,
          a: 'change_phrase_position'
        },
        beforeSend: function() {
          $( '#' + phrase_id + '-' + date ).removeClass( 'pos_1 pos_2 pos_4 pos_7 pos_11 pos_21' );
          $( '#' + phrase_id + '-' + date + ' span' ).html( '' );
        },
        success: function( data ) {
          input.addClass( 'hide' );
          input.parent( 'td' ).children( 'span' ).html( position ).removeClass( 'hide' );

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

          $( '#' + phrase_id + '-' + date ).addClass( new_class );

          jQuery( '#overlay' ).hide();
        }
      });
    });

    $( 'body' ).on( 'click', '.delete', function()
    {
      var site_id   = $( this ).attr( 'site-id' );
      var phrase_id = $( this ).attr( 'phrase-id' );

      $.alert(
      {
        title: 'Potwierdź',
        content: 'Na pewno chcesz usunąć wybraną frazę?',
        type: 'orange',
        closeIcon: true,
        closeIconClass: 'fa fa-close',
        typeAnimated: true,
        animation: 'opacity',
        buttons:
        {
          confirm:
          {
            text: 'Zatwierdź',
            btnClass: 'btn-blue',
            keys: ['enter'],
            action: function()
            {
              document.location.href = '/?p=ranker_sites&rw=del_phrase&phrase_id=' + phrase_id + '&id=' + site_id;
            }
          },
          cancel:
          {
            text: 'Anuluj',
            action: function() {}
          }
        }
      });
    });

    if ( isTouchDevice() === true )
    {
      $( 'body' ).on( 'click', '.site-confirm', function()
      {
        var site_id   = $( this ).attr( 'site-id' );
        $( this ).blur().addClass( 'disabled' );

        jQuery.prompt( 'Na pewno chcesz zatwierdzić wybrane pozycje?',
        {
          title: 'Potwierdź?',
          submit: function(e,v,m,f)
          {
            if ( v === true )
            {
              ajax_site_confirm( site_id, <?= $this -> next_site_id;?> );
            }
          },
          buttons: {
            'tak': true,
            'nie': false
          },
          focus: 0
        });
      });
    }
    else
    {
      $( 'body' ).on( 'dblclick', '.site-confirm', function()
      {
        $( this ).blur().addClass( 'disabled' );
        ajax_site_confirm( $( this ).attr( 'site-id' ), <?= $this -> next_site_id;?> );
      });
    }

    $( 'body' ).on( 'click', '.site-delete', function()
    {
      jQuery.prompt( 'Na pewno chcesz odświeżyć pozycję wybranej frazy?',
      {
        title: 'Potwierdź?',
        submit: function(e,v,m,f)
        {
          if ( v === true )
            document.location.href = "/?p=ranker_sites&rw=del&site-id=<?= $this -> site['id'];?>";
        },
        buttons: {
          'tak': true,
          'nie': false
        },
        focus: 1
      });
    });

    /* wymuszenie odświeżenia pozycji */
    if ( isTouchDevice() === true )
    {
      $( 'body' ).on( 'click', '.refresh', function()
      {
        $( this ).blur().addClass( 'disabled' );

        var site_id   = $( this ).attr( 'site-id' );
        var phrase_id = $( this ).attr( 'phrase-id' );

        jQuery.prompt( 'Na pewno chcesz odświeżyć pozycję wybranej frazy?',
        {
          title: 'Potwierdź?',
          submit: function(e,v,m,f)
          {
            if ( v === true )
              ajax_refresh_phrase_position( site_id, phrase_id );
          },
          buttons:
          {
            'tak': true,
            'nie': false
          },
          focus: 0
        });
        return false;
      });
    }
    else
    {
      $( 'body' ).on( 'dblclick', '.refresh', function()
      {
        $( this ).addClass( 'disabled' );
        ajax_refresh_phrase_position(
                $( this ).attr( 'site-id' ),
                $( this ).attr( 'phrase-id' )
              );
      });
    }

    $( 'body' ).on( 'click', '#refresh-earnings', function()
    {
      var button = $( this );

      if ( button.attr( 'disabled' ) == 'disabled' )
        return false;

      jQuery.ajax(
      {
        type: 'POST',
        cache: false,
        url: '/ajax.php',
        data: {
          a: 'refresh-earnings'
        },
        beforeSend: function() {
          button.attr( 'disabled', 'disabled' );
        },
        success: function( response ) {
          data = jQuery.parseJSON( response );
          $( '#day-profit' ).html( data.day_profit );
          $( '#month-profit' ).html( data.month_profit );
          button.removeAttr( 'disabled' );
        }
      });
    });

    $( '.phrase_url' ).css( 'width', $( '#table-phrases tr:first-child td:last-child' ).width() + 'px' ).css( 'display', 'block' );
  });

  $(window).on( 'resize', function()
  {
    $( '.phrase_url' ).hide().css( 'width', $( '#table-phrases tr:first-child td:last-child' ).width() + 'px' ).css( 'display', 'block' );
  });
</script>