<?php
$title='首页模板设置';
include __DIR__ . '/head.php';
?>

<div class="p-6 space-y-6">
    <div class="bg-white rounded-xl shadow-sm border border-gray-200">
        <div class="p-6 border-b border-gray-200">
            <h2 class="text-lg font-semibold text-gray-800">首页模板设置</h2>
        </div>
        <div class="p-6">
            <div class="flex items-center gap-4 mb-6">
                <label class="text-sm font-medium text-gray-700">分站可更换首页模板</label>
                <select class="px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" id="fenzhan_template" default="<?php echo $conf['fenzhan_template']?>">
                    <option value="0">关闭</option>
                    <option value="1">开启</option>
                </select>
                <button type="button" class="px-4 py-2 bg-blue-600 hover:bg-blue-700 text-white font-medium rounded-lg transition" onclick="fenzhan_template()">
                    <i class="fas fa-save mr-2"></i>保存
                </button>
            </div>

            <div class="bg-blue-50 border border-blue-200 rounded-lg p-4 mb-6">
                <h3 class="text-base font-semibold text-gray-800 mb-2"><i class="fas fa-palette mr-2"></i>当前使用模板</h3>
                <div class="flex flex-col md:flex-row gap-4 items-start">
                    <div class="flex-shrink-0">
                        <img class="w-full max-w-xs rounded-lg shadow-md border border-gray-200" src="../template/<?php echo $conf['template']?>/preview.png" alt="当前模板预览">
                    </div>
                    <div class="flex-1">
                        <p class="text-lg font-medium text-gray-800 mb-2">模板名称：<span class="text-blue-600"><?php echo $conf['template']?></span></p>
                        <p class="text-sm text-gray-600">这是当前正在使用的网站模板</p>
                    </div>
                </div>
            </div>

            <div class="border-t border-gray-200 pt-6">
                <h3 class="text-base font-semibold text-gray-800 mb-4"><i class="fas fa-exchange-alt mr-2"></i>更换模板</h3>
                <div class="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-6">
                    <?php
                        $loadfile = \lib\Template::getList();
                        foreach ($loadfile as $template) {
                            echo '<div class="bg-gray-50 rounded-lg overflow-hidden hover:shadow-lg transition-shadow cursor-pointer" onclick="changeTemplate(\''.$template.'\')">
                                <div class="aspect-video overflow-hidden">
                                    <img class="w-full h-full object-cover" src="../template/'.$template.'/preview.png" alt="'.$template.' 模板预览">
                                </div>
                                <div class="p-4">
                                    <h4 class="text-sm font-medium text-gray-800 text-center">'.$template.'</h4>
                                    <p class="text-xs text-gray-500 text-center mt-1">点击更换</p>
                                </div>
                            </div>';
                        }
                    ?>
                </div>
            </div>

            <div class="mt-6 pt-6 border-t border-gray-200">
                <button type="button" class="px-6 py-2 bg-gray-200 hover:bg-gray-300 text-gray-700 font-medium rounded-lg transition" onclick="if(document.referrer){history.back()}else{location.href='template.php'}">
                    <i class="fas fa-arrow-left mr-2"></i>返回
                </button>
            </div>
        </div>
    </div>
</div>

        <script src="../assets/js/jquery.min.js"></script>
        <script src="../assets/js/bootstrap.min.js"></script>
<?php include 'foot.php'; ?>
<script type="text/javascript">
	function changeTemplate(template){
		showLoading();
		$.ajax({
			type : 'POST',
			url : 'ajax.php?act=set',
			data : {template:template},
			dataType : 'json',
			success : function(data) {
				hideLoading();
				if(data.code == 1){
					showToast('更换模板成功！', 'success');
					setTimeout(function(){
					  window.location.reload();
					}, 1000);
				}else{
					showToast(data.msg, 'error');
				}
			},
			error:function(data){
				hideLoading();
				showToast('服务器错误', 'error');
				return false;
			}
		});
	}
	function fenzhan_template(){
		showLoading();
		var value = $("#fenzhan_template").val();
		$.ajax({
			type : 'POST',
			url : 'ajax.php?act=set',
			data : {fenzhan_template:value},
			dataType : 'json',
			success : function(data) {
				hideLoading();
				showToast(data.msg, 'success');
			},
			error:function(data){
				hideLoading();
				showToast('服务器错误', 'error');
				return false;
			}
		});
	}	
	var items = $("select[default]");
	for (i = 0; i < items.length; i++) {
		$(items[i]).val($(items[i]).attr("default")||0);
	}
</script>
