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

class Clientes extends CI_Controller
{
	public function __construct()
	{
		parent::__construct();

		if ($this->session->userdata('logged_in') !== TRUE) {
			redirect('login');
		}

		$this->load->model('Cliente_model');
		$this->load->model('App_config_model');
	}

	public function index()
	{
		$data['clientes'] = $this->Cliente_model->obtener_todos();
		$data['app_config'] = $this->App_config_model->obtener();
		$this->load->view('clientes/index', $data);
	}

	public function guardar()
	{
		$this->form_validation->set_rules('nombre', 'Nombre', 'trim|required|max_length[120]');
		$this->form_validation->set_rules('cedula', 'Cedula', 'trim|max_length[40]');
		$this->form_validation->set_rules('telefono', 'Telefono', 'trim|max_length[40]');
		$this->form_validation->set_rules('correo', 'Correo', 'trim|valid_email|max_length[120]');
		$this->form_validation->set_rules('instagram', 'Instagram', 'trim|max_length[120]');
		$this->form_validation->set_rules('direccion', 'Direccion', 'trim|max_length[255]');
		$this->form_validation->set_rules('nota', 'Nota', 'trim|max_length[500]');

		if ($this->form_validation->run() !== TRUE) {
			$this->session->set_flashdata('error', validation_errors('<div>', '</div>'));
			redirect('clientes');
		}

		$this->Cliente_model->crear(array(
			'nombre' => $this->input->post('nombre', TRUE),
			'cedula' => $this->input->post('cedula', TRUE),
			'telefono' => $this->input->post('telefono', TRUE),
			'correo' => $this->input->post('correo', TRUE),
			'instagram' => $this->input->post('instagram', TRUE),
			'direccion' => $this->input->post('direccion', TRUE),
			'nota' => $this->input->post('nota', TRUE)
		));

		$this->session->set_flashdata('ok', 'Cliente agregado correctamente.');
		redirect('clientes');
	}

	public function editar()
	{
		$id = (int) $this->input->post('cliente_id', TRUE);
		$actual = $this->Cliente_model->obtener_por_id($id);
		if (!$actual) {
			$this->session->set_flashdata('error', 'El cliente no existe.');
			redirect('clientes');
		}

		$this->form_validation->set_rules('nombre', 'Nombre', 'trim|required|max_length[120]');
		$this->form_validation->set_rules('cedula', 'Cedula', 'trim|max_length[40]');
		$this->form_validation->set_rules('telefono', 'Telefono', 'trim|max_length[40]');
		$this->form_validation->set_rules('correo', 'Correo', 'trim|valid_email|max_length[120]');
		$this->form_validation->set_rules('instagram', 'Instagram', 'trim|max_length[120]');
		$this->form_validation->set_rules('direccion', 'Direccion', 'trim|max_length[255]');
		$this->form_validation->set_rules('nota', 'Nota', 'trim|max_length[500]');

		if ($this->form_validation->run() !== TRUE) {
			$this->session->set_flashdata('error', validation_errors('<div>', '</div>'));
			redirect('clientes');
		}

		$this->Cliente_model->actualizar($id, array(
			'nombre' => $this->input->post('nombre', TRUE),
			'cedula' => $this->input->post('cedula', TRUE),
			'telefono' => $this->input->post('telefono', TRUE),
			'correo' => $this->input->post('correo', TRUE),
			'instagram' => $this->input->post('instagram', TRUE),
			'direccion' => $this->input->post('direccion', TRUE),
			'nota' => $this->input->post('nota', TRUE)
		));

		$this->session->set_flashdata('ok', 'Cliente actualizado correctamente.');
		redirect('clientes');
	}
}
