<?php
namespace admin\factory;
class ShopTransport
{
  public static function lowest_transport_price( $wp ) {
    global $mdb;
    return $mdb -> get( 'pp_shop_transports', 'cost', [ 'AND' => [ 'status' => 1, 'id' => [ 2, 4, 6, 8 ], 'max_wp[>=]' => $wp ], 'ORDER' => [ 'cost' => 'ASC' ] ] );
  }

  public static function transport_save( $transport_id, $name, $name_visible, $description, $status, $cost, $payment_methods, $max_wp, $default, $apilo_carrier_account_id, $sellasist_shipment_method_id, $delivery_free )
  {
    global $mdb;

    if ( !$transport_id )
    {
      if ( $default == 'on' )
        $mdb -> update( 'pp_shop_transports', [ 'default' => '0' ] );

      $mdb -> insert( 'pp_shop_transports', [
        'name' => $name,
        'name_visible' => $name_visible,
        'description' => $description,
        'status' => $status == 'on' ? 1 : 0,
        'default' => $default == 'on' ? 1 : 0,
        'cost' => $cost,
        'max_wp' => $max_wp ? $max_wp : null,
        'apilo_carrier_account_id' => $apilo_carrier_account_id ? $apilo_carrier_account_id : null,
        'sellasist_shipment_method_id' => $sellasist_shipment_method_id ? $sellasist_shipment_method_id : null,
        'delivery_free' => $delivery_free == 'on' ? 1 : 0
      ] );

      $id = $mdb -> id();

      if ( $id )
      {
        if ( is_array( $payment_methods ) ) foreach ( $payment_methods as $payment_method )
        {
          $mdb -> insert( 'pp_shop_transport_payment_methods', [
                  'id_payment_method' => (int)$payment_method,
                  'id_transport'      => (int)$id
          ] );
        }
        else if ( $payment_methods )
        {
          $mdb -> insert( 'pp_shop_transport_payment_methods', [
                  'id_payment_method' => (int)$payment_methods,
                  'id_transport'      => (int)$id
          ] );
        }

        \S::delete_dir( '../temp/' );

        return $id;
      }
    }
    else
    {
      if ( $default == 'on' )
        $mdb -> update( 'pp_shop_transports', [ 'default' => '0' ] );

      $mdb -> update( 'pp_shop_transports', [
        'name' => $name,
        'name_visible' => $name_visible,
        'description' => $description,
        'status' => $status == 'on' ? 1 : 0,
        'default' => $default == 'on' ? 1 : 0,
        'cost' => $cost,
        'max_wp' => $max_wp ? $max_wp : null,
        'apilo_carrier_account_id' => $apilo_carrier_account_id ? $apilo_carrier_account_id : null,
        'sellasist_shipment_method_id' => $sellasist_shipment_method_id ? $sellasist_shipment_method_id : null,
        'delivery_free' => $delivery_free == 'on' ? 1 : 0
      ], [
        'id' => $transport_id
      ] );

      $mdb -> delete( 'pp_shop_transport_payment_methods', [ 'id_transport'      => (int)$transport_id ] );

      if ( is_array( $payment_methods ) ) foreach ( $payment_methods as $payment_method )
      {
        $mdb -> insert( 'pp_shop_transport_payment_methods', [
                'id_payment_method' => (int)$payment_method,
                'id_transport'      => (int)$transport_id
        ] );
      }
      else if ( $payment_methods )
      {
        $mdb -> insert( 'pp_shop_transport_payment_methods', [
                'id_payment_method' => (int)$payment_methods,
                'id_transport'      => (int)$transport_id
        ] );
      }

      \S::delete_dir( '../temp/' );

      return true;
    }
  }

  public static function transport_details( $transport_id )
  {
    global $mdb;

    $transport = $mdb -> get( 'pp_shop_transports', '*', [ 'id' => $transport_id ] );
    $transport['payment_methods'] = $mdb -> select( 'pp_shop_transport_payment_methods', 'id_payment_method', [ 'id_transport' => $transport_id ] );

    return $transport;
  }
}
