<?php
/**
 * Bank Receipt Heading Customization Settings
 * Allows administrators to customize bank receipt headings and information
 */

session_start();
require_once '../config/database.php';

// Check admin access
if (!isset($_SESSION['user_id']) || $_SESSION['user_role'] != 'admin') {
    header('Location: ../login.php');
    exit();
}

// Get system name for branding
$stmt = $pdo->query("SELECT setting_value FROM settings WHERE setting_key = 'site_name' LIMIT 1");
$site_name = $stmt->fetchColumn() ?: 'School Management System';

// Handle form submission
$message = '';
$message_type = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    try {
        $pdo->beginTransaction();
        
        // Update receipt settings
        $settings = [
            'bank_receipt_school_name' => trim($_POST['school_name']),
            'bank_receipt_address' => trim($_POST['address']),
            'bank_receipt_title' => trim($_POST['receipt_title']),
            'bank_receipt_subtitle' => trim($_POST['subtitle']),
            'bank_receipt_watermark' => trim($_POST['watermark'])
        ];
        
        foreach ($settings as $key => $value) {
            $stmt = $pdo->prepare("
                INSERT INTO settings (setting_key, setting_value, created_at) 
                VALUES (?, ?, NOW()) 
                ON DUPLICATE KEY UPDATE setting_value = ?, updated_at = NOW()
            ");
            $stmt->execute([$key, $value, $value]);
        }
        
        $pdo->commit();
        $message = 'ব্যাংক রসিদ সেটিংস সফলভাবে আপডেট হয়েছে!';
        $message_type = 'success';
        
    } catch (Exception $e) {
        $pdo->rollback();
        $message = 'ত্রুটি: ' . $e->getMessage();
        $message_type = 'danger';
    }
}

// Get current settings
$current_settings = [
    'school_name' => 'কার্পাসডাঙ্গা মাধ্যমিক বালিকা বিদ্যালয়',
    'address' => 'কার্পাসডাঙ্গা ভুমিহীন পাড়া, দামুড়হুদা, চুয়াডাঙ্গা',
    'receipt_title' => 'ব্যাংক ডিপোজিট রসিদ',
    'subtitle' => '',
    'watermark' => 'DEPOSIT'
];

$stmt = $pdo->query("SELECT setting_key, setting_value FROM settings WHERE setting_key LIKE 'bank_receipt_%'");
while ($row = $stmt->fetch()) {
    $key = str_replace('bank_receipt_', '', $row['setting_key']);
    $current_settings[$key] = $row['setting_value'];
}

?>

<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ব্যাংক রসিদ সেটিংস - <?php echo htmlspecialchars($site_name); ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Hind+Siliguri:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <style>
        body { 
            font-family: 'Hind Siliguri', sans-serif; 
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }
        .settings-container {
            background: white;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.2);
            overflow: hidden;
            max-width: 800px;
            margin: 0 auto;
        }
        .header {
            background: linear-gradient(135deg, #006747 0%, #004d35 100%);
            color: white;
            padding: 30px;
            text-align: center;
        }
        .content {
            padding: 30px;
        }
        .preview-box {
            background: #f8f9fa;
            border: 1px solid #dee2e6;
            border-radius: 10px;
            padding: 20px;
            margin: 20px 0;
        }
        .preview-header {
            text-align: center;
            border-bottom: 2px solid #000;
            padding-bottom: 10px;
            margin-bottom: 15px;
        }
        .preview-title {
            font-size: 18px;
            font-weight: bold;
            margin-bottom: 5px;
        }
        .preview-address {
            font-size: 14px;
            margin-bottom: 8px;
        }
        .preview-subtitle {
            font-size: 16px;
            font-weight: bold;
        }
        .watermark-preview {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%) rotate(-45deg);
            font-size: 50px;
            font-weight: bold;
            color: rgba(0,0,0,0.1);
            pointer-events: none;
        }
        .form-control:focus {
            border-color: #006747;
            box-shadow: 0 0 0 0.2rem rgba(0,103,71,0.25);
        }
        .btn-save {
            background: linear-gradient(135deg, #006747 0%, #004d35 100%);
            border: none;
            padding: 12px 25px;
            font-size: 16px;
            font-weight: 600;
            border-radius: 8px;
        }
        .btn-save:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(0,103,71,0.3);
        }
    </style>
