<?
foreach ( $this -> work_time_clients as $client ):
  if ( !$client['tasks'] )
    continue;

  $pay_rate = null;
  $pay_rate_summary = 0;
  ?>
    <div class="card mb25">
      <div class="card-header" id="<?= $client['firm'];?>"><?= $client['firm'];?></div>
      <div class="card-body">
        <? if ( $client['tasks'][date( 'Y-m' )] ):?>
          <? $time = 0;?>
          <table class="table table-sm">
            <tr>
              <th colspan="4"><?= date( 'Y-m' );?></th>
            </tr>
            <? foreach ( $client['tasks'][date( 'Y-m' )] as $task ):?>
              <? $time += $task['time'];?>
              <tr>
                <td><?= $task['name'];?></td>
                <td style="width: 100px; text-align: center;"><?= sprintf( "%02d%s%02d%s%02d", floor( $task['time'] / 3600 ), ':', ( $task['time'] / 60) % 60, ':', $task['time'] % 60 );?></td>
                <td class="text-right" style="width: 100px;">
                  <?
                  if ( $task['pay_rate'] )
                  {
                    echo $task['pay_rate'] . ' zł';
                    $pay_rate_summary += $task['pay_rate'];
                  }
                  else
                  {
                    // calculater pay rate for task ( hourly rate * time spend on task )
                    $pay_rate = number_format( $this -> settings['hourly_rate'] * ( $task['time'] / 3600 ), 2, '.', '' );
                    $pay_rate_summary += $pay_rate;
                    echo $pay_rate . ' zł';
                  }
                  ?>
                </td>
                <td class="text-center" style="width: 200px;">
                  <a href="#" class="close-task" task-id="<?= $task['id'];?>" client="<?= $client['firm'];?>">zamknij zadanie</a>
                </td>
              </tr>
            <? endforeach;?>
            <tr>
              <td></td>
              <td class="bold text-center"><?= sprintf( "%02d%s%02d%s%02d", floor( $time / 3600 ), ':', ( $time / 60) % 60, ':', $time % 60 );?></td>
              <td class="bold text-right"><?= number_format( $pay_rate_summary, 2, '.', '' );?> zł</td>
            </tr>
          </table>
        <? endif;?>
        <? if ( $client['tasks'][date( 'Y-m', strtotime( '-1 months', time() ) )] ):?>
          <? $time = 0;?>
          <table class="table table-sm">
            <tr>
              <th colspan="4"><?= date( 'Y-m', strtotime( '-1 months', time() ) );?></th>
            </tr>
            <? foreach ( $client['tasks'][date( 'Y-m', strtotime( '-1 months', time() ) )] as $task ):?>
              <? $time += $task['time'];?>
              <tr>
                <td><?= $task['name'];?></td>
                <td style="width: 100px; text-align: center;"><?= sprintf( "%02d%s%02d%s%02d", floor( $task['time'] / 3600 ), ':', ( $task['time'] / 60) % 60, ':', $task['time'] % 60 );?></td>
                <td class="text-right" style="width: 100px;">
                  <?
                  if ( $task['pay_rate'] )
                  {
                    echo $task['pay_rate'] . ' zł';
                    $pay_rate_summary += $task['pay_rate'];
                  }
                  else
                  {
                    // calculater pay rate for task ( hourly rate * time spend on task )
                    $pay_rate = number_format( $this -> settings['hourly_rate'] * ( $task['time'] / 3600 ), 2, '.', '' );
                    $pay_rate_summary += $pay_rate;
                    echo $pay_rate . ' zł';
                  }
                  ?>
                </td>
                <td class="text-center" style="width: 200px;">
                  <a href="#" class="close-task" task-id="<?= $task['id'];?>" client="<?= $client['firm'];?>">zamknij zadanie</a>
                </td>
              </tr>
            <? endforeach;?>
            <tr>
              <td></td>
              <td class="bold text-center"><?= sprintf( "%02d%s%02d%s%02d", floor( $time / 3600 ), ':', ( $time / 60) % 60, ':', $time % 60 );?></td>
              <td class="bold text-right"><?= number_format( $pay_rate_summary, 2, '.', '' );?> zł</td>
            </tr>
          </table>
        <? endif;?>
        <? if ( $client['tasks'][date( 'Y-m', strtotime( '-2 months', time() ) )] ):?>
          <? $time = 0;?>
          <table class="table table-sm">
            <tr>
              <th colspan="4"><?= date( 'Y-m', strtotime( '-2 months', time() ) );?></th>
            </tr>
            <? foreach ( $client['tasks'][date( 'Y-m', strtotime( '-2 months', time() ) )] as $task ):?>
              <? $time += $task['time'];?>
              <tr>
                <td><?= $task['name'];?></td>
                <td style="width: 100px; text-align: center;"><?= sprintf( "%02d%s%02d%s%02d", floor( $task['time'] / 3600 ), ':', ( $task['time'] / 60) % 60, ':', $task['time'] % 60 );?></td>
                <td class="text-right" style="width: 100px;">
                  <?
                  if ( $task['pay_rate'] )
                  {
                    echo $task['pay_rate'] . ' zł';
                    $pay_rate_summary += $task['pay_rate'];
                  }
                  else
                  {
                    // calculater pay rate for task ( hourly rate * time spend on task )
                    $pay_rate = number_format( $this -> settings['hourly_rate'] * ( $task['time'] / 3600 ), 2, '.', '' );
                    $pay_rate_summary += $pay_rate;
                    echo $pay_rate . ' zł';
                  }
                  ?>
                </td>
                <td class="text-center" style="width: 200px;">
                  <a href="#" class="close-task" task-id="<?= $task['id'];?>" client="<?= $client['firm'];?>">zamknij zadanie</a>
                </td>
              </tr>
            <? endforeach;?>
            <tr>
              <td></td>
              <td class="bold text-center"><?= sprintf( "%02d%s%02d%s%02d", floor( $time / 3600 ), ':', ( $time / 60) % 60, ':', $time % 60 );?></td>
              <td class="bold text-right"><?= number_format( $pay_rate_summary, 2, '.', '' );?> zł</td>
            </tr>
          </table>
        <? endif;?>
      </div>
    </div>
  <?
endforeach;
?>
<script type="text/javascript">
  $( function()
  {
    $( 'body' ).on( 'click', '.close-task', function(e){
      e.preventDefault();
      var task_id = $( this ).attr( 'task-id' );
      var client = $( this ).attr( 'client' );

      $.ajax({
        type: 'POST',
        cache: false,
        url: '/tasks/task_change_status/',
        data: {
          task_id: task_id,
          status: 2
        },
        beforeSend: function() {},
        success: function( response ) {
          document.location.href = '/tasks/work_time/#' + client;
          location.reload();
        }
      });
    });
  });
</script>