<?php
session_start();
require 'db.php';

if (!isset($_SESSION['user_id'])) {
    header("Location: giris.php");
    exit;
}

$user_id = $_SESSION['user_id'];

$stmt = $pdo->prepare("SELECT fullname, username, email, reference_code FROM users WHERE id = ?");
$stmt->execute([$user_id]);
$user = $stmt->fetch();

// Banka bilgilerini getir
$stmt = $pdo->prepare("SELECT id, bank_name, iban FROM bank_accounts WHERE user_id = ?");
$stmt->execute([$user_id]);
$bank_accounts = $stmt->fetchAll(PDO::FETCH_ASSOC);

?>
<!DOCTYPE html>
<html lang="tr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
    <title>Profil Sayfası</title>
    <style>
        body {
            margin: 0;
            font-family: 'Arial', sans-serif;
            background-color: #fff;
            color: #333;
        }
        header {
            background-color: #fff;
            color: #333;
            text-align: center;
            padding: 20px;
        }
        header h1 {
            margin: 0;
            font-size: 24px;
        }
        
        a {
            text-decoration: none; /* Çizgiyi kaldırmak için */
        }
        .top-right {
            position: absolute;
            right: 25px;
            top: 40px;
        }
        .material-icons {
            cursor: pointer;
        }
        .material-icons:active {
            cursor: pointer;
            color: #459D7A;
        }
        .profile-container {
            padding: 20px;
        }
        .profile-header {
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: 20px;
        }
        .profile-picture {
            background-color: #4CAF50;
            border-radius: 50%;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            width: 80px;
            height: 80px;
            color: white;
        }
        .profile-info {
            flex-grow: 1;
            margin-left: 20px;
        }
        .accordion-container {
            margin: 20px 0;
        }
        .accordion-item {
            background-color: #FFFFFF;
            margin-bottom: 8px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            overflow: hidden;
        }
        .accordion-button {
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: 15px;
            font-weight: bold;
            cursor: pointer;
            border: none;
            outline: none;
            width: 90%;
            text-align: left;
            background-color: #fff;
            border-bottom: 1px solid #eeeeee;
        }
        .accordion-button:hover {
            background-color: #f6f6f6;
        }
        .accordion-content {
            display: none;
            padding: 15px;
            border-top: 1px solid #eeeeee;
        }
        .accordion-content a {
            color: #4CAF50;
        }
        .left-icon, .right-icon {
            font-size: 24px;
            color: #6e6e6e;
        }
        .man {
            font-size: 44px;
        }
        .footer-menu {
            display: flex;
            justify-content: space-around;
            padding: 20px 0;
            background-color: #fff;
            position: fixed;
            bottom: 0;
            width: 100%;
            box-shadow: 0 -2px 10px rgba(0,0,0,0.1);
        }
        .active {
            font-weight: 600;
            color: #459D7A !important;
        }
        .footer-menu a {
            text-align: center;
            flex-grow: 1;
            display: flex;
            flex-direction: column;
            align-items: center;
            text-decoration: none;
            color: #333;
        }
        .footer-menu a i {
            font-size: 24px;
        }
        .footer-menu a span {
            font-size: 12px;
            margin-top: 4px;
        }
        .reference-code-container {
            display: flex;
            align-items: center;
        }
        .reference-code {
            margin-right: 10px;
            font-size: 16px;
        }
        .copy-icon {
            margin-right: 5px;
            cursor: pointer;
            color: #459D7A;
        }
        .share-icon {
            margin-right: 5px;
            cursor: pointer;
            color: #459D7A;
        }
        .share-button {
            background-color: #f4f4f4;
            border: 1px solid #ccc;
            padding: 5px 10px;
            margin-left: 10px;
            border-radius: 5px;
            cursor: pointer;
        }
        .share-button i {
            margin-right: 5px;
        }
        .add-bank-button {
            background-color: #4CAF50;
            color: white;
            border: none;
            padding: 8px 16px;
            cursor: pointer;
            border-radius: 4px;
        }
        .add-bank-button:hover {
            background-color: #45a049;
        }
        .bank-account {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 10px;
    padding: 10px;
    border: 1px solid #eee;
    border-radius: 5px;
}

.bank-details {
    flex-grow: 1;
}

.delete-icon {
    color: red;
    cursor: pointer;
}

        .modal {
            display: none;
            position: fixed;
            z-index: 1;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            overflow: auto;
            background-color: rgba(0,0,0,0.4);
            padding-top: 60px;
        }
        .modal-content {
            background-color: #fefefe;
            margin: 5% auto;
            padding: 20px;
            border: 1px solid #888;
            width: 80%;
            max-width: 600px;
            border-radius: 8px;
        }
        .close {
            color: #aaa;
            float: right;
            font-size: 28px;
            font-weight: bold;
        }
        .close:hover,
        .close:focus {
            color: black;
            text-decoration: none;
            cursor: pointer;
        }
        .modal-header {
            padding-bottom: 10px;
            border-bottom: 1px solid #ddd;
        }
        .modal-header h2 {
            margin: 0;
        }
        .modal-body {
            padding: 10px 0;
        }
        .modal-footer {
            padding-top: 10px;
            text-align: right;
        }
    </style>