</head>
<body>
    <div class="settings-container">
        <div class="header">
            <i class="fas fa-file-invoice fa-3x mb-3"></i>
            <h2>ব্যাংক রসিদ সেটিংস</h2>
            <p class="mb-0">ব্যাংক রসিদের হেডিং কাস্টমাইজ করুন</p>
        </div>
        
        <div class="content">
            <?php if ($message): ?>
                <div class="alert alert-<?php echo $message_type; ?> alert-dismissible fade show">
                    <?php echo htmlspecialchars($message); ?>
                    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                </div>
            <?php endif; ?>
            
            <form method="POST" id="settingsForm">
                <div class="mb-4">
                    <label for="schoolName" class="form-label fw-bold">প্রতিষ্ঠানের নাম *</label>
                    <input type="text" class="form-control" id="schoolName" name="school_name" 
                           value="<?php echo htmlspecialchars($current_settings['school_name']); ?>" required>
                    <div class="form-text">রসিদের উপরে প্রদর্শিত প্রতিষ্ঠানের নাম</div>
                </div>
                
                <div class="mb-4">
                    <label for="address" class="form-label fw-bold">ঠিকানা</label>
                    <textarea class="form-control" id="address" name="address" rows="2"><?php echo htmlspecialchars($current_settings['address']); ?></textarea>
                    <div class="form-text">প্রতিষ্ঠানের সম্পূর্ণ ঠিকানা</div>
                </div>
                
                <div class="mb-4">
                    <label for="receiptTitle" class="form-label fw-bold">রসিদ শিরোনাম *</label>
                    <input type="text" class="form-control" id="receiptTitle" name="receipt_title" 
                           value="<?php echo htmlspecialchars($current_settings['receipt_title']); ?>" required>
                    <div class="form-text">রসিদের মূল শিরোনাম (যেমন: ব্যাংক ডিপোজিট রসিদ)</div>
                </div>
                
                <div class="mb-4">
                    <label for="subtitle" class="form-label fw-bold">উপ-শিরোনাম</label>
                    <input type="text" class="form-control" id="subtitle" name="subtitle" 
                           value="<?php echo htmlspecialchars($current_settings['subtitle']); ?>">
                    <div class="form-text">অতিরিক্ত শিরোনাম বা বিবরণ (ঐচ্ছিক)</div>
                </div>
                
                <div class="mb-4">
                    <label for="watermark" class="form-label fw-bold">ওয়াটারমার্ক</label>
                    <input type="text" class="form-control" id="watermark" name="watermark" 
                           value="<?php echo htmlspecialchars($current_settings['watermark']); ?>">
                    <div class="form-text">রসিদের পিছনে প্রদর্শিত ওয়াটারমার্ক টেক্সট</div>
                </div>
                
                <div class="d-grid gap-2 d-md-flex justify-content-md-end">
                    <button type="submit" class="btn btn-save">
                        <i class="fas fa-save"></i> সেটিংস সংরক্ষণ করুন
                    </button>
                    <button type="button" class="btn btn-secondary" onclick="previewReceipt()">
                        <i class="fas fa-eye"></i> প্রিভিউ
                    </button>
                </div>
            </form>
            
            <!-- Preview Section -->
            <div class="preview-box position-relative">
                <h5 class="mb-3"><i class="fas fa-search"></i> প্রিভিউ</h5>
                <div class="preview-header">
                    <div class="preview-title" id="previewTitle"><?php echo htmlspecialchars($current_settings['school_name']); ?></div>
                    <?php if ($current_settings['address']): ?>
                        <div class="preview-address"><?php echo htmlspecialchars($current_settings['address']); ?></div>
                    <?php endif; ?>
                    <div class="preview-subtitle" id="previewSubtitle"><?php echo htmlspecialchars($current_settings['receipt_title']); ?></div>
                </div>
                <div class="watermark-preview" id="previewWatermark"><?php echo htmlspecialchars($current_settings['watermark']); ?></div>
                <div class="text-center mt-4">
                    <small class="text-muted">এটি রসিদের হেডিং কীভাবে প্রদর্শিত হবে তার প্রিভিউ</small>
                </div>
            </div>
            
            <div class="alert alert-info">
                <h6><i class="fas fa-info-circle"></i> নির্দেশনা:</h6>
                <ul class="mb-0">
                    <li>সকল তথ্য বাংক রসিদ প্রিন্টের সময় প্রয়োগ হবে</li>
                    <li>পরিবর্তনগুলো তাৎক্ষণিকভাবে প্রয়োগ হবে</li>
                    <li>ঠিকানা এবং উপ-শিরোনাম ঐচ্ছিক</li>
                </ul>
            </div>
        </div>
    </div>
    
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        // Live preview functionality
        document.getElementById('schoolName').addEventListener('input', function() {
            document.getElementById('previewTitle').textContent = this.value || 'প্রতিষ্ঠানের নাম';
        });
        
        document.getElementById('receiptTitle').addEventListener('input', function() {
            document.getElementById('previewSubtitle').textContent = this.value || 'রসিদ শিরোনাম';
        });
        
        document.getElementById('watermark').addEventListener('input', function() {
            document.getElementById('previewWatermark').textContent = this.value || 'WATERMARK';
        });
        
        function previewReceipt() {
            // Scroll to preview section
            document.querySelector('.preview-box').scrollIntoView({behavior: 'smooth'});
        }
        
        // Form validation
        document.getElementById('settingsForm').addEventListener('submit', function(e) {
            const schoolName = document.getElementById('schoolName').value.trim();
            const receiptTitle = document.getElementById('receiptTitle').value.trim();
            
            if (!schoolName || !receiptTitle) {
                e.preventDefault();
                alert('প্রতিষ্ঠানের নাম এবং রসিদ শিরোনাম অবশ্যই প্রয়োজন!');
                return false;
            }
        });
    </script>
</body>
</html>