
<?php
/* @var $this RelativeController */
/* @var $dataProvider CActiveDataProvider */

echo "<div id='item-container'>";
Yii::app()->controller->renderPartial('/itemRequestDetail/_modal', array('model_id' => $model_id, 'location_id' => $location_id));
echo "</div>";
if ($from_view_page == 'false') {

    echo CHtml::button(Yii::t('default', 'Add'), array(
	'data-toggle' => 'modal',
	'data-target' => '#item-detail-form',
	'class' => 'btn primary',
	'id' => 'add_item',
	'onClick' => '
            $("#modal_item_name").val("");
               $("#modal_quantity").val("");',
    ));
}

//----------------------------------------------------------------
$this->widget('ext.bootstrap.widgets.TbGridView', array(
    'id' => 'item-detail-grid',
    'ajaxUrl' => Yii::app()->createUrl('itemRequestHeader/details'),
    'dataProvider' => $dataProvider,
//	'template'=>"{summary}\n{items}",
//	'enablePagination'=>false,
    'columns' => array(
	array(
	    'name' => 'item_name',
	    'type' => 'raw',
	    'value' => '$data["item_id"]',
	    'header' => yii::t('default', 'Item'),
	),
	array(
	    'name' => 'quantity',
	    'header' => yii::t('default', 'Quantity'),
	),
	array(
	    'class' => 'CButtonColumn',
	    'htmlOptions' => array('width' => '100'),
	    'template' => '{delete}',
	    'afterDelete' => 'function(link,success,data){ if(success) $("#flash-msg").html(data); }',
	    'buttons' => array(
		'delete' => array(
		    'label' => Yii::t('default', 'Delete'),
		    'url' => '"#"',
		    'click' => 'function(){
                             var rowIndex = $(this).closest("tr").index();
                             var rowId = $("#"+$(this).closest("div.grid-view").attr("id")+" > div.keys > span:eq("+rowIndex+")").text();
                             delete_row_and_update_grid(rowId);
                             return false;}',
		),
	    ), 'visible' => ($from_view_page == 'false'),
	)
    ),
	)
);
?>

<script type="text/javascript">
    //---------------------------------------------------------------
    var warning_message="<?php echo Yii::t('default', "You can't add the same item twice."); ?>";
    var error_message1="<?php echo Yii::t('default', "Item Name cannot be blank."); ?>";
    var error_message2="<?php echo Yii::t('default', "Quantity cannot be blank."); ?>";
    var error_message="<?php echo Yii::t('default', "Item and Quantity cannot be blank."); ?>";
    
    function save_changes_and_update_grid(){
        var item_name=$("#modal_item_name").val();
        var quantity=$("#modal_quantity").val();
        if(item_name=="" && quantity==""){
            alert(error_message);
        }   else
            if(item_name=="" ){
                alert(error_message1);
            }
        else
            if( quantity==""){
                alert(error_message2);
            }
        else {
            id++;
            // the array is defined and has at least one element
            //Prevent adding shareholder twice
            if( is_added(item_name,items_detail_array)==false){
                var elem={'id':id,'item_name': item_name,'quantity':quantity};
                items_detail_array[id]=elem;
                $.fn.yiiGridView.update('item-detail-grid', {
                    data: { "data" : JSON.stringify(items_detail_array)+'##'+model_id+'##'+location_id}
                });
                $("#item_detail_hidden_input").val(JSON.stringify(items_detail_array));
            }
            else
            {
                alert(warning_message);
                id--;
		return;
            }
            //empty the modal inputs after hiding it
            $("#modal_item_name").val("");
            $("#modal_quantity").val("");
	    //$('.modal-backdrop').modal('hide');
	    $('.modal').modal('hide');
	    // $('.modal-backdrop').remove();
        }
    }
    //----------------------------------------------------------
    function delete_row_and_update_grid(id){
	var message = '<?php echo YII::t('zii', 'Are you sure you want to delete this item?'); ?>';
	if(! confirm(message)){
	    return;
	}
        $.each(items_detail_array, function(key, value) {
            if(value != null && value != undefined){
                if(items_detail_array[key].id==id ){
                    delete items_detail_array[key];
                }}
        });
        $.fn.yiiGridView.update('item-detail-grid', {
            data: { "data" : JSON.stringify(items_detail_array)+'##'+model_id+'##'+location_id}

        });
        $("#item_detail_hidden_input").val(JSON.stringify(items_detail_array));
    }
    //----------------------------------------------------------
    function is_added(item_name,array){
	var found = false;
	$.each(array, function(key, value) {
	    if(!found){
		if(value != null && value != undefined){
		    if(value.item_name==item_name ){
			found = true;
		    }
		}
	    }
        });
	if(found)
	    return true;
	else
	    return false;
	//        string_array=JSON.stringify(array);
	//        if(string_array.indexOf(item_name)== -1)
	//            return false;
	//        return true;
 
    }

</script>