</head>
<body>
<header>
    <div class="top-right">
        <i class="material-icons">notifications</i>
    </div>
</header>

<div class="profile-container">
    <div class="profile-header">
        <div class="profile-picture">
            <i class="material-icons man">account_circle</i>
        </div>
        <div class="profile-info">
            <h2><?php echo htmlspecialchars($user['fullname']); ?> (<?php echo htmlspecialchars($user['username']); ?>)</h2>
            <p><?php echo htmlspecialchars($user['email']); ?></p>
        </div>
    </div>

    <div class="accordion-container">
        <div class="accordion-item">
            <div class="accordion-button" onclick="toggleAccordion('refcode')">
                <i class="material-icons left-icon">vpn_key</i>
                Referans Kodu
                <i class="material-icons right-icon">chevron_right</i>
            </div>
            <div class="accordion-content" id="refcode">
                <div class="reference-code-container">
                    <span class="reference-code"><?php echo htmlspecialchars($user['reference_code']); ?></span>
                    <i class="material-icons copy-icon" onclick="copyReferenceCode()">content_copy</i>
                    <i class="material-icons share-icon" onclick="shareReferenceCode()">share</i>
                </div>
            </div>
        </div>
        <div class="accordion-item">
            <div class="accordion-button" onclick="toggleAccordion('settings')">
                <i class="material-icons left-icon">settings</i>
                Ayarlar
                <i class="material-icons right-icon">chevron_right</i>
            </div>
            <div class="accordion-content" id="settings">
                <a href="mailto:info@sauspharma.com">Verileri Silme Talebi Gönder</a>
            </div>
        </div>

        <div class="accordion-item">
            <div class="accordion-button" onclick="toggleAccordion('support')">
                <i class="material-icons left-icon">support_agent</i>
                Destek
                <i class="material-icons right-icon">chevron_right</i>
            </div>
            <div class="accordion-content" id="support">
                <a href="mailto:info@sauspharma.com">info@sauspharma.com</a>
            </div>
        </div>

        <div class="accordion-item">
            <div class="accordion-button" onclick="toggleAccordion('bankinfo')">
                <i class="material-icons left-icon">account_balance</i>
                Ödeme Bilgileri
                <i class="material-icons right-icon">chevron_right</i>
            </div>
            <div class="accordion-content" id="bankinfo">
                <?php if (count($bank_accounts) > 0): ?>
                    <?php foreach ($bank_accounts as $bank_account): ?>
                         <div class="bank-account">
            <div class="bank-details">
                <strong><?php echo htmlspecialchars($bank_account['bank_name']); ?></strong>
                <p><?php echo htmlspecialchars($bank_account['iban']); ?></p>
            </div>
            <i class="material-icons delete-icon" onclick="deleteBank(<?php echo $bank_account['id']; ?>)">cancel</i>
        </div>
                    <?php endforeach; ?>
                <?php else: ?>
                    <p>Henüz banka bilgisi eklenmemiş.</p>
                <?php endif; ?>
                <button class="add-bank-button" onclick="openModal()">Yeni Banka Ekle</button>
            </div>
        </div>
        <div class="accordion-item">
            <a href="giris.php">
            <div class="accordion-button">
                <i class="material-icons left-icon">logout</i>
                Çıkış
                <i class="material-icons right-icon">chevron_right</i>
            </div>
        </div>
        </a>
    </div>
    </div>
</div>

<div class="footer-menu">
    <a href="dashboard.php">
        <i class="material-icons">home</i>
        <span>Ana Sayfa</span>
    </a>
    <a href="sale_list.php">
        <i class="material-icons">monetization_on</i>
        <span>Satışlar</span>
    </a>
    <a href="profil.php" class="active">
        <i class="material-icons">person</i>
        <span>Profil</span>
    </a>
</div>

<div id="myModal" class="modal">
    <div class="modal-content">
        <div class="modal-header">
            <span class="close" onclick="closeModal()">&times;</span>
            <h2>Yeni Banka Bilgisi Ekle</h2>
        </div>
        <form id="bankForm" action="save_bank.php" method="POST">
            <div class="modal-body">
                <label for="bankName">Banka Adı:</label><br>
                <input type="text" id="bankName" name="bankName" required><br><br>
                <label for="iban">IBAN:</label><br>
                <input type="text" id="iban" name="iban" required><br><br>
            </div>
            <div class="modal-footer">
                <button type="submit" class="add-bank-button">Kaydet</button>
            </div>
        </form>
    </div>
