

<?php $__env->startSection('content'); ?>
    <?php echo $__env->make('frontend.components.accordion', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>

    <div class="card mp-20 product-detail-page">
        <a href="<?php echo e(route('user.accounts.products')); ?>" class="product-detail-back">&larr; Back to products</a>

        <div class="product-detail-layout">
            <div class="product-detail-info">
                <?php if($product->image): ?>
                    <div class="product-detail-image-wrap">
                        <img src="<?php echo e(asset($product->image)); ?>" alt="">
                    </div>
                <?php else: ?>
                    <div class="product-detail-image-wrap product-detail-image-placeholder">
                        <i class="ti ti-photo"></i>
                    </div>
                <?php endif; ?>
                <h1 class="product-detail-title"><?php echo e($product->name); ?></h1>
                <p class="product-detail-category"><?php echo e($product->productCategory->name ?? '-'); ?></p>
                <?php if($product->description): ?>
                    <div class="product-detail-description"><?php echo nl2br(e($product->description)); ?></div>
                <?php endif; ?>
                <div class="product-detail-meta">
                    <span class="product-detail-price"><?php echo e($currency); ?><?php echo e(number_format($product->price, 2)); ?> per account</span>
                    <span class="product-detail-stock">In stock: <?php echo e($product->in_stock); ?></span>
                </div>
            </div>

            <div class="product-detail-buy-wrap">
                <div class="product-detail-buy-card card">
                    <div class="card-body">
                        <h2 class="product-detail-buy-title">Buy now</h2>
                        <?php if($product->in_stock < 1): ?>
                            <p class="product-detail-out-of-stock">Out of stock. Check back later.</p>
                        <?php else: ?>
                            <form id="account-purchase-form" class="product-detail-buy-form">
                                <?php echo csrf_field(); ?>
                                <input type="hidden" name="product_id" value="<?php echo e($product->id); ?>">
                                <div class="form-group">
                                    <label class="form-label" for="qty">Quantity <span class="required">*</span></label>
                                    <input type="number" class="form-control" name="quantity" id="qty" min="1" max="<?php echo e($product->in_stock); ?>" value="1" required>
                                    <small class="text-muted">Max: <?php echo e($product->in_stock); ?></small>
                                </div>
                                <div class="product-detail-total-wrap">
                                    <span class="product-detail-total-label">Total</span>
                                    <span class="product-detail-total-amount" id="total-amount"><?php echo e($currency); ?><?php echo e(number_format($product->price, 2)); ?></span>
                                </div>
                                <button type="submit" class="product-detail-purchase-btn" id="btn-purchase">
                                    <i class="ti ti-shopping-cart"></i> Purchase
                                </button>
                            </form>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <?php if($related->isNotEmpty()): ?>
    <div class="card mp-20 product-detail-related">
        <h2 class="product-detail-related-title">Related products</h2>
        <div class="product-detail-related-grid">
            <?php $__currentLoopData = $related; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $r): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                <a href="<?php echo e(route('user.accounts.product', $r->id)); ?>" class="product-detail-related-card card">
                    <?php if($r->image): ?>
                        <img src="<?php echo e(asset($r->image)); ?>" alt="">
                    <?php else: ?>
                        <div class="product-detail-related-placeholder"><i class="ti ti-photo"></i></div>
                    <?php endif; ?>
                    <div class="card-body">
                        <h3 class="product-detail-related-name"><?php echo e($r->name); ?></h3>
                        <p class="product-detail-related-meta"><?php echo e($currency); ?><?php echo e(number_format($r->price, 2)); ?> · Stock: <?php echo e($r->in_stock); ?></p>
                    </div>
                </a>
            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
        </div>
    </div>
    <?php endif; ?>
<?php $__env->stopSection(); ?>

<?php $__env->startPush('script'); ?>
<script>
(function() {
    var price = <?php echo e($product->price); ?>;
    var currency = <?php echo json_encode($currency, 15, 512) ?>;
    var qtyEl = document.getElementById('qty');
    var totalEl = document.getElementById('total-amount');
    if (qtyEl && totalEl) {
        function updateTotal() {
            var q = parseInt(qtyEl.value, 10) || 1;
            totalEl.textContent = currency + (price * q).toFixed(2);
        }
        qtyEl.addEventListener('input', updateTotal);
    }

    var form = document.getElementById('account-purchase-form');
    if (form) {
        form.addEventListener('submit', function(e) {
            e.preventDefault();
            var btn = document.getElementById('btn-purchase');
            btn.disabled = true;
            var fd = new FormData(form);
            fetch('<?php echo e(route("user.accounts.purchase")); ?>', {
                method: 'POST',
                headers: {
                    'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute('content'),
                    'Accept': 'application/json',
                },
                body: fd
            })
            .then(function(r) { return r.json(); })
            .then(function(data) {
                btn.disabled = false;
                if (data.status === 'success') {
                    if (window.iziToast) iziToast.success({ message: data.message, position: 'topRight' });
                    window.location.href = '<?php echo e(route("user.log.account-orders.detail", ["id" => 0])); ?>'.replace(/0$/, data.order_id);
                } else {
                    if (window.iziToast) iziToast.error({ message: data.message || 'Error', position: 'topRight' });
                }
            })
            .catch(function() {
                btn.disabled = false;
                if (window.iziToast) iziToast.error({ message: 'Request failed', position: 'topRight' });
            });
        });
    }
})();
</script>
<?php $__env->stopPush(); ?>

<?php echo $__env->make('frontend.layout.dash', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\Users\HP\Desktop\social\product\core\resources\views/frontend/user/accounts/product-detail.blade.php ENDPATH**/ ?>