File "manage_rickshaws.php"

Full path: /home/itsevak/public_html/prepaiddev.itsevak.com/dashboard/manage_rickshaws.php
File size: 14.71 B (14.71 KB bytes)
MIME-type: text/x-php
Charset: utf-8

Download   Open   Edit   Advanced Editor   Back

<?php
require_once '../includes/functions.php';

// Require login and auto owner access
requireUserType('auto_owner');

$db = Database::getInstance();

// Get all rickshaws for the current user
$rickshaws = $db->fetchAll(
    "SELECT * FROM auto_rickshaws WHERE owner_id = ? ORDER BY created_at DESC", 
    [$_SESSION['user_id']]
);

$pageTitle = 'Manage Rickshaws';
require_once '../includes/header.php';
?>

<div class="container py-4">
    <div class="row mb-4">
        <div class="col-12">
            <div class="d-flex justify-content-between align-items-center">
                <h2 class="mb-0">
                    <i class="bi bi-truck me-2"></i>My Auto Rickshaws
                </h2>
                <a href="<?php echo url_for('dashboard/add_rickshaw.php'); ?>" class="btn btn-primary">
                    <i class="bi bi-plus-circle me-2"></i>Add New Rickshaw
                </a>
            </div>
        </div>
    </div>
    
    <?php if (empty($rickshaws)): ?>
        <!-- Empty State -->
        <div class="row">
            <div class="col-12">
                <div class="card">
                    <div class="card-body text-center py-5">
                        <i class="bi bi-truck display-1 text-muted mb-3"></i>
                        <h4>No Rickshaws Found</h4>
                        <p class="text-muted mb-4">You haven't added any auto rickshaws yet. Get started by adding your first rickshaw.</p>
                        <a href="<?php echo url_for('dashboard/add_rickshaw.php'); ?>" class="btn btn-primary">
                            <i class="bi bi-plus-circle me-2"></i>Add Your First Rickshaw
                        </a>
                    </div>
                </div>
            </div>
        </div>
    <?php else: ?>
        <!-- Rickshaws List -->
        <div class="row">
            <div class="col-12">
                <div class="card">
                    <div class="card-body">
                        <div class="table-responsive">
                            <table class="table table-hover">
                                <thead>
                                    <tr>
                                        <th>Number Plate</th>
                                        <th>Local ID</th>
                                        <th>Status</th>
                                        <th>Documents</th>
                                        <th>QR Code</th>
                                        <th>Added On</th>
                                        <th>Actions</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($rickshaws as $rickshaw): ?>
                                        <tr data-rickshaw-id="<?php echo $rickshaw['id']; ?>">
                                            <td>
                                                <strong><?php echo htmlspecialchars($rickshaw['number_plate']); ?></strong>
                                            </td>
                                            <td><?php echo htmlspecialchars($rickshaw['unique_local_id']); ?></td>
                                            <td><?php echo getStatusBadge($rickshaw['status']); ?></td>
                                            <td>
                                                <?php if ($rickshaw['document_photo_1'] || $rickshaw['document_photo_2'] || $rickshaw['document_photo_3']): ?>
                                                    <button type="button" class="btn btn-sm btn-outline-info" 
                                                            onclick="viewRickshawDocuments(<?php echo $rickshaw['id']; ?>, <?php echo htmlspecialchars(json_encode(array_filter([$rickshaw['document_photo_1'], $rickshaw['document_photo_2'], $rickshaw['document_photo_3']]))); ?>)">
                                                        <i class="bi bi-images me-1"></i>View Documents
                                                    </button>
                                                <?php else: ?>
                                                    <span class="text-muted">No documents</span>
                                                <?php endif; ?>
                                            </td>
                                            <td>
                                                <button type="button" class="btn btn-sm btn-outline-success" 
                                                        onclick="generateQRCodeForRickshaw('<?php echo htmlspecialchars($rickshaw['number_plate']); ?>', '<?php echo htmlspecialchars($rickshaw['unique_local_id']); ?>', <?php echo $rickshaw['id']; ?>)">
                                                    <i class="bi bi-qr-code me-1"></i>Generate QR
                                                </button>
                                            </td>
                                            <td><?php echo formatDate($rickshaw['created_at']); ?></td>
                                            <td>
                                                <div class="btn-group" role="group">
                                                    <a href="<?php echo url_for('dashboard/view_rickshaw.php'); ?>?id=<?php echo $rickshaw['id']; ?>" 
                                                       class="btn btn-sm btn-outline-primary" title="View Details">
                                                        <i class="bi bi-eye"></i>
                                                    </a>
                                                    <?php if ($rickshaw['status'] === 'pending_approval'): ?>
                                                        <button type="button" class="btn btn-sm btn-outline-warning" 
                                                                title="Pending Approval" disabled>
                                                            <i class="bi bi-clock"></i>
                                                        </button>
                                                    <?php endif; ?>
                                                </div>
                                            </td>
                                        </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        

        
        <!-- JavaScript Functions -->
        <script>
        // Set base path for document viewer
        window.APP_BASE_PATH = '<?php echo url_for(''); ?>';
        window.UPLOADS_PATH = '<?php echo url_for(''); ?>uploads/';
        
        // Check if required libraries are loaded
        document.addEventListener('DOMContentLoaded', function() {
            // Verify Bootstrap Modal availability
            if (typeof bootstrap === 'undefined' || typeof bootstrap.Modal === 'undefined') {
                console.error('Bootstrap Modal is not available');
            }
        });
        function viewRickshawDocuments(rickshawId, documents) {
            // Filter out empty documents
            documents = documents.filter(doc => doc && doc.trim() !== '');
            
            if (documents.length === 0) {
                alert('No documents available for this rickshaw.');
                return;
            }
            
            // Get the rickshaw number plate for display
            const rickshawRow = document.querySelector(`tr[data-rickshaw-id="${rickshawId}"]`);
            const numberPlate = rickshawRow ? rickshawRow.querySelector('td:first-child strong').textContent : 'Rickshaw';
            
            // Initialize the enhanced document viewer
            if (typeof DocumentViewer !== 'undefined') {
                DocumentViewer.init(rickshawId, documents, numberPlate);
            } else {
                // Fallback to simple modal if DocumentViewer is not available
                showSimpleDocumentModal(documents, numberPlate);
            }
        }
        
        // Fallback function for when DocumentViewer is not available
        function showSimpleDocumentModal(documents, rickshawPlate) {
            
            const modalHTML = `
                <div class="modal fade" id="simpleDocModal" tabindex="-1">
                    <div class="modal-dialog modal-xl">
                        <div class="modal-content">
                            <div class="modal-header">
                                <h5 class="modal-title">
                                    <i class="bi bi-images me-2"></i>
                                    Documents - ${rickshawPlate}
                                </h5>
                                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                            </div>
                            <div class="modal-body">
                                <div class="row">
                                    ${documents.map((doc, index) => {
                                        const imagePath = window.UPLOADS_PATH + doc;
                                        return `
                                        <div class="col-md-6 mb-3">
                                            <div class="text-center">
                                                <img src="${imagePath}" class="img-fluid rounded border" alt="Document ${index + 1}" style="max-height: 400px; object-fit: contain;" onerror="console.error('Image failed to load:', this.src);">
                                                <p class="mt-2 mb-0">
                                                    <small class="text-muted">Document ${index + 1}</small>
                                                </p>
                                            </div>
                                        </div>
                                        `;
                                    }).join('')}
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            `;
            
            // Remove existing modal if any
            const existingModal = document.getElementById('simpleDocModal');
            if (existingModal) {
                existingModal.remove();
            }
            
            // Add modal to body
            document.body.insertAdjacentHTML('beforeend', modalHTML);
            
            // Show modal
            const modalElement = document.getElementById('simpleDocModal');
            
            if (modalElement) {
                const modal = new bootstrap.Modal(modalElement);
                modal.show();
            } else {
                console.error('Modal element not found!');
            }
        }
        </script>
        
        <!-- QR Code Modal -->
        <div class="modal fade" id="qrModal" tabindex="-1">
            <div class="modal-dialog modal-sm">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title">QR Code - <span id="modalRickshawPlate"></span></h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                    </div>
                    <div class="modal-body text-center">
                        <div class="qr-code-container">
                            <div id="modalQrCode"></div>
                            <p class="mt-3 mb-0">
                                <small class="text-muted">
                                    Scan this QR code to verify the rickshaw details
                                </small>
                            </p>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-primary" onclick="downloadModalQRCode()">
                            <i class="bi bi-download me-2"></i>Download QR Code
                        </button>
                    </div>
                </div>
            </div>
        </div>
        
    <?php endif; ?>