</div>

<script>
var originalAlert = window.alert;

// Alert fonksiyonunu özelleştir
window.alert = function(message) {
    // Özel alert oluştur
    var alertBox = document.createElement('div');
    alertBox.style.position = 'fixed';
    alertBox.style.top = '50%';
    alertBox.style.left = '50%';
    alertBox.style.transform = 'translate(-50%, -50%)';
    alertBox.style.backgroundColor = 'white';
    alertBox.style.padding = '20px';
    alertBox.style.borderRadius = '5px';
    alertBox.style.boxShadow = '0 0 10px rgba(0,0,0,0.25)';
    alertBox.style.zIndex = '9999';
    alertBox.style.maxWidth = '80%';

    var title = document.createElement('h2');
    title.textContent = 'Kazan Kazan';
    title.style.marginTop = '0';
    alertBox.appendChild(title);

    var content = document.createElement('p');
    content.textContent = message;
    alertBox.appendChild(content);

    var okButton = document.createElement('button');
    okButton.textContent = 'Tamam';
    okButton.style.display = 'block';
    okButton.style.marginTop = '10px';
    okButton.style.padding = '5px 10px';
    okButton.onclick = function() {
        document.body.removeChild(alertBox);
    };
    alertBox.appendChild(okButton);

    document.body.appendChild(alertBox);
};

    function toggleAccordion(id) {
        var content = document.getElementById(id);
        var accordions = document.getElementsByClassName('accordion-content');

        for (var i = 0; i < accordions.length; i++) {
            var openAccordion = accordions[i];
            if (openAccordion.id !== id) {
                openAccordion.style.display = 'none';
                openAccordion.previousElementSibling.querySelector('.right-icon').textContent = 'chevron_right';
            }
        }

        if (content.style.display === 'block') {
            content.style.display = 'none';
            content.previousElementSibling.querySelector('.right-icon').textContent = 'chevron_right';
        } else {
            content.style.display = 'block';
            content.previousElementSibling.querySelector('.right-icon').textContent = 'expand_less';
        }
    }

    function copyReferenceCode() {
        var referenceCodeElement = document.querySelector('.reference-code');
        var tempInput = document.createElement('input');
        tempInput.value = referenceCodeElement.innerText.trim();
        document.body.appendChild(tempInput);
        tempInput.select();
        document.execCommand('copy');
        document.body.removeChild(tempInput);
        alert('Referans kodu kopyalandı: ' + tempInput.value);
    }

    function shareReferenceCode() {
    var referenceCode = document.querySelector('.reference-code').innerText.trim();
    var shareText = `Referans Kodum: ${referenceCode}`;
    

    if (window.Android) {
        // Android WebView için
        window.Android.share(shareText, shareUrl);
    } else if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.share) {
        // iOS WebView için
        window.webkit.messageHandlers.share.postMessage({text: shareText});
    } else if (navigator.share) {
        // Web Share API
        navigator.share({
            title: 'Referans Kod Paylaşımı',
            text: shareText
          
        }).catch(error => console.error('Paylaşma hatası:', error));
    } else {
        fallbackShare();
    }
}
    function openModal() {
        var modal = document.getElementById('myModal');
        modal.style.display = 'block';
    }

    function closeModal() {
        var modal = document.getElementById('myModal');
        modal.style.display = 'none';
    }

    // Form submit işlemi kontrolü
    document.getElementById('bankForm').addEventListener('submit', function (event) {
        event.preventDefault();
        var form = this;
        var formData = new FormData(form);

        fetch(form.action, {
            method: 'POST',
            body: formData
        })
        .then(response => {
            if (!response.ok) {
                throw new Error('Bir hata oluştu.');
            }
            return response.json();
        })
        .then(data => {
            alert(data.message);
            closeModal();
            // Sayfayı yenilemek isterseniz:
         window.location.reload();
        })
        .catch(error => {
            alert('İşlem sırasında bir hata oluştu.');
            console.error(error);
        });
    });
    
    function deleteBank(bankId) {
        if (confirm('Bu banka hesabını silmek istediğinizden emin misiniz?')) {
            fetch('delete_bank.php', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ bankId: bankId })
            })
            .then(response => {
                if (!response.ok) {
                    throw new Error('Bir hata oluştu.');
                }
                return response.json();
            })
            .then(data => {
                alert(data.message);
                // Sayfayı yenilemek isterseniz:
                window.location.reload();
            })
            .catch(error => {
                alert('İşlem sırasında bir hata oluştu.');
                console.error(error);
            });
        }
    }
</script>

</body>
</html>
