<?php
$title='文章分类';
include __DIR__ . '/head.php';
if($_GET['my'] == 'editClass'){
	$id = intval($_GET['id']);
	$name = daddslashes($_GET['name']);
	$sql="UPDATE `pre_article_class` SET name='{$name}' WHERE id='{$id}' AND zid='1'";
	if($DB->exec($sql)){
		exit("<script language='javascript'>window.location.href='./articleClass.php';</script>");
	}else{
		exit("<script language='javascript'>alert('失败".$DB->error()."');window.location.href='./articleClass.php';</script>");
	}
}
if($_GET['my'] == 'delete'){
	$id = intval($_GET['id']);
	$sql="DELETE FROM `pre_article_class` WHERE id='{$id}' AND zid='1'";
	if($DB->exec($sql)){
		exit("<script language='javascript'>window.location.href='./articleClass.php';</script>");
	}else{
		exit("<script language='javascript'>alert('失败".$DB->error()."');window.location.href='./articleClass.php';</script>");
	}	
}
if($_GET['my'] == 'add_submit'){
	$name = daddslashes($_POST['kw']);
	if($name==''){exit("<script language='javascript'>alert('内容能空');window.location.href='./articleClass.php';</script>");}
	$sql="INSERT INTO `pre_article_class` (`zid`,`name`,`active`) VALUES ('1','".$name."','1')";
	if($DB->exec($sql)){
		exit("<script language='javascript'>window.location.href='./articleClass.php';</script>");
	}else{
		exit("<script language='javascript'>alert('失败".$DB->error()."');window.location.href='./articleClass.php';</script>");
	}
}
?>
<div class="p-4 md:p-6">
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
        <div class="p-4 md:p-6 border-b border-gray-200">
            <h2 class="text-base md:text-lg font-semibold text-gray-800">文章分类</h2>
        </div>
        <div class="p-4 md:p-6">
            <form action="?my=add_submit" method="post" class="flex flex-col sm:flex-row gap-3 mb-6">
                <input type="text" class="flex-1 px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent text-sm" name="kw" placeholder="输入 分类名称" required>
                <button type="submit" class="inline-flex items-center px-6 py-2 bg-blue-500 text-white text-sm font-medium rounded-lg hover:bg-blue-600 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2">
                    <i class="fas fa-plus mr-2"></i>添加
                </button>
            </form>
            <div class="overflow-x-auto">
                <table class="w-full min-w-full divide-y divide-gray-200 table-responsive">
                    <thead class="bg-gray-50">
                        <tr>
                            <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">ID</th>
                            <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">分类名称</th>
                            <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">操作</th>
                        </tr>
                    </thead>
                    <tbody id="list" class="bg-white divide-y divide-gray-200">
                    </tbody>
                </table>
            </div>
            <div class="mt-4 flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4">
                <div class="text-sm text-gray-600">
                    共有 <span id="total" class="font-semibold text-blue-600">0</span> 条/现第：
                    <select id="page" class="inline-block px-3 py-1.5 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-blue-500 focus:border-transparent">
                        <option value="1">1</option>
                    </select>
                    页
                </div>
                <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='articleClass.php'}">
                    <i class="fas fa-arrow-left mr-2"></i>返回
                </button>
            </div>
        </div>
    </div>
</div>
<?php include 'foot.php'; ?>
<script type="text/javascript">
	var pagesize = 30;
	var page = 1;
	$(document).ready(function() {
	    get_list(page,pagesize);
		$('#page').change(function(){
			page = $('#page').val();
			get_list(page,pagesize);
		});
	});
	function get_list(page,pagesize){
		var kw = $("input[name='kw']").val();
		showLoading();
		$.ajax({
			type : 'POST',
			data : {pagesize:pagesize,page:page,kw:kw},
			url : 'ajax.php?act=get_articleClass',
			dataType : 'json',
			success : function(data) {
				hideLoading();
				adddata(data.data);
				$('#total').html(data.total);
				if(data.end >= 1){
					$('#page').html('');
					var end = data.end;
					var page = data.page;
					var start=page-10>1?page-10:1;
					var end=page+10<end?page+10:end;
					for(var i = start; i <= page; i++){
					    $('#page').append('<option value="'+ i +'">' + i + '</option>');
					}
					for(var i = page+1; i <= end; i++){
					    $('#page').append('<option value="'+ i +'">' + i + '</option>');
					}
					$('#page').val(page);
				}
			},
			error:function(data){
				hideLoading();
				showToast('服务器错误', 'error');
				return false;
			}
		});
	}
	function adddata(data){
		$("#list").empty();
		$.each(data, function(i,v){
			var tmp = '<tr class="hover:bg-gray-50">\
		            <td class="px-4 py-3 whitespace-nowrap text-sm text-gray-500">'+v.id+'</td>\
		            <td class="px-4 py-3 whitespace-nowrap"><input type="text" class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent text-sm" id="id'+v.id+'" value="'+v.name+'" placeholder="分类名称" required=""></td>\
		            <td class="px-4 py-3 whitespace-nowrap">\
		                <div class="flex items-center gap-2">\
		                <button onclick="editClass('+v.id+')" class="px-3 py-1 text-xs bg-blue-600 text-white rounded hover:bg-blue-700"><i class="fas fa-edit mr-1"></i>修改</button>\
		                <a href="?my=delete&id='+v.id+'" class="px-3 py-1 text-xs bg-red-600 text-white rounded hover:bg-red-700" onclick="return confirm(\'你确实要删除此分类吗？\');"><i class="fas fa-trash mr-1"></i>删除</a>\
		                </div>\
		            </td>\
		        </tr>';
		    $("#list").append(tmp);
		});
	}
	function editClass(id) {
		var name = $("#id"+id).val();
		window.location.href = '?my=editClass&id='+id+'&name='+name;
	}
</script>