<?
namespace controls;
class Tasks
{
  static public function task_change_dates() {
    global $mdb;

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

    if ( \factory\Tasks::task_change_dates( \S::get( 'task_id' ), \S::get( 'date_start' ), \S::get( 'date_end' ) ) )
      $response = [ 'status' => 'success', 'msg' => 'Data została zmieniona.' ];

    echo json_encode( $response );
    exit;
  }

  static public function task_delete() {
    global $user;

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

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

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

    echo json_encode( $response );
    exit;
  }

  static public function main_view_by_ajax()
  {
    global $user;

    $show_tasks_closed = \S::get_session( 'tasks_closed' );
    $show_tasks_bulk = \S::get_session( 'tasks_bulk' );
    $show_tasks_to_review = \S::get_session( 'tasks_to_review' );

    $open_task_id = \factory\Tasks::get_open_task_id( $user['id'] );
    $projects = \S::get( 'projects' );
    $users = \S::get( 'users' );

    if ( $projects )
      \S::set_session( 'selected_projects', $projects );
    else
      \S::del_session( 'selected_projects' );

    if ( $users )
      \S::set_session( 'selected_users', $users );
    else
      \S::del_session( 'selected_users' );

    echo json_encode( [
      'tasks_new' => \Tpl::view( 'tasks/main_view_by_ajax', [
        'tasks' => \factory\Tasks::get_tasks( 0, $user['id'], $projects, $users ),
        'user' => $user,
        'statuses' => \factory\Tasks::get_statuses(),
        'open_task_id' => $open_task_id
      ] ),
      'tasks_to_review' => \Tpl::view( 'tasks/main_view_by_ajax', [
        'tasks' => $show_tasks_to_review == 'hide' ? null : \factory\Tasks::get_tasks( 1, $user['id'], $projects, $users ),
        'user' => $user,
        'statuses' => \factory\Tasks::get_statuses(),
        'open_task_id' => $open_task_id
      ] ),
      'tasks_closed' => \Tpl::view( 'tasks/main_view_by_ajax', [
        'tasks' => $show_tasks_closed == 'hide' ? null : \factory\Tasks::get_tasks( 2, $user['id'], $projects, $users ),
        'user' => $user,
        'statuses' => \factory\Tasks::get_statuses(),
        'open_task_id' => $open_task_id
      ] ),
      'tasks_bulk' => \Tpl::view( 'tasks/main_view_by_ajax', [
        'tasks' => $show_tasks_bulk == 'hide' ? null : \factory\Tasks::get_tasks( 3, $user['id'], $projects, $users ),
        'user' => $user,
        'statuses' => \factory\Tasks::get_statuses(),
        'open_task_id' => $open_task_id
      ] ),
      'tasks_suspended' => \Tpl::view( 'tasks/main_view_by_ajax', [
        'tasks' => \factory\Tasks::get_tasks( 4, $user['id'], $projects, $users ),
        'user' => $user,
        'statuses' => \factory\Tasks::get_statuses(),
        'open_task_id' => $open_task_id
      ] ),
      'tasks_to_do' => \Tpl::view( 'tasks/main_view_by_ajax', [
        'tasks' => \factory\Tasks::get_tasks( 5, $user['id'], $projects, $users ),
        'user' => $user,
        'statuses' => \factory\Tasks::get_statuses(),
        'open_task_id' => $open_task_id
      ] ),
      'tasks_fvat' => \Tpl::view( 'tasks/main_view_by_ajax', [
        'tasks' => \factory\Tasks::get_tasks( 6, $user['id'], $projects, $users ),
        'user' => $user,
        'statuses' => \factory\Tasks::get_statuses(),
        'open_task_id' => $open_task_id
      ] ),
      'tasks_gantt' => \factory\Tasks::get_tasks_gantt( $user['id'], $projects, $users ),
    ] );
    exit;
  }

