<?php
include('../db/cn.php');
if($_GET){
	$id = $_GET["id"];
	$rec2 = mysqli_query($connection,"delete from lab_test where id ='$id'");	
	}


if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $test_name = $_POST["test_name"];
    $test_price = $_POST["test_price"];

    // Generate unique test number
    $test_no = generateUniqueTestNo($connection);

    // Insert into database
    $query = "INSERT INTO lab_test (test_name, test_price, test_no) VALUES ('$test_name', '$test_price', '$test_no')";
    if (mysqli_query($connection, $query)) {
        echo "";
    } else {
        echo "Error: " . $query . "<br>" . mysqli_error($connection);
    }
}

function generateUniqueTestNo($connection) {
    $unique = false;
    $test_no = '';
    while (!$unique) {
        $test_no = str_pad(random_int(0, 99999), 5, '0', STR_PAD_LEFT); // Generate a 5-digit number
        $result = mysqli_query($connection, "SELECT COUNT(*) as count FROM lab_test WHERE test_no='$test_no'");
        $row = mysqli_fetch_assoc($result);
        if ($row['count'] == 0) {
            $unique = true;
        }
    }
    return $test_no;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <link rel="apple-touch-icon" sizes="76x76" href="../assets/img/apple-icon.png">
    <link rel="icon" type="image/png" href="../img/print_logo.png">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <title>Laboratory L.I.M.S</title>
    <meta content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0, shrink-to-fit=no' name='viewport' />
    <!-- Fonts and icons -->
    <link href="https://fonts.googleapis.com/css?family=Montserrat:400,700,200" rel="stylesheet" />
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/latest/css/font-awesome.min.css" />
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" rel="stylesheet">
    <!-- CSS Files -->
    <link href="../assets/css/bootstrap.min.css" rel="stylesheet" />
    <link href="../assets/css/light-bootstrap-dashboard.css?v=2.0.0" rel="stylesheet" />
    <!-- CSS Just for demo purpose, don't include it in your project -->
    <link href="../assets/css/demo.css" rel="stylesheet" />
</head>
<style>
    .btn-outline-success {
        background: #511738;
        color: #fff;
    }
	 .btn-success {
        background: #709806;
        color: #fff;
    }
	.btn-danger {
        background: #6C0345;
        color: #fff;
    }
	
    .btn-outline-success:hover {
        background: #fff;
        color: #2C2C2C;
        border: 1px solid #2C2C2C;
    }
    .bg-success {
        background: #2C2C2C;
        color: #fff;
    }
</style>
<body>
    <div class="wrapper">
        <div class="sidebar" data-image="../assets/img/sidebar-7.jpg">
            <?php include("common/sd.php"); ?>
        </div>
        <div class="main-panel">
            <!-- Navbar -->
            <?php include("common/hd.php"); ?>
            <!-- End Navbar -->
            <div class="content">
                <div class="container">
                    <div class="row">
                    <div class="col-sm-4" style=" text-transform:capitalize;text-align:center;
                  border-radius:5px;  background:#7A2355; color:#fff; padding:5px;">
                    <?php // SQL query to count the number of patients
						$sql = "SELECT COUNT(*) AS total_patients FROM lab_test";
						
						// Execute the query
						$result = $connection->query($sql);
						
						if ($result && $result->num_rows > 0) {
							// Fetch the result as an associative array
							$rows = $result->fetch_assoc();
							$totalPatients = $rows["total_patients"];
							echo "Total number of Lab Test we Have: " . $totalPatients;
						} else {
							echo "No patients found";
						}

					
					?>
                    </div>
                    <div class="col-sm-2"></div>
                    <div class="col-sm-3"></div>
                    <div class="col-sm-3"></div>
                    
                        <div class="col-sm-4"><h3>Laboratory Test</h3></div>
                        <div class="col-sm-6"></div>
                        <div class="col-sm-2">
                            <button type="button" class="btn btn-outline-success btn-sm" data-toggle="modal" data-target="#addTestModal">
                            Add New Test  <i class="fas fa-vials"></i></button>
                        </div>
                    </div>
                </div>
                <div class="container">
                    <div class="row">
                        <div class="col-sm-12" style="padding:7px;border:1px #CCC solid; border-radius:7px;
                            box-shadow:0px 0px 0px 0.5px #CCC;">
                            <input type="text" id="search" class="form-control" placeholder="Search by test name">
    <table class="table table-stripped table-sm" style="text-align:center;">
        <thead>
            <tr style="background:#7A2355; color:#fff;">
                <td>Sr#</td>
                <td>ID</td>
                <td>Name</td>
                <td>Amount</td>
                <td>Action</td>
            </tr>
        </thead>
        <tbody id="labTestTable">
            <?php  
            $rec = mysqli_query($connection, "SELECT * FROM lab_test");
            while ($row = mysqli_fetch_array($rec)) {
            ?>
            <tr>
                <td><?php echo $row["id"]; ?></td>
                <td><?php echo $row["test_no"]; ?></td>
                <td><?php echo $row["test_name"]; ?></td>
                <td>Rs <?php echo $row["test_price"]; ?></td>
                <td>
                    <a href="lab.php?id=<?php echo $row["id"];?>"><button type="button" class="btn btn-danger btn-sm">Delete</button></a>
                    <a href="lab_test_edit.php?id=<?php echo $row["id"]; ?>"><button type="button" class="btn btn-success btn-sm">Edit</button></a>
                </td>
            </tr>
            <?php } ?>
        </tbody>
    </table>
                        </div>
                    </div>
                </div>
            </div>
            <footer class="footer">
                <div class="container-fluid"></div>
            </footer>
        </div>
    </div>

    <!-- Modal for Adding New Test -->
    <div class="modal fade" id="addTestModal" tabindex="-1" role="dialog" aria-labelledby="addTestModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="addTestModalLabel">Add New Test</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <form id="addTestForm" method="post" action="lab.php">
                        <div class="form-group">
                            <label for="testName">Test Name</label>
                            <input type="text" class="form-control" id="testName" name="test_name" required>
                        </div>
                       
                        <div class="form-group">
                            <label for="testPrice">Test Price</label>
                            <input type="number" class="form-control" id="testPrice" name="test_price" required>
                        </div>
                        <button type="submit" class="btn btn-outline-success">Add Test</button>
                    </form>
                </div>
            </div>
        </div>
    </div>
<script>
    document.addEventListener('DOMContentLoaded', (event) => {
        const searchInput = document.getElementById('search');
        const table = document.getElementById('labTestTable');
        const rows = table.getElementsByTagName('tr');

        searchInput.addEventListener('keyup', function() {
            const filter = searchInput.value.toLowerCase();
            Array.from(rows).forEach(row => {
                const nameCell = row.getElementsByTagName('td')[2];
                if (nameCell) {
                    const name = nameCell.textContent.toLowerCase();
                    row.style.display = name.includes(filter) ? '' : 'none';
                }
            });
        });
    });
</script>
    <!-- Core JS Files -->
    <script src="../assets/js/core/jquery.3.2.1.min.js" type="text/javascript"></script>
    <script src="../assets/js/core/popper.min.js" type="text/javascript"></script>
    <script src="../assets/js/core/bootstrap.min.js" type="text/javascript"></script>
    <!-- Plugin for Switches, full documentation here: http://www.jque.re/plugins/version3/bootstrap.switch/ -->
    <script src="../assets/js/plugins/bootstrap-switch.js"></script>
    <!-- Chartist Plugin -->
    <script src="../assets/js/plugins/chartist.min.js"></script>
    <!-- Notifications Plugin -->
    <script src="../assets/js/plugins/bootstrap-notify.js"></script>
    <!-- Control Center for Light Bootstrap Dashboard: scripts for the example pages etc -->
    <script src="../assets/js/light-bootstrap-dashboard.js?v=2.0.0" type="text/javascript"></script>
    <!-- Light Bootstrap Dashboard DEMO methods, don't include it in your project! -->
    <script src="../assets/js/demo.js"></script>
    <script type="text/javascript">
        // Additional JavaScript if needed
    </script>
</body>
</html>