<?php
namespace admin\controls;
class ShopOrder
{
  static public function order_resend_confirmation_email()
  {
    $order = new \shop\Order( (int)\S::get( 'order_id' ) );
    $response = $order -> order_resend_confirmation_email();

    echo json_encode( [ 'result' => $response ] );
    exit;
  }

  static public function notes_save()
  {
    \shop\Order::notes_save( \S::get( 'order_id' ), \S::get( 'notes' ) );
  }

  static public function order_save()
  {
    if ( \shop\Order::order_save_by_admin(
            \S::get( 'order_id' ), \S::get( 'client_name' ), \S::get( 'client_surname' ), \S::get( 'client_firm' ), \S::get( 'client_street' ), \S::get( 'client_postal_code' ), \S::get( 'client_city' ), \S::get( 'client_email' ),
            \S::get( 'transport_id' ), \S::get( 'inpost_paczkomat' ), \S::get( 'payment_method_id' )
          ) )
      \S::alert( 'Zamówienie zostało zapisane.' );

    header( 'Location: /admin/shop_order/order_details/order_id=' . \S::get( 'order_id' ) );
    exit;
  }

  static public function order_edit()
  {
    return \Tpl::view( 'shop-order/order-edit', [
      'order' => new \shop\Order( (int)\S::get( 'order_id' ) ),
      'order_statuses' => \shop\Order::order_statuses(),
      'transport' => \shop\Transport::transport_list(),
      'payment_methods' => \shop\PaymentMethod::method_list()
    ] );
  }

  public static function order_details()
  {
    return \Tpl::view( 'shop-order/order-details', [
              'order' => new \shop\Order( (int)\S::get( 'order_id' ) ),
              'order_statuses' => \shop\Order::order_statuses(),
              'next_order_id' => \admin\factory\ShopOrder::next_order_id( (int)\S::get( 'order_id' ) ),
              'prev_order_id' => \admin\factory\ShopOrder::prev_order_id( (int)\S::get( 'order_id' ) ),
            ] );
  }

  public static function view_list()
  {
    return \Tpl::view(
            'shop-order/view-list'
          );
  }

  public static function order_status_change()
  {
    global $mdb;

    $order = new \shop\Order( (int)\S::get( 'order_id' ) );
    $response = $order -> update_status( (int)\S::get( 'status' ), \S::get( 'email' ) == 'true' ? 1 : 0 );

    echo json_encode( $response );
    exit;
  }

  public static function order_delete()
  {
    global $user;

    if ( \shop\Order::order_delete( (int)\S::get( 'id' ) ) )
    {
      \S::alert( 'Zamówienie zostało usunięte' );
      \Log::save_log( 'Usunięcie zamówienia | ID: ' . (int)\S::get( 'id' ), $user['id'] );
    }

    header( 'Location: /admin/shop_order/view_list/' );
    exit;
  }

  // set_order_as_unpaid
  public static function set_order_as_unpaid()
  {
    $order = new \shop\Order( (int)\S::get( 'order_id' ) );
    $order -> set_as_unpaid();
    header( 'Location: /admin/shop_order/order_details/order_id=' . (int)\S::get( 'order_id' ) );
    exit;
  }

  // set_order_as_paid
  public static function set_order_as_paid()
  {
    $order = new \shop\Order( (int)\S::get( 'order_id' ) );
    if ( $order -> set_as_paid() )
    {
      $order -> update_status( 4, (int)\S::get( 'send_mail' ) );
    }
    header( 'Location: /admin/shop_order/order_details/order_id=' . (int)\S::get( 'order_id' ) );
    exit;
  }
}
