<?php
/**
 * Cart totals sidebar template.
 *
 * Override: {theme}/titancart/cart/totals.php
 *
 * @var array  $totals   Totals from CartTotals::calculate().
 * @var array  $cart     Cart row.
 * @var string $currency Currency symbol.
 * @package TitanCart
 */

defined( 'ABSPATH' ) || exit;

$show_shipping_estimate = \TitanCart\Services\SettingsService::bool( 'checkout', 'estimate_shipping_on_cart', true );
$show_tax_on_cart       = \TitanCart\Services\SettingsService::bool( 'appearance', 'show_tax_on_cart', false );

$subtotal        = (float) ( $totals['subtotal']          ?? 0 );
$coupon_discount = (float) ( $totals['coupon_discount']   ?? 0 );
$shipping_cost   = (float) ( $totals['shipping_cost']     ?? 0 );
$tax_total       = (float) ( $totals['tax_total']         ?? 0 );
$payment_sur     = (float) ( $totals['payment_surcharge'] ?? 0 );
$tip_total       = (float) ( $totals['tip_total']         ?? 0 );
$grand_total     = (float) ( $totals['total']             ?? 0 );
$checkout_url    = apply_filters( 'titancart_checkout_url', home_url( '/checkout/' ) );
?>
<div class="tc-cart-totals">
    <h2 class="tc-cart-totals__heading"><?php esc_html_e( 'Cart Total', 'titancart-lite' ); ?></h2>

    <table class="tc-cart-totals__table">
        <tr class="tc-cart-totals__row">
            <th><?php esc_html_e( 'Subtotal', 'titancart-lite' ); ?></th>
            <td id="tc-totals-subtotal"><?php echo esc_html( $currency . number_format( $subtotal, 2 ) ); ?></td>
        </tr>

        <?php if ( $coupon_discount > 0 ) : ?>
            <tr class="tc-cart-totals__row tc-cart-totals__row--discount">
                <th>
                    <?php esc_html_e( 'Coupon discount', 'titancart-lite' ); ?>
                    <?php if ( ! empty( $cart['coupon_code'] ) ) : ?>
                        <span class="tc-cart-totals__coupon-code">
                            (<?php echo esc_html( $cart['coupon_code'] ); ?>)
                        </span>
                    <?php endif; ?>
                </th>
                <td id="tc-totals-discount">
                    &minus;<?php echo esc_html( $currency . number_format( $coupon_discount, 2 ) ); ?>
                </td>
            </tr>
        <?php endif; ?>

        <?php if ( $show_shipping_estimate ) : ?>
        <tr class="tc-cart-totals__row">
            <th><?php esc_html_e( 'Shipping', 'titancart-lite' ); ?></th>
            <td id="tc-totals-shipping">
                <?php if ( $shipping_cost > 0 ) :
                    echo esc_html( $currency . number_format( $shipping_cost, 2 ) );
                else : ?>
                    <em><?php esc_html_e( 'Calculated at checkout', 'titancart-lite' ); ?></em>
                <?php endif; ?>
            </td>
        </tr>
        <?php endif; ?>

        <?php if ( $show_tax_on_cart || $tax_total > 0 ) : ?>
            <tr class="tc-cart-totals__row">
                <th><?php esc_html_e( 'Tax', 'titancart-lite' ); ?></th>
                <td id="tc-totals-tax"><?php echo esc_html( $currency . number_format( $tax_total, 2 ) ); ?></td>
            </tr>
        <?php endif; ?>

        <?php if ( $payment_sur > 0 ) : ?>
            <tr class="tc-cart-totals__row">
                <th><?php esc_html_e( 'Payment surcharge', 'titancart-lite' ); ?></th>
                <td id="tc-totals-surcharge"><?php echo esc_html( $currency . number_format( $payment_sur, 2 ) ); ?></td>
            </tr>
        <?php endif; ?>

        <?php if ( $tip_total > 0 ) : ?>
            <tr class="tc-cart-totals__row">
                <th><?php echo esc_html( apply_filters( 'titancart_tip_line_label', __( 'Tip', 'titancart-lite' ), $cart ) ); ?></th>
                <td id="tc-totals-tip"><?php echo esc_html( $currency . number_format( $tip_total, 2 ) ); ?></td>
            </tr>
        <?php endif; ?>

        <tr class="tc-cart-totals__row tc-cart-totals__row--total">
            <th><?php esc_html_e( 'Total', 'titancart-lite' ); ?></th>
            <td id="tc-totals-grand">
                <strong><?php echo esc_html( $currency . number_format( $grand_total, 2 ) ); ?></strong>
            </td>
        </tr>
    </table>

    <a href="<?php echo esc_url( $checkout_url ); ?>"
       class="tc-btn tc-btn--primary tc-btn--full tc-cart-totals__checkout-btn">
        <?php esc_html_e( 'Proceed to Checkout', 'titancart-lite' ); ?>
    </a>

    <a href="<?php echo esc_url( apply_filters( 'titancart_shop_url', titancart_shop_url() ) ); ?>"
       class="tc-cart-totals__continue-link">
        &larr; <?php esc_html_e( 'Continue Shopping', 'titancart-lite' ); ?>
    </a>
</div>
