<?php
session_start();
require_once 'config.php';

// Check if user is logged in
if (!isset($_SESSION['token'])) {
    header('Location: login.php');
    exit;
}

$token = $_SESSION['token'];
$user_data = $_SESSION['user_data'] ?? [];

// Test token validity
$profile_test = api_get_profil($token);
if (!$profile_test['success']) {
    $login_result = api_login('Widia', 'qwerty5*');
    if ($login_result['success']) {
        $token = $login_result['data']['token'];
        $_SESSION['token'] = $token;
        $_SESSION['user_data'] = $login_result['data'];
        $user_data = $login_result['data'];
    } else {
        header('Location: login.php');
        exit;
    }
}

// Get user profile data
$profile_result = api_get_profil($token);
$profile_data = $profile_result['success'] ? $profile_result['data'] : [];

// Extract user info
$pegawai = $profile_data['pegawai'] ?? $profile_data['data']['pegawai'] ?? $profile_data['data'] ?? [];
$user_name = $pegawai['nama_lengkap'] ?? $pegawai['nama'] ?? $user_data['nama'] ?? $user_data['nama_lengkap'] ?? 'User';
$user_jabatan = $pegawai['jabatan']['nama_jabatan'] ?? $pegawai['jabatan'] ?? $user_data['jabatan'] ?? $user_data['nama_jabatan'] ?? 'Karyawan';

$error_message = '';
$success_message = '';

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $tanggal_mulai = $_POST['tanggal_mulai'] ?? '';
    $tanggal_selesai = $_POST['tanggal_selesai'] ?? '';
    $alasan = $_POST['alasan'] ?? '';
    $jenis_cuti = $_POST['jenis_cuti'] ?? 'tahunan';

    if (empty($tanggal_mulai) || empty($tanggal_selesai) || empty($alasan)) {
        $error_message = 'Semua field harus diisi';
    } else {
        // Submit cuti request
        $cuti_data = [
            'tanggal_mulai' => $tanggal_mulai,
            'tanggal_selesai' => $tanggal_selesai,
            'alasan' => $alasan,
            'jenis_cuti' => $jenis_cuti
        ];

        $result = api_save_cuti($token, $cuti_data);

        if ($result['success']) {
            $success_message = 'Pengajuan cuti berhasil dikirim';
        } else {
            $error_message = $result['data']['pesan'] ?? 'Gagal mengajukan cuti';
        }
    }
}
?>

<!DOCTYPE html>
<html lang="id">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover, maximum-scale=1">
    <meta name="theme-color" content="#F2F2F7">
    <meta name="color-scheme" content="light">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="default">
    <title>Bank BIJ — Pengajuan cuti</title>
    <link rel="icon" type="image/png" href="<?php echo htmlspecialchars(site_public_asset('assets/images/bij_logo.png')); ?>">
    <link rel="stylesheet" href="<?php echo asset('css/style.css'); ?>">
    <link rel="stylesheet" href="<?php echo asset('css/tailwind.css'); ?>">
    <link rel="stylesheet" href="<?php echo asset('css/ios-app.css'); ?>">
    <link rel="stylesheet" href="<?php echo asset('fa-7.1.0-web/css/all.min.css'); ?>">
</head>

