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

/**
 * Admin / Partners — manage clients & partner logos.
 */
#[\AllowDynamicProperties]
class Partners_admin extends Admin_Controller {

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

	public function index()
	{
		$this->render('partners/index', array(
			'page_title' => 'Partners',
			'partners'   => $this->partner_model->get_all(),
		));
	}

	public function create()
	{
		$this->render('partners/form', array(
			'page_title' => 'Add Partner',
			'partner'    => $this->_blank(),
			'mode'       => 'create',
		));
	}

	public function edit($id)
	{
		$partner = $this->partner_model->get($id);
		if ( ! $partner) show_404();
		$this->render('partners/form', array(
			'page_title' => 'Edit Partner',
			'partner'    => $partner,
			'mode'       => 'edit',
		));
	}

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

		$this->load->library('form_validation');
		$this->form_validation->set_rules('name', 'Name', 'required|trim');

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

		$data = array(
			'name'        => $this->input->post('name', TRUE),
			'website'     => $this->input->post('website', TRUE),
			'category'    => $this->input->post('category', TRUE) ?: 'partner',
			'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'),
		);

		$logo = $this->upload('logo', 'partners', 'jpg|jpeg|png|webp|svg');
		if ($logo) $data['logo'] = $logo;

		if ($id)
		{
			$this->partner_model->update($id, $data);
			$this->log('Updated partner', $data['name']);
			$msg = 'Partner updated.';
		}
		else
		{
			$this->partner_model->insert($data);
			$this->log('Created partner', $data['name']);
			$msg = 'Partner added.';
		}

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

	public function delete($id)
	{
		$p = $this->partner_model->get($id);
		if ($p)
		{
			$this->partner_model->delete($id);
			$this->log('Deleted partner', $p['name']);
			$this->session->set_flashdata('success', 'Partner deleted.');
		}
		redirect('admin/partners');
	}

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

	public function feature($id)
	{
		$p = $this->partner_model->get($id);
		if ($p) $this->partner_model->update($id, array('is_featured' => $p['is_featured'] ? 0 : 1));
		redirect('admin/partners');
	}

	private function _blank()
	{
		return array(
			'id' => 0, 'name' => '', 'logo' => '', 'website' => '',
			'category' => 'client', 'is_featured' => 1, 'is_active' => 1, 'sort_order' => 0,
		);
	}
}
