<style>
    .timepicker {
	height: 20px !important;
    }

    .ui-timepicker-div dl dd {
	margin: -25px 0 10px 84px !important;
    }

</style>
<?php
/* @var $this ItemRequestHeaderController */
/* @var $model ItemRequestHeader */
/* @var $form CActiveForm */
?>

<?php
$form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
	    'id' => 'item-request-header-form',
	    'enableAjaxValidation' => false,
	)); ?>
<div id='dialog'></div>
<div id="loading-div">
    <div id="flash-msg">
	<br>
	<?php $this->widget('bootstrap.widgets.TbAlert'); ?>
    </div>
    <?php
	if (Yii::app()->user->checkAccess('Food Manager')) {
	    echo $form->dropDownListRow($model, 'location_id', array($model->location_id => Location::getLocationCode($model->location_id)), array('disabled' => 'disabled'));
	    echo $form->dropDownListRow($model, 'department_id', array($model->department_id => Department::getDepartmentName($model->department_id)), array('disabled' => 'disabled'));
//	echo $form->textFieldRow($model, 'department_id', );
	} else {
	    echo "<br />";
	    echo Yii::t('default', 'Location');
	    echo "<br />";

	    echo $form->dropDownList($model, 'location_id', User::model()->getLocationsInGeomarket(),
		    array(
			'ajax' => array(
			    'type' => 'POST',
			    'data' => 'js:{location_id:$(this).val(),ajax:true}',
			    'url' => CController::createUrl('itemRequestHeader/getDepartments'),
			    //'update' => '#ItemRequestHeader_department_id',
//			'beforeSend' => 'js:function(){
//			    $("#loading-div").addClass("loading");
//			}',
			    'success' => 'js:function(data) {
			    
			   // $("#loading-div").removeClass("loading");
			    $("#ItemRequestHeader_department_id").find("option").remove().end().append(data);
			 }',
			)
	    ));
//	echo $state->dropDownList('location',
//		$model->location_id,
//		User::model()->getLocationsInGeomarket(),
//		array(
//		    'name' => 'ItemRequestHeader[location_id]'
//		),
//		CController::createUrl('itemRequestHeader/getDepartments'),
//		'ItemRequestHeader_department_id'
//	);

	    $departments = ($model->isNewRecord && !isset($model->location_id) && !$model->hasErrors()) ? array() : Department::getDepartmentsInLocation($model->location_id);

	    //$model->city_id = $model->hasErrors() ? $selected_city_id : $model->city_id;

	    $empty = !$model->isNewRecord ? array() : array('empty' => Yii::t('default', 'Select Department'));