</div>

<script>
function generateQRCodeForRickshaw(numberPlate, uniqueLocalId, rickshawId) {
    // Set modal title
    document.getElementById('modalRickshawPlate').textContent = numberPlate;
    
    // Clear previous QR code
    const modalQrCode = document.getElementById('modalQrCode');
    modalQrCode.innerHTML = '';
    
    // Create QR data dynamically (no sensitive information)
    const qrCodeData = {
        type: 'auto_rickshaw',
        number_plate: numberPlate,
        unique_local_id: uniqueLocalId,
        generated_at: new Date().toISOString()
    };
    
    try {
        // Use jquery.qrcode.js to generate the QR code
        $(modalQrCode).qrcode({
            text: JSON.stringify(qrCodeData),
            width: 200,
            height: 200,
            ecLevel: 'H', // Error correction level
            render: 'canvas' // Use canvas for better performance
        });
        
        // Show the modal
        const modal = new bootstrap.Modal(document.getElementById('qrModal'));
        modal.show();
        
    } catch (error) {
        console.error('Error generating QR code:', error);
        alert('Error generating QR code. Please try again.');
    }
}

function downloadModalQRCode() {
    const modalQrCode = document.getElementById('modalQrCode');
    const rickshawPlate = document.getElementById('modalRickshawPlate').textContent;
    const fileName = `${rickshawPlate}_qr_code.png`;

    if (!modalQrCode || !modalQrCode.querySelector('canvas')) {
        alert('QR code not generated yet. Please try again.');
        return;
    }

    try {
        const canvas = modalQrCode.querySelector('canvas');
        
        // Create a download link
        const link = document.createElement('a');
        link.download = fileName;
        
        // Convert canvas to blob and create URL
        canvas.toBlob(function(blob) {
            const url = URL.createObjectURL(blob);
            link.href = url;
            
            // Trigger download
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
            
            // Clean up the URL
            URL.revokeObjectURL(url);
        }, 'image/png');
        
    } catch (error) {
        console.error('Error downloading QR code:', error);
        alert('Error downloading QR code. Please try again.');
    }
}
</script>

<?php require_once '../includes/footer.php'; ?>