<?php

/**

 * Order Review Settings - Enterprise Dashboard

 *

 * @package    SensFRX

 * @subpackage SensFRX/admin/partials

 */



// If this file is called directly, abort.

if (!defined('WPINC')) {

    die;

}



// Get current settings

$options = get_option('sensfrx_policy_options', []);



// Helper function to render email editor

function sensfrx_render_email_editor($field_name, $content, $editor_id) {

    $editor_settings = array(

        'textarea_name' => 'sensfrx_policy_options[' . $field_name . ']',

        'media_buttons' => false,

        'textarea_rows' => 6,

        'teeny' => true,

        'quicktags' => array('buttons' => 'strong,em,link,ul,li'),

        'tinymce' => array(

            'toolbar1' => 'bold,italic,underline,link,unlink,bullist,forecolor,removeformat',

            'toolbar2' => '',

            'height' => 150

        )

    );

    wp_editor($content, $editor_id, $editor_settings);

}

?>



<div class="sensfrx-enterprise-wrapper">

    <!-- Navigation Tabs -->

    <nav class="sensfrx-nav-tabs">

        <a href="?page=sensfrx_fpwoo&tab=dashboard" class="sensfrx-nav-tab">Dashboard</a>

        <a href="?page=sensfrx_fpwoo&tab=validation-rules" class="sensfrx-nav-tab">Validation Rules</a>

        <a href="?page=sensfrx_fpwoo&tab=fraud" class="sensfrx-nav-tab">Fraud Intelligence</a>

        <a href="?page=sensfrx_fpwoo&tab=settings" class="sensfrx-nav-tab">Settings</a>

        <a href="?page=sensfrx_fpwoo&tab=fraud-protection" class="sensfrx-nav-tab">Fraud Protection</a>

        <a href="?page=sensfrx_fpwoo&tab=activity" class="sensfrx-nav-tab">Activity Logs</a>

        <a href="?page=sensfrx_fpwoo&tab=review" class="sensfrx-nav-tab sensfrx-nav-tab-highlight active">

            Fraud Review <span class="sensfrx-nav-badge" id="sensfrx-review-count">0</span>

        </a>

        <?php /* COMMENTED OUT - Email Customization UI - Will add partial customization later

        <a href="?page=sensfrx_fpwoo&tab=email-notifications" class="sensfrx-nav-tab">Email Notifications</a>

        */ ?>

    </nav>



    <!-- Main Content -->

    <div class="sensfrx-enterprise-dashboard">

        <!-- Page Header -->

        <div class="sensfrx-order-header">

            <div>

                <h1 class="sensfrx-page-title">Automatic Order Protection</h1>

                <p class="sensfrx-page-subtitle">Every order is scanned for fraud. Choose what happens based on risk level.</p>

            </div>

        </div>



        <form id="sensfrx-order-review-form" method="post" action="options.php">

            <?php settings_fields('sensfrx_plugin_policy_options'); ?>



            <!-- How It Works Section -->

            <div class="sensfrx-info-banner">

                <div class="sensfrx-info-icon">

                    <svg width="24" height="24" viewBox="0 0 24 24" fill="none">

                        <circle cx="12" cy="12" r="10" stroke="currentColor" stroke-width="2"/>

                        <path d="M12 16v-4m0-4h.01" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>

                    </svg>

                </div>

                <div class="sensfrx-info-content">

                    <h3 class="sensfrx-info-title">How It Works</h3>

                    <p class="sensfrx-info-text">

                        We automatically scan every order and assign a risk score. <strong>Low risk orders go through automatically.</strong>

                        Medium risk orders wait for your review. High risk orders are blocked. You can customize email messages for each scenario below.

                    </p>

                </div>

            </div>



            <!-- Risk Levels Section -->

            <div class="sensfrx-section">

                <h2 class="sensfrx-section-title">What Happens to Each Order?</h2>

                <p class="sensfrx-section-subtitle">Configure how orders are handled based on their fraud risk level</p>



                <div class="sensfrx-risk-grid">



                    <!-- Low Risk -->

                    <div class="sensfrx-risk-panel sensfrx-risk-low">

                        <div class="sensfrx-risk-header">

                            <div class="sensfrx-risk-badge sensfrx-badge-low">

                                <svg width="20" height="20" viewBox="0 0 20 20" fill="none">

                                    <path d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 111.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z" fill="currentColor"/>

                                </svg>

                            </div>

                            <div>

                                <h3 class="sensfrx-risk-title">Low Risk</h3>

                                <p class="sensfrx-risk-description">Orders automatically approved</p>

                            </div>

                        </div>



                        <div class="sensfrx-risk-body">

                            <div class="sensfrx-risk-explanation">

                                <p>These orders look safe and will be <strong>processed immediately</strong> without any delays. Your customer gets a smooth checkout experience.</p>

                            </div>



                            <div class="sensfrx-field-group">

                                <label class="sensfrx-field-label">

                                    <span class="sensfrx-label-text">Customer Email Message <span class="sensfrx-optional">(Optional)</span></span>

                                    <span class="sensfrx-label-help">Send a confirmation when order is approved</span>

                                </label>

                                <?php

                                sensfrx_render_email_editor(

                                    'sensfrx_allow_payment_email',

                                    isset($options['sensfrx_allow_payment_email']) ? $options['sensfrx_allow_payment_email'] : '',

                                    'sensfrx_allow_payment_email'

                                );

                                ?>

                            </div>

                        </div>

                    </div>



                    <!-- Medium Risk -->

                    <div class="sensfrx-risk-panel sensfrx-risk-medium">

                        <div class="sensfrx-risk-header">

                            <div class="sensfrx-risk-badge sensfrx-badge-medium">

                                <svg width="20" height="20" viewBox="0 0 20 20" fill="none">

                                    <path d="M8.257 3.099c.765-1.36 2.722-1.36 3.486 0l5.58 9.92c.75 1.334-.213 2.98-1.742 2.98H4.42c-1.53 0-2.493-1.646-1.743-2.98l5.58-9.92zM11 13a1 1 0 11-2 0 1 1 0 012 0zm-1-8a1 1 0 00-1 1v3a1 1 0 002 0V6a1 1 0 00-1-1z" fill="currentColor"/>

                                </svg>

                            </div>

                            <div>

                                <h3 class="sensfrx-risk-title">Medium Risk</h3>

                                <p class="sensfrx-risk-description">Needs your manual review</p>

                            </div>

                        </div>



                        <div class="sensfrx-risk-body">

                            <div class="sensfrx-risk-explanation">

                                <p>These orders show some warning signs. They'll appear in your <strong>Fraud Review tab</strong> where you can approve or block them manually.</p>

                            </div>



                            <div class="sensfrx-field-group">

                                <label class="sensfrx-field-label">

                                    <span class="sensfrx-label-text">Customer Email Message <span class="sensfrx-optional">(Optional)</span></span>

                                    <span class="sensfrx-label-help">What customers see while order is being reviewed</span>

                                </label>

                                <?php

                                sensfrx_render_email_editor(

                                    'sensfrx_challenge_payment_email',

                                    isset($options['sensfrx_challenge_payment_email']) ? $options['sensfrx_challenge_payment_email'] : '',

                                    'sensfrx_challenge_payment_email'

                                );

                                ?>

                            </div>



                            <div class="sensfrx-checkbox-wrapper">

                                <label class="sensfrx-checkbox-label">

                                    <input

                                        type="checkbox"

                                        name="sensfrx_policy_options[hold_medium_risk]"

                                        value="1"

                                        <?php checked(isset($options['hold_medium_risk']) ? $options['hold_medium_risk'] : 1, 1); ?>

                                        class="sensfrx-checkbox-input"

                                    />

                                    <span class="sensfrx-checkbox-text">

                                        <strong>Put medium risk orders on hold</strong> until I review them

                                    </span>

                                </label>

                            </div>

                        </div>

                    </div>



                    <!-- High Risk -->

                    <div class="sensfrx-risk-panel sensfrx-risk-high">

                        <div class="sensfrx-risk-header">

                            <div class="sensfrx-risk-badge sensfrx-badge-high">

                                <svg width="20" height="20" viewBox="0 0 20 20" fill="none">

                                    <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z" fill="currentColor"/>

                                </svg>

                            </div>

                            <div>

                                <h3 class="sensfrx-risk-title">High Risk</h3>

                                <p class="sensfrx-risk-description">Automatically blocked</p>

                            </div>

                        </div>



                        <div class="sensfrx-risk-body">

                            <div class="sensfrx-risk-explanation">

                                <p>These orders show clear fraud patterns and are <strong>blocked immediately</strong> to protect your business. The customer is notified professionally.</p>

                            </div>



                            <div class="sensfrx-field-group">

                                <label class="sensfrx-field-label">

                                    <span class="sensfrx-label-text">Customer Email Message <span class="sensfrx-optional">(Optional)</span></span>

                                    <span class="sensfrx-label-help">Message shown when an order cannot be processed</span>

                                </label>

                                <?php

                                sensfrx_render_email_editor(

                                    'sensfrx_deny_payment_email',

                                    isset($options['sensfrx_deny_payment_email']) ? $options['sensfrx_deny_payment_email'] : '',

                                    'sensfrx_deny_payment_email'

                                );

                                ?>

                            </div>

                        </div>

                    </div>



                </div>

            </div>



            <!-- Customer Registration Section -->

            <div class="sensfrx-section">

                <h2 class="sensfrx-section-title">Customer Registration</h2>

                <p class="sensfrx-section-subtitle">Email messages for new customer signups</p>



                <div class="sensfrx-two-column-grid">

                    <div class="sensfrx-settings-card">

                        <h3 class="sensfrx-card-heading">When Registration is Approved</h3>

                        <div class="sensfrx-field-group">

                            <label class="sensfrx-field-label">

                                <span class="sensfrx-label-text">Welcome Email <span class="sensfrx-optional">(Optional)</span></span>

                            </label>

                            <?php

                            sensfrx_render_email_editor(

                                'sensfrx_allow_register_email',

                                isset($options['sensfrx_allow_register_email']) ? $options['sensfrx_allow_register_email'] : '',

                                'sensfrx_allow_register_email'

                            );

                            ?>

                        </div>

                    </div>



                    <div class="sensfrx-settings-card">

                        <h3 class="sensfrx-card-heading">When Registration Needs Review</h3>

                        <div class="sensfrx-field-group">

                            <label class="sensfrx-field-label">

                                <span class="sensfrx-label-text">Verification Email <span class="sensfrx-optional">(Optional)</span></span>

                            </label>

                            <?php

                            sensfrx_render_email_editor(

                                'sensfrx_challenge_register_email',

                                isset($options['sensfrx_challenge_register_email']) ? $options['sensfrx_challenge_register_email'] : '',

                                'sensfrx_challenge_register_email'

                            );

                            ?>

                        </div>

                    </div>



                    <div class="sensfrx-settings-card">

                        <h3 class="sensfrx-card-heading">When Registration is Blocked</h3>

                        <div class="sensfrx-field-group">

                            <label class="sensfrx-field-label">

                                <span class="sensfrx-label-text">Blocked Email <span class="sensfrx-optional">(Optional)</span></span>

                            </label>

                            <?php

                            sensfrx_render_email_editor(

                                'sensfrx_deny_register_email',

                                isset($options['sensfrx_deny_register_email']) ? $options['sensfrx_deny_register_email'] : '',

                                'sensfrx_deny_register_email'

                            );

                            ?>

                        </div>

                    </div>



                    <div class="sensfrx-settings-card">

                        <h3 class="sensfrx-card-heading">Advanced Options</h3>



                        <div class="sensfrx-field-group" style="margin-bottom: 20px;">

                            <label class="sensfrx-field-label">

                                <span class="sensfrx-label-text">Webhook URL <span class="sensfrx-optional">(Optional)</span></span>

                                <span class="sensfrx-label-help">Get instant notifications about order reviews</span>

                            </label>

                            <input

                                type="url"

                                name="sensfrx_policy_options[sensfrx_webhook_allow]"

                                value="<?php echo esc_attr(isset($options['sensfrx_webhook_allow']) ? $options['sensfrx_webhook_allow'] : ''); ?>"

                                class="sensfrx-field-input"

                                placeholder="https://yoursite.com/webhook"

                            />

                        </div>



                        <div class="sensfrx-field-group">

                            <label class="sensfrx-field-label">

                                <span class="sensfrx-label-text">Trusted Customer Emails <span class="sensfrx-optional">(Optional)</span></span>

                                <span class="sensfrx-label-help">One email per line - these customers skip fraud checks</span>

                            </label>

                            <textarea

                                name="sensfrx_policy_options[sensfrx_whitle_list_email]"

                                rows="3"

                                class="sensfrx-field-textarea"

                                placeholder="john@example.com&#10;jane@example.com"

                            ><?php echo esc_textarea(isset($options['sensfrx_whitle_list_email']) ? $options['sensfrx_whitle_list_email'] : ''); ?></textarea>

                        </div>

                    </div>

                </div>

            </div>



            <!-- Save Actions -->

            <div class="sensfrx-save-section">

                <div class="sensfrx-save-inner">

                    <div class="sensfrx-save-info">

                        <svg width="20" height="20" viewBox="0 0 20 20" fill="none" style="opacity: 0.6;">

                            <path d="M10 18a8 8 0 100-16 8 8 0 000 16zM9 9V6h2v3h3v2h-3v3H9v-3H6V9h3z" fill="currentColor"/>

                        </svg>

                        <span>Changes are saved automatically</span>

                    </div>

                    <div class="sensfrx-save-actions">

                        <?php submit_button('Save Changes', 'primary sensfrx-save-btn', 'submit', false); ?>

                        <button type="button" onclick="location.reload()" class="sensfrx-cancel-btn">Reset</button>

                    </div>

                </div>

            </div>



        </form>

    </div>

