 
<?php
class ServicesController extends Controller {

    public function index() {
        $treatmentModel = $this->model('Treatment');

        // Start session if not started
        if (session_status() == PHP_SESSION_NONE) session_start();

        // Handle Add
        if (isset($_POST['add'])) {
            $imageName = null;
            if (!empty($_FILES['image']['name'])) {
                $allowed = ['png','jpg','jpeg','webp'];
                $ext = strtolower(pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION));
                if (in_array($ext, $allowed)) {
                    $imageName = time() . '_' . $_FILES['image']['name'];
                    move_uploaded_file($_FILES['image']['tmp_name'], 'assets/images/treatments/' . $imageName);
                } else {
                    $_SESSION['message'] = "Invalid image format. Only PNG, JPG, JPEG, WEBP allowed.";
                    header('Location: ' . BASE_URL . '/services');
                    exit;
                }
            }

            $treatmentModel->add([
                'title' => $_POST['title'],
                'short_description' => $_POST['short_description'],
                'description' => $_POST['description'],
                'image' => $imageName
            ]);

            $_SESSION['message'] = "Treatment added successfully!";
            header('Location: ' . BASE_URL . '/services');
            exit;
        }

        // Handle Edit
        if (isset($_POST['edit'])) {
            $id = $_POST['id'];
            $imageName = $_POST['old_image'];

            if (!empty($_FILES['image']['name'])) {
                $allowed = ['png','jpg','jpeg','webp'];
                $ext = strtolower(pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION));
                if (in_array($ext, $allowed)) {
                    $imageName = time() . '_' . $_FILES['image']['name'];
                    move_uploaded_file($_FILES['image']['tmp_name'], 'assets/images/treatments/' . $imageName);
                } else {
                    $_SESSION['message'] = "Invalid image format. Only PNG, JPG, JPEG, WEBP allowed.";
                    header('Location: ' . BASE_URL . '/services');
                    exit;
                }
            }

            $treatmentModel->update($id, [
                'title' => $_POST['title'],
                'short_description' => $_POST['short_description'],
                'description' => $_POST['description'],
                'image' => $imageName
            ]);

            $_SESSION['message'] = "Treatment updated successfully!";
            header('Location: ' . BASE_URL . '/services');
            exit;
        }

        // Handle Delete
        if (isset($_GET['delete'])) {
            $id = $_GET['delete'];
            $treatment = $treatmentModel->get($id);
            if ($treatment && $treatment['image'] && file_exists('assets/images/treatments/' . $treatment['image'])) {
                unlink('assets/images/treatments/' . $treatment['image']);
            }
            $treatmentModel->delete($id);
            $_SESSION['message'] = "Treatment deleted successfully!";
            header('Location: ' . BASE_URL . '/services');
            exit;
        }

        // Fetch all treatments
        $data['treatments'] = $treatmentModel->getAll();
        $data['title'] = "Services"; 

        $this->view('templates/header', $data);
        $this->view('services', $data);
        $this->view('templates/footer', $data);
    }
}
