<?php
include "config.php";
include "header.php";

/* ============================
   GET PATIENT ID
============================ */
if(!isset($_GET['id'])){
    die("Patient ID Missing");
}

$pid = $_GET['id'];

$patient = mysqli_query($conn,"SELECT * FROM patients WHERE id='$pid'");
$p = mysqli_fetch_assoc($patient);

/* ============================
   SAVE PRESCRIPTION
============================ */
if(isset($_POST['save']))
{
    $diagnosis = $_POST['diagnosis'];
    $date = date("Y-m-d");

    mysqli_query($conn,
    "INSERT INTO prescriptions(patient_id,diagnosis,created_at)
     VALUES('$pid','$diagnosis','$date')");

    $prescription_id = mysqli_insert_id($conn);

    foreach($_POST['medicine'] as $key=>$value)
    {
        $med = $_POST['medicine'][$key];
        $dose = $_POST['dose'][$key];
        $days = $_POST['days'][$key];

        mysqli_query($conn,
        "INSERT INTO prescription_medicines
        (prescription_id,medicine_name,dose,days)
        VALUES('$prescription_id','$med','$dose','$dose','$days')");
    }

    echo "<script>alert('Prescription Saved Successfully');</script>";
}
?>

<style>
@media(max-width:768px){
table{font-size:13px;white-space:nowrap;}
}
</style>

<div class="container mt-3">

<!-- ================= PATIENT INFO CARD ================= -->

<div class="card mb-3">
<div class="card-body">

<h4>Patient Information</h4>

<div class="table-responsive">
<table class="table table-bordered">

<tr>
<th>Name</th>
<td><?php echo $p['name']; ?></td>

<th>Age</th>
<td><?php echo $p['age']; ?></td>
</tr>

<tr>
<th>Gender</th>
<td><?php echo $p['gender']; ?></td>

<th>Mobile</th>
<td><?php echo $p['mobile']; ?></td>
</tr>

<tr>
<th>Address</th>
<td colspan="3"><?php echo $p['address']; ?></td>
</tr>

</table>
</div>

</div>
</div>

<!-- ================= PRESCRIPTION FORM ================= -->

<div class="card">
<div class="card-body">

<h4>New Prescription</h4>

<form method="post">

<div class="mb-3">
<label>Diagnosis</label>
<textarea name="diagnosis" class="form-control" required></textarea>
</div>

<div class="table-responsive">
<table class="table table-bordered" id="medTable">

<tr>
<th>Medicine</th>
<th>Dose</th>
<th>Days</th>
<th>Action</th>
</tr>

<tr>
<td><input type="text" name="medicine[]" class="form-control" required></td>
<td><input type="text" name="dose[]" class="form-control" required></td>
<td><input type="text" name="days[]" class="form-control" required></td>
<td>
<button type="button" class="btn btn-success" onclick="addRow()">+</button>
</td>
</tr>

</table>
</div>

<button type="submit" name="save" class="btn btn-primary">
Save Prescription
</button>

</form>

</div>
</div>

</div>

<script>
function addRow(){
  let table = document.getElementById("medTable");
  let row = table.insertRow(-1);
  row.innerHTML = `
  <td><input type="text" name="medicine[]" class="form-control" required></td>
  <td><input type="text" name="dose[]" class="form-control" required></td>
  <td><input type="text" name="days[]" class="form-control" required></td>
  <td>
    <button type="button" class="btn btn-danger" onclick="removeRow(this)">X</button>
  </td>
  `;
}

function removeRow(btn){
  btn.parentElement.parentElement.remove();
}
</script>

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