<?php
session_start();
if ($_SESSION['role'] != 'admin') {
    header("Location: ../login.php");
    exit();
}
include '../config/db.php';

$message = '';
$error = '';

// Handle signature upload
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $position = $_POST['position'];
    $signatory_name = trim($_POST['signatory_name']);
    $designation = trim($_POST['designation']);
    
    // Create upload directory if not exists
    $upload_dir = '../assets/uploads/signatures/';
    if (!file_exists($upload_dir)) {
        mkdir($upload_dir, 0777, true);
    }
    
    // Handle file upload
    $signature_path = null;
    if (isset($_FILES['signature_image']) && $_FILES['signature_image']['error'] == 0) {
        $allowed = ['png', 'jpg', 'jpeg', 'gif'];
        $filename = $_FILES['signature_image']['name'];
        $fileext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
        
        if (in_array($fileext, $allowed)) {
            $new_filename = "signature_" . $position . "_" . time() . "." . $fileext;
            $target = $upload_dir . $new_filename;
            
            if (move_uploaded_file($_FILES['signature_image']['tmp_name'], $target)) {
                $signature_path = "assets/uploads/signatures/" . $new_filename;
            }
        }
    }
    
    // Update database
    if ($signature_path) {
        $stmt = $conn->prepare("UPDATE org_signatures SET signature_image = ?, signatory_name = ?, designation = ? WHERE position = ?");
        $stmt->bind_param("ssss", $signature_path, $signatory_name, $designation, $position);
    } else {
        $stmt = $conn->prepare("UPDATE org_signatures SET signatory_name = ?, designation = ? WHERE position = ?");
        $stmt->bind_param("sss", $signatory_name, $designation, $position);
    }
    
    if ($stmt->execute()) {
        $message = "Signature settings updated successfully!";
    } else {
        $error = "Error updating settings: " . $conn->error;
    }
    $stmt->close();
}

// Get current signatures
$president = $conn->query("SELECT * FROM org_signatures WHERE position = 'president'")->fetch_assoc();
$secretary = $conn->query("SELECT * FROM org_signatures WHERE position = 'secretary'")->fetch_assoc();
$treasurer = $conn->query("SELECT * FROM org_signatures WHERE position = 'treasurer'")->fetch_assoc();

