<style>
    span.required {
	color: #eee!important;
    }

    .red-star {
	color: #eee!important;
	display: none;
    }
</style>
<?php
/* @var $this AssetController */
/* @var $model Asset */
$type_of_asset_name = $type_of_asset == Setting::getVehicleTypeOfAsset() ? 'Vehicles' : ($type_of_asset == Setting::getRadioActiveTypeOfAsset() ? 'Radioactives' : 'Assets');

$this->breadcrumbs = array(
    yii::t('default', 'Dashboard') => array('view'),
    (($status == 'Fine' || $status == 'Days Count') ? 'Expired' . " " . $type_of_asset_name . " \\ " . $status : $status . " " . $type_of_asset_name) .
    (($segment == 'Total') ? ' - Total Segments' : ' - ' . $segment),
);

Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
	$('.search-form').toggle();
	return false;
});
$('.search-form form').submit(function(){
	$('#asset-grid').yiiGridView('update', {
		data: $(this).serialize()
	});
	return false;
});
  $('#cancel_button').click(function(){
	$('#renew-form').removeClass('in');
    });
");
?>

<?php echo CHtml::link('Export', array('AssetExportToExcel', 'type_of_asset_id' => $type_of_asset, 'segment' => $segment, 'status' => $status, 'date' => $date, 'is_current' => ($model instanceof VAsset) ? true : false), array('class' => 'btn btn-success')); ?>
&nbsp;
<?php echo CHtml::link('Advanced Search', '#', array('class' => 'search-button btn')); ?>
&nbsp;
<?php
if(!Yii::app()->user->isGuest)
echo CHtml::link(Yii::t('default', 'Renew'), array('/asset/generalRenew'), array('class' => 'btn btn-primary', 'data-dismiss' => 'modal')); ?>


<div class="search-form" style="display:none">
    <?php
    $this->renderPartial('/asset/_search', array(
	'model' => $model,
    )); ?>
</div><!-- search-form -->

<div id="flash-msg">
    <br>
    <?php $this->widget('bootstrap.widgets.TbAlert'); ?>
</div>


