<?php
require_once 'config.php';

// إنشاء الاتصال
$pdo = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset=utf8mb4', DB_USER, DB_PASS);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
?>
<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
    <meta charset="UTF-8">
    <title>لوحة إدارة التراخيص</title>
    <!-- خط عربي حديث -->
    <link href="https://fonts.googleapis.com/css2?family=Cairo:wght@400;700&display=swap" rel="stylesheet">
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
    <link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
    <style>
        body { font-family: 'Cairo', sans-serif; }
        table th, table td { text-align: center; }
        .tab-btn { transition: all 0.3s; }
        .tab-btn.active { transform: scale(1.05); }
        .action-btn { transition: all 0.2s; }
        .action-btn:hover { transform: scale(1.1); }
    </style>
</head>
<body class="bg-gray-100">

<div class="max-w-7xl mx-auto mt-10 bg-white shadow-2xl rounded-2xl p-6">
    <h2 class="text-4xl font-bold text-blue-800 text-center mb-8">لوحة إدارة التراخيص</h2>

    <div class="flex flex-col sm:flex-row justify-between items-center mb-6 gap-4">
        <div class="flex gap-3">
            <button id="tabLicenses" class="tab-btn bg-blue-600 text-white px-5 py-2 rounded-lg hover:bg-blue-700 active">تراخيص العملاء</button>
            <button id="tabTrials" class="tab-btn bg-gray-400 text-white px-5 py-2 rounded-lg hover:bg-gray-500">التراخيص التجريبية</button>
        </div>
        <button id="addLicenseBtn" class="bg-green-600 text-white px-6 py-2 rounded-lg hover:bg-green-700 font-bold shadow-lg">
            + إضافة ترخيص جديد
        </button>
    </div>

    <!-- جدول التراخيص -->
    <div class="overflow-x-auto">
        <table class="min-w-full border rounded-lg shadow-lg bg-white" id="licensesTable">
            <thead class="bg-blue-700 text-white text-lg">
                <tr>
                    <th class="p-3">#</th>
                    <th class="p-3">مفتاح الترخيص</th>
                    <th class="p-3">العميل</th>
                    <th class="p-3">النطاق</th>
                    <th class="p-3">IP</th>
                    <th class="p-3">تاريخ الانتهاء</th>
                    <th class="p-3">الحالة</th>
                    <th class="p-3">إجراءات</th>
                </tr>
            </thead>
            <tbody id="licenseData" class="text-gray-700 text-md"></tbody>
        </table>
    </div>
</div>

<!-- نافذة الإضافة / التعديل -->
<div id="licenseModal" class="fixed inset-0 bg-black bg-opacity-50 hidden flex items-center justify-center z-50">
    <div class="bg-white rounded-2xl p-6 w-96 shadow-2xl">
        <h3 class="text-2xl font-bold mb-4" id="modalTitle">إضافة ترخيص جديد</h3>
        <form id="licenseForm" class="space-y-3">
            <input type="hidden" name="id" id="license_id">

            <div>
                <label class="block font-semibold">اسم العميل:</label>
                <input type="text" name="client_name" id="client_name" class="border p-2 w-full rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-400">
            </div>
            <div>
                <label class="block font-semibold">النطاق:</label>
                <input type="text" name="domain" id="domain" class="border p-2 w-full rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-400">
            </div>
            <div>
                <label class="block font-semibold">IP:</label>
                <input type="text" name="ip" id="ip" class="border p-2 w-full rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-400">
            </div>
            <div>
                <label class="block font-semibold">تاريخ الانتهاء:</label>
                <input type="date" name="expiry_date" id="expiry_date" class="border p-2 w-full rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-400">
            </div>

            <div class="flex justify-between mt-4">
                <button type="button" id="saveBtn" class="bg-blue-600 text-white px-5 py-2 rounded-lg hover:bg-blue-700 font-bold shadow-md action-btn">حفظ</button>
                <button type="button" id="closeModal" class="bg-gray-500 text-white px-5 py-2 rounded-lg hover:bg-gray-600 font-bold shadow-md action-btn">إغلاق</button>
            </div>
        </form>
    </div>
</div>

<script>
$(function(){
    let currentTab = 'licenses';
    loadLicenses();

    function setActiveTab(btn) {
        $('.tab-btn').removeClass('bg-blue-600 active').addClass('bg-gray-400');
        $(btn).removeClass('bg-gray-400').addClass('bg-blue-600 active');
    }

    $('#tabLicenses').click(function(){
        currentTab = 'licenses';
        setActiveTab(this);
        loadLicenses();
    });

    $('#tabTrials').click(function(){
        currentTab = 'trials';
        setActiveTab(this);
        loadTrials();
    });

    function loadLicenses(){
        $.get('manage_licenses_ajax.php', {action:'list_licenses'}, function(data){
            $('#licenseData').html(data);
        });
    }

    function loadTrials(){
        $.get('manage_licenses_ajax.php', {action:'list_trials'}, function(data){
            $('#licenseData').html(data);
        });
    }

    $('#addLicenseBtn').click(function(){
        $('#license_id').val('');
        $('#licenseForm')[0].reset();
        $('#modalTitle').text('إضافة ترخيص جديد');
        $('#licenseModal').removeClass('hidden');
    });

    $('#closeModal').click(function(){
        $('#licenseModal').addClass('hidden');
    });

    $('#saveBtn').click(function(){
        $.post('manage_licenses_ajax.php', $('#licenseForm').serialize() + '&action=save_license', function(res){
            alert(res);
            $('#licenseModal').addClass('hidden');
            currentTab==='licenses' ? loadLicenses() : loadTrials();
        });
    });

    $(document).on('click', '.editBtn', function(){
        let id = $(this).data('id');
        $.get('manage_licenses_ajax.php', {action:'get_license', id:id}, function(data){
            let r = JSON.parse(data);
            $('#license_id').val(r.id);
            $('#client_name').val(r.client_name);
            $('#domain').val(r.domain);
            $('#ip').val(r.ip);
            $('#expiry_date').val(r.expiry_date);
            $('#modalTitle').text('تعديل ترخيص');
            $('#licenseModal').removeClass('hidden');
        });
    });

    $(document).on('click', '.deleteBtn', function(){
        if(confirm('هل أنت متأكد من حذف هذا السجل؟')){
            $.post('manage_licenses_ajax.php', {action:'delete_license', id:$(this).data('id'), type:currentTab}, function(res){
                alert(res);
                currentTab==='licenses' ? loadLicenses() : loadTrials();
            });
        }
    });

    $(document).on('click', '.toggleStatusBtn', function(){
        $.post('manage_licenses_ajax.php', {action:'toggle_status', id:$(this).data('id'), type:currentTab}, function(res){
            alert(res);
            currentTab==='licenses' ? loadLicenses() : loadTrials();
        });
    });
});
</script>
</body>
</html>
