<?php
if (!defined('IN_CONTEXT')) die('access violation error!');

/**
 * Show menu list recursively
 * 
 */
function showMIList(&$mi_tree, $level = 0) {
	$row_idx = 0;
	
    foreach ($mi_tree as $mi) {
?>
        <tr class="row_style_<?php echo $row_idx; ?>">
        	<td class="left"><?php echo str_repeat('&nbsp;&nbsp;&nbsp;', $level).$mi->name; ?></td>
        	<td>
        		<a href="#" onclick="move('<?php echo $mi->id; ?>', '<?php echo $mi->siblings['prev']; ?>');return false;" title="<?php _e('Move Up'); ?>">&uarr;</a>
        		<a href="#" onclick="move('<?php echo $mi->id; ?>', '<?php echo $mi->siblings['next']; ?>');return false;" title="<?php _e('Move Down'); ?>">&darr;</a>
        	</td>
        	<td><?php echo $mi->link; ?></td>
        	<td><?php echo Toolkit::switchText($mi->published, array('0' => __('Unpublished'), '1' => __('Published'))); ?></td>
        	<td>
        		<span class="small">
        			<a href="index.php?<?php echo Html::xuriquery('mod_menu_item', 'admin_edit', array('mi_id' => $mi->id, 'menu_id'=> ParamHolder::get('menu_id', 0))); ?>" title="<?php _e('Edit'); ?>"><?php _e('Edit'); ?></a>
        			&nbsp;
        			<a href="#" onclick="delete_mi(<?php echo $mi->id; ?>);return false;" title="<?php _e('Delete'); ?>"><?php _e('Delete'); ?></a>
        		</span>
        	</td>
        </tr>
<?php
        $row_idx = 1 - $row_idx;
        
        if (sizeof($mi->slaves['MenuItem']) > 0) {
            $level++;
            showMIList($mi->slaves['MenuItem'], $level);
            $level--;
        }
    }
}
?>
<script type="text/javascript" language="javascript">
<!--
function on_del_success(response) {
    var o_result = _eval_json(response);
    if (!o_result) {
        return on_failure(response);
    }
    
    var stat = document.getElementById("adminmilst_stat");
    if (o_result.result == "ERROR") {
        stat.innerHTML = o_result.errmsg;
        return false;
    } else if (o_result.result == "OK") {
	    stat.innerHTML = "<?php _e('OK, refreshing...'); ?>";
	    reloadPage();
    } else {
        return on_failure(response);
    }
}

function on_del_failure(response) {
    document.getElementById("adminmilst_stat").innerHTML = "<?php _e('Request failed!'); ?>";
    return false;
}

function delete_mi(mi_id) {
	if (confirm("Delete the selected menu item?")) {
	    var stat = document.getElementById("adminmilst_stat");
	    stat.style.display = "block";
	    stat.innerHTML = "<?php _e('Deleting selected menu item...'); ?>";
		_ajax_request("mod_menu_item", 
			"admin_delete", 
	        {
	            mi_id:mi_id
	        }, 
			on_del_success, 
			on_del_failure);
	}
}

function on_mov_success(response) {
    on_del_success(response);
}

function on_mov_failure(response) {
    on_del_failure(response);
}

function move(mi_id, sib_mi_id) {
	if (sib_mi_id == "0") {
	    return false;
	}
	
	var stat = document.getElementById("adminmilst_stat");
	stat.style.display = "block";
	stat.innerHTML = "<?php _e('Moving Menu Item...'); ?>";
	_ajax_request("mod_menu_item", 
		"admin_move", 
	    {
	        mi_id:mi_id,
	        sib_mi_id:sib_mi_id
	    }, 
		on_mov_success, 
		on_mov_failure);
}
//-->
</script>
<div class="content_title">
	<h3><?php _e('Menu Item'); ?></h3>
</div>
<div class="space"></div>
<div class="content_toolbar">
	<table cellspacing="0" class="wrap_table">
		<tbody>
			<tr>
				<td><a href="index.php?<?php echo Html::xuriquery('mod_menu_item', 'admin_add', array('menu_id' => ParamHolder::get('menu_id', 0))); ?>" title=""><?php _e('New Menu Item'); ?></a></td>
				<td>|</td>
				<td><a href="index.php?<?php echo Html::xuriquery('mod_menu', 'admin_list'); ?>" title=""><?php _e('Back'); ?></a></td>
				<td><?php include_once(P_TPL.'/common/language_switch.php'); ?></td>
			</tr>
		</tbody>
	</table>
</div>
<div class="space"></div>
<div class="status_bar">
	<span id="adminmilst_stat" class="status" style="display:none;"></span>
</div>
<div class="space"></div>
<table cellspacing="1" class="list_table" id="admin_menu_item_list">
	<thead>
		<tr>
            <th><?php _e('Name'); ?></th>
            <th><?php _e('Order'); ?></th>
            <th><?php _e('Link'); ?></th>
            <th><?php _e('Publish'); ?></th>
            <th><?php _e('Operation'); ?></th>
        </tr>
    </thead>
    <tbody>
    <?php
    if (sizeof($menuitems) > 0) {
    	showMIList($menuitems);
    } else {
    ?>
    	<tr class="row_style_0">
    		<td colspan="5"><?php _e('No Records!'); ?></td>
    	</tr>
    <?php
    }
    ?>
    </tbody>
</table>
