<? global $user;?>
<? if ( $this -> unassigned_tasks and count( $this -> unassigned_tasks ) ):?>
  <div class="task-group">
    <div class="title">
      Zadania nieprzypisane
    </div>
    <div class="dragbleList unassigned-tasks">
      <ul class="tasks-list sortable-list">
        <? if ( is_array( $this -> unassigned_tasks ) ):?>
          <? foreach ( $this -> unassigned_tasks as $task ):?>
            <? $task_open = \factory\Projects::is_task_open( $task['id'], $user['id'] );?>
            <li class="sortable-item task <? if ( $task['status'] ):?>done<? endif;?> <? if ( $task_open ):?>open<? endif;?> <? if ( $task['date_end'] and $task['date_end'] <= date( 'Y-m-d' ) and !$task['status'] ):?>delayed<? endif;?>" task-id="<?= $task['id'];?>" id="task-<?= $task['id'];?>">
              <div class="manage-menu">
                <span class="context-menu" data-container-id="task-<?= $task['id'];?>-menu"></span>
                <div class="context-menu-container" id="task-<?= $task['id'];?>-menu">
                  <ul>
                    <li>
                      <a href="/projects/task_edit/id=<?= $task['id'];?>" class="task-edit">
                        Edytuj
                      </a>
                    </li>
                    <li>
                      <a href="#" class="task-remove" task-id="<?= $task['id'];?>">
                        Usuń zadanie
                      </a>
                    </li>
                    <li>
                      <a href="#" class="task-remove-all" task-id="<?= $task['id'];?>">
                        Usuń zadania i zadania powiązane
                      </a>
                    </li>
                  </ul>
                </div>
              </div>
              <div class="dates <? if ( $task['date_end'] ):?>set<? endif;?>">
                <? if ( $task['date_end'] ):?>
                  <?= $task['date_end'];?>
                <? else:?>
                  -
                <? endif;?>
              </div>
              <div class="flex flex-ac">
                <? if ( !$task_open ):?>
                  <a href="#" class="task-start" task-id="<?= $task['id'];?>">
                    <i class="fa fa-play"></i>
                  </a>
                <? else:?>
                  <a href="#" class="task-end" task-id="<?= $task['id'];?>">
                    <i class="fa fa-stop"></i>
                  </a>
                <? endif;?>
                <div class="time">
                  <?= sprintf( "%02d%s%02d%s%02d", floor( $task['total_time'] / 3600 ), ':', ( $task['total_time'] / 60) % 60, ':', $task['total_time'] % 60 );;?>
                </div>
                <div class="name">
                  <div class="top">
                    <? if ( $open_subtasks = \factory\Projects::count_open_subtasks( $task['id'] ) ):?>
                      <a href="#" class="open_subtasks_count" onclick="return false;" task-id="<?= $task['id'];?>">
                        <?= $open_subtasks;?>
                      </a>
                    <? endif;?>
                    <? if ( $task['text'] ):?>
                      <a href="#" class="task-details" task-id="<?= $task['id'];?>">
                        <i class="fa fa-info"></i>
                      </a>
                    <? endif;?>
                    <? if ( $task['client_id'] ):?>
                      <span class="client_info" task-id="<?= $task['id'];?>"><?= \factory\Crm::get_client_name( (int)$task['client_id'] );?></span>
                    <? endif;?>
                    <? if ( $task['pay_rate'] and $this -> user['id'] == 1 ):?>
                      <span class="pay-rate">stawka: <strong><?= $task['pay_rate'];?> zł</strong></span>
                    <? endif;?>
                  </div>
                  <div class="bottom">
                    <span class="task-details" task-id="<?= $task['id'];?>">
                      <?= $task['name'];?><? if ( count( $task['actions'] ) ):?> <? $count = 0; foreach ( $task['actions'] as $action ) if ( $action['status'] == null ) $count++; if ( $count ) echo '(' . $count . ')';?><? endif;?>
                    </span>
                  </div>
                </div>
              </div>
              <div class="options">
                <div class="users">
                  <? if ( is_array( $task['users'] ) ): foreach ( $task['users'] as $user_tmp_id ):?>
                    <?
                      $user_tmp = \factory\Users::user_details( $user_tmp_id );
                      echo '<div class="user" style="background: ' . $user_tmp['color'] . '">' . $user_tmp['name'][0] . $user_tmp['surname'][0] . '</div>';
                    ?>
                  <? endforeach; endif;?>
                </div>
                <div class="client" task-id="<?= $task['id'];?>">
                  <?= $task['client'];?>
                </div>
              </div>
              <script type="text/javascript">
                $( function()
                {
                  $(document).ready(function(){
                    var tableContextMenu = new ContextMenu("task-<?= $task['id'];?>-menu", menuItemClickListener);
                  });
                });
              </script>
            </li>
          <? endforeach;?>
        <? endif;?>
      </ul>
    </div>
  </div>
<? endif;?>
<? if ( $user['id'] == 1 ):?>
  <script type="text/javascript">
    $( '.unassigned-tasks .sortable-list' ).sortable({
      stop : function(event, ui) {
        var tasks_order = $( this ).sortable('toArray', { attribute: 'task-id' } );

        $.ajax({
          type: 'POST',
          cache: false,
          url: '/projects/tasks_order_save/',
          data: {
            tasks_order: tasks_order
          },
          beforeSend: function() {},
          success: function( response ) {

          }
        });
      }
    });
  </script>
<? endif;?>