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



$user_id = $_SESSION['user_id'];

/* ================= ADD ASSIGNMENT ================= */
if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $doctor_id = $_POST['doctor_id'];
    $product_id = $_POST['product_id'];
    $meeting_date = $_POST['meeting_date'];
    $follow_up_date = $_POST['follow_up_date'];
    $notes = $_POST['notes'];

    $stmt = $conn->prepare("
        INSERT INTO doctor_assignments 
        (doctor_id, product_id, meeting_date, follow_up_date, notes, created_by)
        VALUES (?, ?, ?, ?, ?, ?)
    ");

    $stmt->bind_param("iisssi", $doctor_id, $product_id, $meeting_date, $follow_up_date, $notes, $user_id);
    $stmt->execute();
}

/* ================= FETCH DATA ================= */

// doctors
$doctors = $conn->query("SELECT id, name FROM doctors");

// products
$products = $conn->query("
    SELECT id, product_name 
    FROM sample_products
");

// assignments
$assignments = $conn->query("
    SELECT da.*, d.name AS doctor_name, p.product_name 
    FROM doctor_assignments da
    JOIN doctors d ON da.doctor_id = d.id
    JOIN products p ON da.product_id = p.id
    ORDER BY da.id DESC
");
?>

<!DOCTYPE html>
<html>
<head>
    <title>Assignments & Meetings</title>
    <style>
  body {
    font-family: 'Inter', Arial;
    background: #f4f7fb;
    color: #1e293b;
}

/* CONTAINER */
.container {
    margin-left: 260px;
    padding: 40px 20px 20px;
}

/* TITLE */
h2 {
    margin-bottom: 16px;
    font-weight: 700;
    font-size: 22px;
    color: #0f172a;
}

/* CARD */
.card {
    background: #ffffff;
    border-radius: 14px;
    padding: 16px;
    margin-bottom: 18px;
    box-shadow: 0 8px 24px rgba(2,6,23,.08);
    border: 1px solid #e2e8f0;
}

/* CARD HEADINGS */
.card h3{
    margin-bottom: 12px;
    font-size:16px;
}

/* FORM GRID */
.form-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
}

/* FULL WIDTH */
.full {
    grid-column: span 2;
}

/* LABEL */
label {
    font-size: 12px;
    font-weight: 600;
    color:#334155;
}

/* INPUTS */
input, select, textarea {
    width: 100%;
    padding: 9px 10px;
    border-radius: 8px;
    border: 1px solid #dbe4ee;
    background: #fff;
    font-size:13px;
    outline: none;
    transition: 0.2s;
}

input:focus, select:focus, textarea:focus {
    border-color: #2563eb;
    box-shadow: 0 0 0 2px rgba(37,99,235,.15);
}

/* BUTTON */
button {
    background: #374155;
    color: #fff;
    padding: 10px;
    border: none;
    border-radius: 10px;
    cursor: pointer;
    font-weight: 600;
    font-size:13px;
}

button:hover {
    opacity:0.9;
}

/* TABLE */
table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 8px;
    border-radius: 12px;
    overflow: hidden;
}

/* TABLE HEADER */
th {
    text-align: left;
    background: #334155;
    padding: 10px;
    font-size: 13px;
    color: #fff;
}

/* TABLE DATA */
td {
    padding: 10px;
    border-bottom: 1px solid #eef2f7;
    font-size: 13px;
}

/* ROW HOVER */
tr:hover td {
    background: #f8fafc;
}

/* RESPONSIVE */
@media(max-width:768px){
    .container {
        margin-left:0;
        padding: 16px 12px;
    }

    .form-grid {
        grid-template-columns: 1fr;
    }

    .full {
        grid-column: span 1;
    }
}
    </style>
</head>
<body>

<?php include 'salesexecutivesidebar.php'; ?>
<?php include '../header.php';?>

<div class="container">

    <h2>Assignments & Meetings</h2>

    <!-- ================= ADD FORM ================= -->
    <div class="card">
        <h3>Add Assignment</h3>

       <form method="POST" class="form-grid">

    <div>
        <label>Doctor</label>
        <select name="doctor_id" required>
            <option value="">Select Doctor</option>
            <?php while($d = $doctors->fetch_assoc()) { ?>
                <option value="<?= $d['id'] ?>"><?= $d['name'] ?></option>
            <?php } ?>
        </select>
    </div>

    <div>
        <label>Product</label>
        <select name="product_id" required>
            <option value="">Select Product</option>
            <?php while($p = $products->fetch_assoc()) { ?>
                <option value="<?= $p['id'] ?>"><?= $p['product_name'] ?></option>
            <?php } ?>
        </select>
    </div>

    <div>
        <label>Meeting Date</label>
        <input type="date" name="meeting_date">
    </div>

    <div>
        <label>Follow Up Date</label>
        <input type="date" name="follow_up_date">
    </div>

    <div class="full">
        <label>Notes</label>
        <textarea name="notes" rows="3"></textarea>
    </div>

    <div class="full">
        <button type="submit">Save Assignment</button>
    </div>

</form>
    </div>

    <!-- ================= LIST ================= -->
    <div class="card">
        <h3>All Assignments</h3>

        <table>
            <tr>
                <th>Doctor</th>
                <th>Product</th>
                <th>Meeting</th>
                <th>Follow-up</th>
                <th>Notes</th>
            </tr>

            <?php while($row = $assignments->fetch_assoc()) { ?>
            <tr>
                <td><?= $row['doctor_name'] ?></td>
                <td><?= $row['product_name'] ?></td>
                <td><?= $row['meeting_date'] ?></td>
                <td><?= $row['follow_up_date'] ?></td>
                <td><?= $row['notes'] ?></td>
            </tr>
            <?php } ?>

        </table>
    </div>

</div>

</body>
</html>