<?php $__env->startSection('content'); ?>
<div class="container-fluid py-5" style="background: #f8fafc; min-height: 100vh; font-family: 'Plus Jakarta Sans', sans-serif;">
    <div class="container">
        
        <div class="d-flex justify-content-between align-items-end mb-5">
            <div>
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb mb-2">
                        <li class="breadcrumb-item small"><a href="<?php echo e(route('home')); ?>" class="text-decoration-none text-muted">Home</a></li>
                        <li class="breadcrumb-item small active" aria-current="page">Service History</li>
                    </ol>
                </nav>
                <h2 class="fw-bold text-dark mb-0">Riwayat Jasa Service</h2>
                <p class="text-muted small mb-0">Pantau status perbaikan printer Anda secara real-time.</p>
            </div>
            <a href="<?php echo e(route('jasa.create')); ?>" class="btn btn-primary rounded-pill px-4 py-2 fw-bold shadow-sm">
                <i class="lni lni-plus me-2"></i> Order Service Baru
            </a>
        </div>

        <?php if($jasas->isEmpty()): ?>
            <div class="text-center py-5 bg-white rounded-5 shadow-sm border border-light">
                <img src="https://img.freepik.com/free-vector/no-data-concept-illustration_114360-616.jpg" style="max-width: 250px;" class="mb-4 opacity-75">
                <h4 class="fw-bold text-dark">Belum ada riwayat service</h4>
                <p class="text-muted">Sepertinya Anda belum pernah melakukan order service printer.</p>
                <a href="<?php echo e(route('jasa.create')); ?>" class="btn btn-outline-primary rounded-pill px-5 mt-2 fw-bold">Buat Order Sekarang</a>
            </div>
        <?php else: ?>
            <div class="row g-4">
                <?php $__currentLoopData = $jasas; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $j): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                    <div class="col-12" id="jasa-card-<?php echo e($j->id); ?>">
                        <div class="card border-0 shadow-sm rounded-5 overflow-hidden bg-white position-relative" style="transition: all 0.3s ease;">
                            <div class="row g-0">
                                
                                <!-- Status Color Bar -->
                                <div class="col-auto" style="width: 8px; background: 
                                    <?php if($j->status == 'pending'): ?> #f59e0b 
                                    <?php elseif($j->status == 'proses'): ?> #3b82f6 
                                    <?php elseif($j->status == 'selesai'): ?> #10b981 
                                    <?php else: ?> #ef4444 <?php endif; ?>">
                                </div>

                                <div class="col p-4 p-md-5">
                                    <div class="d-flex flex-wrap justify-content-between align-items-start gap-3">
                                        
                                        <!-- Main Info -->
                                        <div class="flex-grow-1">
                                            <div class="d-flex align-items-center gap-3 mb-3">
                                                <span class="badge rounded-pill px-3 py-2 text-uppercase fw-bold" 
                                                      style="font-size: 10px; letter-spacing: 0.5px;
                                                      <?php if($j->status == 'pending'): ?> background: #fef3c7; color: #92400e;
                                                      <?php elseif($j->status == 'proses'): ?> background: #dbeafe; color: #1e40af;
                                                      <?php elseif($j->status == 'selesai'): ?> background: #d1fae5; color: #065f46;
                                                      <?php else: ?> background: #fee2e2; color: #991b1b; <?php endif; ?>">
                                                    <?php echo e($j->status); ?>

                                                </span>
                                                <span class="text-muted small"><i class="lni lni-calendar me-1"></i> <?php echo e($j->created_at->format('d M Y, H:i')); ?></span>
                                            </div>
                                            <h4 class="fw-bold text-dark mb-1"><?php echo e($j->merek_printer); ?> - <?php echo e($j->tipe_printer); ?></h4>
                                            <p class="text-primary fw-semibold small mb-3"><i class="lni lni-warning me-1"></i> <?php echo e($j->kategori_keluhan); ?></p>
                                            
                                            <div class="bg-light p-3 rounded-4 mb-3 border border-white">
                                                <p class="text-muted small mb-0 fst-italic">"<?php echo e($j->deskripsi_masalah); ?>"</p>
                                            </div>

                                            <div class="row g-3">
                                                <div class="col-md-6">
                                                    <div class="small text-muted mb-1">Metode Servis</div>
                                                    <div class="fw-bold text-dark"><i class="lni lni-delivery me-1 text-primary"></i> <?php echo e($j->jenis_layanan); ?></div>
                                                </div>
                                                <div class="col-md-6">
                                                    <div class="small text-muted mb-1">Lokasi</div>
                                                    <div class="fw-bold text-dark text-truncate" style="max-width: 250px;">
                                                        <i class="lni lni-map-marker me-1 text-primary"></i> 
                                                        <?php echo e($j->jenis_layanan == 'Antar ke Toko' ? 'Workshop Pusat' : ($j->address ? $j->address->nama_penerima : 'N/A')); ?>

                                                    </div>
                                                </div>
                                            </div>
                                        </div>

                                        <!-- Action Controls -->
                                        <div class="ms-md-auto text-md-end d-flex flex-column gap-3" style="min-width: 250px;">
                                            
                                            
                                            <?php if(strtolower($j->status) == 'selesai'): ?>
                                                <div class="mt-2 mb-3">
                                                    <?php if($j->rating): ?>
                                                        <div class="bg-light p-3 rounded-4 border shadow-sm text-center">
                                                            <div class="small fw-bold text-muted mb-2">Penilaian Anda:</div>
                                                            <div class="text-warning mb-1 fs-5">
                                                                <?php for($i = 1; $i <= 5; $i++): ?>
                                                                    <i class="lni lni-star-fill <?php echo e($i <= $j->rating ? '' : 'opacity-25'); ?>"></i>
                                                                <?php endfor; ?>
                                                            </div>
                                                            <?php if($j->testimoni): ?>
                                                                <div class="small text-muted fst-italic">"<?php echo e($j->testimoni); ?>"</div>
                                                            <?php endif; ?>
                                                        </div>
                                                    <?php else: ?>
                                                        <div class="bg-primary bg-opacity-10 p-4 rounded-4 border-2 border-primary border-dashed text-center">
                                                            <div class="small fw-800 text-primary mb-3 text-uppercase" style="font-size: 11px; letter-spacing: 1px;">🎉 Layanan Selesai! Mohon Beri Rating:</div>
                                                            <form action="<?php echo e(route('jasa.rating', $j->id)); ?>" method="POST" class="rating-form">
                                                                <?php echo csrf_field(); ?>
                                                                <div class="d-flex justify-content-center gap-2 mb-3">
                                                                    <?php for($i = 1; $i <= 5; $i++): ?>
                                                                        <input type="radio" name="rating" value="<?php echo e($i); ?>" id="star<?php echo e($j->id); ?>-<?php echo e($i); ?>" class="d-none star-input" required>
                                                                        <label for="star<?php echo e($j->id); ?>-<?php echo e($i); ?>" class="star-label cursor-pointer" data-id="<?php echo e($j->id); ?>" data-value="<?php echo e($i); ?>" style="transition: all 0.2s;">
                                                                            <i class="lni lni-star-fill text-muted fs-3"></i>
                                                                        </label>
                                                                    <?php endfor; ?>
                                                                </div>
                                                                <textarea name="testimoni" class="form-control border-white shadow-sm rounded-3 mb-3" placeholder="Bagaimana hasil servis kami? (Opsional)" rows="2" style="font-size: 13px;"></textarea>
                                                                <button type="submit" class="btn btn-primary w-100 rounded-pill fw-bold py-2 shadow-sm">Kirim Penilaian Sekarang</button>
                                                            </form>
                                                        </div>
                                                    <?php endif; ?>
                                                </div>
                                            <?php endif; ?>

                                            <div class="d-flex gap-2">
                                                <a href="https://wa.me/<?php echo e($shop_settings['shop_wa'] ?? '6281234567890'); ?>" target="_blank" class="btn btn-outline-success btn-sm flex-grow-1 rounded-pill fw-bold">
                                                    <i class="ti ti-brand-whatsapp me-1"></i> Hubungi Toko
                                                </a>
                                                <?php if($j->status == 'pending' || $j->status == 'batal'): ?>
                                                    <button class="btn btn-outline-danger btn-sm rounded-circle p-2 btn-delete-jasa" data-id="<?php echo e($j->id); ?>">
                                                        <i class="ti ti-trash"></i>
                                                    </button>
                                                <?php endif; ?>
                                            </div>
                                        </div>

                                    </div>
                                </div>

                                <!-- Multimedia Preview -->
                                <?php if($j->foto_video_kondisi): ?>
                                    <div class="col-md-3 bg-light border-start d-flex align-items-center justify-content-center p-3">
                                        <?php $ext = pathinfo($j->foto_video_kondisi, PATHINFO_EXTENSION); ?>
                                        <?php if(in_array($ext, ['jpg','jpeg','png'])): ?>
                                            <img src="<?php echo e(asset('storage/service/' . $j->foto_video_kondisi)); ?>" class="img-fluid rounded-4 shadow-sm" style="max-height: 150px; cursor: pointer;" onclick="window.open(this.src)">
                                        <?php else: ?>
                                            <div class="bg-primary bg-opacity-10 p-4 rounded-circle text-primary">
                                                <i class="lni lni-video" style="font-size: 30px;"></i>
                                            </div>
                                        <?php endif; ?>
                                    </div>
                                <?php endif; ?>

                            </div>
                        </div>
                    </div>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
            </div>

            <div class="mt-5 d-flex justify-content-center">
                <?php echo e($jasas->links()); ?>

            </div>
        <?php endif; ?>

    </div>