  static public function main_view()
  {
    global $user;

    if ( \S::get( 'tasks_closed' ) == 'hide' )
    {
      \S::set_session( 'tasks_closed', 'hide' );
      header( 'Location: /tasks/main_view/' );
      exit;
    }

    if ( \S::get( 'tasks_closed' ) == 'show' )
    {
      \S::del_session( 'tasks_closed' );
      header( 'Location: /tasks/main_view/' );
      exit;
    }

    if ( \S::get( 'tasks_bulk' ) == 'hide' )
    {
      \S::set_session( 'tasks_bulk', 'hide' );
      header( 'Location: /tasks/main_view/' );
      exit;
    }

    if ( \S::get( 'tasks_bulk' ) == 'show' )
    {
      \S::del_session( 'tasks_bulk' );
      header( 'Location: /tasks/main_view/' );
      exit;
    }

    // tasks_to_review
    if ( \S::get( 'tasks_to_review' ) == 'hide' )
    {
      \S::set_session( 'tasks_to_review', 'hide' );
      header( 'Location: /tasks/main_view/' );
      exit;
    }

    if ( \S::get( 'tasks_to_review' ) == 'show' )
    {
      \S::del_session( 'tasks_to_review' );
      header( 'Location: /tasks/main_view/' );
      exit;
    }

    $show_tasks_closed = \S::get_session( 'tasks_closed' );
    $show_tasks_bulk = \S::get_session( 'tasks_bulk' );
    $show_tasks_to_review = \S::get_session( 'tasks_to_review' );

    return \Tpl::view( 'tasks/main_view', [
      'tasks_new' => \factory\Tasks::get_tasks( 0, $user['id'], \S::get_session( 'selected_projects' ), \S::get_session( 'selected_users' ) ),
      'tasks_to_review' => $show_tasks_to_review == 'hide' ? null : \factory\Tasks::get_tasks( 1, $user['id'], \S::get_session( 'selected_projects' ), \S::get_session( 'selected_users' ) ),
      'tasks_closed' => $show_tasks_closed == 'hide' ? null : \factory\Tasks::get_tasks( 2, $user['id'], \S::get_session( 'selected_projects' ), \S::get_session( 'selected_users' ) ), // zakończone
      'tasks_bulk' => $show_tasks_bulk == 'hide' ? null : \factory\Tasks::get_tasks( 3, $user['id'], \S::get_session( 'selected_projects' ), \S::get_session( 'selected_users' )  ), // opłaty
      'tasks_suspended' => \factory\Tasks::get_tasks( 4, $user['id'], \S::get_session( 'selected_projects' ), \S::get_session( 'selected_users' ) ), // projektowe
      'tasks_to_do' => \factory\Tasks::get_tasks( 5, $user['id'], \S::get_session( 'selected_projects' ), \S::get_session( 'selected_users' ) ), // do zrobienia
      'tasks_fvat' => \factory\Tasks::get_tasks( 6, $user['id'], \S::get_session( 'selected_projects' ), \S::get_session( 'selected_users' ) ), // faktury
      'user' => $user,
      'statuses' => \factory\Tasks::get_statuses(),
      'open_task_id' => \factory\Tasks::get_open_task_id( $user['id'] ),
      'projects' => \factory\Projects::user_projects( $user['id'] ),
      'selected_projects' => \S::get_session( 'selected_projects' ),
      'selected_users' => \S::get_session( 'selected_users' ),
      'tasks_filtrs' => \factory\Tasks::get_filtrs( $user['id'] ),
      'show_tasks_closed' => $show_tasks_closed,
      'show_tasks_bulk' => $show_tasks_bulk,
      'show_tasks_to_review' => $show_tasks_to_review,
      'users' => \factory\Users::users_list(),
      'tasks_gantt' => \factory\Tasks::get_tasks_gantt( $user['id'], \S::get_session( 'selected_projects' ), \S::get_session( 'selected_users' ) ),
      'selected_gantt_users' => \S::get_session( 'selected_gantt_users' )
    ] );
  }

