<?php
session_start();
include "config.php";
include "header.php";

if(!isset($_SESSION['admin'])){
    header("Location:index.php");
    exit();
}

// Add medicine
if(isset($_POST['save'])){
    $name = $_POST['medicine_name'];
    $strength = $_POST['strength'];
    $type = $_POST['type'];

    mysqli_query($conn,"INSERT INTO medicines(medicine_name,strength,type)
    VALUES('$name','$strength','$type')");
}
?>

<h2>Medicine Master</h2>

<hr>

<h3>Add Medicine</h3>

<form method="post">
    Medicine Name:<br>
    <input type="text" name="medicine_name" required><br><br>

    Strength (eg: 500mg):<br>
    <input type="text" name="strength"><br><br>

    Type:<br>
    <select name="type">
        <option>Tablet</option>
        <option>Syrup</option>
        <option>Injection</option>
        <option>Cream</option>
        <option>Drop</option>
    </select><br><br>

    <button type="submit" name="save" class="btn btn-success">Save Medicine</button>
</form>

<hr>

<h3>Medicine List</h3>

<table border="1" cellpadding="8">
<tr>
    
    <th>Name</th>
    <th>Strength</th>
    <th>Type</th>
</tr>

<?php
$q = mysqli_query($conn,"SELECT * FROM medicines ORDER BY medicine_name");

while($row = mysqli_fetch_assoc($q)){
    echo "<tr>
    
        <td>{$row['medicine_name']}</td>
        <td>{$row['strength']}</td>
        <td>{$row['type']}</td>
    </tr>";
}
?>

</table>