//	$location = $model->location;
//	$departments = array();
//	$departments[''] = yii::t('default', 'Select Department');
//	if ($location != '') {
//	    $selected_location = Location::model()->findByPk($location);
//
//	    if (isset($selected_location->departments) && ($selected_location->departments != null)) {
//		foreach ($selected_location->departments as $department) {
//		    $departments[$department->id] = $department->name;
//		}
//	    }
//	}
	    echo $form->dropDownListRow($model, 'department_id', $departments, $empty);
	}

	echo CHtml::label('Date', 'ItemRequestHeader_date_time');


	Yii::import('application.extensions.CJuiDateTimePicker.CJuiDateTimePicker');
	$this->widget('CJuiDateTimePicker', array(
	    'model' => $model,
	    'attribute' => 'date_time',
	    'mode' => 'datetime', //use "time","date" or "datetime" (default)
	    'language' => '',
	    'options' => array(
		'showSecond' => true,
		'timeFormat' => 'hh:mm:ss',
		'dateFormat' => 'yy-mm-dd',
		'showAnim' => 'fold',
		'changeMonth' => true,
		'changeYear' => true,
		'yearRange' => 'c-30:c+5',
	    )
	));




	Yii::app()->controller->renderPartial('/itemRequestDetail/index', array('model' => $model, 'from_view_page' => 'false', 'location_id' => $model->location_id));
    ?>
        <div class="actions">
    	<script language="javascript" type="text/javascript">
    	    var view_url= "<?php echo Yii::app()->createAbsoluteUrl("/itemRequestHeader/view", array('new_request' => 1)); ?>";
    	    var is_confirmed = false;
    	    var location_id = null;

    	    function addParam(url, param, value) {
    		var a = document.createElement('a');
    		a.href = url;
    		a.search += a.search.substring(0,1) == "?" ? "&" : "?";
    		a.search += encodeURIComponent(param);
    		if (value)
    		    a.search += "=" + encodeURIComponent(value);
    		return a.href;
    	    }
    	</script>
	<?php
	echo CHtml::ajaxSubmitButton(
		Yii::t('default', 'Save'),
		array('create'),
		array(
		    'type' => "post",
		    'beforeSend' => 'js:function(xhr){
			    if(is_confirmed == false){
				    $dialog_content = "<p>Are you sure you want to request the selected items?</p>";
				    $dialog_content += "<p>Please note that you will charge the department ("+$("#ItemRequestHeader_department_id :selected").text()+") for the items below. Press “Ok” to continue or “cancel” to revert.</p>";
				     $dialog_content += "<table><tr><td>Item</td><td>Quantity</td></tr>";
				    $.each(items_detail_array, function(key, value) {
					if(value != undefined)
					    $dialog_content += "<tr><td>"+value.item_name+"</td><td>"+value.quantity+"</td></tr>";

				    });
				    $dialog_content += "</table>";
				    $("#dialog").html($dialog_content);
				    $("#dialog").dialog({
					    width: "765px",
					    height:265,
					    title: "Confirmation Message",
					    modal: true,
					    buttons: {
					    Yes: function() {
					    	    is_confirmed = true;
						   $(this).dialog("close");						   
						   $("#submit_btn").click();
						   
					    },
					    No: function() {
						    $(this).dialog("close");
					    }
					}
				    });
				    xhr.abort();				  
				}
				else{
				    $("#loading-div").addClass("loading");
				    $(".bootstrap-widget-content").css("background","none");
					$(".bootstrap-widget-header").css("background","none");
				    }
		    }',
		    'success' => 'js:function(data) {
			   if(data.indexOf("</div>") != -1){
				$("#loading-div").removeClass("loading");
				$(".bootstrap-widget-content").css("background","#FFF");
				$(".bootstrap-widget-header").css("background","#FFF");
                                $("#flash-msg").html(data);
				}
                           else
                                window.location.href=addParam(view_url,"id",data);


		    }',
		    'error' => 'js:function(data){
			$("#loading-div").removeClass("loading");
			$(".bootstrap-widget-content").css("background","#FFF");
				$(".bootstrap-widget-header").css("background","#FFF");
			}'
		),
		array('type' => 'submit', 'class' => 'btn primary', 'name' => 'submit', 'id' => 'submit_btn')
	);
//    echo CHtml::submitButton(Yii::t('default', 'Save'),
//	    array('class' => 'btn primary',
//		"onClick" =>
//		Yii::app()->user->checkAccess('Food Manager') ?
//			"if (confirm('" . Yii::t('default', 'Are you sure you want to issue the item?') . "')){
//                           return confirm('Please note that you will charge the department ('+$('#Ticket_department_id').val()+') for ('+$('#Ticket_quantity').val()+') meal(s), press “Ok” to continue or “cancel” to revert') ;
//                           }else{
//                           return false;
//                           }" :
//			"if (confirm('" . Yii::t('default', 'Are you sure you want to issue the item?') . "')){
//                           return confirm('Please note that you will charge the department ('+$('#Ticket_department_id :selected').text()+') for ('+$('#Ticket_quantity').val()+') meal(s), press “Ok” to continue or “cancel” to revert') ;
//                           }else{
//                           return false;
//                           }"
//	    )
//    );
	?>
	&ensp;
	<?php echo CHtml::link(Yii::t('default', 'Cancel'), array('index'), array('class' => 'btn primary', 'style' => 'padding: 6px 12px !important')); ?>
    </div>
</div>
<?php $this->endWidget(); ?>


