<?php
/**
 * AZApp
 * @author	M. Isman Subakti
 * @copyright	06-04-2016
 */
defined('BASEPATH') OR exit('No direct script access allowed');
require_once("AZ.php");

class CI_AZDatetime extends CI_AZ {
	protected $format = "DD-MM-YYYY HH:mm:ss";
	protected $min_date = "";

	public function __construct() {
		$this->ci =& get_instance();
	}

	public function set_format($data) {
		return $this->format = $data;
	}

	public function set_min_date($data) {
		return $this->min_date = $data;
	}

	public function render() {
		$style = '';
		if (count($this->style) > 0) {
			$style = " style='";
		}
		foreach ($this->style as $key => $value) {
			$style .= $key.':'.$value.';';
		}
		if (count($this->style) > 0) {
			$style  .= "' ";
		}

		$data_attr = "";
		foreach ($this->attr as $key => $value) {
			$data_attr .= " ".$key."='".$value."'";
		}

		$data = '
			<div class="input-group az-datetime" '.$style.'>
	            <input type="text" '.$data_attr.' class="form-control'.$this->class.'" id="'.$this->id.'" name="'.$this->name.'" value="'.$this->value.'"/>
	            <span class="input-group-addon">
	            <span class="glyphicon glyphicon-calendar"></span>
	            </span>
	        </div>
	    ';

	    $js = "
	    	jQuery('#".$this->id."').datetimepicker({
	        	format: '".$this->format."',";
	    if (strlen($this->min_date) > 0) {
	    	$js .= "minDate:'".$this->min_date."',";
	    }
	    $js .= "
	    	});
		";

		$ci =& get_instance();
		$ci->load->library('AZApp');
		$azapp = $ci->azapp;
		$azapp->add_js_ready($js);

	    return $data;
	}


}