<?php
namespace controls;
class Projects
{
  static public function project_save()
  {
    global $user;

    if ( !$user )
    {
      header( 'Location: /logowanie' );
      exit;
    }

    $response = [ 'status' => 'error', 'msg' => 'Podczas zapisywania projektu wystąpił błąd. Proszę spróbować ponownie.' ];
    $values   = \S::json_to_array( \S::get( 'values' ) );

    if ( $id = \factory\Projects::project_save( $values['id'], $values['name'], $values['users'], $values['client_id'] ) )
      $response = [ 'status' => 'ok', 'msg' => 'Projekt został zapisany.', 'id' => $id ];

    echo json_encode( $response );
    exit;
  }

  static public function project_edit()
  {
    global $user;

    if ( !$user )
    {
      header( 'Location: /logowanie' );
      exit;
    }

    return \Tpl::view( 'projects/project_edit', [
      'project' => \factory\Projects::project_details( \S::get( 'project_id' ) ),
      'users' => \factory\Users::users_list(),
      'clients' => \factory\Crm::get_client_list()
    ] );
  }

  static public function main_view()
  {
    return \Tpl::view( 'projects/main_view', [
      'projects' => \factory\Projects::projects_list(),
      'users' => \factory\Users::users_list()
    ] );
  }

  // zapisz kolejność zadań
  static public function task_order_save() {
    global $user;

    if ( !$user )
      return \controls\Users::login_form();

    \factory\Projects::task_order_save( \S::get( 'order' ) );

    echo json_encode( [ 'status' => 'ok' ] );
    exit;
  }

  public static function action_mark_as_done()
  {
    global $user;

    if ( !$user )
      return \controls\Users::login_form();

    if ( $action_name = \factory\Projects::action_mark_as_done( \S::get( 'action_id' ) ) )
      $response = [ 'status' => 'ok', 'msg' => 'Czynność została oznaczona jako wykonana.', 'name' => $action_name ];

    echo json_encode( $response );
    exit;
  }

  public static function action_edit()
  {
    global $user;

    if ( !$user )
      return \controls\Users::login_form();

    $action_name = \factory\Projects::action_name( \S::get( 'action_id' ) );
    $response = [ 'status' => 'ok', 'name' => $action_name ];

    echo json_encode( $response );
    exit;
  }

  public static function task_update()
  {
    global $user;
    \factory\Projects::task_update( $user['id'], \S::get( 'task_id' ), \S::get( 'text' ) );
    exit;
  }

  static public function task_text_update()
  {
    \factory\Projects::task_text_update( \S::get( 'history_id' ), \S::get( 'text' ) );
    exit;
  }

  static public function task_text_new()
  {
    \factory\Projects::task_text_new( \S::get( 'task_id' ), \S::get( 'text' ) );
    exit;
  }

  public static function project_delete()
  {
    global $user;

    if ( !$user )
      return \controls\Users::login_form();

    if ( \factory\Projects::project_delete( \S::get( 'project_id' ) ) )
      \S::alert( 'Wybrany projekt został usunięty.' );
    else
      \S::alert( 'Podczas usuwania projektu wystąpił błąd. Prosimy spróbować ponownie.' );

    header( 'Location: ' . $_SERVER['HTTP_REFERER'] );
    exit;
  }

  public static function task_change_status()
  {
    global $user;

    if ( !$user )
      return \controls\Users::login_form();

    echo json_encode( \factory\Projects::task_change_status( \S::get( 'task_id' ), \S::get( 'status' ), $user['id'] ) );
    exit;
  }

  static public function ajax_user_tasks()
  {
    global $user;

    echo json_encode( [
      'unassigned_tasks' => \Tpl::view( 'projects/unassigned-tasks', [
        'unassigned_tasks' => \factory\Projects::get_unassigned_tasks( $user['id'], \S::get( 'project_id' ) ),
        'user' => $user
      ] ),
      'inprogress_tasks' => \Tpl::view( 'projects/inprogress-tasks', [
        'inprogress_tasks' => \factory\Projects::get_inprogress_tasks( $user['id'], \S::get( 'project_id' ) ),
        'user' => $user
      ] ),
      'toreview_tasks' => \Tpl::view( 'projects/toreview-tasks', [
        'toreview_tasks' => \factory\Projects::get_toreview_tasks( $user['id'], \S::get( 'project_id' ) ),
        'user' => $user
      ] ),
      'closed_tasks' => \Tpl::view( 'projects/closed-tasks', [
        'closed_tasks' => \factory\Projects::get_closed_tasks( $user['id'], \S::get( 'project_id' ) ),
        'user' => $user
      ] ),
      'inprogress_tasks_roman' => \Tpl::view( 'projects/inprogress-tasks', [
        'inprogress_tasks' => \factory\Projects::get_inprogress_tasks( 5, \S::get( 'project_id' ) ),
        'user_tmp' => 'Roman Pyrih',
        'user' => $user
      ] ),
      'inprogress_tasks_grzegorz' => \Tpl::view( 'projects/inprogress-tasks', [
        'inprogress_tasks' => \factory\Projects::get_inprogress_tasks( 3, \S::get( 'project_id' ) ),
        'user_tmp' => 'Grzegorz Pyziak',
        'user' => $user
      ] ),
      'inprogress_tasks_fees' => \Tpl::view( 'projects/inprogress-tasks', [
        'inprogress_tasks' => \factory\Projects::get_inprogress_tasks( 10, \S::get( 'project_id' ) ),
        'user_tmp' => 'Opłaty',
        'user' => $user
      ] )
    ] );
    exit;
  }

