<?php
$store = isset($store) ? $store : array();
$storeId = isset($storeId) ? $storeId : '';
$all_stores = isset($all_stores) ? $all_stores : array();
$assigned_staff = isset($assigned_staff) ? $assigned_staff : array();
$store_logs = isset($store_logs) ? $store_logs : array();
?>

<div class="container-fluid py-4">
    <div class="d-flex flex-wrap justify-content-between align-items-center mb-4 gap-3">
        <div>
            <h4 class="fw-bold mb-1">
                <i class="fa-solid fa-store text-primary me-2"></i>
                <?php echo htmlspecialchars(isset($store['name']) ? $store['name'] : 'Store Details'); ?>
            </h4>
            <p class="text-muted mb-0">ID: <span class="font-monospace text-dark"><?php echo htmlspecialchars($storeId); ?></span></p>
        </div>
        <div class="d-flex gap-2">
            <!-- Store Selector Dropdown -->
            <div class="dropdown">
                <button class="btn btn-outline-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown">
                    Switch Store
                </button>
                <ul class="dropdown-menu dropdown-menu-end shadow-sm">
                    <?php foreach ($all_stores as $sId => $s): ?>
                        <li>
                            <a class="dropdown-item <?php echo $sId === $storeId ? 'active' : ''; ?>" href="<?php echo base_url('admin/store_details/' . $sId); ?>">
                                <?php echo htmlspecialchars(isset($s['name']) ? $s['name'] : $sId); ?>
                            </a>
                        </li>
                    <?php endforeach; ?>
                </ul>
            </div>
            <a href="<?php echo base_url('admin/stores'); ?>" class="btn btn-outline-primary">
                <i class="fa-solid fa-list me-1"></i> Store List
            </a>
        </div>
    </div>

    <!-- Store Profile Overview -->
    <div class="row g-4 mb-4">
        <div class="col-lg-4">
            <div class="card border-0 shadow-sm rounded-4 h-100">
                <div class="card-body p-4">
                    <h5 class="fw-bold mb-3 border-bottom pb-2">Branch Information</h5>
                    <div class="mb-3">
                        <small class="text-muted d-block">Store Name</small>
                        <span class="fw-semibold fs-6"><?php echo htmlspecialchars(isset($store['name']) ? $store['name'] : '-'); ?></span>
                    </div>
                    <div class="mb-3">
                        <small class="text-muted d-block">Address</small>
                        <span class="fw-semibold"><?php echo htmlspecialchars(isset($store['address']) ? $store['address'] : '-'); ?></span>
                    </div>
                    <div class="row mb-3">
                        <div class="col-6">
                            <small class="text-muted d-block">City</small>
                            <span class="fw-semibold"><?php echo htmlspecialchars(isset($store['city']) ? $store['city'] : 'Islamabad'); ?></span>
                        </div>
                        <div class="col-6">
                            <small class="text-muted d-block">Country</small>
                            <span class="fw-semibold"><?php echo htmlspecialchars(isset($store['country']) ? $store['country'] : 'Pakistan'); ?></span>
                        </div>
                    </div>
                    <div class="mb-3">
                        <small class="text-muted d-block">GPS Coordinates</small>
                        <span class="font-monospace small bg-light p-2 rounded d-block mt-1">
                            Lat: <?php echo isset($store['lat']) ? $store['lat'] : '-'; ?>, Lng: <?php echo isset($store['lng']) ? $store['lng'] : '-'; ?>
                        </span>
                    </div>
                    <div class="mb-3">
                        <small class="text-muted d-block">Status</small>
                        <?php if (!empty($store['isActive'])): ?>
                            <span class="badge bg-success bg-opacity-10 text-success rounded-pill px-3 py-1 mt-1">Active</span>
                        <?php else: ?>
                            <span class="badge bg-danger bg-opacity-10 text-danger rounded-pill px-3 py-1 mt-1">Inactive</span>
                        <?php endif; ?>
                    </div>
                    <div>
                        <small class="text-muted d-block">Registration Date</small>
                        <span class="text-dark"><?php echo isset($store['createdAt']) ? date('F d, Y h:i A', $store['createdAt'] / 1000) : '-'; ?></span>
                    </div>
                </div>
            </div>
        </div>

        <!-- Assigned Staff in this store -->
        <div class="col-lg-8">
            <div class="card border-0 shadow-sm rounded-4 h-100">
                <div class="card-header bg-transparent border-0 pt-4 px-4 pb-0 d-flex justify-content-between align-items-center">
                    <h5 class="fw-bold mb-0"><i class="fa-solid fa-users text-primary me-2"></i>Assigned Staff (<?php echo count($assigned_staff); ?>)</h5>
                    <a href="<?php echo base_url('admin/add_staff'); ?>" class="btn btn-sm btn-outline-primary"><i class="fa-solid fa-user-plus me-1"></i> Add Staff</a>
                </div>
                <div class="card-body p-4">
                    <?php if (empty($assigned_staff)): ?>
                        <div class="text-center py-5 text-muted">
                            <i class="fa-solid fa-users-slash fs-1 opacity-25 mb-3 d-block"></i>
                            No staff assigned to this store branch yet.
                        </div>
                    <?php else: ?>
                        <div class="row g-3">
                            <?php foreach ($assigned_staff as $stId => $st): ?>
                                <div class="col-md-6">
                                    <div class="card border border-light shadow-none bg-light bg-opacity-50 rounded-3 p-3">
                                        <div class="d-flex align-items-center gap-3">
                                            <img src="<?php echo htmlspecialchars(!empty($st['imageUrl']) ? $st['imageUrl'] : 'https://upload.wikimedia.org/wikipedia/commons/9/9e/Placeholder_Person.jpg'); ?>" class="rounded-circle object-fit-cover" width="48" height="48" alt="Avatar">
                                            <div class="flex-grow-1 min-w-0">
                                                <div class="fw-bold text-dark text-truncate"><?php echo htmlspecialchars(isset($st['fullName']) ? $st['fullName'] : 'Staff'); ?></div>
                                                <div class="small text-muted"><?php echo htmlspecialchars(isset($st['designation']) ? $st['designation'] : 'Staff'); ?> · <span class="badge bg-secondary"><?php echo htmlspecialchars(isset($st['shift']) ? $st['shift'] : 'Morning'); ?></span></div>
                                                <div class="small text-muted mt-1"><i class="fa-solid fa-id-card me-1"></i><?php echo htmlspecialchars(isset($st['staffId']) ? $st['staffId'] : 'N/A'); ?> · <i class="fa-solid fa-phone me-1"></i><?php echo htmlspecialchars(isset($st['phone']) ? $st['phone'] : ''); ?></div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            <?php endforeach; ?>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>

    <!-- Store CheckIn History Logs -->
    <div class="card border-0 shadow-sm rounded-4">
        <div class="card-header bg-transparent border-0 pt-4 px-4 pb-0">
            <h5 class="fw-bold mb-0"><i class="fa-solid fa-clock-rotate-left text-primary me-2"></i>Check-In Submission Logs for this Store</h5>
        </div>
        <div class="card-body p-4">
            <?php if (empty($store_logs)): ?>
                <div class="text-center py-4 text-muted">
                    No check-in logs submitted for this store branch.
                </div>
            <?php else: ?>
                <div class="table-responsive">
                    <table class="table table-hover align-middle mb-0">
                        <thead class="table-light">
                            <tr>
                                <th>Staff Member</th>
                                <th>Schedule ID</th>
                                <th>Submitted Time</th>
                                <th>Tasks Verified</th>
                                <th>Status</th>
                                <th>Evidence Attached</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($store_logs as $logId => $log): ?>
                                <tr>
                                    <td>
                                        <div class="fw-semibold text-dark"><?php echo htmlspecialchars(isset($log['staffId']) ? $log['staffId'] : 'Staff'); ?></div>
                                    </td>
                                    <td class="font-monospace small text-muted">
                                        <?php echo htmlspecialchars(isset($log['scheduleId']) ? $log['scheduleId'] : '-'); ?>
                                    </td>
                                    <td class="small text-muted">
                                        <?php echo isset($log['submittedAt']) ? date('M d, Y h:i A', $log['submittedAt'] / 1000) : '-'; ?>
                                    </td>
                                    <td>
                                        <span class="badge bg-light text-dark border">
                                            <?php echo isset($log['tasks']) ? count($log['tasks']) : 0; ?> Tasks
                                        </span>
                                    </td>
                                    <td>
                                        <span class="badge bg-success bg-opacity-10 text-success rounded-pill px-3 py-1">
                                            <?php echo ucfirst(isset($log['status']) ? $log['status'] : 'completed'); ?>
                                        </span>
                                    </td>
                                    <td>
                                        <?php if (!empty($log['tasks']) && is_array($log['tasks'])): ?>
                                            <div class="d-flex gap-1">
                                                <?php foreach ($log['tasks'] as $t): ?>
                                                    <?php if (!empty($t['mediaUrl'])): ?>
                                                        <a href="<?php echo htmlspecialchars($t['mediaUrl']); ?>" target="_blank" class="btn btn-sm btn-outline-primary p-1" title="<?php echo htmlspecialchars(isset($t['taskName']) ? $t['taskName'] : 'Media'); ?>">
                                                            <i class="fa-solid <?php echo (isset($t['mediaType']) && $t['mediaType'] === 'video') ? 'fa-video' : 'fa-image'; ?>"></i>
                                                        </a>
                                                    <?php endif; ?>
                                                <?php endforeach; ?>
                                            </div>
                                        <?php endif; ?>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            <?php endif; ?>
        </div>
    </div>
</div>
