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

$cols = 4;
$sceenpic = substr($_SERVER['HTTP_REFERER'],0,strpos($_SERVER['HTTP_REFERER'],"admin/index.php"));
?>
<style type="text/css">
.template_grid {
	width: 100%;
}
.template_grid td {
	text-align: center;
	background: #f8f8f8;
	border:0px;
	padding-top:20px;
}
.template_grid td img.tpl_thumb {
	border-top: 2px solid #dedede;
	border-left: 2px solid #dedede;
	border-right: 2px solid #999;
	border-bottom: 2px solid #999;
	width: 160px;
	height: 120px;
	margin-top: 4px;
	padding:5px;
}

.template_grid td img.tpl_thumb1 {
	border-top: 2px solid #dedede;
	border-left: 2px solid #dedede;
	border-right: 2px solid #999;
	border-bottom: 2px solid #999;
	border-color:#f68b17;
	width: 160px;
	height: 120px;
	margin-top: 4px;
	padding:5px;
}

.template_grid .small {
    display: block;
    padding: 3px 0px;
}
</style>
<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("admintpllst_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("admintpllst_stat").innerHTML = "<?php _e('Request failed!'); ?>";
    reloadPage();
    return false;
}

function delete_tpl(tpl_id) {
    if (confirm("Delete the selected template?")) {
        var stat = document.getElementById("admintpllst_stat");
        stat.style.display = "block";
        stat.innerHTML = "<?php _e('Deleting selected template...'); ?>";
        _ajax_request("mod_template", 
            "admin_delete", 
            {
                tpl_id:tpl_id
            }, 
            on_del_success, 
            on_del_failure);
    }
}

function on_tog_success(response) {
    var o_result = _eval_json(response);
    if (!o_result) {
        return on_failure(response);
    }
    
    var stat = document.getElementById("admintpllst_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_tog_failure(response) {
    on_del_failure(response);
}

function toggle_default(tpl_id, is_remote) {
    var remote_tpl = "0";
    if (is_remote) {
        remote_tpl = "1";
    }
    var stat = document.getElementById("admintpllst_stat");
    stat.style.display = "block";
    stat.innerHTML = "<?php _e('Setting default template...'); ?>";
    _ajax_request("mod_template", 
        "admin_make_default", 
        {
            tpl_id:tpl_id,
            is_remote:remote_tpl
        }, 
        on_tog_success, 
        on_tog_failure);
}

function make_default(tpl_name) {
	if (confirm("<?php _e('As the sitestar website uses the module type layout,the page moldules maybe will have certain shifting after cutting the template. Please using mouse to drag the shifting module to the region which you assign, then you will complete the page layout again. ');?>")) {
    var stat = document.getElementById("admintpllst_stat");
    stat.style.display = "block";
    stat.innerHTML = "<?php _e('Setting default template...'); ?>";
    _ajax_request("mod_template", 
        "admin_make_owntpl", 
        {
            tpl_name:tpl_name
        }, 
        on_tog_success, 
        on_tog_failure);
	}
}
//-->
</script>
<ul style="margin-left:1px;">
	<li style="background:url(<?php echo P_TPL_WEB; ?>/images/cog.png) no-repeat 0px 18px;"><a href="#" onclick="reset_tpl_data();return false;"><?php _e('Reset Template Data');?></a></li>
	<li><?php _e('BBS Templates');?></li>
</ul>
<div class="status_bar">
    <span id="admintpllst_stat" class="status" style="display:none;"></span>
</div>
<table class="list_table" id="admin_tpl_list" width="100%" border="0" cellspacing="1" cellpadding="2" style="line-height:24px;">
	<thead>
	</thead>
    <tbody>
	    <tr>
		    <td>
			    <table class="template_grid" cellspacing="0">
			    	<?php
			    	//获取本地模板文件夹名称
			    	for($i = 0; $i < count($template_owns); $i++)
			    	{
			    		if(DEFAULT_TPL == $template_owns[$i])	//show choosed template
			    		{
			    			echo "<tr>";
			    			echo '<td width="'.intval(100 / $cols).'%">';
			    			echo '<a href="index.php?'.Html::xuriquery('mod_modules', 'index').'" target="_blank" title="'.__('Manage Layout').'"><div><img style="position:relative;left:2px;" class="tpl_thumb" src="../template/'.$template_owns[$i].'/screenshot.gif" onmouseover="this.className=\''.'tpl_thumb1'.'\'" onmouseout="this.className=\''.'tpl_thumb'.'\'"/></div></a><span class="small">';
			    			$remote_flag = isset($templates[$i]['package_url'])?'true':'false';
				            //echo '<a style="text-align:left;padding-left:58px;" href="#" onclick="toggle_default(\''.$templates[$i]['id'].'\', '.$remote_flag.');return false;" title="'.__('Set as default template').'">'.__('Update template').'</a>';
							echo __('default template');
				            echo '&nbsp;';
			            	echo '</span></td><td></td></tr>';
			            	break;
			    		}
			    	}
					
			    echo '</table>';
				 
			    echo '<table class="template_grid" cellspacing="0">';
			    	$i = 0;
			        while($i < count($template_owns))
			        {
			        	echo "<tr>";
			        	for($j = 0; $j < $cols; $j++)
			        	{
			        		if($i >= count($template_owns))
			        		{
			        			break;	
			        		}
			    	?>
			            <td width="<?php echo intval(100 / $cols); ?>%">
			            	<a rel="pictures" class="thickbox" href="../template/<?php echo $template_owns[$i];?>/images/<?php echo $template_owns[$i];?>.jpg"><div id=<?php echo '"'.$templates[$i]['id'].'"';?>><img class="tpl_thumb" src="<?php echo '../template/'.$template_owns[$i].'/screenshot.gif'; ?>" onmouseover="this.className='tpl_thumb1';" onmouseout="this.className='tpl_thumb';" /></div></a>
			            	<span class="small">
				            <?php
								echo "<a href='#' onclick=\"make_default('$template_owns[$i]')\">".__('Set as default template')."</a>";
				            ?>
				            &nbsp;
			            	</span>
			            </td>
			        <?php
				        	$i++;
			        	}
			        	for($a = 0; $a < $cols-$j; $a++)
			        	{
			        		 echo '<td width="'.intval(100 / $cols).'%"></td>';
			        	}
			        	echo "</tr>";
			        }
			        ?>
			    </table>
		    </td>
	    </tr>
    </tbody>
</table>
