<?php
$shop_info = $this->common_m->shop_info($shop_id);

$cart_total = $this->cart->total();
$cart_contents = $this->cart->contents();


if ($shop_info['is_area_delivery'] == 1) {
    $shipping = isset($cost) && $cost != 0 ? $cost : 0;
} else {
    $shipping = $shop_info['delivery_charge_in'] ?? 0;

}


$coupon_price     = isset($coupon_price) ? $coupon_price : 0;
$tips             = isset($tips) && !empty($tips) ? $tips : 0;
$is_service_charge = isset($is_service_charge) ? $is_service_charge : 0;

// Call cart summary calculator
$summary = __cartOrder($shop_id, $cart_total, $cart_contents, [
    'cost'              => $shipping,
    'tips'              => $tips,
    'coupon_price'      => $coupon_price,
    'is_service_charge' => $is_service_charge,
]);

// Unpack results
$grandSubTotal   = $summary['subtotal'];
$tax_fee         = $summary['tax_fee'];
$tax_status      = $summary['tax_status'];
$is_item_tax     = $summary['is_item_tax'];
$taxDetails      = $summary['tax_details'];
$shipping        = $summary['shipping'];
$discount        = $summary['discount'];
$coupon_price    = $summary['coupon_discount'];
$service_charge  = $summary['service_charge'];
$tips            = $summary['tips'];
$get_total       = $summary['grand_total'];
?>

<div class="row">
    <div class="col-lg-12 col-sm-12">
        <div class="upperSum">
            <div class="flex justify-between">
                <p><?= lang('qty') ?? 'Qty'; ?> :</p>
                <span class="cart_count"><?= $this->cart->total_items(); ?></span>
            </div>

            <div class="flex justify-between">
                <p><?= lang('sub_total') ?? 'Sub Total'; ?> :</p>
                <span class="total_price"><?= currency_position($grandSubTotal, $shop_id); ?></span>
            </div>

            <div class="dis_none <?= $shop_info['is_area_delivery'] == 1 || !empty($shipping) ? "showShipping" : "show_address"; ?>">
                <div class="flex justify-between">
                    <p><?= lang('shipping') ?? 'Shipping'; ?> :</p>
                    <span class="d_charge">
                        <?= $shipping != 0 ? currency_position($shipping, $shop_id) : (lang('Free') ?? 'Free'); ?>
                    </span>
                </div>
            </div>

            <div class="discount_tax">
                <?php if ($is_item_tax == 1): ?>
                    <?php foreach (__tax($taxDetails)->details as $tax): ?>
                        <div class="flex justify-between">
                            <p><?= lang('tax'); ?> <span class="text-sm"><?= tax($tax['percent'], $tax['tax_status']); ?></span> :</p>
                            <span class="d_charge"><?= currency_position($tax['total_price'], $shop_id); ?></span>
                        </div>
                    <?php endforeach; ?>
                <?php elseif ($tax_fee != 0): ?>
                    <div class="flex justify-between">
                        <p><?= lang('tax'); ?> <span class="text-sm">(<?= tax($shop_info['tax_fee'], $tax_status); ?>)</span> :</p>
                        <span class="d_charge"><?= currency_position($tax_fee, $shop_id); ?></span>
                    </div>
                <?php endif; ?>

                <?php if ($discount != 0): ?>
                    <div class="flex justify-between">
                        <p><?= lang('discount') ?? 'Discount'; ?> :</p>
                        <span class="d_charge"><?= currency_position($discount, $shop_id); ?></span>
                    </div>
                <?php endif; ?>
            </div>

            <?php if ($coupon_price != 0): ?>
                <div class="couponPricearea">
                    <div class="flex justify-between">
                        <p><?= lang('coupon_discount') ?? 'Coupon Discount'; ?> :</p>
                        <span class="coupon_price"><?= currency_position($coupon_price, $shop_id); ?></span>
                    </div>
                </div>
            <?php endif; ?>

            <?php if ($tips != 0): ?>
                <div class="tips">
                    <div class="flex justify-between">
                        <p><?= lang('tips') ?? 'Tip'; ?> :</p>
                        <span class="tip_price"><?= currency_position($tips, $shop_id); ?></span>
                    </div>
                </div>
            <?php endif; ?>

            <?php if ($service_charge != 0): ?>
                <div class="tips serviceCharge dis_none">
                    <div class="flex justify-between">
                        <p><?= lang('service_charge') ?? 'Service Charge'; ?> <?= $service_charges->details ?? ''; ?> :</p>
                        <span class="tip_price"><?= currency_position($service_charge, $shop_id); ?></span>
                    </div>
                </div>
            <?php endif; ?>
        </div>

        <?php
        $showShippingClass = $shop_info['is_area_delivery'] == 1 || !empty($shipping) ? "showShipping" : "show_address";
        $adjusted_total = $get_total - ($shipping ?? 0);
        ?>

        <div class="dis_none <?= $showShippingClass; ?>">
            <div class="flex justify-between">
                <h4><?= lang('total') ?? 'Total'; ?> :</h4>
                <h4><?= currency_position($get_total, $shop_id); ?></h4>
            </div>
        </div>

        <div class="show_price adjust">
            <div class="flex justify-between">
                <h4><?= lang('total') ?? 'Total'; ?> :</h4>
                <h4><?= currency_position($adjusted_total, $shop_id); ?></h4>
            </div>
        </div>



        <?php if ($shop_info['is_area_delivery'] == 0 && !empty($shipping)): ?>
            <div class="show_price defaultshipping">
                <div class="flex justify-between">
                    <h4><?= lang('total') ?? 'Total'; ?> :</h4>
                    <h4><?= currency_position($adjusted_total, $shop_id); ?></h4>
                </div>
            </div>
        <?php endif; ?>
    </div>
</div>