/**
  * @param Bring_WC_Order_Adapter $order
  * @return string
  */
 static function get_booking_status_info($order)
 {
     $result = ['text' => __('No', 'bring-fraktguiden'), 'icon' => 'dashicons-minus'];
     if (self::is_step2()) {
         $result = ['text' => __('In progress', 'bring-fraktguiden'), 'icon' => ''];
     }
     if ($order->is_booked()) {
         $result = ['text' => __('Booked', 'bring-fraktguiden'), 'icon' => 'dashicons-yes'];
     }
     if ($order->has_booking_errors()) {
         $result = ['text' => __('Failed', 'bring-fraktguiden'), 'icon' => 'dashicons-warning'];
     }
     return $result;
 }
    /**
     * @param Bring_WC_Order_Adapter $order
     */
    static function render_start($order)
    {
        ?>
    <?php 
        if (!$order->has_booking_errors()) {
            ?>
      <div>
        <?php 
            _e('Press start to start booking', 'bring-fraktguiden');
            ?>
        <br>
        <?php 
            $next_status = Fraktguiden_Helper::get_option('auto_set_status_after_booking_success');
            if ($next_status != 'none') {
                $order_statuses = wc_get_order_statuses();
                printf(__('Order status will be set to %s upon successful booking', 'bring-fraktguiden'), strtolower($order_statuses[$next_status]));
            }
            ?>
      </div>
      <?php 
        }
        ?>

    <?php 
    }
 /**
  * @param WC_Order $wc_order
  */
 static function send_booking($wc_order)
 {
     $order = new Bring_WC_Order_Adapter($wc_order);
     $order_id = $wc_order->id;
     $test_mode = self::is_test_mode();
     $api_key = self::get_api_key();
     $api_uid = self::get_api_uid();
     $client_url = self::get_client_url();
     $customer_number = isset($_REQUEST['_bring-customer-number']) ? $_REQUEST['_bring-customer-number'] : '';
     if (isset($_REQUEST['_bring-shipping-date']) && isset($_REQUEST['_bring-shipping-date-hour']) && isset($_REQUEST['_bring-shipping-date-minutes'])) {
         $shipping_date_time = $_REQUEST['_bring-shipping-date'] . 'T' . $_REQUEST['_bring-shipping-date-hour'] . ':' . $_REQUEST['_bring-shipping-date-minutes'] . ':00';
     } else {
         $shipping_date = self::create_shipping_date();
         $shipping_date_time = $shipping_date['date'] . "T" . $shipping_date['hour'] . ":" . $shipping_date['minute'] . ":00";
     }
     $additional_info = '';
     if (isset($_REQUEST['_bring_additional_info'])) {
         $additional_info = filter_var($_REQUEST['_bring_additional_info'], FILTER_SANITIZE_STRING);
     }
     $sender_address = self::get_sender_address($additional_info);
     $recipient_address = $order->get_recipient_address_formatted();
     // One booking request per. order shipping item.
     foreach ($order->get_fraktguiden_shipping_items() as $item_id => $shipping_method) {
         $service_id = Fraktguiden_Helper::parse_shipping_method_id($shipping_method['method_id'])['service'];
         $packages = $order->get_packages_formatted($item_id);
         $pickup_point = $order->get_pickup_point_for_shipping_item_formatted($item_id);
         $booking_request = new Bring_Booking_Request(new WP_Bring_Request());
         $booking_request->set_test_mode($test_mode)->set_content_type('application/json')->set_accept('application/json')->set_api_key($api_key)->set_api_uid($api_uid)->set_client_url($client_url);
         $consignment = new Bring_Booking_Consignment_Creator();
         $consignment->set_purchase_order($order_id)->set_shipping_date_time($shipping_date_time)->set_sender_address($sender_address)->set_recipient_address($recipient_address)->set_product_id($service_id)->set_customer_number($customer_number)->set_packages($packages);
         if (!empty($pickup_point)) {
             $consignment->set_pickup_point($pickup_point);
         }
         if (Fraktguiden_Helper::get_option('evarsling') == 'yes') {
             $product_services = ['recipientNotification' => ['email' => $recipient_address['contact']['email'], 'mobile' => $recipient_address['contact']['phoneNumber']]];
             $consignment->set_product_services($product_services);
         }
         $booking_request->add_consignment_data($consignment->create_data());
         // Start sending the booking
         if ($booking_request->is_valid()) {
             $original_order_status = $wc_order->get_status();
             // Set order status to awaiting shipping.
             $wc_order->update_status('wc-bring-shipment');
             // Send the booking.
             $response = $booking_request->send();
             // Save the response json to the order.
             $order->update_booking_response($response);
             // Download labels pdf
             if (!$order->has_booking_errors()) {
                 Bring_Booking_Labels::download_to_local($order);
             }
             // Create new status and order note.
             if (!$order->has_booking_errors()) {
                 // Check if the plugin has been configured to set a specific order status after success.
                 $status = Fraktguiden_Helper::get_option('auto_set_status_after_booking_success');
                 if ($status == 'none') {
                     // Set status back to the previous status
                     $status = $original_order_status;
                 }
                 $status_note = __("Booked with Bring" . "\n", 'bring-fraktguiden');
             } else {
                 // If there are errors, set the status back to the original status.
                 $status = $original_order_status;
                 $status_note = __("Booking errors. See the Bring Booking box for details." . "\n", 'bring-fraktguiden');
             }
             // Update status.
             $wc_order->update_status($status, $status_note);
         } else {
             // @todo: Not valid. show message?
         }
     }
 }