<?php $this->load->view('superadmin/layouts/header'); ?>

<!-- Page Header -->
<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h3 class="mb-1">Master Data Management</h3>
        <p class="text-muted mb-0">View and manage data distribution across portals</p>
    </div>
    <div class="d-flex gap-2">
        <a href="<?= base_url('superadmin/data/assign') ?>" class="btn btn-sa-primary">
            <i class="bi bi-plus-circle me-2"></i>Assign Data
        </a>
        <a href="<?= base_url('superadmin/data/reassign') ?>" class="btn btn-outline-primary">
            <i class="bi bi-arrow-left-right me-2"></i>Reassign Data
        </a>
    </div>
</div>

<!-- Flash Messages -->
<?php if($this->session->flashdata('success')): ?>
<div class="alert alert-success alert-dismissible fade show" role="alert">
    <i class="bi bi-check-circle me-2"></i><?= $this->session->flashdata('success') ?>
    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>

<?php if($this->session->flashdata('error')): ?>
<div class="alert alert-danger alert-dismissible fade show" role="alert">
    <i class="bi bi-exclamation-circle me-2"></i><?= $this->session->flashdata('error') ?>
    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>

<!-- Stats Cards -->
<div class="row mb-4">
    <div class="col-md-4">
        <div class="stat-card">
            <div class="d-flex justify-content-between align-items-center">
                <div>
                    <p class="stat-label mb-1">Total Records</p>
                    <h3 class="stat-value mb-0"><?= number_format($total_data ?? 0) ?></h3>
                </div>
                <div class="stat-icon bg-gradient-primary">
                    <i class="bi bi-database-fill"></i>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="stat-card">
            <div class="d-flex justify-content-between align-items-center">
                <div>
                    <p class="stat-label mb-1">Assigned</p>
                    <h3 class="stat-value mb-0"><?= number_format(($total_data ?? 0) - ($unassigned_data ?? 0)) ?></h3>
                </div>
                <div class="stat-icon bg-gradient-success">
                    <i class="bi bi-check-circle-fill"></i>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="stat-card">
            <div class="d-flex justify-content-between align-items-center">
                <div>
                    <p class="stat-label mb-1">Unassigned</p>
                    <h3 class="stat-value mb-0"><?= number_format($unassigned_data ?? 0) ?></h3>
                </div>
                <div class="stat-icon bg-gradient-warning">
                    <i class="bi bi-exclamation-circle-fill"></i>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Data Distribution -->
<div class="table-card">
    <div class="card-header">
        <h5><i class="bi bi-pie-chart me-2"></i>Data Distribution by Portal</h5>
    </div>
    <div class="card-body">
        <table class="table table-hover">
            <thead>
                <tr>
                    <th>Portal</th>
                    <th>Portal Code</th>
                    <th>Records Assigned</th>
                    <th>Percentage</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php if(!empty($data_stats)): ?>
                <?php foreach($data_stats as $stat): ?>
                <tr>
                    <td>
                        <strong><?= htmlspecialchars($stat->name) ?></strong>
                    </td>
                    <td>
                        <code><?= htmlspecialchars($stat->code) ?></code>
                    </td>
                    <td>
                        <span class="badge bg-primary"><?= number_format($stat->data_count) ?></span>
                    </td>
                    <td>
                        <?php 
                        $percentage = $total_data > 0 ? round(($stat->data_count / $total_data) * 100, 1) : 0;
                        ?>
                        <div class="progress" style="height: 20px; width: 200px;">
                            <div class="progress-bar bg-success" role="progressbar" style="width: <?= $percentage ?>%">
                                <?= $percentage ?>%
                            </div>
                        </div>
                    </td>
                    <td>
                        <div class="btn-group btn-group-sm">
                            <a href="<?= base_url('superadmin/data/view/' . $stat->code) ?>" class="btn btn-outline-info" title="View Data">
                                <i class="bi bi-eye"></i>
                            </a>
                            <a href="<?= base_url('superadmin/data/unassign/' . $stat->code) ?>" class="btn btn-outline-warning" title="Unassign All" onclick="return confirm('Are you sure you want to unassign all data from this portal?')">
                                <i class="bi bi-x-circle"></i>
                            </a>
                        </div>
                    </td>
                </tr>
                <?php endforeach; ?>
                <?php else: ?>
                <tr>
                    <td colspan="5" class="text-center py-4 text-muted">
                        <i class="bi bi-database fs-1 d-block mb-2"></i>
                        No data distribution found
                    </td>
                </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<?php $this->load->view('superadmin/layouts/footer'); ?>
