<div class="row block-header">
  <div class="col-12">
    <h2>statystyki <strong>finansowe</strong><? if ( $this -> client_id ): echo ' - ' . \factory\Finances::client_name( $this -> client_id ); endif;?></h2>
    <small>Analiza kosztów i przychodów (wg kategorii)</small>
  </div>
</div>
<div class="finance-summary">
  <div class="column">
    <div class="panel panel-tile text-center br-a br-grey">
      <div class="panel-body">
        <h1 class="fs30 mt5 mbn <?= ( $this -> wallet_summary > 0 ) ? 'text-system' : 'text-danger';?>"><?= \S::number_display( $this -> wallet_summary );?></h1>
      </div>
      <div class="panel-footer br-t p12">
        <span class="fs12">od początku</span>
      </div>
    </div>
  </div>
  <div class="column">
    <div class="panel panel-tile text-center br-a br-grey">
      <div class="panel-body">
        <h1 class="fs30 mt5 mbn <?= ( $this -> wallet_summary_this_month > 0 ) ? 'text-system' : 'text-danger';?>"><?= \S::number_display( $this -> wallet_summary_this_month );?></h1>
      </div>
      <div class="panel-footer br-t p12">
        <span class="fs11">suma w tym miesiącu</span>
      </div>
    </div>
  </div>
  <div class="column">
    <div class="panel panel-tile text-center br-a br-grey">
      <div class="panel-body">
        <h1 class="fs30 mt5 mbn <?= ( $this -> wallet_income_this_month > 0 ) ? 'text-system' : 'text-danger';?>"><?= \S::number_display( $this -> wallet_income_this_month );?></h1>
      </div>
      <div class="panel-footer br-t p12">
        <span class="fs11">dochody w tym miesiącu</span>
      </div>
    </div>
  </div>
  <div class="column">
    <div class="panel panel-tile text-center br-a br-grey">
      <div class="panel-body">
        <h1 class="fs30 mt5 mbn <?= ( $this -> wallet_expenses_this_month > 0 ) ? 'text-system' : 'text-danger';?>"><?= \S::number_display( $this -> wallet_expenses_this_month );?></h1>
      </div>
      <div class="panel-footer br-t p12">
        <span class="fs11">wydatki w tym miesiącu</span>
      </div>
    </div>
  </div>
