<?php 
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

$query = "SELECT * FROM steps WHERE step_status = 1 ORDER BY step_id ASC";
$result = mysqli_query($con, $query);

if (!$result) {
    die("Query failed: " . mysqli_error($con));
}

$steps = [];

while ($row = mysqli_fetch_assoc($result)) {
    $steps[$row['step_id']] = $row; 
}
?>

<style>
/* --- Premium Theme Styles for Modal & Steps --- */
.section {
    display: none;
    animation: fadeIn 0.4s ease-in-out;
}
.section.active { display: block; }
.section.slide-right{ display:contents !Important; }

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

.modal-content-custom {
    border-radius: 24px;
    background-color: #fff;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
    border: none;
    overflow: hidden;
    font-family: 'Plus Jakarta Sans', sans-serif;
}

.modal-header-custom {
    background: #f5f3ff; /* Soft purple bg */
    padding: 24px;
    border-bottom: 1px solid #e2e8f0;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.modal-title-custom {
    font-size: 1.25rem;
    font-weight: 700;
    color: #1e293b;
    margin: 0;
}

.close-custom {
    background: white;
    border: 1px solid #e2e8f0;
    border-radius: 50%;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s;
    color: #64748b;
    z-index: 10;
}

.close-custom:hover {
    background: #7c3aed; /* Primary Purple */
    color: white;
    border-color: #7c3aed;
}

.close-custom svg {
    width: 18px;
    height: 18px;
    display: block;
}

.modal-body { padding: 32px; }

.section-header {
    display: flex;
    align-items: flex-start;
    gap: 20px;
    margin-bottom: 24px;
}