<?php
    $this->widget('yiiwheels.widgets.grid.WhGridView', array(
	'id' => 'asset-grid',
	'dataProvider' => $model->search(null, $type_of_asset, $segment, $status, $date),
	'filter' => $model,
	'afterAjaxUpdate' => 'function(id,options) {
                hide_column();
		show_hide_column();
		$("td.empty").show();
	    }',
	'columns' => array(
	    'type_of_asset_id',
	    ($model instanceof VAsset) ? 'id' : 'asset_id',
	    array(
		'type' => 'html',
		'name' => 'tid_no',
		'value' => 'CHtml::link($data->tid_no, array("/tid/view", "id" => $data->tid_id))',
	    //'filter' => Tid::getNumberForView()
	    ),
	    array(
		'type' => 'html',
		'name' => 'invoice_number',
		'value' => 'CHtml::link($data->invoice_number, array("/invoice/view", "id" => $data->invoice_id))',
	    //'filter' => Invoice::getNumberForView()
	    ),
	    'po_number',
	    array(
		'name' => 'segment_name',
		'value' => '$data->segment_name',
		'filter' => Segment::getNamesForView()
	    ),
	    'serial_number',
	    array(
		'type' => 'html',
		'name' => 'asset_number',
		'value' => ($model instanceof VAsset) ? 'CHtml::link($data->asset_number, array("/asset/view", "id" => $data->id))' : 'CHtml::link($data->asset_number, array("/asset/view", "id" => $data->asset_id))',
	    ),
	    'part_number',
	    array(
		'name' => 'type_of_asset_name',
		'value' => '$data->type_of_asset_name',
		'filter' => TypeOfAsset::getNamesForView()
	    ),
	    array(
		'name' => 'asset_status',
		'value' => '$data->asset_status',
		'filter' => Asset::getAssetStatusOptions()
	    ),
	    'chassis_number',
	    'plate_number',
	    'radioactive_license_number',
	    'expiry_date',
	    'grace_period',
	    ($model instanceof VAsset) ? 'expiry_days' : 'days_count',
	    array(
		'name' => 'status',
		'value' => '$data->status',
		'filter' => Asset::getStatusOptions()
	    ),
	    array(
		'class' => 'CButtonColumn',
		'htmlOptions' => array('width' => '60'),
		'template' => '{update} {delete} {renew}',
		'afterDelete' => 'function(link,success,data){ if(success) $("#flash-msg").html(data); }',
		'buttons' => array(
		    'delete' => array
			(
			'url' => (($model instanceof VAsset) ? 'Yii::app()->createAbsoluteUrl("asset/delete", array("id" => $data->id))' : 'Yii::app()->createAbsoluteUrl("asset/delete", array("id" => $data->asset_id))'),
			'visible' => 'Yii::app()->user->checkAccess(User::SYSTEM_ADMIN_ROLE) && !Yii::app()->user->isGuest',
		    ),
		    'update' => array
			(
			'url' => (($model instanceof VAsset) ? 'Yii::app()->createAbsoluteUrl("asset/update", array("id" => $data->id))' : 'Yii::app()->createAbsoluteUrl("asset/update", array("id" => $data->asset_id))'),
			'visible'=>'!Yii::app()->user->isGuest'
		    ),
		    'renew' => array
			(
			'label' => Yii::t('default', 'Renew'),
			'url' => '"#"',
			'click' => 'function(){
                            
			     model_id =  $(this).parent().parent().children(":nth-child(2)").text();;
			     var type_of_asset = $(this).parent().parent().children(":nth-child(1)").text();
			     checkTypeOfAssetModal(type_of_asset);
			     $("#renew-form").addClass("in");
                             return false;}',
			'imageUrl' => Yii::app()->request->baseUrl . '/css/flags/renew.png',
			'visible' => '($data instanceof VAsset)  && ($data->type_of_asset_id == Setting::getRadioActiveTypeOfAsset() || $data->type_of_asset_id == Setting::getVehicleTypeOfAsset()) && !Yii::app()->user->isGuest',
		    ),
		),
	    ),
	),
    ));

    Yii::app()->controller->renderPartial('/asset/_modal', array('from_view' => null));
?>

    <script type="text/javascript">
        var model_id =  '';
        var expiry_date =  '';


        function show_hide_column() {
    	var name =  '<?php echo $type_of_asset_name ?>';
    	grid = $('#asset-grid');
    	$('tr', grid).each(function() {
    	    $('td:eq(1), th:eq(1)', this).hide();
    	});
    	if(name == 'Radioactives')
    	    $('tr', grid).each(function() {
    		$('td:eq(6), th:eq(6),td:eq(8), th:eq(8),td:eq(11), th:eq(11),td:eq(12), th:eq(12),td:eq(9), th:eq(9)', this).hide();
    	    });
    	else
    	    if(name =='Vehicles' )
    		$('tr', grid).each(function() {
    		    $('td:eq(6), th:eq(6),td:eq(8), th:eq(8),td:eq(13), th:eq(13),td:eq(9), th:eq(9)', this).hide();
    		});
    	else
    	    $('tr', grid).each(function() {
    		$('td:eq(13), th:eq(13),td:eq(11), th:eq(11),td:eq(12), th:eq(12)', this).hide();
    	    });
    	var status = '<?php echo $status ?>';

	if(status == 'Grace'){
	    $('tr', grid).each(function() {
		$('td:eq(15), th:eq(15)', this).show();
	    });
	    $('tr', grid).each(function() {
		$('td:eq(16), th:eq(16)', this).hide();
	    });
	}
	else
	    if(status == 'Days Count'){
		$('tr', grid).each(function() {
		    $('td:eq(16), th:eq(16)', this).show();
		});
	    $('tr', grid).each(function() {
		$('td:eq(15), th:eq(15)', this).hide();
	    });
	}
	else{
	    $('tr', grid).each(function() {
		$('td:eq(16), th:eq(16),td:eq(15), th:eq(15)', this).hide();
	    });
	}
    }

    show_hide_column();

</script>