</div>
<div class="finance-manager">
  <div class="column-left">
    <div class="clients-list-container">
      <div class="finance-clients">
        <a href="/finances/main_view/client_clear=true" class="btn btn-sm btn-success mb10" style="display:block;"><i class="fa fa-bars"></i> wszyscy klienci</a>
        <? if ( is_array( $this -> clients ) and count( $this -> clients ) ):?>
          <table class="table table-sm table-hover mb0" style="background: #FFF; font-size: 12px;">
            <thead>
              <tr>
                <th>Klient</th>
                <th class="text-right" style="width: 90px;">Dochód</th>
              </tr>
            </thead>
            <tbody>
              <? foreach ( $this -> clients as $client ):?>
                <tr class="<?= ( $client['id'] == $this -> client_id ) ? 'table-primary' : '';?>" style="cursor: pointer;" onclick="document.location.href='/finances/main_view/client_id=<?= $client['id'];?>'">
                  <td><?= $client['firm'];?></td>
                  <td class="text-right <?= $client['total'] > 0 ? 'text-success' : 'text-danger';?>"><?= \S::number_display( $client['total'] );?></td>
                </tr>
              <? endforeach;?>
            </tbody>
          </table>
        <? else:?>
          <small class="text-muted">Brak operacji z przypisanymi klientami w wybranym okresie.</small>
        <? endif;?>
      </div>
    </div>
  </div>
  <div class="column-middle">
    <div class="flex">
      <div class="input-group finance-calendar" style="width: 240px;">
        <input type="text" name="date" class="form-control date-range" value="<?= $this->date_from . ' - ' . $this->date_to; ?>" />
      </div>
      <select class="form-control finance-group" name="group_id">
        <? foreach ($this->groups as $group) : ?>
        <option value="<?= $group['id']; ?>" <? if ($this->group_id == $group['id']) : ?>selected="selected"
          <? endif; ?>><?= $group['name']; ?>
        </option>
        <? endforeach; ?>
      </select>
    </div>
    <div class="categories">
      <table class="table" style="background: #FFF;">
        <thead>
          <tr>
            <th>Kategoria <a href="/finances/category_edit/" class="btn btn-success btn-xs btn-add-category" title="dodaj kategorię"><i class="fa fa-plus"></i></a></th>
            <th class="text-right" style="width: 100px;">Koszty</th>
            <th class="text-right" style="width: 50px;">Ilość</th>
            <th class="text-right" style="width: 100px;">Przychód</th>
            <th class="text-right" style="width: 50px;">Ilość</th>
            <th class="text-right" style="width: 100px">Dochód</th>
          </tr>
        </thead>
        <tbody>
          <? if (is_array($this->categories)) : foreach ($this->categories as $category) : ?>
          <? unset($summary_costs);
              unset($summary_income); ?>
          <tr>
            <th colspan="6" class="category-name">
              <div class="manage-menu">
                <span class="context-menu" data-container-id="category-<?= $category['id'];?>-menu"></span>
                <div class="context-menu-container" id="category-<?= $category['id'];?>-menu">
                  <ul>
                    <li>
                      <a href="/finances/category_edit/parent-id=<?= $category['id']; ?>" title="dodaj podkategorię">
                        Dodaj podkategorię
                      </a>
                    </li>
                    <li>
                      <a href="#" class="category-delete" category_id="<?= $category['id']; ?>" title="usuń kategorię">
                        Usuń kategorię
                      </a>
                    </li>
                  </ul>
                </div>
              </div>
              <?= $category['name']; ?>
            </th>
          </tr>
          <? if (is_array($category['subcategories'])) : foreach ($category['subcategories'] as $subcategory) : ?>
          <tr>
            <td class="pl15 category-name">
              <div class="manage-menu">
                <span class="context-menu" data-container-id="category-<?= $subcategory['id'];?>-menu"></span>
                <div class="context-menu-container" id="category-<?= $subcategory['id'];?>-menu">
                  <ul>
                    <li>
                      <a href="/finances/category_edit/id=<?= $subcategory['id']; ?>" title="edytuj podkategorię">
                        Edytuj podkategorię
                      </a>
                    </li>
                    <li>
                      <a href="/finances/operation_edit/category-id=<?= $subcategory['id']; ?>" title="dodaj operację">
                        Dodaj operację
                      </a>
                    </li>
                    <li>
                      <a href="/finances/operations_list/category-id=<?= $subcategory['id']; ?>" title="pokaż listę operacji">
                        Pokaż listę operacji
                      </a>
                    </li>
                    <li>
                      <a href="#" class="category-delete" category_id="<?= $subcategory['id']; ?>" title="usuń kategorię">
                        Usuń kategorię
                      </a>
                    </li>
                  </ul>
                </div>
              </div>
              <?= $subcategory['name']; ?>
            </td>
            <td class="text-right">
              <?= \S::number_display( $subcategory['costs'] ); ?>
            </td>
            <td class="text-right">
              <?= $subcategory['costs_count'] * (-1); ?>
            </td>
            <td class="text-right">
              <?= \S::number_display( $subcategory['income'] ); ?>
            </td>
            <td class="text-right">
              <?= $subcategory['income_count']; ?>
            </td>
            <td class="text-right">
              <span><?= \S::number_display( $subcategory['income'] + $subcategory['costs'] );?></span>
            </td>
          </tr>
          <?
                  $summary_costs  += $subcategory['costs'];
                  $summary_income += $subcategory['income'];
                  $total_costs    += $subcategory['costs'];
                  $total_income   += $subcategory['income'];
                  ?>
          <? endforeach;
              endif; ?>
          <tr>
            <th class="text-right">
              Razem:
            </th>
            <th class="text-right">
              <?= \S::number_display($summary_costs); ?>
            </th>
            <th class="text-right" colspan="2">
              <?= \S::number_display($summary_income); ?>
            </th>
            <th class="text-right" colspan="2">
              <span class="<? if ( $summary_income + $summary_costs > 0 ):?>text-success<? elseif ( $summary_income + $summary_costs < 0 ):?>text-danger<? endif;?>"><?= \S::number_display( $summary_income + $summary_costs );?></span>
            </th>
          </tr>
          <? endforeach;
          endif; ?>
          <tr>
            <th class="text-right">
              Razem:
            </th>
            <th class="text-right">
              <?= \S::number_display($total_costs); ?>
            </th>
            <th class="text-right" colspan="2">
              <?= \S::number_display($total_income); ?>
            </th>
            <th class="text-right" colspan="2">
              <span class="<? if ( $total_income + $total_costs > 0 ):?>text-success<? elseif ( $total_income + $total_costs < 0 ):?>text-danger<? endif;?>"><?= \S::number_display( $total_income + $total_costs );?></span>
            </th>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
  <div class="column-right">
    <table class="table" style="background: #FFF;">
      <thead>
        <tr>
          <th class="text-right" style="width: 90px;">Data</th>
          <th class="text-right" style="width: 100px;">Kwota</th>
          <th>Szczegóły</th>
          <th></th>
        </tr>
      </thead>
      <tbody>
        <? if ( is_array( $this -> operations_list ) ): foreach ( $this -> operations_list as $operation ):?>
          <tr>
            <td class="text-right nowrap">
              <?= $operation['date'];?>
            </td>
            <td class="text-right <?= $operation['amount'] > 0 ? 'text-success' : 'text-danger';?>">
              <?= \S::number_display( $operation['amount'] );?>
            </td>
            <td>
              <?= $operation['name'];?>
              <? if ( $operation['description'] ):?>
                <br><small><?= $operation['description'];?></small>
              <? endif;?>
              <br><small><?= \factory\Finances::get_operation_tags( $operation['id'] );?></small>
            </td>
            <td class="pl15 actions">
              <a href="/finances/operation_edit/category-id=<?= $operation['category_id'];?>&operation-id=<?= $operation['id'];?>" title="edytuj operację"><i class="fa fa-cog"></i></a>
              <a href="/finances/operation_delete/category-id=<?= $operation['category_id'];?>&operation-id=<?= $operation['id'];?>" class="operation-delete" title="usuń operację"><i class="fa fa-times"></i></a>
            </td>
          </tr>
          <? $total = $total + $operation['amount'];?>
        <? endforeach; endif;?>
        <tr>
          <th class="text-right" colspan='2'>
            <?= \S::number_display( $total );?>
          </th>
          <th colspan="3"></th>
        </tr>
      </tbody>
    </table>
  </div>