  static public function action_change_status() {
    global $user;

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

    $response = [ 'status' => 'error', 'msg' => 'Podczas zmiany statusu czynności wystąpił błąd. Proszę spróbować ponownie.' ];

    if ( \factory\Tasks::action_change_status( \S::get( 'action_id' ), \S::get( 'status' ) ) )
      $response = [ 'status' => 'ok', 'msg' => 'Status czynności został zmieniony.' ];

    echo json_encode( $response );
    exit;
  }

  static public function comment_delete() {
    global $user;

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

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

    if ( \factory\Tasks::comment_delete( \S::get( 'comment_id' ) ) )
      $response = [ 'status' => 'ok', 'msg' => 'Komentarz został usunięty.' ];

    echo json_encode( $response );
    exit;
  }

  static public function comment_save()
  {
    global $user;

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

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

    if ( $id = \factory\Tasks::comment_save( \S::get( 'task_id' ), $user['id'], \S::get( 'text' ) ) )
    {
      \factory\Tasks::clear_task_opened( \S::get( 'task_id' ) );
      $response = [ 'status' => 'ok', 'msg' => 'Komentarz został zapisany.', 'id' => $id ];
    }

    echo json_encode( $response );
    exit;
  }

  static public function action_delete()
  {
    global $user;

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

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

    if ( \factory\Tasks::action_delete( \S::get( 'action_id' ) ) )
      $response = [ 'status' => 'ok', 'msg' => 'Czynność została usunięta.' ];

    echo json_encode( $response );
    exit;
  }

  static public function action_save()
  {
    global $user;

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

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

    if ( $id = \factory\Tasks::action_save( \S::get( 'task_id' ), \S::get( 'text' ) ) )
    {
      \factory\Tasks::clear_task_opened( \S::get( 'task_id' ) );
      $response = [ 'status' => 'ok', 'msg' => 'Czynność została zapisana.', 'id' => $id ];
    }

    echo json_encode( $response );
    exit;
  }

  // Zapisanie kolejności zadań
  static public function tasks_order_save()
  {
    global $mdb;

    foreach ( \S::get( 'tasks_order' ) as $key => $val ) {
      $mdb -> update( 'tasks', [ 'o' => $key ], [ 'id' => $val ] );
    }
    exit;
  }

  static public function send_email_task_change_status( $task_id )
  {
    $task = \factory\Tasks::task_details( $task_id );

    $statuses = \factory\Tasks::get_statuses();

    if ( $task['status_change_mail'] ) {
      \S::send_email(
        'biuro@project-pro.pl',
        'crmPRO - zmieniono status zadania',
        '<p>Witaj<br/>zmieniono status zadania <b>' . $task['name'] . ' - ' . \factory\Crm::get_client_name( (int)$task['client_id'] ) . '</b> na <b>' . $statuses[ $task['status'] ] . '</b>.</p>'
      );
    }
  }

  static public function task_change_status()
  {
    global $mdb;

    if ( $mdb -> update( 'tasks', [ 'status' => \S::get( 'status' ) ], [ 'id' => \S::get( 'task_id' ) ] ) )
    {
      if ( \S::get( 'status' ) == 3 or \S::get( 'status' ) == 1 )
      {
        self::send_email_task_change_status( \S::get( 'task_id' ) );
      }

      if ( \S::get( 'status' ) == 2 )
      {
        $mdb -> update( 'tasks', [ 'date_complete' => date( 'Y-m-d H:i:s' ) ], [ 'id' => \S::get( 'task_id' ) ] );
      }

      echo json_encode( [ 'status' => 'success' ] );
    }
    else
    {
      echo json_encode( [ 'status' => 'error' ] );
    }
    exit;
  }

