<?php
use BWDK\Woo\Order\ReferenceCodeHelper;

$order = $this->order;
$reference_code = $order instanceof \WC_Order ? ReferenceCodeHelper::extractFromOrder($order) : '';
?>

<div class="bwdk-woocommerce-order">



    <?php
    if ( $order ) :

        ?>

        <?php if ( $order->has_status( 'failed' ) ) : ?>

        <p class="bwdk-woocommerce-notice bwdk-woocommerce-notice--error bwdk-woocommerce-thankyou-order-failed"><?php esc_html_e( 'Unfortunately your order cannot be processed as the originating bank/merchant has declined your transaction. Please attempt your purchase again.', 'woocommerce' ); ?></p>

        <p class="bwdk-woocommerce-notice bwdk-woocommerce-notice--error bwdk-woocommerce-thankyou-order-failed-actions">
            <a href="<?php echo esc_url( $order->get_checkout_payment_url() ); ?>" class="button pay"><?php esc_html_e( 'Pay', 'woocommerce' ); ?></a>
            <?php if ( is_user_logged_in() ) : ?>
                <a href="<?php echo esc_url( wc_get_page_permalink( 'myaccount' ) ); ?>" class="button pay"><?php esc_html_e( 'My account', 'woocommerce' ); ?></a>
            <?php endif; ?>
        </p>

    <?php else : ?>

        <?php wc_get_template( 'checkout/order-received.php', array( 'order' => $order ) ); ?>

        <ul class="bwdk-woocommerce-order-overview bwdk-woocommerce-thankyou-order-details order_details">

            <li class="bwdk-woocommerce-order-overview__order order">
                <?php esc_html_e( 'Order number:', 'woocommerce' ); ?>
                <strong><?php echo $order->get_order_number(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></strong>
            </li>

            <li class="bwdk-woocommerce-order-overview__date date">
                <?php esc_html_e( 'Date:', 'woocommerce' ); ?>
                <strong><?php echo wc_format_datetime( $order->get_date_created() ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></strong>
            </li>

            <?php if ( is_user_logged_in() && $order->get_user_id() === get_current_user_id() && $order->get_billing_email() ) : ?>
                <li class="bwdk-woocommerce-order-overview__email email">
                    <?php esc_html_e( 'Email:', 'woocommerce' ); ?>
                    <strong><?php echo $order->get_billing_email(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></strong>
                </li>
            <?php endif; ?>

            <li class="bwdk-woocommerce-order-overview__total total">
                <?php esc_html_e( 'Total:', 'woocommerce' ); ?>
                <strong><?php echo $order->get_formatted_order_total(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></strong>
            </li>

            <?php if ( $order->get_payment_method_title() ) : ?>
                <li class="bwdk-woocommerce-order-overview__payment-method method">
                    <?php esc_html_e( 'Payment method:', 'woocommerce' ); ?>
                    <strong><?php echo wp_kses_post( $order->get_payment_method_title() ); ?></strong>
                </li>
            <?php endif; ?>

            <?php if ( $reference_code !== '' ) : ?>
                <li class="bwdk-woocommerce-order-overview__digikala-reference reference">
                    <?php esc_html_e( 'کد پیگیری دیجی‌کالایی', 'bwdk' ); ?>
                    <strong class="ltr"><?php echo esc_html( $reference_code ); ?></strong>
                </li>
            <?php endif; ?>

        </ul>

    <?php endif; ?>

        <?php

        if ( ! $order ) {
            return;
        }

        $order_items        = $order->get_items( apply_filters( 'woocommerce_purchase_order_item_types', 'line_item' ) );
        $show_purchase_note = $order->has_status( apply_filters( 'woocommerce_purchase_note_order_statuses', array( 'completed', 'processing' ) ) );

        $actions            = array_filter(
            wc_get_account_orders_actions( $order ),
            function ( $key ) {
                return 'view' !== $key;
            },
            ARRAY_FILTER_USE_KEY
        );

        $show_customer_details = $order->get_user_id() === get_current_user_id();

        ?>
        <section class="bwdk-woocommerce-order-details">
            <?php do_action( 'woocommerce_order_details_before_order_table', $order ); ?>

            <h2 class="bwdk-woocommerce-order-details__title"><?php esc_html_e( 'Order details', 'woocommerce' ); ?></h2>

            <table class="bwdk-woocommerce-table bwdk-woocommerce-table--order-details shop_table order_details">

                <thead>
                <tr>
                    <th class="bwdk-woocommerce-table__product-name product-name"><?php esc_html_e( 'Product', 'woocommerce' ); ?></th>
                    <th class="bwdk-woocommerce-table__product-table product-total"><?php esc_html_e( 'Total', 'woocommerce' ); ?></th>
                </tr>
                </thead>

                <tbody>
                <?php
                do_action( 'woocommerce_order_details_before_order_table_items', $order );

                foreach ( $order_items as $item_id => $item ) {
                    $product = $item->get_product();

                    wc_get_template(
                        'order/order-details-item.php',
                        array(
                            'order'              => $order,
                            'item_id'            => $item_id,
                            'item'               => $item,
                            'show_purchase_note' => $show_purchase_note,
                            'purchase_note'      => $product ? $product->get_purchase_note() : '',
                            'product'            => $product,
                        )
                    );
                }

                do_action( 'woocommerce_order_details_after_order_table_items', $order );
                ?>
                </tbody>

                <?php
                if ( ! empty( $actions ) ) :
                    ?>
                    <tfoot>
                    <tr>
                        <th class="order-actions--heading"><?php esc_html_e( 'Actions', 'woocommerce' ); ?>:</th>
                        <td>
                            <?php
                            $wp_button_class = wc_wp_theme_get_element_class_name( 'button' ) ? ' ' . wc_wp_theme_get_element_class_name( 'button' ) : '';
                            foreach ( $actions as $key => $action ) { // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
                                if ( empty( $action['aria-label'] ) ) {
                                    // Generate the aria-label based on the action name.
                                    /* translators: %1$s Action name, %2$s Order number. */
                                    $action_aria_label = sprintf( __( '%1$s order number %2$s', 'woocommerce' ), $action['name'], $order->get_order_number() );
                                } else {
                                    $action_aria_label = $action['aria-label'];
                                }
                                echo '<a href="' . esc_url( $action['url'] ) . '" class="bwdk-woocommerce-button' . esc_attr( $wp_button_class ) . ' button ' . sanitize_html_class( $key ) . ' order-actions-button " aria-label="' . esc_attr( $action_aria_label ) . '">' . esc_html( $action['name'] ) . '</a>';
                                unset( $action_aria_label );
                            }
                            ?>
                        </td>
                    </tr>
                    </tfoot>
                <?php endif ?>
                <tfoot>
                <?php
                foreach ( $order->get_order_item_totals() as $key => $total ) {
                    ?>
                    <tr>
                        <th scope="row"><?php echo esc_html( $total['label'] ); ?></th>
                        <td><?php echo wp_kses_post( $total['value'] ); ?></td>
                    </tr>
                    <?php
                }
                ?>
                <?php if ( $order->get_customer_note() ) : ?>
                    <tr>
                        <th><?php esc_html_e( 'Note:', 'woocommerce' ); ?></th>
                        <td><?php echo wp_kses( nl2br( wptexturize( $order->get_customer_note() ) ), array( 'br' => array() ) ); ?></td>
                    </tr>
                <?php endif; ?>
                </tfoot>
            </table>

            <?php do_action( 'woocommerce_order_details_after_order_table', $order ); ?>
        </section>

        <?php
        /**
         * Action hook fired after the order details.
         *
         * @since 4.4.0
         * @param WC_Order $order Order data.
         */
        do_action( 'woocommerce_after_order_details', $order );

        $show_shipping = ! wc_ship_to_billing_address_only() && $order->needs_shipping_address();
        ?>
        <section class="bwdk-woocommerce-customer-details">

            <?php if ( $show_shipping ) : ?>

            <section class="bwdk-woocommerce-columns bwdk-woocommerce-columns--2 bwdk-woocommerce-columns--addresses col2-set addresses">
                <div class="bwdk-woocommerce-column bwdk-woocommerce-column--1 bwdk-woocommerce-column--billing-address col-1">

                    <?php endif; ?>

                    <h2 class="bwdk-woocommerce-column__title"><?php esc_html_e( 'Billing address', 'woocommerce' ); ?></h2>

                    <address>
                        <?php echo wp_kses_post( $order->get_formatted_billing_address( esc_html__( 'N/A', 'woocommerce' ) ) ); ?>

                        <?php if ( $order->get_billing_phone() ) : ?>
                            <p class="bwdk-woocommerce-customer-details--phone"><?php echo esc_html( $order->get_billing_phone() ); ?></p>
                        <?php endif; ?>

                        <?php if ( $order->get_billing_email() ) : ?>
                            <p class="bwdk-woocommerce-customer-details--email"><?php echo esc_html( $order->get_billing_email() ); ?></p>
                        <?php endif; ?>

                        <?php
                        /**
                         * Action hook fired after an address in the order customer details.
                         *
                         * @since 8.7.0
                         * @param string $address_type Type of address (billing or shipping).
                         * @param WC_Order $order Order object.
                         */
                        do_action( 'woocommerce_order_details_after_customer_address', 'billing', $order );
                        ?>
                    </address>

                    <?php if ( $show_shipping ) : ?>

                </div><!-- /.col-1 -->

                <div class="bwdk-woocommerce-column bwdk-woocommerce-column--2 bwdk-woocommerce-column--shipping-address col-2">
                    <h2 class="bwdk-woocommerce-column__title"><?php esc_html_e( 'Shipping address', 'woocommerce' ); ?></h2>
                    <address>
                        <?php echo wp_kses_post( $order->get_formatted_shipping_address( esc_html__( 'N/A', 'woocommerce' ) ) ); ?>

                        <?php if ( $order->get_shipping_phone() ) : ?>
                            <p class="bwdk-woocommerce-customer-details--phone"><?php echo esc_html( $order->get_shipping_phone() ); ?></p>
                        <?php endif; ?>

                        <?php
                        /**
                         * Action hook fired after an address in the order customer details.
                         *
                         * @since 8.7.0
                         * @param string $address_type Type of address (billing or shipping).
                         * @param WC_Order $order Order object.
                         */
                        do_action( 'woocommerce_order_details_after_customer_address', 'shipping', $order );
                        ?>
                    </address>
                </div><!-- /.col-2 -->

            </section><!-- /.col2-set -->

        <?php endif; ?>

            <?php do_action( 'woocommerce_order_details_after_customer_details', $order ); ?>

        </section>

    <?php else : ?>

        <?php wc_get_template( 'checkout/order-received.php', array( 'order' => false ) ); ?>

    <?php endif; ?>

</div>