function getSignaturePreview($path) {
    if (!empty($path) && file_exists("../" . $path)) {
        return "../" . $path;
    }
    return null;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Manage Signatures - Admin Panel</title>
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Poppins', sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 2rem;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
        }

        /* Header */
        .header {
            background: white;
            border-radius: 20px;
            padding: 1.5rem 2rem;
            margin-bottom: 2rem;
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            gap: 1rem;
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
        }

        .header h1 {
            font-size: 1.8rem;
            color: #333;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .header h1 i {
            color: #2c5f2d;
        }

        .back-btn {
            background: #6c757d;
            color: white;
            padding: 0.75rem 1.5rem;
            border-radius: 10px;
            text-decoration: none;
            transition: all 0.3s;
        }

        .back-btn:hover {
            background: #5a6268;
            transform: translateY(-2px);
        }

        /* Signature Cards */
        .signatures-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(350px, 1fr));
            gap: 1.5rem;
            margin-bottom: 2rem;
        }

        .signature-card {
            background: white;
            border-radius: 20px;
            overflow: hidden;
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
            transition: transform 0.3s;
        }

        .signature-card:hover {
            transform: translateY(-5px);
        }

        .card-header {
            background: linear-gradient(135deg, #2c5f2d, #1a3a1a);
            color: white;
            padding: 1.2rem 1.5rem;
        }

        .card-header h2 {
            font-size: 1.2rem;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .card-body {
            padding: 1.5rem;
        }

        /* Current Signature Display */
        .current-signature {
            text-align: center;
            padding: 1rem;
            background: #f8f9fa;
            border-radius: 15px;
            margin-bottom: 1.5rem;
        }

        .signature-preview {
            margin-bottom: 0.5rem;
        }

        .signature-preview img {
            max-width: 200px;
            max-height: 80px;
            border: 1px solid #ddd;
            padding: 0.5rem;
            background: white;
            border-radius: 8px;
        }

        .signature-name {
            font-weight: 600;
            color: #333;
            margin-top: 0.5rem;
        }

        .signature-designation {
            font-size: 0.8rem;
            color: #666;
        }

        .no-signature {
            color: #999;
            font-style: italic;
            padding: 1rem;
        }

        /* Form Styles */
        .form-group {
            margin-bottom: 1rem;
        }

        .form-group label {
            display: block;
            margin-bottom: 0.5rem;
            font-weight: 500;
            color: #333;
        }

        .form-group label i {
            color: #2c5f2d;
            margin-right: 5px;
        }

        .form-group input {
            width: 100%;
            padding: 0.75rem;
            border: 2px solid #e0e0e0;
            border-radius: 10px;
            font-family: 'Poppins', sans-serif;
            transition: all 0.3s;
        }

        .form-group input:focus {
            outline: none;
            border-color: #2c5f2d;
            box-shadow: 0 0 0 3px rgba(44,95,45,0.1);
        }

        .file-input {
            padding: 0.5rem;
            border: 2px dashed #e0e0e0;
            border-radius: 10px;
            background: #fafafa;
        }

        .btn-submit {
            width: 100%;
            padding: 0.75rem;
            background: linear-gradient(135deg, #2c5f2d, #1a3a1a);
            color: white;
            border: none;
            border-radius: 10px;
            font-size: 1rem;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s;
        }

        .btn-submit:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(44,95,45,0.3);
        }

        /* Alert Messages */
        .alert {
            padding: 1rem;
            border-radius: 10px;
            margin-bottom: 1rem;
            display: flex;
            align-items: center;
            gap: 10px;
            animation: slideIn 0.3s ease-out;
        }

        .alert-success {
            background: #d4edda;
            color: #155724;
            border-left: 4px solid #28a745;
        }

        .alert-error {
            background: #f8d7da;
            color: #721c24;
            border-left: 4px solid #dc3545;
        }

        @keyframes slideIn {
            from {
                opacity: 0;
                transform: translateY(-10px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        /* Preview Note */
        .preview-note {
            font-size: 0.7rem;
            color: #999;
            margin-top: 0.5rem;
            text-align: center;
        }

        @media (max-width: 768px) {
            body {
                padding: 1rem;
            }

            .signatures-grid {
                grid-template-columns: 1fr;
            }

            .header {
                flex-direction: column;
                text-align: center;
            }
        }
    </style>
</head>
<body>
<div class="container">
    <div class="header">
        <h1>
            <i class="fas fa-signature"></i>
            Manage Authorised Signatures
        </h1>
        <a href="dashboard.php" class="back-btn">
            <i class="fas fa-arrow-left"></i> Back to Dashboard
        </a>
    </div>

    <?php if($message): ?>
    <div class="alert alert-success">
        <i class="fas fa-check-circle"></i>
        <span><?= $message ?></span>
    </div>
    <?php endif; ?>

    <?php if($error): ?>
    <div class="alert alert-error">
        <i class="fas fa-exclamation-circle"></i>
        <span><?= $error ?></span>
    </div>
    <?php endif; ?>

    <div class="signatures-grid">
        <!-- President Signature -->
        <div class="signature-card">
            <div class="card-header">
                <h2><i class="fas fa-crown"></i> President Signature</h2>
            </div>
            <div class="card-body">
                <div class="current-signature">
                    <div class="signature-preview">
                        <?php if($president && getSignaturePreview($president['signature_image'])): ?>
                            <img src="<?= getSignaturePreview($president['signature_image']) ?>" alt="President Signature">
                        <?php else: ?>
                            <div class="no-signature">No signature uploaded yet</div>
                        <?php endif; ?>
                    </div>
                    <div class="signature-name"><?= htmlspecialchars($president['signatory_name'] ?? 'Not set') ?></div>
                    <div class="signature-designation"><?= htmlspecialchars($president['designation'] ?? 'President') ?></div>
                </div>
                
                <form method="POST" enctype="multipart/form-data">
                    <input type="hidden" name="position" value="president">
                    <div class="form-group">
                        <label><i class="fas fa-user-tie"></i> Signatory Name</label>
                        <input type="text" name="signatory_name" value="<?= htmlspecialchars($president['signatory_name'] ?? '') ?>" required>
                    </div>
                    <div class="form-group">
                        <label><i class="fas fa-briefcase"></i> Designation</label>
                        <input type="text" name="designation" value="<?= htmlspecialchars($president['designation'] ?? 'President') ?>" required>
                    </div>
                    <div class="form-group">
                        <label><i class="fas fa-upload"></i> Signature Image (PNG with transparent background recommended)</label>
                        <input type="file" name="signature_image" class="file-input" accept=".png,.jpg,.jpeg,.gif">
                        <div class="preview-note">Leave empty to keep current signature</div>
                    </div>
                    <button type="submit" class="btn-submit">
                        <i class="fas fa-save"></i> Update President Signature
                    </button>
                </form>
            </div>
        </div>

        <!-- Secretary Signature -->
        <div class="signature-card">
            <div class="card-header">
                <h2><i class="fas fa-file-signature"></i> Secretary Signature</h2>
            </div>
            <div class="card-body">
                <div class="current-signature">
                    <div class="signature-preview">
                        <?php if($secretary && getSignaturePreview($secretary['signature_image'])): ?>
                            <img src="<?= getSignaturePreview($secretary['signature_image']) ?>" alt="Secretary Signature">
                        <?php else: ?>
                            <div class="no-signature">No signature uploaded yet</div>
                        <?php endif; ?>
                    </div>
                    <div class="signature-name"><?= htmlspecialchars($secretary['signatory_name'] ?? 'Not set') ?></div>
                    <div class="signature-designation"><?= htmlspecialchars($secretary['designation'] ?? 'Secretary') ?></div>
                </div>
                
                <form method="POST" enctype="multipart/form-data">
                    <input type="hidden" name="position" value="secretary">
                    <div class="form-group">
                        <label><i class="fas fa-user"></i> Signatory Name</label>
                        <input type="text" name="signatory_name" value="<?= htmlspecialchars($secretary['signatory_name'] ?? '') ?>" required>
                    </div>
                    <div class="form-group">
                        <label><i class="fas fa-briefcase"></i> Designation</label>
                        <input type="text" name="designation" value="<?= htmlspecialchars($secretary['designation'] ?? 'Secretary') ?>" required>
                    </div>
                    <div class="form-group">
                        <label><i class="fas fa-upload"></i> Signature Image (PNG with transparent background recommended)</label>
                        <input type="file" name="signature_image" class="file-input" accept=".png,.jpg,.jpeg,.gif">
                        <div class="preview-note">Leave empty to keep current signature</div>
                    </div>
                    <button type="submit" class="btn-submit">
                        <i class="fas fa-save"></i> Update Secretary Signature
                    </button>
                </form>
            </div>
        </div>

        <!-- Treasurer Signature -->
        <div class="signature-card">
            <div class="card-header">
                <h2><i class="fas fa-chart-line"></i> Treasurer Signature</h2>
            </div>
            <div class="card-body">
                <div class="current-signature">
                    <div class="signature-preview">
                        <?php if($treasurer && getSignaturePreview($treasurer['signature_image'])): ?>
                            <img src="<?= getSignaturePreview($treasurer['signature_image']) ?>" alt="Treasurer Signature">
                        <?php else: ?>
                            <div class="no-signature">No signature uploaded yet</div>
                        <?php endif; ?>
                    </div>
                    <div class="signature-name"><?= htmlspecialchars($treasurer['signatory_name'] ?? 'Not set') ?></div>
                    <div class="signature-designation"><?= htmlspecialchars($treasurer['designation'] ?? 'Treasurer') ?></div>
                </div>
                
                <form method="POST" enctype="multipart/form-data">
                    <input type="hidden" name="position" value="treasurer">
                    <div class="form-group">
                        <label><i class="fas fa-user"></i> Signatory Name</label>
                        <input type="text" name="signatory_name" value="<?= htmlspecialchars($treasurer['signatory_name'] ?? '') ?>" required>
                    </div>
                    <div class="form-group">
                        <label><i class="fas fa-briefcase"></i> Designation</label>
                        <input type="text" name="designation" value="<?= htmlspecialchars($treasurer['designation'] ?? 'Treasurer') ?>" required>
                    </div>
                    <div class="form-group">
                        <label><i class="fas fa-upload"></i> Signature Image (PNG with transparent background recommended)</label>
                        <input type="file" name="signature_image" class="file-input" accept=".png,.jpg,.jpeg,.gif">
                        <div class="preview-note">Leave empty to keep current signature</div>
                    </div>
                    <button type="submit" class="btn-submit">
                        <i class="fas fa-save"></i> Update Treasurer Signature
                    </button>
                </form>
            </div>
        </div>
    </div>
</div>
</body>
</html>