</div>



<style>

/* Page Header */

.sensfrx-order-header {

    margin-bottom: 40px;

    padding-bottom: 24px;

    border-bottom: 2px solid #f3f4f6;

}



.sensfrx-page-title {

    margin: 0 0 8px 0;

    font-size: 32px;

    font-weight: 700;

    color: #111827;

    letter-spacing: -0.02em;

}



.sensfrx-page-subtitle {

    margin: 0;

    font-size: 16px;

    color: #6b7280;

    line-height: 1.6;

}



/* Info Banner */

.sensfrx-info-banner {

    display: flex;

    gap: 20px;

    padding: 24px;

    background: linear-gradient(135deg, #eff6ff 0%, #dbeafe 100%);

    border-radius: 12px;

    border: 1px solid #bfdbfe;

    margin-bottom: 48px;

}



.sensfrx-info-icon {

    flex-shrink: 0;

    width: 48px;

    height: 48px;

    display: flex;

    align-items: center;

    justify-content: center;

    background: white;

    border-radius: 12px;

    color: #2563eb;

}



.sensfrx-info-content {

    flex: 1;

}



.sensfrx-info-title {

    margin: 0 0 8px 0;

    font-size: 18px;

    font-weight: 600;

    color: #1e40af;

}



.sensfrx-info-text {

    margin: 0;

    font-size: 15px;

    line-height: 1.7;

    color: #1e40af;

}



/* Section Styling */

.sensfrx-section {

    margin-bottom: 56px;

}



.sensfrx-section-title {

    margin: 0 0 8px 0;

    font-size: 24px;

    font-weight: 700;

    color: #111827;

}



.sensfrx-section-subtitle {

    margin: 0 0 32px 0;

    font-size: 15px;

    color: #6b7280;

    line-height: 1.5;

}



/* Risk Grid */

.sensfrx-risk-grid {

    display: grid;

    grid-template-columns: repeat(auto-fit, minmax(380px, 1fr));

    gap: 24px;

}



/* Risk Panels */

.sensfrx-risk-panel {

    background: white;

    border-radius: 16px;

    overflow: hidden;

    box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px 0 rgba(0, 0, 0, 0.06);

    transition: all 0.3s ease;

}



.sensfrx-risk-panel:hover {

    box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);

    transform: translateY(-2px);

}



