<? global $config;?>
<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">
        <select name="month_global" class="form-control ranker-select" id="month_global" onChange="document.location.href='./?p=reseller_summary&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='./?p=reseller_summary&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>
<div class="row">
  <div class="col-xs-12 col-sm-6 col-md-5 col-lg-4">
    <div class="alert alert-dark light p5">
      <small>Stawki podstawowe</small>
    </div>
    <div class="row">
      <div class="col-xs-12">
        <div class="panel panel-info panel-border top">
          <div class="panel-body p0">
            <table class="table table-bordered table-condensed table-striped table-hover">
              <thead>
                <tr>
                  <th>Strona</th>
                  <th class="text-right">Netto</th>
                  <th class="text-right">Brutto</th>
                </tr>
              </thead>
              <tbody>
                <? $client_sum = 0;?>
                <? if ( is_array( $this -> _sites ) ): foreach ( $this -> _sites as $site ):?>
                  <tr>
                    <td><?= $site['name'];?></td>
                    <td class="text-right">
                      <?
                      $site_sum = 0;

                      if ( is_array( $site['phrases'] ) ) foreach ( $site['phrases'] as $phrase )
                      {
                        $discount = '';

                        if ( $site['discount'] )
                          $discount = $site['discount'];

                        if ( $phrase['discount'] )
                          $discount = $phrase['discount'];

                        if ( is_array( $phrase['prices'] ) ) foreach ( $phrase['prices'] as $price )
                        {
                          $price_c  = \S::number( $price['price'] - \S::number( $price['price'] * $discount / 100 ) );  
                          $site_sum = $site_sum + $price_c * $price['count'];
                        }
                      }
                      $client_sum = $client_sum + $site_sum;
                      echo \S::number_display( $site_sum );
                      ?>
                    </td>
                    <td class="text-right">
                      <?= \S::number_display( $site_sum * $config['profit']['vat'] );?>
                    </td>
                  </tr>
                <? endforeach; endif;?>
                <tr>
                  <td class="text-right">Razem:</td>
                  <td class="alert-dark dark text-right"><?= \S::number_display( $client_sum );?></td>
                  <td class="alert-dark dark text-right"><?= \S::number_display( $client_sum * $config['profit']['vat'] );?></td>
                </tr>
              </tbody>
            </table>
          </div>
        </div>
      </div>
    </div>
  </div>
  <div class="col-xs-12 col-sm-6 col-md-5 col-lg-4">
    <div class="alert alert-dark light p5">
      <small>Stawki resellera</small>
    </div>
    <div class="row">
      <div class="col-xs-12">
        <div class="panel panel-info panel-border top">
          <div class="panel-body p0">
            <table class="table table-bordered table-condensed table-striped table-hover">
              <thead>
                <tr>
                  <th>Strona</th>
                  <th class="text-right">Netto</th>
                  <th class="text-right">Brutto</th>
                </tr>
              </thead>
              <tbody>
                <?
                $client_sum = 0;
                if ( is_array( $this -> _sites_res ) ) foreach ( $this -> _sites_res as $site )
                {
                  ?>
                  <tr>
                    <td><?= $site['name'];?></td>
                    <td class="text-right">
                      <?
                      $site_sum = 0;

                      if ( is_array( $site['phrases'] ) ) foreach ( $site['phrases'] as $phrase )
                      {
                        $discount = '';

                        if ( $phrase['discount'] )
                          $discount = $phrase['discount'];

                        if ( is_array( $phrase['prices'] ) ) foreach ( $phrase['prices'] as $price )
                        {
                          $price_c  = \S::number( $price['price'] - \S::number( $price['price'] * $discount / 100 ) );  
                          $site_sum = $site_sum + $price_c * $price['count'];
                        }
                      }

                      $client_sum = $client_sum + $site_sum;
                      echo \S::number_display( $site_sum );
                      ?>
                    </td>
                    <td class="text-right">
                      <?= \S::number_display( $site_sum * $config['profit']['vat'] );?>
                    </td>
                  </tr>
                  <?
                }
                ?>
                  <tr>
                    <td class="text-right">Razem:</td>
                    <td class="alert-dark dark text-right"><?= \S::number_display( $client_sum );?></td>
                    <td class="alert-dark dark text-right"><?= \S::number_display( $client_sum * $config['profit']['vat'] );?></td>
                  </tr>
              </tbody>
            </table>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
<script type="text/javascript">
  $( document ).ready( function() {
    $( 'td.position' ).dblclick( function() {
      var id = $( this ).attr( 'id' );
      $( 'td#' + id ).children( 'span' ).addClass( 'hide' );
      $( 'td#' + id ).children( 'input' ).removeClass( 'hide' ).focus();
    })

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

      xajax_changePhrasePosition( phrase_id, date, position );

      $( this ).addClass( 'hide' );
      $( this ).parent( 'td' ).children( 'span' ).html( position ).removeClass( 'hide' );
    })
  })
</script>