<body class="ios-app">
    <div class="mobile-frame p-4">
        <header class="ios-screen-head">
            <div class="ios-screen-head__row">
                <div class="bank-logo flex-shrink-0">
                    <img src="<?php echo asset('logo_bij3.png'); ?>" alt="Logo Bank BIJ">
                </div>
                <div class="ios-head-meta">
                    <h1 class="ios-head-brand">BPR <span class="ios-accent">Intan Jabar</span></h1>
                    <p class="ios-head-page">Pengajuan cuti</p>
                </div>
                <div class="ios-head-user">
                    <span class="ios-head-name"><?php echo htmlspecialchars($user_name); ?></span>
                    <span class="ios-head-role"><?php echo htmlspecialchars($user_jabatan); ?></span>
                </div>
            </div>
        </header>

        <nav class="ios-nav-back" aria-label="Navigasi">
            <a href="cuti.php"><i class="fas fa-chevron-left" aria-hidden="true"></i> Cuti</a>
        </nav>

        <!-- Error/Success Messages -->
        <?php if (!empty($error_message)): ?>
            <div class="mb-4 p-3 bg-red-100 border border-red-400 text-red-700 rounded-lg">
                <i class="fas fa-exclamation-circle mr-2"></i>
                <?php echo htmlspecialchars($error_message); ?>
            </div>
        <?php endif; ?>

        <?php if (!empty($success_message)): ?>
            <div class="mb-4 p-3 bg-green-100 border border-green-400 text-green-700 rounded-lg">
                <i class="fas fa-check-circle mr-2"></i>
                <?php echo htmlspecialchars($success_message); ?>
            </div>
        <?php endif; ?>

        <!-- Form Pengajuan Cuti -->
        <div class="dashboard-card p-4">
            <h2 class="text-lg font-bold text-gray-800 mb-4">Form Pengajuan Cuti</h2>

            <form method="POST" class="space-y-4">
                <!-- Jenis Cuti -->
                <div>
                    <label for="jenis_cuti" class="block text-sm font-medium text-gray-700 mb-2">Jenis Cuti</label>
                    <select id="jenis_cuti" name="jenis_cuti" class="w-full p-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                        <option value="tahunan">Cuti Tahunan</option>
                        <option value="sakit">Cuti Sakit</option>
                        <option value="melahirkan">Cuti Melahirkan</option>
                        <option value="lainnya">Lainnya</option>
                    </select>
                </div>

                <!-- Tanggal Mulai -->
                <div>
                    <label for="tanggal_mulai" class="block text-sm font-medium text-gray-700 mb-2">Tanggal Mulai</label>
                    <input type="date" id="tanggal_mulai" name="tanggal_mulai" required
                        class="w-full p-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                </div>

                <!-- Tanggal Selesai -->
                <div>
                    <label for="tanggal_selesai" class="block text-sm font-medium text-gray-700 mb-2">Tanggal Selesai</label>
                    <input type="date" id="tanggal_selesai" name="tanggal_selesai" required
                        class="w-full p-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                </div>

                <!-- Alasan -->
                <div>
                    <label for="alasan" class="block text-sm font-medium text-gray-700 mb-2">Alasan Cuti</label>
                    <textarea id="alasan" name="alasan" rows="4" required
                        placeholder="Jelaskan alasan pengajuan cuti..."
                        class="w-full p-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500"></textarea>
                </div>

                <!-- Submit Button -->
                <div class="pt-4">
                    <button type="submit" class="w-full py-3 bg-blue-700 text-white rounded-lg font-medium hover:bg-blue-800 transition-colors">
                        <i class="fas fa-paper-plane mr-2"></i>
                        Ajukan Cuti
                    </button>
                </div>
            </form>
        </div>

        <!-- Info Cuti -->
        <div class="dashboard-card p-4 mt-4">
            <h3 class="text-lg font-bold text-gray-800 mb-3">Informasi Cuti</h3>
            <div class="space-y-2 text-sm text-gray-600">
                <div class="flex justify-between">
                    <span>Cuti Tahunan Tersisa:</span>
                    <span class="font-medium">12 hari</span>
                </div>
                <div class="flex justify-between">
                    <span>Cuti Sakit Tersisa:</span>
                    <span class="font-medium">5 hari</span>
                </div>
                <div class="flex justify-between">
                    <span>Total Cuti Tahun Ini:</span>
                    <span class="font-medium">3 hari</span>
                </div>
            </div>
        </div>

        <!-- Footer -->
        <div class="text-center mt-6 ios-foot">
            <p>© 2024 Bank BIJ • v2.4.1</p>
        </div>
    </div>

    <script>
        // Set minimum date to today
        const today = new Date().toISOString().split('T')[0];
        document.getElementById('tanggal_mulai').min = today;
        document.getElementById('tanggal_selesai').min = today;

        // Update tanggal selesai when tanggal mulai changes
        document.getElementById('tanggal_mulai').addEventListener('change', function() {
            const tanggalMulai = this.value;
            const tanggalSelesai = document.getElementById('tanggal_selesai');
            tanggalSelesai.min = tanggalMulai;

            if (tanggalSelesai.value && tanggalSelesai.value < tanggalMulai) {
                tanggalSelesai.value = tanggalMulai;
            }
        });
    </script>
</body>

</html>