.sensfrx-risk-low {

    border-top: 4px solid #10b981;

}



.sensfrx-risk-medium {

    border-top: 4px solid #f59e0b;

}



.sensfrx-risk-high {

    border-top: 4px solid #ef4444;

}



.sensfrx-risk-header {

    display: flex;

    align-items: center;

    gap: 16px;

    padding: 24px 24px 20px 24px;

    background: #fafafa;

}



.sensfrx-risk-badge {

    width: 56px;

    height: 56px;

    border-radius: 14px;

    display: flex;

    align-items: center;

    justify-content: center;

    flex-shrink: 0;

}



.sensfrx-badge-low {

    background: #10b981;

    color: white;

}



.sensfrx-badge-medium {

    background: #f59e0b;

    color: white;

}



.sensfrx-badge-high {

    background: #ef4444;

    color: white;

}



.sensfrx-risk-title {

    margin: 0 0 4px 0;

    font-size: 20px;

    font-weight: 700;

    color: #111827;

}



.sensfrx-risk-description {

    margin: 0;

    font-size: 14px;

    color: #6b7280;

    font-weight: 500;

}



.sensfrx-risk-body {

    padding: 24px;

}



.sensfrx-risk-explanation {

    padding: 16px;

    background: #f9fafb;

    border-radius: 10px;

    margin-bottom: 24px;

    border-left: 3px solid #e5e7eb;

}



