<?php
namespace admin\factory;
class ShopOrder
{
  static public function send_order_to_apilo( int $order_id ) {

    global $mdb;

    // początek - anulowanie zamówienia w apilo
    $apilo_settings = \admin\factory\Integrations::apilo_settings();

    $new_status = 8; // zamówienie anulowwane

    $order = \admin\factory\ShopOrder::order_details( $order_id );

    if ( $order['apilo_order_id'] ) {

      $access_token = \admin\factory\Integrations::apilo_get_access_token();

      $ch = curl_init();
      curl_setopt( $ch, CURLOPT_URL, "https://projectpro.apilo.com/rest/api/orders/" . $order['apilo_order_id'] . '/status/' );
      curl_setopt( $ch, CURLOPT_POST, 1 );
      curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, "PUT");
      curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( [
        'id' => $order['apilo_order_id'],
        'status' => (int)\front\factory\ShopStatuses::get_apilo_status_id( $new_status )
      ] ) );
      curl_setopt( $ch, CURLOPT_HTTPHEADER, [
        "Authorization: Bearer " . $access_token,
        "Accept: application/json",
        "Content-Type: application/json"
      ] );
      curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
      $apilo_result = curl_exec( $ch );

      $apilo_result = json_decode( $apilo_result, true );

      if ( $apilo_result['updates'] == 1 ) {

        // zmień ID zamówienia na największe ID zamówienia + 1, oraz usuń ID zamówienia z apilo
        $new_order_id = $mdb -> max( 'pp_shop_orders', 'id' ) + 1;

        // pobierz listę kolumn zamówienia
        $query = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'pp_shop_orders' AND COLUMN_NAME != 'id'";
        $columns = $mdb -> query( $query ) -> fetchAll( \PDO::FETCH_COLUMN );
        $columns_list = implode( ', ', $columns );

        // kopiuj stare zamówienie do nowego ID
        $mdb -> query( 'INSERT INTO pp_shop_orders (' . $columns_list . ') SELECT ' . $columns_list . ' FROM pp_shop_orders pso WHERE pso.id = ' . $order_id );
        $new_order_id = $mdb -> id();

        // pobierz listę kolumn produktów zamówienia
        $query = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'pp_shop_order_products' AND COLUMN_NAME != 'id' AND COLUMN_NAME != 'order_id'";
        $columns = $mdb -> query( $query ) -> fetchAll( \PDO::FETCH_COLUMN );
        $columns_list = implode( ', ', $columns );

        // kopiuj produkty zamówienia do nowego zamówienia
        $mdb -> query( 'INSERT INTO pp_shop_order_products (order_id, ' . $columns_list . ') SELECT ' . $new_order_id . ',' . $columns_list . ' FROM pp_shop_order_products psop WHERE psop.order_id = ' . $order_id );

        // pobierz listę kolumn z tabeli pp_shop_order_statuses
        $query = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'pp_shop_order_statuses' AND COLUMN_NAME != 'id' AND COLUMN_NAME != 'order_id'";
        $columns = $mdb -> query( $query ) -> fetchAll( \PDO::FETCH_COLUMN );
        $columns_list = implode( ', ', $columns );

        // kopiuj statusy zamówienia do nowego zamówienia
        $mdb -> query( 'INSERT INTO pp_shop_order_statuses (order_id, ' . $columns_list . ') SELECT ' . $new_order_id . ',' . $columns_list . ' FROM pp_shop_order_statuses psos WHERE psos.order_id = ' . $order_id );

        // usuń stare zamówienie
        $mdb -> delete( 'pp_shop_orders', [ 'id' => $order_id ] );
        $mdb -> delete( 'pp_shop_order_products', [ 'order_id' => $order_id ] );
        $mdb -> delete( 'pp_shop_order_statuses', [ 'order_id' => $order_id ] );

        // zmień wartość kolumny apilo_order_id na NULL
        $mdb -> update( 'pp_shop_orders', [ 'apilo_order_id' => NULL ], [ 'id' => $new_order_id ] );

        return true;
      }

      curl_close( $ch );
    }

    return false;
  }

  static public function next_order_id( int $order_id )
  {
    global $mdb;

    if ( !$order_id )
      return false;

    return $mdb -> get( 'pp_shop_orders', 'id', [ 'id[>]' => $order_id, 'ORDER' => [ 'id' => 'ASC' ], 'LIMIT' => 1 ] );
  }

  static public function prev_order_id( int $order_id )
  {
    global $mdb;

    if ( !$order_id )
      return false;

    return $mdb -> get( 'pp_shop_orders', 'id', [ 'id[<]' => $order_id, 'ORDER' => [ 'id' => 'DESC' ], 'LIMIT' => 1 ] );
  }

  static public function order_details( int $order_id )
  {
    global $mdb;

    $order = $mdb -> get( 'pp_shop_orders', '*', [ 'id' => $order_id ] );
    $order['products'] = $mdb -> select( 'pp_shop_order_products', '*', [ 'order_id' => $order_id ] );

    return $order;
  }
}