  static public function task_delete()
  {
    global $user;

    if ( !$user )
      return \controls\Users::login_form();

    $response = [ 'status' => 'error', 'msg' => 'Podczas zapisywania zadania wystąpił błąd. Proszę spróbować ponownie' ];

    if ( \factory\Projects::task_delete( \S::get( 'task_id' ) ) )
    $response = [ 'status' => 'ok', 'msg' => 'Zadanie zostało usunięte', 'taks_id' => \S::get( 'task_id' ) ];

    echo json_encode( $response );
    exit;
  }

  static public function open_task_details()
  {
    echo json_encode( [
      'task' => \factory\Projects::task_details( \S::get( 'task_id' ) )
    ] );
    exit;
  }

  public static function task_details()
  {
    global $user, $settings;

    $task_details = \factory\Tasks::task_details( \S::get( 'task_id' ) );

    if ( $user['id'] == 1 )
    {
      if ( $task_details['pay_rate'] )
        $task_pay_rate = 'Stała stawka: <strong>' . $task_details['pay_rate'] . ' zł</strong>';
      else
        $task_pay_rate = 'Roboczogodziny: <strong>' . number_format( ( $task_details['total_time'] / 3600 ) * $settings['hourly_rate'], 2, '.', ' ' ) . ' zł</strong>';
    }

    echo json_encode( [
      'task_html' => \Tpl::view( 'projects/task-details', [
        'task' => $task_details
      ] ),
      'task' => $task_details,
      'task_total_time' => sprintf( "%02d%s%02d%s%02d", floor( $task_details['total_time'] / 3600 ), ':', ( $task_details['total_time'] / 60) % 60, ':', $task_details['total_time'] % 60 ),
      'task_pay_rate' => $task_pay_rate
    ] );
    exit;
  }

  public static function project_default()
  {
    global $user;

    \factory\Projects::set_project_as_default( $user['id'], \S::get( 'project_id' ) );
    header( 'Location: /projects/tasks/project_id=' . \S::get( 'project_id' ) );
    exit;
  }

  public static function tasks()
  {
    global $user;

    if ( !$user )
      return \controls\Users::login_form();

    return \Tpl::view( 'projects/tasks', [
      'projects' => \factory\Projects::user_projects( $user['id'] ),
      'open_task' => \factory\Projects::open_task( $user['id'] ),
      'current_project' => \S::get( 'project_id' ),
      'user' => $user,
      'default_project' => \factory\Users::get_default_project( $user['id'] ),
      'unassigned_tasks' => \Tpl::view( 'projects/unassigned-tasks', [
        'unassigned_tasks' => \factory\Projects::get_unassigned_tasks( $user['id'], \S::get( 'project_id' ) ),
        'user' => $user
      ] ),
      'inprogress_tasks' => \Tpl::view( 'projects/inprogress-tasks', [
        'inprogress_tasks' => \factory\Projects::get_inprogress_tasks( $user['id'], \S::get( 'project_id' ) ),
        'user' => $user
      ] ),
      'toreview_tasks' => \Tpl::view( 'projects/toreview-tasks', [
        'toreview_tasks' => \factory\Projects::get_toreview_tasks( $user['id'], \S::get( 'project_id' ) ),
        'user' => $user
      ] ),
      'closed_tasks' => \Tpl::view( 'projects/closed-tasks', [
        'closed_tasks' => \factory\Projects::get_closed_tasks( $user['id'], \S::get( 'project_id' ) ),
        'user' => $user
      ] ),
      'inprogress_tasks_roman' => \Tpl::view( 'projects/inprogress-tasks', [
        'inprogress_tasks' => \factory\Projects::get_inprogress_tasks( 5, \S::get( 'project_id' ) ),
        'user_tmp' => 'Roman Pyrih',
        'user' => $user
      ] ),
      'inprogress_tasks_grzegorz' => \Tpl::view( 'projects/inprogress-tasks', [
        'inprogress_tasks' => \factory\Projects::get_inprogress_tasks( 3, \S::get( 'project_id' ) ),
        'user_tmp' => 'Grzegorz Pyziak',
        'user' => $user
      ] ),
      'inprogress_tasks_suspended' => \Tpl::view( 'projects/inprogress-tasks', [
        'inprogress_tasks' => \factory\Projects::get_inprogress_tasks( 8, \S::get( 'project_id' ) ),
        'user_tmp' => 'Projekty',
        'user' => $user
      ] ),
      'inprogress_tasks_fees' => \Tpl::view( 'projects/inprogress-tasks', [
        'inprogress_tasks' => \factory\Projects::get_inprogress_tasks( 10, \S::get( 'project_id' ) ),
        'user_tmp' => 'Opłaty',
        'user' => $user
      ] )
    ] );
  }
}