.sensfrx-risk-explanation p {

    margin: 0;

    font-size: 14px;

    line-height: 1.7;

    color: #374151;

}



/* Form Fields */

.sensfrx-field-group {

    margin-bottom: 0;

}



.sensfrx-field-label {

    display: block;

    margin-bottom: 10px;

}



.sensfrx-label-text {

    display: block;

    font-size: 14px;

    font-weight: 600;

    color: #111827;

    margin-bottom: 4px;

}



.sensfrx-optional {

    font-weight: 400;

    color: #9ca3af;

    font-size: 13px;

}



.sensfrx-label-help {

    display: block;

    font-size: 13px;

    color: #6b7280;

    font-weight: 400;

    line-height: 1.5;

}



.sensfrx-field-input,

.sensfrx-field-textarea {

    width: 100%;

    padding: 12px 16px;

    border: 2px solid #e5e7eb;

    border-radius: 10px;

    font-size: 14px;

    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;

    line-height: 1.6;

    color: #111827;

    transition: all 0.2s ease;

    background: white;

}



.sensfrx-field-textarea {

    resize: vertical;

    min-height: 100px;

}



.sensfrx-field-input:focus,

.sensfrx-field-textarea:focus {

    outline: none;

    border-color: #3b82f6;

    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);

}



.sensfrx-field-input::placeholder,

