<?php
date_default_timezone_set('Asia/Dhaka');

$isLoggedIn = isset($_SESSION['login']) && $_SESSION['login'];
$sessionUserId = isset($_SESSION['userid']) ? (int)$_SESSION['userid'] : 0;
$sessionUserType = isset($_SESSION['usertype']) ? $_SESSION['usertype'] : '';

if (!$isLoggedIn || $sessionUserId <= 0 || $sessionUserType !== 'RE') {
    echo '<div class="col-md-12"><div class="card"><div class="card-body text-center"><h5 class="text-danger mb-1">Session Problem</h5><p class="mb-0">Please login as reseller to access this page.</p></div></div></div>';
    return;
}

if (isset($_POST['clear_reseller_data'])) {
    $resellerId = $sessionUserId;


    try {
        $obj->rawSql("CALL reset_individual_reseller_data($resellerId)");
        $obj->notificationStore('Your reseller data cleared successfully.', 'success');
        echo '<script>window.location="?page=reseller-bsdsecretresellersetting";</script>';
    } catch (Throwable $e) {
        $obj->notificationStore('Failed to clear reseller data: ' . $e->getMessage(), 'danger');
        echo '<script>window.location="?page=reseller-bsdsecretresellersetting";</script>';
    }
}
?>

<div class="col-md-12">
    <div class="card border-danger">
        <div class="card-body text-center py-4">
            <h5 class="text-danger mb-2">Secret Reseller Data Clear</h5>
            <p class="text-muted mb-3">This action will clear only your reseller data.</p>
            <button type="button" id="clearResellerDataBtn" class="btn btn-danger btn-sm px-4">Clear Data</button>
            <form method="post" id="clearResellerDataForm" style="display:none;">
                <input type="hidden" name="clear_reseller_data" value="1">
            </form>
        </div>
    </div>
</div>

<?php $obj->start_script(); ?>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script>
document.getElementById('clearResellerDataBtn').addEventListener('click', function() {
    const randomText = 'CLEAR-' + Math.random().toString(36).slice(2, 8).toUpperCase();

    Swal.fire({
        title: 'Are you sure?',
        text: 'Are you sure to clear data?',
        icon: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#d33',
        cancelButtonColor: '#6c757d',
        confirmButtonText: 'Yes, Continue'
    }).then((firstStep) => {
        if (!firstStep.isConfirmed) return;

        Swal.fire({
            title: 'Type the text to continue',
            html: 'Please type <b>' + randomText + '</b> to continue.',
            input: 'text',
            inputPlaceholder: 'Type exactly here...',
            showCancelButton: true,
            confirmButtonText: 'Verify',
            cancelButtonText: 'Cancel',
            inputValidator: (value) => {
                if ((value || '').trim() !== randomText) {
                    return 'Typed text does not match.';
                }
            }
        }).then((secondStep) => {
            if (!secondStep.isConfirmed) return;

            Swal.fire({
                title: 'Final confirmation',
                text: 'Are you sure to clear your data now?',
                icon: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#d33',
                cancelButtonColor: '#6c757d',
                confirmButtonText: 'Yes, Clear Data',
                cancelButtonText: 'Cancel'
            }).then((thirdStep) => {
                if (thirdStep.isConfirmed) {
                    document.getElementById('clearResellerDataForm').submit();
                }
            });
        });
    });
});
</script>
<?php $obj->end_script(); ?>
