<?php
require_once '../includes/auth.php';
require_once '../db.php';

// Require at least one report sub-permission
$reportPerms = [
    'reports_sales', 'reports_purchase', 'reports_gst', 'reports_gstr1',
    'reports_profit', 'reports_ledger_customer', 'reports_ledger_vendor',
    'reports_stock', 'reports_consolidated', 'reports_master_profit',
];
$anyReport = false;
foreach ($reportPerms as $rp) {
    if (hasPermission($rp)) { $anyReport = true; break; }
}
if (!$anyReport) {
    http_response_code(403);
    include '../includes/403.php';
    exit;
}

require_once '../includes/header.php';
require_once '../includes/sidebar.php';
?>

<div class="page-header">
    <h2><i class="fas fa-chart-bar" style="color:var(--primary);margin-right:8px;"></i>Reports</h2>
</div>

<div class="report-grid">

    <?php if (hasPermission('reports_sales')): ?>
    <a href="sales_report.php" class="report-card">
        <div class="report-icon"><i class="fas fa-chart-line"></i></div>
        <div class="report-title">Sales Report</div>
        <div class="report-desc">Invoice wise / Customer wise / Date wise</div>
    </a>
    <?php endif; ?>

    <?php if (hasPermission('reports_purchase')): ?>
    <a href="purchase_report.php" class="report-card">
        <div class="report-icon"><i class="fas fa-receipt"></i></div>
        <div class="report-title">Purchase Report</div>
        <div class="report-desc">Vendor wise / Date wise purchases</div>
    </a>
    <?php endif; ?>

    <?php if (hasPermission('reports_gst')): ?>
    <a href="gst_report.php" class="report-card">
        <div class="report-icon"><i class="fas fa-calculator"></i></div>
        <div class="report-title">GST Report</div>
        <div class="report-desc">Tax summary + HSN wise GST breakup</div>
    </a>
    <?php endif; ?>

    <?php if (hasPermission('reports_gstr1')): ?>
    <a href="gstr1_report.php" class="report-card">
        <div class="report-icon"><i class="fas fa-file-invoice"></i></div>
        <div class="report-title">GSTR1 Report</div>
        <div class="report-desc">GSTR1 summary + HSN wise summary</div>
    </a>
    <?php endif; ?>

    <?php if (hasPermission('reports_profit')): ?>
    <a href="profit_report.php" class="report-card">
        <div class="report-icon"><i class="fas fa-chart-pie"></i></div>
        <div class="report-title">Profit Report</div>
        <div class="report-desc">Profit calculation invoice wise / product wise</div>
    </a>
    <?php endif; ?>

    <?php if (hasPermission('reports_ledger_customer')): ?>
    <a href="customer_ledger.php" class="report-card">
        <div class="report-icon"><i class="fas fa-user"></i></div>
        <div class="report-title">Customer Ledger</div>
        <div class="report-desc">Customer transactions + balance</div>
    </a>
    <?php endif; ?>

    <?php if (hasPermission('reports_ledger_vendor')): ?>
    <a href="vendor_ledger.php" class="report-card">
        <div class="report-icon"><i class="fas fa-industry"></i></div>
        <div class="report-title">Vendor Ledger</div>
        <div class="report-desc">Vendor purchases + outstanding balance</div>
    </a>
    <?php endif; ?>

    <?php if (hasPermission('reports_stock')): ?>
    <a href="stock_movement.php" class="report-card">
        <div class="report-icon"><i class="fas fa-boxes"></i></div>
        <div class="report-title">Stock Movement</div>
        <div class="report-desc">IN / OUT / RETURN movement report</div>
    </a>
    <?php endif; ?>

    <?php if (hasPermission('reports_consolidated')): ?>
    <a href="consolidated_profit.php" class="report-card" style="border-left:4px solid #7c3aed;">
        <div class="report-icon"><i class="fas fa-layer-group" style="color:#7c3aed;"></i></div>
        <div class="report-title">Consolidated Profit</div>
        <div class="report-desc">Multi-company combined profit report</div>
    </a>
    <?php endif; ?>

    <?php if (hasPermission('reports_master_profit')): ?>
    <a href="master_profit_report.php" class="report-card" style="border-left:4px solid #0891b2;">
        <div class="report-icon"><i class="fas fa-sitemap" style="color:#0891b2;"></i></div>
        <div class="report-title">Master Sales Profit</div>
        <div class="report-desc">Profit grouped by master invoice across companies</div>
    </a>
    <?php endif; ?>

</div>

<style>
.report-grid{
    display:grid;
    grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
    gap:20px;
    margin-top:20px;
}

.report-card{
    background:#fff;
    border:1px solid #ddd;
    padding:18px;
    border-radius:10px;
    text-decoration:none;
    color:#111;
    transition:0.2s;
    box-shadow:0 2px 6px rgba(0,0,0,0.05);
}

.report-card:hover{
    border-color:#2563eb;
    transform:translateY(-3px);
    box-shadow:0 5px 15px rgba(0,0,0,0.10);
}

.report-icon{
    font-size:32px;
    margin-bottom:10px;
}

.report-title{
    font-size:18px;
    font-weight:bold;
    margin-bottom:6px;
}

.report-desc{
    font-size:13px;
    color:#555;
    line-height:1.4;
}
</style>

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