.sensfrx-field-textarea::placeholder {

    color: #9ca3af;

}



/* Checkbox */

.sensfrx-checkbox-wrapper {

    margin-top: 20px;

}



.sensfrx-checkbox-label {

    display: flex;

    align-items: flex-start;

    gap: 12px;

    padding: 16px;

    background: #fef3c7;

    border-radius: 10px;

    cursor: pointer;

    border: 2px solid transparent;

    transition: all 0.2s ease;

}



.sensfrx-checkbox-label:hover {

    background: #fde68a;

    border-color: #f59e0b;

}



.sensfrx-checkbox-input {

    width: 20px;

    height: 20px;

    margin-top: 2px;

    cursor: pointer;

    flex-shrink: 0;

    accent-color: #f59e0b;

}



.sensfrx-checkbox-text {

    font-size: 14px;

    line-height: 1.6;

    color: #92400e;

}



/* Two Column Grid */

.sensfrx-two-column-grid {

    display: grid;

    grid-template-columns: repeat(auto-fit, minmax(420px, 1fr));

    gap: 24px;

}



.sensfrx-settings-card {

    background: white;

    padding: 28px;

    border-radius: 12px;

    border: 1px solid #e5e7eb;

}



.sensfrx-card-heading {

    margin: 0 0 20px 0;

    font-size: 17px;

    font-weight: 600;

    color: #111827;

    padding-bottom: 12px;

    border-bottom: 2px solid #f3f4f6;

}



