<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Admin / Testimonials — manage brand & talent testimonials.
 */
#[\AllowDynamicProperties]
class Testimonials_admin extends Admin_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->model('testimonial_model');
	}

	public function index()
	{
		$this->render('testimonials/index', array(
			'page_title'   => 'Testimonials',
			'testimonials' => $this->testimonial_model->get_all(),
		));
	}

	public function create()
	{
		$this->render('testimonials/form', array(
			'page_title'  => 'Add Testimonial',
			'testimonial' => $this->_blank(),
			'mode'        => 'create',
		));
	}

	public function edit($id)
	{
		$testimonial = $this->testimonial_model->get($id);
		if ( ! $testimonial) show_404();
		$this->render('testimonials/form', array(
			'page_title'  => 'Edit Testimonial',
			'testimonial' => $testimonial,
			'mode'        => 'edit',
		));
	}

	public function save()
	{
		$id = (int) $this->input->post('id');

		$this->load->library('form_validation');
		$this->form_validation->set_rules('author_name', 'Author Name', 'required|trim');
		$this->form_validation->set_rules('quote', 'Quote', 'required|trim');

		if ($this->form_validation->run() === FALSE)
		{
			$this->session->set_flashdata('error', strip_tags(validation_errors()));
			redirect($id ? 'admin/testimonials/edit/'.$id : 'admin/testimonials/create');
		}

		$data = array(
			'author_name' => $this->input->post('author_name', TRUE),
			'author_role' => $this->input->post('author_role', TRUE),
			'source_type' => $this->input->post('source_type', TRUE) ?: 'brand',
			'quote'       => $this->input->post('quote', TRUE),
			'rating'      => (int) $this->input->post('rating') ?: 5,
			'is_featured' => $this->input->post('is_featured') ? 1 : 0,
			'is_active'   => $this->input->post('is_active') ? 1 : 0,
			'sort_order'  => (int) $this->input->post('sort_order'),
		);

		$photo = $this->upload('photo', 'media');
		if ($photo) $data['photo'] = $photo;

		if ($id)
		{
			$this->testimonial_model->update($id, $data);
			$this->log('Updated testimonial', $data['author_name']);
			$msg = 'Testimonial updated.';
		}
		else
		{
			$this->testimonial_model->insert($data);
			$this->log('Created testimonial', $data['author_name']);
			$msg = 'Testimonial added.';
		}

		$this->session->set_flashdata('success', $msg);
		redirect('admin/testimonials');
	}

	public function delete($id)
	{
		$t = $this->testimonial_model->get($id);
		if ($t)
		{
			$this->testimonial_model->delete($id);
			$this->log('Deleted testimonial', $t['author_name']);
			$this->session->set_flashdata('success', 'Testimonial deleted.');
		}
		redirect('admin/testimonials');
	}

	public function toggle($id)
	{
		$t = $this->testimonial_model->get($id);
		if ($t) $this->testimonial_model->update($id, array('is_active' => $t['is_active'] ? 0 : 1));
		redirect('admin/testimonials');
	}

	private function _blank()
	{
		return array(
			'id' => 0, 'author_name' => '', 'author_role' => '', 'source_type' => 'brand',
			'quote' => '', 'photo' => '', 'rating' => 5,
			'is_featured' => 1, 'is_active' => 1, 'sort_order' => 0,
		);
	}
}
