<?php
include '../config.php';
session_start();

$userid = isset($_SESSION['userid']) ? $_SESSION['userid'] : 'Not set';
$username = isset($_SESSION['username']) ? $_SESSION['username'] : '';
$is_admin = ($username === 'admin');
$message = "";
$edit_mode = false;
$edit_id = 0;

// Filters
$limit = 20;
$page = (isset($_GET['page']) && is_numeric($_GET['page']) && $_GET['page'] > 0) ? intval($_GET['page']) : 1;
$offset = ($page - 1) * $limit;

// Main query with JOIN to get customer details
$sql2 = "SELECT p.*, c.customer_name, c.company_name, c.mobile, c.location
         FROM proposals p
         LEFT JOIN tb_positiveleads c ON p.lead_id = c.id
         ORDER BY p.id DESC
         LIMIT " . intval($limit) . " OFFSET " . intval($offset);

$result2 = mysqli_query($conn, $sql2);
$proposal_rows = array();
if ($result2 && mysqli_num_rows($result2) > 0) {
    while ($row = mysqli_fetch_assoc($result2)) {
        $proposal_rows[] = $row;
    }
}

// Count total records for pagination
$count_sql = "SELECT COUNT(*) as total FROM proposals";
$total_result = mysqli_query($conn, $count_sql);
$total_row = mysqli_fetch_assoc($total_result);
$total_records = $total_row['total'];
$total_pages = ceil($total_records / $limit);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Positives Programers International</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="author" content="DexignLab">
<meta name="robots" content="index, follow">
<meta name="format-detection" content="telephone=no">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" type="image/png" href="assets/images/favicon.png">
<link href="assets/vendor/bootstrap-select/dist/css/bootstrap-select.min.css" rel="stylesheet">
<link href="assets/vendor/bootstrap-datepicker-master/css/bootstrap-datepicker.min.css" rel="stylesheet">
<link href="assets/vendor/owl-carousel/owl.carousel.css" rel="stylesheet">
<link href="assets/css/jquery.localizationTool.css" rel="stylesheet">
<link class="main-css" href="assets/css/style.css" rel="stylesheet">
</head>
<body>
<div id="preloader"><div class="lds-ripple"><div></div><div></div></div></div>
<div id="main-wrapper">
<?php include "header.php"; ?>
<div class="content-body">
<div class="container-fluid">
<div class="row">
<div class="col-xl-12">

<div class="row">
<div class="table-responsive">
<table class="table table-hover table-responsive-sm table-small">
<thead>
<tr>
<th>#</th>
<th>Company</th>
<th>Customer Name</th>
<th>Mobile No.</th>
<th>Location</th>
<th>Proposal No</th>
<th>BDM</th>
<th>Quotation By</th>
<th>Total</th>
<th>Discount</th>
<th>Proposal Date</th>
<th style="text-align:left!important;">Action</th>
</tr>
</thead>
<tbody>
<?php if (!empty($proposal_rows)): ?>
<?php foreach ($proposal_rows as $index => $row): ?>
<tr>
<td><?php echo ($offset + $index + 1); ?></td>
<td><?php echo htmlspecialchars($row['company_name']); ?></td>
<td><?php echo htmlspecialchars($row['customer_name']); ?></td>
<td><?php echo htmlspecialchars($row['mobile']); ?></td>
<td><?php echo htmlspecialchars($row['location']); ?></td>
<td><?php echo htmlspecialchars($row['proposal_no']); ?></td>
<td><?php echo htmlspecialchars($row['bdm']); ?></td>
<td><?php echo htmlspecialchars($row['qtnby']); ?></td>
<td><?php echo htmlspecialchars($row['total']); ?></td>
<td><?php echo htmlspecialchars($row['discount']); ?></td>
<td><?php echo !empty($row['created_at']) ? date('d/m/Y', strtotime($row['created_at'])) : 'N/A'; ?></td>
<td style="text-align:center;">
    <span style="gap:2px;width:100%;text-align:center;" class="d-flex justify-content-center">
        <a href="all-proposal-form.php?orderid=<?php echo $row['id']; ?>" title="View Proposal" style="color:#007bff;">
            <i class="fa fa-eye"></i>
        </a>
    </span>
</td>

</tr>
<?php endforeach; ?>
<?php else: ?>
<tr><td colspan="12" style="text-align:center;">No proposals found.</td></tr>
<?php endif; ?>
</tbody>
</table>

<nav aria-label="Page navigation">
<ul class="pagination">
<?php for($p = 1; $p <= $total_pages; $p++): ?>
<li class="page-item <?php echo ($p == $page) ? 'active' : ''; ?>">
<a class="page-link" href="?page=<?php echo $p; ?>"><?php echo $p; ?></a>
</li>
<?php endfor; ?>
</ul>
</nav>

</div>
</div>
</div>
</div>
</div>
<?php include "footer.php"; ?>
</div>

<script src="assets/vendor/global/global.min.js"></script>
<script src="assets/vendor/bootstrap-select/dist/js/bootstrap-select.min.js"></script>
<script src="assets/vendor/bootstrap-datepicker-master/js/bootstrap-datepicker.min.js"></script>
<script src="assets/vendor/apexchart/apexchart.js"></script>
<script src="assets/vendor/chartjs/chart.bundle.min.js"></script>
<script src="assets/vendor/peity/jquery.peity.min.js"></script>
<script src="assets/js/dashboard/dashboard-1.js"></script>
<script src="assets/vendor/owl-carousel/owl.carousel.js"></script>
<script src="assets/js/jquery.localizationTool.js"></script>
<script src="assets/js/translator.js"></script>
<script src="assets/js/custom.min.js"></script>
<script src="assets/js/dlabnav-init.js"></script>
<script src="assets/js/demo.js"></script>
</body>
</html>
<?php mysqli_close($conn); ?>
