<?php
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
namespace controls;

/**
 * Description of class
 *
 * @author Project-Pro - Jacek
 */
class Crm
{

  static public function client_delete()
  {
    global $user;

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

    if ( \factory\Crm::client_delete( \S::get( 'client_id' ) ) )
    {
      \S::alert( 'Klient został usunięty' );
      header( 'Location: /crm/main_view/' );
      exit;
    }
  }

  public static function client_save()
  {
    global $user;

    if ( !$user or!\controls\Users::permissions( $user[ 'id' ], 'wiki' ) )
      return false;

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

    if ( $id = \factory\Crm::client_save(
        (int)$values[ 'id' ], $values[ 'firm' ], $values[ 'emails' ], $values[ 'phones' ], $values[ 'notes' ]
      ) )
      $response = [ 'status' => 'ok', 'msg' => 'Projekt został zapisany.', 'id' => $id ];

    echo json_encode( $response );
    exit;
  }

  public static function client_edit()
  {
    global $user;

    if ( !$user or !\controls\Users::permissions( $user[ 'id' ], 'wiki' ) )
      return false;

    return \Tpl::view( 'crm/client-edit', [
          'client' => \factory\Crm::client_details( (int)\S::get( 'id' ) )
      ] );
  }

  public static function main_view()
  {
    global $user;

    if ( !$user or !\controls\Users::permissions( $user[ 'id' ], 'wiki' ) )
      return false;

    return \Tpl::view( 'crm/main-view' );
  }

}