prepare("INSERT INTO patients (name, phone, dob, gender, blood_group, insurance_company_id, policy_number, address) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"); $stmt->execute([$name, $phone, $dob, $gender, $blood_group, $insurance_company_id, $policy_number, $address]); $_SESSION['flash_message'] = __('add_patient') . ' ' . __('successfully'); $redirect = true; } } elseif ($_POST['action'] === 'edit_patient') { $id = $_POST['id'] ?? ''; $name = $_POST['name'] ?? ''; $phone = $_POST['phone'] ?? ''; $dob = $_POST['dob'] ?: null; $gender = $_POST['gender'] ?? ''; $blood_group = $_POST['blood_group'] ?? ''; $insurance_company_id = $_POST['insurance_company_id'] ?: null; $policy_number = $_POST['policy_number'] ?? ''; $address = $_POST['address'] ?? ''; if ($id && $name) { $stmt = $db->prepare("UPDATE patients SET name = ?, phone = ?, dob = ?, gender = ?, blood_group = ?, insurance_company_id = ?, policy_number = ?, address = ? WHERE id = ?"); $stmt->execute([$name, $phone, $dob, $gender, $blood_group, $insurance_company_id, $policy_number, $address, $id]); $_SESSION['flash_message'] = __('edit_patient') . ' ' . __('successfully'); $redirect = true; } } elseif ($_POST['action'] === 'delete_patient') { $id = $_POST['id'] ?? ''; if ($id) { $stmt = $db->prepare("DELETE FROM patients WHERE id = ?"); $stmt->execute([$id]); $_SESSION['flash_message'] = __('delete') . ' ' . __('successfully'); $redirect = true; } } elseif ($_POST['action'] === 'add_doctor') { $name_en = $_POST['name_en'] ?? ''; $name_ar = $_POST['name_ar'] ?? ''; $tel = $_POST['tel'] ?? ''; $email = $_POST['email'] ?? ''; $spec_en = $_POST['specialization_en'] ?? ''; $spec_ar = $_POST['specialization_ar'] ?? ''; $dept_id = $_POST['department_id'] ?: null; if ($name_en && $name_ar) { $stmt = $db->prepare("INSERT INTO doctors (name_en, name_ar, tel, email, specialization_en, specialization_ar, department_id) VALUES (?, ?, ?, ?, ?, ?, ?)"); $stmt->execute([$name_en, $name_ar, $tel, $email, $spec_en, $spec_ar, $dept_id]); $_SESSION['flash_message'] = __('add_doctor') . ' ' . __('successfully'); $redirect = true; } } elseif ($_POST['action'] === 'edit_doctor') { $id = $_POST['id'] ?? ''; $name_en = $_POST['name_en'] ?? ''; $name_ar = $_POST['name_ar'] ?? ''; $tel = $_POST['tel'] ?? ''; $email = $_POST['email'] ?? ''; $spec_en = $_POST['specialization_en'] ?? ''; $spec_ar = $_POST['specialization_ar'] ?? ''; $dept_id = $_POST['department_id'] ?: null; if ($id && $name_en && $name_ar) { $stmt = $db->prepare("UPDATE doctors SET name_en = ?, name_ar = ?, tel = ?, email = ?, specialization_en = ?, specialization_ar = ?, department_id = ? WHERE id = ?"); $stmt->execute([$name_en, $name_ar, $tel, $email, $spec_en, $spec_ar, $dept_id, $id]); $_SESSION['flash_message'] = __('edit_doctor') . ' ' . __('successfully'); $redirect = true; } } elseif ($_POST['action'] === 'delete_doctor') { $id = $_POST['id'] ?? ''; if ($id) { $stmt = $db->prepare("DELETE FROM doctors WHERE id = ?"); $stmt->execute([$id]); $_SESSION['flash_message'] = __('delete') . ' ' . __('successfully'); $redirect = true; } } elseif ($_POST['action'] === 'add_nurse') { $name_en = $_POST['name_en'] ?? ''; $name_ar = $_POST['name_ar'] ?? ''; $tel = $_POST['tel'] ?? ''; $email = $_POST['email'] ?? ''; $dept_id = $_POST['department_id'] ?: null; if ($name_en && $name_ar) { $stmt = $db->prepare("INSERT INTO nurses (name_en, name_ar, tel, email, department_id) VALUES (?, ?, ?, ?, ?)"); $stmt->execute([$name_en, $name_ar, $tel, $email, $dept_id]); $_SESSION['flash_message'] = __('add_nurse') . ' ' . __('successfully'); $redirect = true; } } elseif ($_POST['action'] === 'edit_nurse') { $id = $_POST['id'] ?? ''; $name_en = $_POST['name_en'] ?? ''; $name_ar = $_POST['name_ar'] ?? ''; $tel = $_POST['tel'] ?? ''; $email = $_POST['email'] ?? ''; $dept_id = $_POST['department_id'] ?: null; if ($id && $name_en && $name_ar) { $stmt = $db->prepare("UPDATE nurses SET name_en = ?, name_ar = ?, tel = ?, email = ?, department_id = ? WHERE id = ?"); $stmt->execute([$name_en, $name_ar, $tel, $email, $dept_id, $id]); $_SESSION['flash_message'] = __('edit_nurse') . ' ' . __('successfully'); $redirect = true; } } elseif ($_POST['action'] === 'delete_nurse') { $id = $_POST['id'] ?? ''; if ($id) { $stmt = $db->prepare("DELETE FROM nurses WHERE id = ?"); $stmt->execute([$id]); $_SESSION['flash_message'] = __('delete') . ' ' . __('successfully'); $redirect = true; } } elseif ($_POST['action'] === 'add_department') { $name_en = $_POST['name_en'] ?? ''; $name_ar = $_POST['name_ar'] ?? ''; if ($name_en && $name_ar) { $stmt = $db->prepare("INSERT INTO departments (name_en, name_ar) VALUES (?, ?)"); $stmt->execute([$name_en, $name_ar]); $_SESSION['flash_message'] = __('add_department') . ' ' . __('successfully'); $redirect = true; } } elseif ($_POST['action'] === 'edit_department') { $id = $_POST['id'] ?? ''; $name_en = $_POST['name_en'] ?? ''; $name_ar = $_POST['name_ar'] ?? ''; if ($id && $name_en && $name_ar) { $stmt = $db->prepare("UPDATE departments SET name_en = ?, name_ar = ? WHERE id = ?"); $stmt->execute([$name_en, $name_ar, $id]); $_SESSION['flash_message'] = __('edit_department') . ' ' . __('successfully'); $redirect = true; } } elseif ($_POST['action'] === 'delete_department') { $id = $_POST['id'] ?? ''; if ($id) { $stmt = $db->prepare("DELETE FROM departments WHERE id = ?"); $stmt->execute([$id]); $_SESSION['flash_message'] = __('delete') . ' ' . __('successfully'); $redirect = true; } } elseif ($_POST['action'] === 'book_appointment') { $patient_id = $_POST['patient_id'] ?? ''; $doctor_id = $_POST['doctor_id'] ?? ''; $date = $_POST['date'] ?? ''; $reason = $_POST['reason'] ?? ''; if ($patient_id && $doctor_id && $date) { $stmt = $db->prepare("INSERT INTO appointments (patient_id, doctor_id, start_time, end_time, reason) VALUES (?, ?, ?, DATE_ADD(?, INTERVAL 30 MINUTE), ?)"); $stmt->execute([$patient_id, $doctor_id, $date, $date, $reason]); $_SESSION['flash_message'] = __('book_appointment') . ' ' . __('successfully'); $redirect = true; } } elseif ($_POST['action'] === 'record_visit') { $patient_id = $_POST['patient_id'] ?? ''; $doctor_id = $_POST['doctor_id'] ?? ''; $appointment_id = $_POST['appointment_id'] ?: null; $weight = $_POST['weight'] ?? ''; $bp = $_POST['blood_pressure'] ?? ''; $hr = $_POST['heart_rate'] ?? ''; $temp = $_POST['temperature'] ?? ''; $symptoms = $_POST['symptoms'] ?? ''; $diagnosis = $_POST['diagnosis'] ?? ''; $treatment = $_POST['treatment_plan'] ?? ''; if ($patient_id && $doctor_id) { $db->beginTransaction(); $stmt = $db->prepare("INSERT INTO visits (patient_id, doctor_id, appointment_id, weight, blood_pressure, heart_rate, temperature, symptoms, diagnosis, treatment_plan) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); $stmt->execute([$patient_id, $doctor_id, $appointment_id, $weight, $bp, $hr, $temp, $symptoms, $diagnosis, $treatment]); if ($appointment_id) { $stmt = $db->prepare("UPDATE appointments SET status = 'Completed' WHERE id = ?"); $stmt->execute([$appointment_id]); } $db->commit(); $_SESSION['flash_message'] = __('add_visit') . ' ' . __('successfully'); $redirect = true; } } elseif ($_POST['action'] === 'edit_visit') { $id = $_POST['id'] ?? ''; $patient_id = $_POST['patient_id'] ?? ''; $doctor_id = $_POST['doctor_id'] ?? ''; $weight = $_POST['weight'] ?? ''; $bp = $_POST['blood_pressure'] ?? ''; $hr = $_POST['heart_rate'] ?? ''; $temp = $_POST['temperature'] ?? ''; $symptoms = $_POST['symptoms'] ?? ''; $diagnosis = $_POST['diagnosis'] ?? ''; $treatment = $_POST['treatment_plan'] ?? ''; if ($id && $patient_id && $doctor_id) { $stmt = $db->prepare("UPDATE visits SET patient_id = ?, doctor_id = ?, weight = ?, blood_pressure = ?, heart_rate = ?, temperature = ?, symptoms = ?, diagnosis = ?, treatment_plan = ? WHERE id = ?"); $stmt->execute([$patient_id, $doctor_id, $weight, $bp, $hr, $temp, $symptoms, $diagnosis, $treatment, $id]); $_SESSION['flash_message'] = __('edit_visit') . ' ' . __('successfully'); $redirect = true; } } elseif ($_POST['action'] === 'delete_visit') { $id = $_POST['id'] ?? ''; if ($id) { $stmt = $db->prepare("DELETE FROM visits WHERE id = ?"); $stmt->execute([$id]); $_SESSION['flash_message'] = __('delete') . ' ' . __('successfully'); $redirect = true; } } elseif ($_POST['action'] === 'create_bill') { $patient_id = $_POST['patient_id'] ?? ''; $visit_id = $_POST['visit_id'] ?: null; $items = $_POST['items'] ?? []; $amounts = $_POST['amounts'] ?? []; if ($patient_id && !empty($items)) { $db->beginTransaction(); $total = array_sum($amounts); // Check if patient has insurance $stmt = $db->prepare("SELECT insurance_company_id FROM patients WHERE id = ?"); $stmt->execute([$patient_id]); $patient = $stmt->fetch(); $insurance_covered = 0; if ($patient && $patient['insurance_company_id']) { $insurance_covered = $total * 0.8; // 80% coverage } $patient_payable = $total - $insurance_covered; $stmt = $db->prepare("INSERT INTO bills (patient_id, visit_id, total_amount, insurance_covered, patient_payable) VALUES (?, ?, ?, ?, ?)"); $stmt->execute([$patient_id, $visit_id, $total, $insurance_covered, $patient_payable]); $bill_id = $db->lastInsertId(); $stmt = $db->prepare("INSERT INTO bill_items (bill_id, description, amount) VALUES (?, ?, ?)"); foreach ($items as $index => $desc) { if ($desc && isset($amounts[$index])) { $stmt->execute([$bill_id, $desc, $amounts[$index]]); } } $db->commit(); $_SESSION['flash_message'] = __('create_bill') . ' ' . __('successfully'); $redirect = true; } } elseif ($_POST['action'] === 'create_report') { $visit_id = $_POST['visit_id'] ?? ''; $type = $_POST['report_type'] ?? ''; $findings = $_POST['findings'] ?? ''; $recom = $_POST['recommendations'] ?? ''; if ($visit_id && $type) { $stmt = $db->prepare("INSERT INTO provisional_reports (visit_id, report_type, findings, recommendations) VALUES (?, ?, ?, ?)"); $stmt->execute([$visit_id, $type, $findings, $recom]); $_SESSION['flash_message'] = __('successfully'); $redirect = true; } } elseif ($_POST['action'] === 'add_employee') { $name_en = $_POST['name_en'] ?? ''; $name_ar = $_POST['name_ar'] ?? ''; $dob = $_POST['dob'] ?: null; $mobile = $_POST['mobile'] ?? ''; $email = $_POST['email'] ?? ''; $dept_id = $_POST['department_id'] ?: null; $passion_en = $_POST['passion_en'] ?? ''; $passion_ar = $_POST['passion_ar'] ?? ''; if ($name_en && $name_ar) { $stmt = $db->prepare("INSERT INTO employees (name_en, name_ar, dob, mobile, email, department_id, passion_en, passion_ar) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"); $stmt->execute([$name_en, $name_ar, $dob, $mobile, $email, $dept_id, $passion_en, $passion_ar]); $_SESSION['flash_message'] = __('add_employee') . ' ' . __('successfully'); $redirect = true; } } elseif ($_POST['action'] === 'edit_employee') { $id = $_POST['id'] ?? ''; $name_en = $_POST['name_en'] ?? ''; $name_ar = $_POST['name_ar'] ?? ''; $dob = $_POST['dob'] ?: null; $mobile = $_POST['mobile'] ?? ''; $email = $_POST['email'] ?? ''; $dept_id = $_POST['department_id'] ?: null; $passion_en = $_POST['passion_en'] ?? ''; $passion_ar = $_POST['passion_ar'] ?? ''; if ($id && $name_en && $name_ar) { $stmt = $db->prepare("UPDATE employees SET name_en = ?, name_ar = ?, dob = ?, mobile = ?, email = ?, department_id = ?, passion_en = ?, passion_ar = ? WHERE id = ?"); $stmt->execute([$name_en, $name_ar, $dob, $mobile, $email, $dept_id, $passion_en, $passion_ar, $id]); $_SESSION['flash_message'] = __('edit_employee') . ' ' . __('successfully'); $redirect = true; } } elseif ($_POST['action'] === 'delete_employee') { $id = $_POST['id'] ?? ''; if ($id) { $stmt = $db->prepare("DELETE FROM employees WHERE id = ?"); $stmt->execute([$id]); $_SESSION['flash_message'] = __('delete') . ' ' . __('successfully'); $redirect = true; } } elseif ($_POST['action'] === 'add_poison') { $name_en = $_POST['name_en'] ?? ''; $name_ar = $_POST['name_ar'] ?? ''; $desc_en = $_POST['description_en'] ?? ''; $desc_ar = $_POST['description_ar'] ?? ''; if ($name_en && $name_ar) { $stmt = $db->prepare("INSERT INTO poisons (name_en, name_ar, description_en, description_ar) VALUES (?, ?, ?, ?)"); $stmt->execute([$name_en, $name_ar, $desc_en, $desc_ar]); $_SESSION['flash_message'] = __('add_poison') . ' ' . __('successfully'); $redirect = true; } } elseif ($_POST['action'] === 'edit_poison') { $id = $_POST['id'] ?? ''; $name_en = $_POST['name_en'] ?? ''; $name_ar = $_POST['name_ar'] ?? ''; $desc_en = $_POST['description_en'] ?? ''; $desc_ar = $_POST['description_ar'] ?? ''; if ($id && $name_en && $name_ar) { $stmt = $db->prepare("UPDATE poisons SET name_en = ?, name_ar = ?, description_en = ?, description_ar = ? WHERE id = ?"); $stmt->execute([$name_en, $name_ar, $desc_en, $desc_ar, $id]); $_SESSION['flash_message'] = __('edit_poison') . ' ' . __('successfully'); $redirect = true; } } elseif ($_POST['action'] === 'delete_poison') { $id = $_POST['id'] ?? ''; if ($id) { $stmt = $db->prepare("DELETE FROM poisons WHERE id = ?"); $stmt->execute([$id]); $_SESSION['flash_message'] = __('delete') . ' ' . __('successfully'); $redirect = true; } } elseif ($_POST['action'] === 'add_test_group') { $name_en = $_POST['name_en'] ?? ''; $name_ar = $_POST['name_ar'] ?? ''; if ($name_en && $name_ar) { $stmt = $db->prepare("INSERT INTO test_groups (name_en, name_ar) VALUES (?, ?)"); $stmt->execute([$name_en, $name_ar]); $_SESSION['flash_message'] = __('add_test_group') . ' ' . __('successfully'); $redirect = true; } } elseif ($_POST['action'] === 'edit_test_group') { $id = $_POST['id'] ?? ''; $name_en = $_POST['name_en'] ?? ''; $name_ar = $_POST['name_ar'] ?? ''; if ($id && $name_en && $name_ar) { $stmt = $db->prepare("UPDATE test_groups SET name_en = ?, name_ar = ? WHERE id = ?"); $stmt->execute([$name_en, $name_ar, $id]); $_SESSION['flash_message'] = __('edit_test_group') . ' ' . __('successfully'); $redirect = true; } } elseif ($_POST['action'] === 'delete_test_group') { $id = $_POST['id'] ?? ''; if ($id) { $stmt = $db->prepare("DELETE FROM test_groups WHERE id = ?"); $stmt->execute([$id]); $_SESSION['flash_message'] = __('delete') . ' ' . __('successfully'); $redirect = true; } } elseif ($_POST['action'] === 'add_test') { $name_en = $_POST['name_en'] ?? ''; $name_ar = $_POST['name_ar'] ?? ''; $group_id = $_POST['group_id'] ?: null; $price = $_POST['price'] ?? 0; $range = $_POST['normal_range'] ?? ''; if ($name_en && $name_ar) { $stmt = $db->prepare("INSERT INTO laboratory_tests (name_en, name_ar, group_id, price, normal_range) VALUES (?, ?, ?, ?, ?)"); $stmt->execute([$name_en, $name_ar, $group_id, $price, $range]); $_SESSION['flash_message'] = __('add_test') . ' ' . __('successfully'); $redirect = true; } } elseif ($_POST['action'] === 'edit_test') { $id = $_POST['id'] ?? ''; $name_en = $_POST['name_en'] ?? ''; $name_ar = $_POST['name_ar'] ?? ''; $group_id = $_POST['group_id'] ?: null; $price = $_POST['price'] ?? 0; $range = $_POST['normal_range'] ?? ''; if ($id && $name_en && $name_ar) { $stmt = $db->prepare("UPDATE laboratory_tests SET name_en = ?, name_ar = ?, group_id = ?, price = ?, normal_range = ? WHERE id = ?"); $stmt->execute([$name_en, $name_ar, $group_id, $price, $range, $id]); $_SESSION['flash_message'] = __('edit_test') . ' ' . __('successfully'); $redirect = true; } } elseif ($_POST['action'] === 'delete_test') { $id = $_POST['id'] ?? ''; if ($id) { $stmt = $db->prepare("DELETE FROM laboratory_tests WHERE id = ?"); $stmt->execute([$id]); $_SESSION['flash_message'] = __('delete_test') . ' ' . __('successfully'); $redirect = true; } } elseif ($_POST['action'] === 'add_inquiry') { $patient_name = $_POST['patient_name'] ?? ''; $test_ids = $_POST['test_ids'] ?? []; $results = $_POST['results'] ?? []; $source = $_POST['source'] ?? 'Internal'; $date = $_POST['inquiry_date'] ?: date('Y-m-d H:i'); $status = $_POST['status'] ?? 'Pending'; $notes = $_POST['notes'] ?? ''; if ($patient_name) { $db->beginTransaction(); $stmt = $db->prepare("INSERT INTO laboratory_inquiries (patient_name, source, inquiry_date, status, notes) VALUES (?, ?, ?, ?, ?)"); $stmt->execute([$patient_name, $source, $date, $status, $notes]); $inquiry_id = $db->lastInsertId(); if (!empty($test_ids)) { $testStmt = $db->prepare("INSERT INTO inquiry_tests (inquiry_id, test_id, result) VALUES (?, ?, ?)"); foreach ($test_ids as $index => $tid) { if ($tid) { $testStmt->execute([$inquiry_id, $tid, $results[$index] ?? '']); } } } $db->commit(); $_SESSION['flash_message'] = __('add_inquiry') . ' ' . __('successfully'); $redirect = true; } } elseif ($_POST['action'] === 'edit_inquiry') { $id = $_POST['id'] ?? ''; $patient_name = $_POST['patient_name'] ?? ''; $test_ids = $_POST['test_ids'] ?? []; $results = $_POST['results'] ?? []; $source = $_POST['source'] ?? 'Internal'; $date = $_POST['inquiry_date'] ?: date('Y-m-d H:i'); $status = $_POST['status'] ?? 'Pending'; $notes = $_POST['notes'] ?? ''; if ($id && $patient_name) { $db->beginTransaction(); $stmt = $db->prepare("UPDATE laboratory_inquiries SET patient_name = ?, source = ?, inquiry_date = ?, status = ?, notes = ? WHERE id = ?"); $stmt->execute([$patient_name, $source, $date, $status, $notes, $id]); // Remove old tests and insert new ones $stmt = $db->prepare("DELETE FROM inquiry_tests WHERE inquiry_id = ?"); $stmt->execute([$id]); if (!empty($test_ids)) { $testStmt = $db->prepare("INSERT INTO inquiry_tests (inquiry_id, test_id, result) VALUES (?, ?, ?)"); foreach ($test_ids as $index => $tid) { if ($tid) { $testStmt->execute([$id, $tid, $results[$index] ?? '']); } } } $db->commit(); $_SESSION['flash_message'] = __('edit_inquiry') . ' ' . __('successfully'); $redirect = true; } } elseif ($_POST['action'] === 'delete_inquiry') { $id = $_POST['id'] ?? ''; if ($id) { $stmt = $db->prepare("DELETE FROM laboratory_inquiries WHERE id = ?"); $stmt->execute([$id]); $_SESSION['flash_message'] = __('delete') . ' ' . __('successfully'); $redirect = true; } } elseif ($_POST['action'] === 'add_xray_group') { $name_en = $_POST['name_en'] ?? ''; $name_ar = $_POST['name_ar'] ?? ''; if ($name_en && $name_ar) { $stmt = $db->prepare("INSERT INTO xray_groups (name_en, name_ar) VALUES (?, ?)"); $stmt->execute([$name_en, $name_ar]); $_SESSION['flash_message'] = __('add_xray_group') . ' ' . __('successfully'); $redirect = true; } } elseif ($_POST['action'] === 'edit_xray_group') { $id = $_POST['id'] ?? ''; $name_en = $_POST['name_en'] ?? ''; $name_ar = $_POST['name_ar'] ?? ''; if ($id && $name_en && $name_ar) { $stmt = $db->prepare("UPDATE xray_groups SET name_en = ?, name_ar = ? WHERE id = ?"); $stmt->execute([$name_en, $name_ar, $id]); $_SESSION['flash_message'] = __('edit_xray_group') . ' ' . __('successfully'); $redirect = true; } } elseif ($_POST['action'] === 'delete_xray_group') { $id = $_POST['id'] ?? ''; if ($id) { $stmt = $db->prepare("DELETE FROM xray_groups WHERE id = ?"); $stmt->execute([$id]); $_SESSION['flash_message'] = __('delete') . ' ' . __('successfully'); $redirect = true; } } elseif ($_POST['action'] === 'add_xray_test') { $group_id = $_POST['group_id'] ?: null; $name_en = $_POST['name_en'] ?? ''; $name_ar = $_POST['name_ar'] ?? ''; $price = $_POST['price'] ?? 0; if ($name_en && $name_ar) { $stmt = $db->prepare("INSERT INTO xray_tests (group_id, name_en, name_ar, price) VALUES (?, ?, ?, ?)"); $stmt->execute([$group_id, $name_en, $name_ar, $price]); $_SESSION['flash_message'] = __('add_xray_test') . ' ' . __('successfully'); $redirect = true; } } elseif ($_POST['action'] === 'edit_xray_test') { $id = $_POST['id'] ?? ''; $group_id = $_POST['group_id'] ?: null; $name_en = $_POST['name_en'] ?? ''; $name_ar = $_POST['name_ar'] ?? ''; $price = $_POST['price'] ?? 0; if ($id && $name_en && $name_ar) { $stmt = $db->prepare("UPDATE xray_tests SET group_id = ?, name_en = ?, name_ar = ?, price = ? WHERE id = ?"); $stmt->execute([$group_id, $name_en, $name_ar, $price, $id]); $_SESSION['flash_message'] = __('edit_xray_test') . ' ' . __('successfully'); $redirect = true; } } elseif ($_POST['action'] === 'delete_xray_test') { $id = $_POST['id'] ?? ''; if ($id) { $stmt = $db->prepare("DELETE FROM xray_tests WHERE id = ?"); $stmt->execute([$id]); $_SESSION['flash_message'] = __('delete') . ' ' . __('successfully'); $redirect = true; } } elseif ($_POST['action'] === 'add_xray_inquiry') { $patient_name = $_POST['patient_name'] ?? ''; $xray_ids = $_POST['xray_ids'] ?? []; $results = $_POST['results'] ?? []; $source = $_POST['source'] ?? 'Internal'; $date = $_POST['inquiry_date'] ?: date('Y-m-d H:i'); $status = $_POST['status'] ?? 'Pending'; $notes = $_POST['notes'] ?? ''; if ($patient_name) { $db->beginTransaction(); $stmt = $db->prepare("INSERT INTO xray_inquiries (patient_name, source, inquiry_date, status, notes) VALUES (?, ?, ?, ?, ?)"); $stmt->execute([$patient_name, $source, $date, $status, $notes]); $inquiry_id = $db->lastInsertId(); if (!empty($xray_ids)) { $testStmt = $db->prepare("INSERT INTO xray_inquiry_items (inquiry_id, xray_id, result) VALUES (?, ?, ?)"); foreach ($xray_ids as $index => $tid) { if ($tid) { $testStmt->execute([$inquiry_id, $tid, $results[$index] ?? '']); } } } $db->commit(); $_SESSION['flash_message'] = __('add_xray_inquiry') . ' ' . __('successfully'); $redirect = true; } } elseif ($_POST['action'] === 'edit_xray_inquiry') { $id = $_POST['id'] ?? ''; $patient_name = $_POST['patient_name'] ?? ''; $xray_ids = $_POST['xray_ids'] ?? []; $results = $_POST['results'] ?? []; $source = $_POST['source'] ?? 'Internal'; $date = $_POST['inquiry_date'] ?: date('Y-m-d H:i'); $status = $_POST['status'] ?? 'Pending'; $notes = $_POST['notes'] ?? ''; if ($id && $patient_name) { $db->beginTransaction(); $stmt = $db->prepare("UPDATE xray_inquiries SET patient_name = ?, source = ?, inquiry_date = ?, status = ?, notes = ? WHERE id = ?"); $stmt->execute([$patient_name, $source, $date, $status, $notes, $id]); $stmt = $db->prepare("DELETE FROM xray_inquiry_items WHERE inquiry_id = ?"); $stmt->execute([$id]); if (!empty($xray_ids)) { $testStmt = $db->prepare("INSERT INTO xray_inquiry_items (inquiry_id, xray_id, result) VALUES (?, ?, ?)"); foreach ($xray_ids as $index => $tid) { if ($tid) { $testStmt->execute([$id, $tid, $results[$index] ?? '']); } } } $db->commit(); $_SESSION['flash_message'] = __('edit_xray_inquiry') . ' ' . __('successfully'); $redirect = true; } } elseif ($_POST['action'] === 'delete_xray_inquiry') { $id = $_POST['id'] ?? ''; if ($id) { $stmt = $db->prepare("DELETE FROM xray_inquiries WHERE id = ?"); $stmt->execute([$id]); $_SESSION['flash_message'] = __('delete') . ' ' . __('successfully'); $redirect = true; } } } if ($redirect) { header("Location: " . $_SERVER['REQUEST_URI']); exit; } }