  static public function task_end()
  {
    global $user;

    if ( !$user )
      return json_encode( [ 'status' => 'error' ] );

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

  static public function task_start()
  {
    global $user;

    if ( !$user )
      return json_encode( [ 'status' => 'error' ] );

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

  static public function task_edit()
  {
    global $user;

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

    return \Tpl::view( 'tasks/task_edit', [
      'projects' => \factory\Projects::user_projects( $user['id'] ),
      'priorities' => \factory\Tasks::$priorities,
      'task' => \factory\Tasks::task_details( \S::get( 'task_id' ) ),
      'parent_tasks' => \factory\Tasks::parent_tasks( $user['id'] ),
      'users' => \factory\Users::users_list(),
      'clients' => \factory\Crm::get_client_list(),
      'user' => $user
    ] );
  }

  static public function task_save()
  {
    global $user;

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

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

    if ( $id = \factory\Tasks::task_save(
            $values['id'], null, $user['id'], $values['name'], $values['text'], $values['date_start'], $values['date_end'], $values['project_id'], $values['client_id'], $values['pay_rate'], $values['reminders_interval'], $values['recursively'], $values['frequency'], $values['period'], $values['users'], null, null, $values['send_email_notification'], $values['status_change_mail'], false, $values['status'], $values['show_in_calendar'], $values['priority']
          ) )
    {
      \factory\Tasks::clear_task_opened( $id );
      $response = [ 'status' => 'ok', 'msg' => 'Zadanie zostało zapisane.', 'id' => $id ];
    }

    echo json_encode( $response );
    exit;
  }

  static public function task_popup()
  {
    global $user;

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

    \factory\Tasks::set_task_opened_by_user( \S::get( 'task_id' ), $user['id'] );

    echo \Tpl::view( 'tasks/task_popup', [
      'task' => \factory\Tasks::task_details( \S::get( 'task_id' ), $user['id'] ),
      'task_works' => \factory\Tasks::task_works( \S::get( 'task_id' ) ),
      'user' => $user,
      'statuses' => \factory\Tasks::get_statuses()
    ] );
    exit;
  }

  static public function filtr_save_form() {
    echo json_encode( [
      'status' => 'success',
      'title' => 'Zapisz filtr',
      'popup_content' => \Tpl::view( 'tasks/filtr_save_form', [
        'projects' => \S::get( 'projects' ),
        'users' => \S::get( 'users' )
      ] )
    ] );
    exit;
  }

  static public function filtr_save() {
    global $user;

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

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

    if ( \factory\Tasks::filtr_save( $user['id'], \S::get( 'name' ), \S::get( 'projects' ), \S::get( 'users' ), \S::get( 'is_default' ) ) )
      $response = [ 'status' => 'success', 'msg' => 'Filtr został zapisany.' ];

    echo json_encode( $response );
    exit;
  }

  static public function filtr_update() {
    global $user;

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

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

    if ( \factory\Tasks::filtr_update( \S::get( 'filtr_id' ), \S::get( 'projects' ), \S::get( 'users' ) ) )
      $response = [ 'status' => 'success', 'msg' => 'Filtr został zaktualizowany.' ];

    echo json_encode( $response );
    exit;
  }

  static public function work_time()
  {
    return \Tpl::view( 'tasks/work-time', [
      'work_time_clients' => \factory\Tasks::work_time_clients(),
      'settings' => \factory\Crm::settings()
    ] );
  }

  static public function change_task_work_date_start() {
    $result = \factory\Tasks::change_task_work_date_start( \S::get( 'task_work_id' ), \S::get( 'date_start' ) );
    exit;
  }

  static public function change_task_work_date_end() {
    $result = \factory\Tasks::change_task_work_date_end( \S::get( 'task_work_id' ), \S::get( 'date_end' ) );
    exit;
  }

  static public function work_delete() {

    if ( \factory\Tasks::work_delete( \S::get( 'work_id' ) ) )
      echo json_encode( [ 'status' => 'success' ] );
    else
      echo json_encode( [ 'status' => 'error' ] );
    exit;
  }
}