</div>
<script type="text/javascript">
  function menuItemClickListener(menu_item, parent) {}

$(function() {

  $('body').on('click', '.category-delete', function() {
    var category_id = $(this).attr('category_id');
    $.alert({
      title: 'Potwierdź',
      content: 'Na pewno chcesz usunąć wybraną kategorię',
      type: 'orange',
      closeIcon: true,
      closeIconClass: 'fa fa-close',
      typeAnimated: true,
      animation: 'opacity',
      columnClass: 'col-sm-8',
      theme: 'modern',
      buttons: {
        cancel: {
          text: 'Zamknij',
          btnClass: 'btn-default',
          action: function() {}
        },
        confirm: {
          text: 'Usuń kategorię',
          btnClass: 'btn-red',
          keys: ['enter'],
          action: function() {
            document.location.href = '/finances/category_delete/category_id=' + category_id;
          }
        }
      }
    });
  });

  $('body').on('change', '.finance-group', function() {
    document.location.href = '/finances/main_view/group-id=' + $(this).val();
  });

  $('body').on('change', '.date-range', function() {
    // replace spaces
    var date = $(this).val().replace(/ /g, '_');
    document.location.href = '/finances/main_view/dates=' + date;
  });

  jQuery('body').on('click', '.date-range-icon', function() {
    $(this).parents('.input-group').children('input').trigger('click');
  });
});
</script>