<?php

$db = new PDO('sqlite:/var/www/shortlink/database/shortlink.db');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$message = '';
$shortUrl = '';

function base62($number)
{
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $result = '';

    do {
        $result = $characters[$number % 62] . $result;
        $number = intdiv($number, 62);
    } while ($number > 0);

    return $result;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $url = trim($_POST['url'] ?? '');

    if ($url === '') {
        $message = 'กรุณาใส่ URL';
    } elseif (!filter_var($url, FILTER_VALIDATE_URL)) {
        $message = 'URL ไม่ถูกต้อง';
    } else {

        $stmt = $db->prepare(
            'INSERT INTO links (code, url) VALUES (?, ?)'
        );

        /*
         * ใช้ temporary code ก่อน
         * เพื่อให้ SQLite สร้าง ID
         */
        $stmt->execute(['temp-' . uniqid(), $url]);

        $id = $db->lastInsertId();
        $code = base62((int)$id);

        $update = $db->prepare(
            'UPDATE links SET code = ? WHERE id = ?'
        );

        $update->execute([$code, $id]);

        $shortUrl = 'https://go.b2usite.site/' . $code;
    }
}

?>
<!DOCTYPE html>
<html lang="th">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>B2U Short URL</title>

<style>

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    font-family: Arial, sans-serif;
    background: #f5f5f5;
}

.container {
    width: 100%;
    max-width: 600px;
    padding: 25px;
}

.box {
    background: white;
    padding: 35px;
    border-radius: 16px;
    box-shadow: 0 5px 25px rgba(0,0,0,0.08);
}

h1 {
    text-align: center;
    margin-top: 0;
}

.subtitle {
    text-align: center;
    color: #666;
    margin-bottom: 30px;
}

input[type="url"] {
    width: 100%;
    padding: 15px;
    border: 1px solid #ccc;
    border-radius: 8px;
    font-size: 16px;
    margin-bottom: 12px;
}

button {
    width: 100%;
    padding: 15px;
    border: none;
    border-radius: 8px;
    background: #222;
    color: white;
    font-size: 16px;
    cursor: pointer;
}

button:hover {
    opacity: 0.85;
}

.error {
    margin-top: 15px;
    padding: 12px;
    background: #ffe5e5;
    color: #b00000;
    border-radius: 8px;
}

.result {
    margin-top: 20px;
    padding: 20px;
    background: #f0f8ff;
    border-radius: 10px;
    text-align: center;
}

.result a {
    font-size: 20px;
    font-weight: bold;
    word-break: break-all;
}

.copy {
    margin-top: 12px;
    background: #555;
}

</style>

</head>

<body>

<div class="container">

<div class="box">

<h1>🔗 B2U Short URL</h1>

<div class="subtitle">
ย่อลิงก์ให้สั้นและใช้งานง่าย
</div>

<form method="post">

<input
    type="url"
    name="url"
    placeholder="https://example.com/your-long-url"
    required
>

<button type="submit">
    ย่อลิงก์
</button>

</form>

<?php if ($message): ?>

<div class="error">
    <?= htmlspecialchars($message) ?>
</div>

<?php endif; ?>

<?php if ($shortUrl): ?>

<div class="result">

<div>ลิงก์ของคุณ</div>

<br>

<a
    id="shortUrl"
    href="<?= htmlspecialchars($shortUrl) ?>"
    target="_blank"
>
    <?= htmlspecialchars($shortUrl) ?>
</a>

<button
    class="copy"
    onclick="copyUrl()"
    type="button"
>
    📋 Copy
</button>

</div>

<script>

function copyUrl() {

    const url =
        document.getElementById('shortUrl').href;

    navigator.clipboard.writeText(url);

    alert('คัดลอกลิงก์แล้ว');

}

</script>

<?php endif; ?>

</div>

</div>

</body>
</html>
