<?php
// Load existing fraud settings from variables (e.g., from config or DB)
$recentclick   = $fraudsettings_recentclick ?? 0;
$clickseconds  = $fraudsettings_clickseconds ?? '';
$clickaction   = $fraudsettings_clickaction ?? '';
$recentsale    = $fraudsettings_recentsale ?? 0;
$saleseconds   = $fraudsettings_saleseconds ?? '';
$saleaction    = $fraudsettings_saleaction ?? '';
$loginretry    = $fraudsettings_login_retry ?? '';
$logindelay    = $fraudsettings_login_delay ?? '';
$declinesale   = $fraudsettings_decline_recentsale ?? 0;

// Sanitize message
$msg = isset($_POST['msg']) ? htmlspecialchars(trim($_POST['msg'])) : '';

// Override values if form was submitted with a message
if (!empty($msg)) {
    $recentclick   = !empty($_POST['chk_click']) ? 1 : 0;
    $clickseconds  = trim($_POST['txt_clickseconds'] ?? '');
    $clickaction   = trim($_POST['cmb_clickaction'] ?? '');
    $recentsale    = !empty($_POST['chk_sale']) ? 1 : 0;
    $saleseconds   = trim($_POST['txt_saleseconds'] ?? '');
    $saleaction    = trim($_POST['cmb_saleaction'] ?? '');
    $declinesale   = !empty($_POST['chk_samesale']) ? 1 : 0;
    $loginretry    = trim($_POST['txt_login_retry'] ?? '');
    $logindelay    = trim($_POST['txt_login_delay'] ?? '');
}

// Default fallbacks
$clickseconds = $clickseconds ?: ($fraudsettings_clickseconds ?? '');
$saleseconds  = $saleseconds ?: ($fraudsettings_saleseconds ?? '');
$loginretry   = $loginretry   ?: ($fraudsettings_login_retry ?? '');
$logindelay   = $logindelay   ?: ($fraudsettings_login_delay ?? '');
?>

<form name="frm_fraud" action="fraudsettings_manage.php" onsubmit="return managefraud();" method="post">
    <table border="0" cellpadding="2" cellspacing="1" width="90%" class="tablebdr">
        <tr>
            <td colspan="2" class="tdhead">Fraud Protection</td>
        </tr>
        <?php if ($msg): ?>
        <tr>
            <td colspan="2" class="textred" align="center"><small><?= $msg ?></small></td>
        </tr>
        <?php endif; ?>

        <tr>
            <td colspan="2">
                <table width="100%" cellpadding="0" cellspacing="0">
                    <tr>
                        <td>
                            <input type="checkbox" name="chk_click" <?= $recentclick == 1 ? "checked" : "" ?> />
                            Check multiple repeating clicks from same IP within
                            <input type="number" name="txt_clickseconds" value="<?= htmlspecialchars($clickseconds) ?>" style="width:60px;" /> seconds from initial click
                        </td>
                    </tr>
                    <tr>
                        <td>
                            What to do with repeating clicks:
                            <select name="cmb_clickaction">
                                <option value="do not save" <?= $clickaction == 'do not save' ? "selected" : "" ?>>do not save</option>
                                <option value="save as click" <?= $clickaction == 'save as click' ? "selected" : "" ?>>save as click</option>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <input type="checkbox" name="chk_sale" <?= $recentsale == 1 ? "checked" : "" ?> />
                            Check multiple repeating sales from same IP within
                            <input type="number" name="txt_saleseconds" value="<?= htmlspecialchars($saleseconds) ?>" style="width:60px;" /> seconds from initial sale
                        </td>
                    </tr>
                    <tr>
                        <td>
                            What to do with repeating sales:
                            <select name="cmb_saleaction">
                                <option value="decline" <?= $saleaction == 'decline' ? "selected" : "" ?>>decline</option>
                                <option value="save" <?= $saleaction == 'save' ? "selected" : "" ?>>save</option>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <input type="checkbox" name="chk_samesale" <?= $declinesale == 1 ? "checked" : "" ?> />
                            Automatically decline multiple sales with same (non-empty) OrderId as the initial sale
                        </td>
                    </tr>
                </table>
            </td>
        </tr>

        <tr>
            <td colspan="2" class="tdhead">Login Protection</td>
        </tr>
        <tr>
            <td colspan="2">
                <table width="100%" cellpadding="0" cellspacing="0">
                    <tr>
                        <td width="20%">Login Protection Retries</td>
                        <td width="10%">
                            <input type="number" name="txt_login_retry" value="<?= htmlspecialchars($loginretry) ?>" style="width:60px;" />
                        </td>
                        <td>
                            <b>miniHelp:</b> Number of allowed incorrect login attempts before temporary block.
                        </td>
                    </tr>
                    <tr>
                        <td colspan="3">&nbsp;After these attempts, the account will be blocked for the specified delay below.</td>
                    </tr>
                    <tr>
                        <td colspan="3">&nbsp;Set to 0 to disable login protection.</td>
                    </tr>
                    <tr><td colspan="3"><hr></td></tr>
                    <tr>
                        <td>Login Protection Delay</td>
                        <td>
                            <input type="number" name="txt_login_delay" value="<?= htmlspecialchars($logindelay) ?>" style="width:60px;" />
                        </td>
                        <td>
                            <b>miniHelp:</b> Time in seconds the account remains blocked after exceeding login retries.
                        </td>
                    </tr>
                    <tr><td colspan="3"><hr></td></tr>
                </table>
            </td>
        </tr>

        <tr>
            <td colspan="2" align="center" height="40">
                <input type="submit" name="fraud_submit" value="Submit" />
            </td>
        </tr>
    </table>
</form>

<script>
function managefraud() {
    const form = document.frm_fraud;

    if (form.chk_click.checked && form.txt_clickseconds.value.trim() === '') {
        alert('Please enter the seconds for repeating clicks');
        form.txt_clickseconds.focus();
        return false;
    }

    if (form.chk_sale.checked && form.txt_saleseconds.value.trim() === '') {
        alert('Please enter the seconds for repeating sale');
        form.txt_saleseconds.focus();
        return false;
    }

    if (form.txt_login_retry.value.trim() === '') {
        alert('Please enter the login retry attempts');
        form.txt_login_retry.focus();
        return false;
    }

    if (form.txt_login_delay.value.trim() === '') {
        alert('Please enter the login delay in seconds');
        form.txt_login_delay.focus();
        return false;
    }

    return true;
}
</script>
