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

class Empleado_model extends CI_Model
{
	public function __construct()
	{
		parent::__construct();
		$this->crear_tabla_si_no_existe();
	}

	public function obtener_todos()
	{
		return $this->db
			->order_by('id', 'DESC')
			->get('empleados')
			->result();
	}

	public function crear($data)
	{
		return $this->db->insert('empleados', array(
			'nombre' => $data['nombre'],
			'cargo' => $data['cargo'],
			'telefono' => $data['telefono']
		));
	}

	private function crear_tabla_si_no_existe()
	{
		$sql = "CREATE TABLE IF NOT EXISTS empleados (
			id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
			nombre VARCHAR(100) NOT NULL,
			cargo VARCHAR(100) NOT NULL,
			telefono VARCHAR(30) NULL,
			creado_en TIMESTAMP DEFAULT CURRENT_TIMESTAMP
		) ENGINE=InnoDB DEFAULT CHARSET=utf8";

		$this->db->query($sql);
	}
}