</div>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
    
    // Star Rating Interaction
    $('.star-label').on('click', function() {
        const val = $(this).data('value');
        const id = $(this).data('id');
        
        // Reset all stars in this card
        $(`label[data-id="${id}"] i`).removeClass('text-warning').addClass('text-muted');
        
        // Highlight selected stars
        for(let i = 1; i <= val; i++) {
            $(`label[data-id="${id}"][data-value="${i}"] i`).removeClass('text-muted').addClass('text-warning');
        }
    });

    // Star Hover Effect
    $('.star-label').on('mouseenter', function() {
        const val = $(this).data('value');
        const id = $(this).data('id');
        for(let i = 1; i <= val; i++) {
            $(`label[data-id="${id}"][data-value="${i}"] i`).addClass('text-warning opacity-75');
        }
    }).on('mouseleave', function() {
        const id = $(this).data('id');
        const selectedVal = $(`input[name="rating"][data-id="${id}"]:checked`).val() || 0;
        
        $(`.star-label[data-id="${id}"] i`).removeClass('opacity-75');
        
        // Revert to selected state
        $(`.star-label[data-id="${id}"] i`).each(function() {
            const val = $(this).parent().data('value');
            if(val > selectedVal) {
                $(this).removeClass('text-warning').addClass('text-muted');
            } else {
                $(this).addClass('text-warning');
            }
        });
    });

    // Ajax Status Changer
    $('.status-changer').on('change', function() {
        const id = $(this).data('id');
        const status = $(this).val();
        const select = $(this);

        select.prop('disabled', true);
        
        $.ajax({
            url: `/service/update-status/${id}`,
            type: "POST",
            data: {
                _token: "<?php echo e(csrf_token()); ?>",
                status: status
            },
            success: function(res) {
                if(res.success) {
                    location.reload(); // Reload agar warna badge berubah sesuai status baru
                }
            },
            error: function() {
                alert('Gagal memperbarui status.');
                select.prop('disabled', false);
            }
        });
    });

    // Ajax Delete Jasa
    $('.btn-delete-jasa').on('click', function() {
        const id = $(this).data('id');
        const card = $(`#jasa-card-${id}`);

        if(confirm('Hapus riwayat service ini dari daftar?')) {
            $.ajax({
                url: `/service/delete/${id}`,
                type: "DELETE",
                data: { _token: "<?php echo e(csrf_token()); ?>" },
                success: function(res) {
                    if(res.success) {
                        card.fadeOut(400, function() { $(this).remove(); });
                    }
                }
            });
        }
    });

});
</script>

<style>
    .card:hover {
        transform: translateY(-5px);
        box-shadow: 0 15px 30px rgba(0,0,0,0.1) !important;
    }
    .status-changer:focus {
        box-shadow: none;
        border-color: #3b82f6;
    }
    .text-truncate-2 {
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        overflow: hidden;
    }
</style>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layout.temp', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\laragon\www\jasa_printer\resources\views/jasa/index.blade.php ENDPATH**/ ?>