<?php
require 'config.php';
$msg = '';

//新增应用（上传图片）
if($_POST['action'] == 'add'){
    $name = $_POST['name'];
    $link = $_POST['link'];
    $icon_name = time().'.'.pathinfo($_FILES['icon']['name'],PATHINFO_EXTENSION);
    $icon_path = 'upload/'.$icon_name;
    move_uploaded_file($_FILES['icon']['tmp_name'],$icon_path);

    $sql = "INSERT INTO apps(name,icon,link) VALUES ('$name','$icon_path','$link')";
    if(mysqli_query($conn,$sql)){
        $msg = '<div class="text-green-600">添加成功</div>';
    }else{
        $msg = '<div class="text-red-600">添加失败</div>';
    }
}

//删除应用
if($_GET['del']){
    $id = $_GET['del'];
    $res = mysqli_query($conn,"SELECT icon FROM apps WHERE id=$id");
    $row = mysqli_fetch_assoc($res);
    @unlink($row['icon']); //删除服务器图片文件
    mysqli_query($conn,"DELETE FROM apps WHERE id=$id");
    header('Location:admin.php');
}

//读取所有应用
$apps = mysqli_query($conn,"SELECT * FROM apps ORDER BY id DESC");
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>应用市场后台管理</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-100 p-4">
<div class="max-w-4xl mx-auto">
    <h1 class="text-2xl font-bold mb-4">应用管理后台</h1>
    <?php echo $msg; ?>

    <!-- 添加应用表单 -->
    <div class="bg-white rounded-lg p-5 shadow mb-6">
        <h2 class="text-lg font-semibold mb-3">新增应用</h2>
        <form method="post" enctype="multipart/form-data">
            <input type="hidden" name="action" value="add">
            <div class="mb-3">
                <label class="block mb-1">应用名称</label>
                <input type="text" name="name" required class="w-full border rounded px-3 py-2">
            </div>
            <div class="mb-3">
                <label class="block mb-1">图标图片</label>
                <input type="file" name="icon" accept="image/*" required class="w-full border rounded px-3 py-2">
            </div>
            <div class="mb-3">
                <label class="block mb-1">跳转链接</label>
                <input type="url" name="link" required class="w-full border rounded px-3 py-2">
            </div>
            <button type="submit" class="bg-blue-600 text-white px-4 py-2 rounded">提交添加</button>
        </form>
    </div>

    <!-- 应用列表 -->
    <div class="bg-white rounded-lg p-5 shadow">
        <h2 class="text-lg font-semibold mb-3">已添加应用</h2>
        <div class="space-y-4">
            <?php while($item = mysqli_fetch_assoc($apps)){ ?>
            <div class="flex items-center justify-between border-b pb-3">
                <div class="flex items-center gap-3">
                    <img src="<?php echo $item['icon']; ?>" class="w-10 h-10 rounded object-cover">
                    <div>
                        <div class="font-medium"><?php echo $item['name']; ?></div>
                        <div class="text-sm text-gray-500"><?php echo $item['link']; ?></div>
                    </div>
                </div>
                <a href="admin.php?del=<?php echo $item['id']; ?>" onclick="return confirm('确定删除？')" class="text-red-500">删除</a>
            </div>
            <?php } ?>
        </div>
    </div>
    <div class="mt-4"><a href="index.php" class="text-blue-600">← 返回应用市场首页</a></div>
</div>
</body>
</html>
