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

$pid = $_GET['pid'];


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


if(isset($_POST['save'])){

$diag = $_POST['diagnosis'];
$adv  = $_POST['advice'];

mysqli_query($conn,"INSERT INTO prescriptions(patient_id,diagnosis,advice)
VALUES('$pid','$diag','$adv')");

$pres_id = mysqli_insert_id($conn);

for($i=0;$i<count($_POST['medicine']);$i++){

$m  = $_POST['medicine'][$i];
$d  = $_POST['dosage'][$i];
$f  = $_POST['frequency'][$i];
$du = $_POST['duration'][$i];

mysqli_query($conn,"INSERT INTO prescription_items
(prescription_id,medicine_name,dosage,frequency,duration)
VALUES('$pres_id','$m','$d','$f','$du')");
}

$today=date("Y-m-d");

$r=mysqli_query($conn,"SELECT MAX(token_no) AS t 
FROM opd_tokens WHERE visit_date='$today'");
$d=mysqli_fetch_assoc($r);
$token = $d['t'] + 1;

mysqli_query($conn,
"INSERT INTO opd_tokens(patient_id,token_no,visit_date)
VALUES('$pid','$token','$today')");


header("Location: print_prescription.php?id=$pres_id");
exit();
}
?>

<!DOCTYPE html>
<html>
<head>
<title>Create Prescription</title>

<style>

body{
font-family:Arial;
background:#f4f6f9;
}

.med-row{
background:white;
padding:10px;
border-radius:8px;
margin-bottom:10px;
display:flex;
gap:10px;
flex-wrap:wrap;
}

.med-row input{
width:100%;
padding:8px;
border:1px solid #ccc;
border-radius:5px;
}

.col{
flex:1;
min-width:45%;
position:relative;
}

.remove-btn{
background:red;
color:white;
border:none;
padding:8px;
border-radius:5px;
width:100%;
}

.add-btn{
background:#007bff;
color:white;
padding:10px;
border:none;
border-radius:6px;
}

.save-btn{
background:#28a745;
color:white;
padding:10px;
border:none;
border-radius:6px;
width:100%;
}

.suggest-box{
position:absolute;
background:white;
border:1px solid #ccc;
width:100%;
max-height:150px;
overflow-y:auto;
z-index:9999;
}

.suggest-item{
padding:8px;
border-bottom:1px solid #eee;
cursor:pointer;
}

.suggest-item:hover{
background:#007bff;
color:white;
}

</style>
</head>

<body>

<h3>Create Prescription</h3>

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

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

<h4>Patient Information</h4>

<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>




<form method="post">

<label>Diagnosis</label>
<textarea name="diagnosis" required style="width:100%;height:70px;"></textarea>

<br><br>

<div id="medblock">

<!-- MEDICINE ROW -->
<div class="med-row">

<div class="col">
<input type="text"
name="medicine[]"
placeholder="Search Medicine"
class="med-input"
onkeyup="searchMed(this)"
autocomplete="off"
required>
<div class="suggest-box"></div>
</div>

<div class="col">
<input name="dosage[]" placeholder="Dosage" required>
</div>

<div class="col">
<input name="frequency[]" placeholder="Frequency" required>
</div>

<div class="col">
<input name="duration[]" placeholder="Duration" required>
</div>

<div class="col">
<button type="button" class="remove-btn" onclick="removeRow(this)">X</button>
</div>

</div>
<!-- END ROW -->

</div>

<br>

<button type="button" class="add-btn" onclick="addRow()">+ Add Medicine</button>

<br><br>

<label>Advice</label>
<textarea name="advice" style="width:100%;height:70px;"></textarea>

<br><br>

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

</form>

<script>

let currentInput = null;

function searchMed(input){
currentInput = input;

let q = input.value;
let box = input.parentElement.querySelector(".suggest-box");

if(q.length<1){
 box.innerHTML="";
 return;
}

fetch("search_medicine.php?q="+q)
.then(res=>res.text())
.then(data=>{
 box.innerHTML=data;
});
}

function selectMed(div){
if(currentInput){
 currentInput.value = div.innerText.trim();
 currentInput.parentElement.querySelector(".suggest-box").innerHTML="";
}
}

function addRow(){
let block=document.getElementById("medblock");
let first=block.querySelector(".med-row");
let newRow=first.cloneNode(true);
newRow.querySelectorAll("input").forEach(i=>i.value="");
newRow.querySelector(".suggest-box").innerHTML="";
block.appendChild(newRow);
}

function removeRow(btn){
let rows=document.querySelectorAll(".med-row");
if(rows.length>1){
 btn.closest(".med-row").remove();
}
}

</script>

</body>
</html>