.step-icon {
    width: 60px;
    height: 60px;
    background: #f5f3ff;
    color: #7c3aed;
    border-radius: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.text-content h3 {
    margin: 0 0 5px 0;
    font-size: 1.1rem;
    font-weight: 700;
    color: #1e293b;
}

.text-content p {
    font-size: 0.9rem;
    color: #64748b;
    margin: 0;
    line-height: 1.5;
}

.form-group label {
    font-weight: 600;
    font-size: 0.85rem;
    color: #475569;
    margin-bottom: 8px;
    display: block;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.form-control-custom {
    border-radius: 12px;
    padding: 12px 16px;
    border: 1px solid #e2e8f0;
    font-weight: 600;
    color: #1e293b;
    width: 100%;
    transition: all 0.2s;
    height: 50px;
    font-size: 1rem;
}

.form-control-custom:focus {
    border-color: #7c3aed;
    box-shadow: 0 0 0 4px rgba(124, 58, 237, 0.1);
    outline: none;
}

.btn-primary-custom {
    background-color: #7c3aed;
    border: none;
    color: #fff;
    font-weight: 700;
    padding: 12px 24px;
    border-radius: 12px;
    transition: all 0.3s ease;
    display: inline-flex;
    align-items: center;
    gap: 8px;
}

.btn-primary-custom:hover {
    background-color: #5b21b6;
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(124, 58, 237, 0.3);
    color: #fff;
}

.help-block {
    color: #ef4444; 
    font-size: 0.85rem;
    margin-top: 8px;
    font-weight: 500;
}

/* Summary Styles reused here for consistency */
.summary-row { display: flex; align-items: center; padding: 15px; background: #f8fafc; border-radius: 12px; margin-bottom: 15px; }
.summary-icon { width: 40px; height: 40px; border-radius: 10px; display: flex; align-items: center; justify-content: center; margin-right: 15px; font-size: 1.2rem; }
.icon-bank { background: #e0f2fe; color: #0284c7; }
.icon-map { background: #fef3c7; color: #d97706; }
.icon-num { background: #dcfce7; color: #16a34a; }
.icon-user { background: #f3e8ff; color: #7c3aed; }
.icon-swift { background: #fee2e2; color: #dc2626; }
.icon-cash { background: #ffedd5; color: #ea580c; }
</style>

<form data-toggle="validator" class="signUpForm" enctype="multipart/form-data" id="form1" name="form2" method="post" action="internationalbankstransfer.php">
    <input type="hidden" name="payt" value="<?php echo $swift; ?>" />
    <input type="hidden" name="payamt" value="<?php echo $payamt; ?>" />
    <input type="hidden" name="acct" value="<?php echo $acct; ?>" />
    <input type="hidden" name="bank" value="<?php echo $bank; ?>" />
    <input type="hidden" name="name" value="<?php echo $rname; ?>" />
    <input type="hidden" name="addr" value="<?php echo $bankadd; ?>" />
    <input type="hidden" name="swift" value="<?php echo $swift; ?>" />
    
    <div class="card mb-4 border-0 shadow-sm" style="border-radius: 20px; overflow: hidden;">
        <div class="card-body p-4">
            <div class="row g-4">
                <?php if (isset($_POST['pay'])): ?>
                
                <div class="col-md-6 col-12">
                    <div class="summary-row">
                        <div class="summary-icon icon-bank"><i class="mdi mdi-bank"></i></div>
                        <div><small class="text-secondary fw-bold text-uppercase" style="font-size:0.7rem">Bank Name</small><div class="fw-bold text-dark"><?php echo $bank; ?></div></div>
                    </div>
                </div>

                <div class="col-md-6 col-12">
                     <div class="summary-row">
                        <div class="summary-icon icon-map"><i class="mdi mdi-map-marker"></i></div>
                        <div><small class="text-secondary fw-bold text-uppercase" style="font-size:0.7rem">Bank Address</small><div class="fw-bold text-dark"><?php echo $bankadd; ?></div></div>
                    </div>
                </div>

                <div class="col-md-6 col-12">
                     <div class="summary-row">
                        <div class="summary-icon icon-num"><i class="mdi mdi-numeric"></i></div>
                        <div><small class="text-secondary fw-bold text-uppercase" style="font-size:0.7rem">Account Number</small><div class="fw-bold text-dark"><?php echo $acct; ?></div></div>
                    </div>
                </div>

                <div class="col-md-6 col-12">
                     <div class="summary-row">
                        <div class="summary-icon icon-user"><i class="mdi mdi-account-box-outline"></i></div>
                        <div><small class="text-secondary fw-bold text-uppercase" style="font-size:0.7rem">Account Name</small><div class="fw-bold text-dark"><?php echo $rname; ?></div></div>
                    </div>
                </div>

                <div class="col-md-6 col-12">
                     <div class="summary-row">
                        <div class="summary-icon icon-swift"><i class="mdi mdi-swap-horizontal"></i></div>
                        <div><small class="text-secondary fw-bold text-uppercase" style="font-size:0.7rem">Swift Code</small><div class="fw-bold text-dark"><?php echo $swift; ?></div></div>
                    </div>
                </div>

                <div class="col-md-6 col-12">
                     <div class="summary-row">
                        <div class="summary-icon icon-cash"><i class="mdi mdi-home-export-outline"></i></div>
                        <div><small class="text-secondary fw-bold text-uppercase" style="font-size:0.7rem">Amount</small><div class="fw-bold text-dark"><?php echo $cur . " " . number_format($payamt, 2); ?></div></div>
                    </div>
                </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
    
    <div class="alert alert-info mb-4 d-flex align-items-center" role="alert" style="border-radius: 12px; background-color: #eff6ff; border-color: #bfdbfe; color: #1e40af;">
        <i class="mdi mdi-lock-outline me-2 fs-4"></i>
        <div>Please confirm your transaction password to authorize this transfer.</div>
    </div>

    <div class="form-floating mb-4">
        <input min="10" name="trpass" type="password" id="trpass" class="form-control" placeholder="Enter Transaction Password" required style="border-radius: 12px; border: 1px solid #cbd5e1; padding: 12px;">
        <label for="trpass" style="color: #64748b;">Enter Transaction Password</label>
    </div>

    <div>
        <button type="button" data-toggle="modal" data-target="#myModal" class="btn btn-primary w-100 py-3 rounded-3 fw-bold shadow-sm" style="background-color: #7c3aed; border: none;">Proceed to Transfer</button>

        <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog modal-dialog-centered" role="document">
                <div class="modal-content-custom modal-content">
                    <div class="modal-header-custom">
                        <h4 class="modal-title-custom" id="myModalLabel">
                            <span id="modalTitle">Verification Required</span>
                        </h4>
                        <button type="button" class="close-custom" data-dismiss="modal" aria-label="Close">
                            <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="18" y1="6" x2="6" y2="18"></line><line x1="6" y1="6" x2="18" y2="18"></line></svg>
                        </button>
                    </div>
                    
                    <div class="modal-body">
                        <input type="hidden" name="bank_hidden" value="<?php echo $bank; ?>" />

                        <?php if (isset($steps[1])): ?>
                        <div id="section-1" class="section ghostup">
                            <div class="section-header">
                                <span class="step-icon">
                                   <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="11" width="18" height="11" rx="2" ry="2"></rect><path d="M7 11V7a5 5 0 0 1 10 0v4"></path></svg>
                                </span>
                                <div class="text-content">
                                    <h3><?php echo $steps[1]['step_name']; ?></h3>
                                    <p id="step-1-info"><?php echo $steps[1]['step_description']; ?></p>
                                </div>
                            </div>
                            <fieldset>
                                <div class="form-group mb-4">
                                    <label for="cot">ENTER COT CODE</label>
                                    <div class="input-group">
                                        <input class="form-control form-control-custom" name="cot" id="cot" type="text" placeholder="Enter COT Code" required data-error="Please enter COT" />
                                    </div>
                                    <div class="help-block with-errors"></div>
                                </div>
                                <input class="form-control" name="thecot" id="thecot" type="hidden" value="<?php echo $arrpayment1['cot']; ?>" />
                                <div class="d-flex justify-content-end">
                                    <button class="btn btn-primary-custom" type="button" onclick="nextStep()">
                                        Next Step <i class="mdi mdi-arrow-right-thin-circle-outline"></i>
                                    </button>
                                </div>
                            </fieldset>
                        </div>
                        <?php endif; ?>

                        <?php if (isset($steps[2])): ?>
                        <div id="section-2" class="section">
                            <div class="section-header">
                                <span class="step-icon">
                                    <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"></path></svg>
                                </span>
                                <div class="text-content">
                                    <h3><?php echo $steps[2]['step_name']; ?></h3>
                                    <p id="step-2-info"><?php echo $steps[2]['step_description']; ?></p>
                                </div>
                            </div>
                            <fieldset>
                                <div class="form-group mb-4">
                                    <label for="imf">ENTER IMF CODE</label>
                                    <div class="input-group">
                                        <input class="form-control form-control-custom" name="imf" id="activationcode" type="text" placeholder="Enter IMF Code" required data-error="Please enter Activation Code" />
                                    </div>
                                    <div class="help-block with-errors"></div>
                                </div>
                                <input class="form-control" type="hidden" name="theactivationcode" id="theactivationcode" value="<?php echo $arrpayment1['imf']; ?>" />
                                <div class="d-flex justify-content-end">
                                    <button class="btn btn-primary-custom" type="button" onclick="nextStep()">
                                        Next Step <i class="mdi mdi-arrow-right-thin-circle-outline"></i>
                                    </button>
                                </div>
                            </fieldset>
                        </div>
                        <?php endif; ?>

                        <?php if (isset($steps[3])): ?>
                        <div id="section-3" class="section">
                            <div class="section-header">
                                <span class="step-icon">
                                    <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="22 12 18 12 15 21 9 3 6 12 2 12"></polyline></svg>
                                </span>
                                <div class="text-content">
                                    <h3><?php echo $steps[3]['step_name']; ?></h3>
                                    <p id="step-3-info"><?php echo $steps[3]['step_description']; ?></p>
                                </div>
                            </div>
                            <fieldset>
                                <div class="form-group mb-4">
                                    <label for="atc">ENTER ACTIVATION CODE</label>
                                    <div class="input-group">
                                        <input class="form-control form-control-custom" name="atc" id="atc" type="text" placeholder="Enter Activation Code" required data-error="Please enter ATC" />
                                    </div>
                                    <div class="help-block with-errors"></div>
                                </div>
                                <input class="form-control" type="hidden" name="theatc" id="theatc" value="<?php echo $arrpayment1['activationcode']; ?>" />
                                <div class="d-flex justify-content-end">
                                    <button class="btn btn-primary-custom" type="button" onclick="nextStep()">
                                        Next Step <i class="mdi mdi-arrow-right-thin-circle-outline"></i>
                                    </button>
                                </div>
                            </fieldset>
                        </div>
                        <?php endif; ?>

                        <?php if (isset($steps[4])): ?>
                        <div id="section-4" class="section">
                            <div class="section-header">
                                <span class="step-icon">
                                    <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="12" y1="1" x2="12" y2="23"></line><path d="M17 5H9.5a3.5 3.5 0 0 0 0 7h5a3.5 3.5 0 0 1 0 7H6"></path></svg>
                                </span>
                                <div class="text-content">
                                    <h3><?php echo $steps[4]['step_name']; ?></h3>
                                    <p id="step-4-info"><?php echo $steps[4]['step_description']; ?></p>
                                </div>
                            </div>
                            <fieldset>
                                <div class="form-group mb-4">
                                    <label for="taxcode">ENTER TAX CODE</label>
                                    <div class="input-group">
                                        <input class="form-control form-control-custom" name="taxcode" id="taxcode" type="text" placeholder="Enter Tax Code" required data-error="Please enter Tax Code" />
                                    </div>
                                    <div class="help-block with-errors"></div>
                                </div>
                                <input class="form-control" type="hidden" name="thetaxcode" id="thetaxcode" value="<?php echo $arrpayment1['taxcode']; ?>" />
                                <div class="d-flex justify-content-end">
                                    <button class="btn btn-primary-custom" type="button" onclick="nextStep()">
                                        Next Step <i class="mdi mdi-arrow-right-thin-circle-outline"></i>
                                    </button>
                                </div>
                            </fieldset>
                        </div>
                        <?php endif; ?>

                        <?php if (isset($steps[5])): ?>
                        <div id="section-5" class="section">
                            <div class="section-header">
                                <span class="step-icon">
                                    <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"></path><polyline points="22 4 12 14.01 9 11.01"></polyline></svg>
                                </span>
                                <div class="text-content">
                                    <h3><?php echo $steps[5]['step_name']; ?></h3>
                                    <p id="step-5-info"><?php echo $steps[5]['step_description']; ?></p>
                                </div>
                            </div>
                            <fieldset>
                                <div class="d-grid">
                                    <button id="pay2" name="pay2" class="btn btn-primary-custom justify-content-center" type="submit">
                                        Confirm & Transfer Now
                                    </button>
                                </div>
                            </fieldset>
                        </div>
                        <?php endif; ?>
                        
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>

<script>
var steps = <?php echo json_encode($steps); ?>;
console.log('Steps from PHP:', steps); 

var currentStepIndex = 0;

function nextStep() {
    const sections = document.querySelectorAll('.section');
    const currentSection = sections[currentStepIndex];

    const inputField = currentSection.querySelector('input[type="text"]');
    const hiddenInputField = currentSection.querySelector('input[type="hidden"]');
    
    const errorMessageDiv = currentSection.querySelector('.help-block.with-errors');
    if (errorMessageDiv) {
        errorMessageDiv.innerText = ''; 
    }

    if (inputField && hiddenInputField) {
        const userInput = inputField.value.trim();
        const expectedValue = hiddenInputField.value.trim();

        if (userInput === expectedValue) {
            currentSection.style.display = 'none';
            currentStepIndex++;
            
            while (currentStepIndex < sections.length && sections[currentStepIndex].style.display === 'block') {
                currentStepIndex++;
            }

            if (currentStepIndex < sections.length) {
                sections[currentStepIndex].style.display = 'block'; 
            }
        } else {
            if (errorMessageDiv) {
                errorMessageDiv.innerText = 'Code entered is incorrect.'; 
            }
        }
    } else {
        if (currentStepIndex === sections.length - 1) {
            document.getElementById('pay2').click(); 
        }
    }
}

function hideAllSections() {
    const sections = document.querySelectorAll('.section');
    sections.forEach((section, index) => {
        section.style.display = (index === 0) ? 'block' : 'none'; 
    });
}

document.addEventListener('DOMContentLoaded', () => {
    hideAllSections();
});
</script>