/* Save Section */

.sensfrx-save-section {

    position: sticky;

    bottom: 0;

    margin: 48px -32px -32px -32px;

    padding: 20px 32px;

    background: white;

    border-top: 1px solid #e5e7eb;

    box-shadow: 0 -4px 6px -1px rgba(0, 0, 0, 0.05);

    z-index: 10;

}



.sensfrx-save-inner {

    display: flex;

    justify-content: space-between;

    align-items: center;

    gap: 24px;

    max-width: 100%;

}



.sensfrx-save-info {

    display: flex;

    align-items: center;

    gap: 10px;

    color: #6b7280;

    font-size: 14px;

    flex: 1;

}



.sensfrx-save-info svg {

    flex-shrink: 0;

}



.sensfrx-save-actions {

    display: flex;

    gap: 12px;

    align-items: center;

}



.sensfrx-save-btn {

    padding: 12px 28px !important;

    font-size: 15px !important;

    font-weight: 600 !important;

    background: #047857 !important;

    border: none !important;

    border-radius: 8px !important;

    cursor: pointer !important;

    transition: all 0.2s ease !important;

    box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05) !important;

    white-space: nowrap !important;

}



.sensfrx-save-btn:hover {

    background: #065f46 !important;

    box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.1) !important;

    transform: translateY(-1px);

}



.sensfrx-cancel-btn {

    padding: 12px 24px;

    font-size: 14px;

    font-weight: 500;

    background: white;

    color: #6b7280;

    border: 1px solid #d1d5db;

    border-radius: 8px;

    cursor: pointer;

    transition: all 0.2s ease;

    white-space: nowrap;

}



.sensfrx-cancel-btn:hover {

    background: #f9fafb;

    color: #374151;

    border-color: #9ca3af;

}



/* Responsive */

@media (max-width: 1200px) {

    .sensfrx-risk-grid {

        grid-template-columns: 1fr;

    }



    .sensfrx-two-column-grid {

        grid-template-columns: 1fr;

    }

}



@media (max-width: 768px) {

    .sensfrx-page-title {

        font-size: 24px;

    }



    .sensfrx-section-title {

        font-size: 20px;

    }



    .sensfrx-info-banner {

        flex-direction: column;

    }



    .sensfrx-save-inner {

        flex-direction: column;

        align-items: stretch;

        gap: 16px;

    }



    .sensfrx-save-info {

        order: 2;

        justify-content: center;

        font-size: 13px;

    }



    .sensfrx-save-actions {

        order: 1;

        flex-direction: column-reverse;

        width: 100%;

    }



    .sensfrx-save-btn,

    .sensfrx-cancel-btn {

        width: 100% !important;

        text-align: center;

    }

}



/* Email Editor Styling */

.sensfrx-field-textarea {

    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;

}



.mce-tinymce {

    border: 1px solid #e5e7eb !important;

    border-radius: 8px !important;

    overflow: hidden !important;

}



.mce-panel {

    background: #f9fafb !important;

    border-color: #e5e7eb !important;

}

</style>



<script>

jQuery(document).ready(function($) {

    // Auto-save indicator

    let saveTimeout;

    $('#sensfrx-order-review-form :input').on('change keyup', function() {

        clearTimeout(saveTimeout);

        const $info = $('.sensfrx-save-info span');

        $info.text('Unsaved changes...');



        saveTimeout = setTimeout(function() {

            $info.text('Changes are saved automatically');

        }, 2000);

    });

});

</script>

