 
<?php
class TestimonialController extends Controller {

    public function index() {
        $testimonialsModel = $this->model('Testimonial');

        // 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/testimonials/' . $imageName);
                } else {
                    $_SESSION['message'] = "Invalid image format. Only PNG, JPG, JPEG, WEBP allowed.";
                    header('Location: ' . BASE_URL . '/testimonial');
                    exit;
                }
            }

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

            $_SESSION['message'] = "Testimonials added successfully!";
            header('Location: ' . BASE_URL . '/testimonial');
            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/testimonials/' . $imageName);
                } else {
                    $_SESSION['message'] = "Invalid image format. Only PNG, JPG, JPEG, WEBP allowed.";
                    header('Location: ' . BASE_URL . '/testimonial');
                    exit;
                }
            }

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

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

        // Handle Delete
        if (isset($_GET['delete'])) {
            $id = $_GET['delete'];
            $testimonials = $testimonialsModel->get($id);

            if ($testimonials && !empty($testimonials['image']) && file_exists('assets/images/testimonials/' . $testimonials['image'])) {
                unlink('assets/images/testimonials/' . $testimonials['image']);
            }

            $testimonialsModel->delete($id);
            $_SESSION['message'] = "testimonials deleted successfully!";
            header('Location: ' . BASE_URL . '/testimonial');
            exit;
        }

        // Fetch all testimonials
        $data['testimonials'] = $testimonialsModel->getAll();
        $data['title'] = "Testimonial"; 

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