<?php 

if ( ! defined( 'ABSPATH' ) ) exit;
/**
 * afdp_ Payments Tab - Multi-Currency & Settings (Professional Design)
 * Primary Color: #e53935
 */
function afdp_payments_tab() {
    // --- 1. SAVE LOGIC ---
    if (isset($_POST['afdp_save_payments']) && isset($_POST['afdp_payments_nonce'])) {
        if (wp_verify_nonce(sanitize_key(wp_unslash($_POST['afdp_payments_nonce'])), 'afdp_save_payments_action')) {
            
            update_option('afdp_currency', isset($_POST['afdp_currency']) ? sanitize_text_field(wp_unslash($_POST['afdp_currency'])) : 'USD');
            update_option('afdp_enable_bacs', isset($_POST['afdp_enable_bacs']) ? 'yes' : 'no');
            
            if (isset($_POST['afdp_bacs_details'])) {
                update_option('afdp_bacs_details', sanitize_textarea_field(wp_unslash($_POST['afdp_bacs_details'])));
            }

            update_option('afdp_enable_cod', isset($_POST['afdp_enable_cod']) ? 'yes' : 'no');
            
            if (isset($_POST['afdp_cod_note'])) {
                update_option('afdp_cod_note', sanitize_text_field(wp_unslash($_POST['afdp_cod_note'])));
            }

            echo '<div class="notice notice-success is-dismissible"><p>Settings updated successfully.</p></div>';
        }
    }

    $afdp_current_currency = get_option('afdp_currency', 'USD');
    $afdp_bacs_enabled     = get_option('afdp_enable_bacs', 'no');
    $afdp_bacs_details     = get_option('afdp_bacs_details', '');
    $afdp_cod_enabled      = get_option('afdp_enable_cod', 'yes');
    $afdp_cod_note         = get_option('afdp_cod_note', 'Pay with cash upon delivery.');
    ?>

    <style>
        .afdp-payments-wrap { max-width: 850px; margin-top: 10px; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; }
        
        /* Header Section */
        .afdp-settings-header { margin-bottom: 25px; }
        .afdp-settings-header h2 { font-size: 22px; font-weight: 800; color: #1e293b; margin: 0; }
        .afdp-settings-header p { color: #64748b; margin: 4px 0 0; font-size: 14px; }

        /* Card Styling */
        .afdp-card { 
            background: #fff; border-radius: 12px; border: 1px solid #e2e8f0; 
            box-shadow: 0 1px 3px rgba(0,0,0,0.02); margin-bottom: 20px; overflow: hidden;
        }
        .afdp-card-header { 
            padding: 15px 20px; border-bottom: 1px solid #f1f5f9; background: #fafafa;
            display: flex; align-items: center; justify-content: space-between;
        }
        .afdp-card-header h3 { 
            font-size: 13px; font-weight: 800; text-transform: uppercase; color: #475569; 
            margin: 0; display: flex; align-items: center; gap: 8px; letter-spacing: 0.025em;
        }
        .afdp-card-body { padding: 25px; }

        /* Form Controls */
        .afdp-form-row { display: grid; grid-template-columns: 240px 1fr; gap: 30px; align-items: start; }
        .afdp-input-label { font-weight: 700; font-size: 14px; color: #1e293b; }
        .afdp-input-label small { display: block; font-weight: 400; color: #64748b; font-size: 12px; margin-top: 4px; line-height: 1.5; }
        
        .afdp-field-input { 
            width: 100%; max-width: 380px; padding: 10px 14px; border: 1px solid #cbd5e1; 
            border-radius: 8px; font-size: 14px; color: #1e293b; transition: all 0.2s ease;
            outline: none !important;
        }
        .afdp-field-input:focus { 
            border-color: #e53935; box-shadow: 0 0 0 3px rgba(229, 57, 53, 0.1) !important; 
        }

        /* Modern Toggle Switch */
        .afdp-switch { position: relative; display: inline-block; width: 44px; height: 22px; }
        .afdp-switch input { opacity: 0; width: 0; height: 0; }
        .afdp-slider {
            position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0;
            background-color: #cbd5e1; transition: .3s; border-radius: 34px;
        }
        .afdp-slider:before {
            position: absolute; content: ""; height: 16px; width: 16px; left: 3px; bottom: 3px;
            background-color: white; transition: .3s; border-radius: 50%;
        }
        input:checked + .afdp-slider { background-color: #10b981; }
        input:checked + .afdp-slider:before { transform: translateX(22px); }

        /* Save Button */
        .afdp-action-bar { margin-top: 30px; display: flex; justify-content: flex-end; }
        .afdp-btn-save {
            background: #e53935; color: #fff !important; padding: 12px 30px; 
            border-radius: 8px; font-weight: 700; border: none; cursor: pointer;
            font-size: 14px; transition: all 0.2s; display: flex; align-items: center; gap: 8px;
            outline: none !important; box-shadow: none !important;
        }
        .afdp-btn-save:hover { background: #d32f2f; transform: translateY(-1px); }
        .afdp-btn-save .dashicons { font-size: 18px; width: 18px; height: 18px; }

        /* Responsive */
        @media (max-width: 782px) {
            .afdp-form-row { grid-template-columns: 1fr; gap: 10px; }
        }
    </style>

    <div class="afdp-payments-wrap">
        <form method="post" action="">
            <?php wp_nonce_field('afdp_save_payments_action', 'afdp_payments_nonce'); ?>
            
            <header class="afdp-settings-header">
                <h2>Payment Configuration</h2>
                <p>Configure how customers pay for their orders and set your local currency.</p>
            </header>

            <div class="afdp-card">
                <div class="afdp-card-header">
                    <h3><span class="dashicons dashicons-admin-site"></span> Global Localization</h3>
                </div>
                <div class="afdp-card-body">
                    <div class="afdp-form-row">
                        <div class="afdp-input-label">
                            Store Currency
                            <small>This currency will be applied to all product prices and checkout totals.</small>
                        </div>
                        <div>
                            <select name="afdp_currency" class="afdp-field-input">
                                <optgroup label="Popular">
                                    <option value="USD" <?php selected($afdp_current_currency, 'USD'); ?>>US Dollar ($)</option>
                                    <option value="EUR" <?php selected($afdp_current_currency, 'EUR'); ?>>Euro (€)</option>
                                    <option value="GBP" <?php selected($afdp_current_currency, 'GBP'); ?>>Pound Sterling (£)</option>
                                    <option value="INR" <?php selected($afdp_current_currency, 'INR'); ?>>Indian Rupee (₹)</option>
                                    <option value="BDT" <?php selected($afdp_current_currency, 'BDT'); ?>>Bangladeshi Taka (৳)</option>
                                </optgroup>
                                <optgroup label="Other">
                                    <option value="AED" <?php selected($afdp_current_currency, 'AED'); ?>>UAE Dirham (د.إ)</option>
                                    <option value="CAD" <?php selected($afdp_current_currency, 'CAD'); ?>>Canadian Dollar (C$)</option>
                                    <option value="SAR" <?php selected($afdp_current_currency, 'SAR'); ?>>Saudi Riyal (SR)</option>
                                    <option value="JPY" <?php selected($afdp_current_currency, 'JPY'); ?>>Japanese Yen (¥)</option>
                                </optgroup>
                            </select>
                        </div>
                    </div>
                </div>
            </div>

            <div class="afdp-card">
                <div class="afdp-card-header">
                    <h3><span class="dashicons dashicons-bank"></span> Direct Bank Transfer</h3>
                    <label class="afdp-switch">
                        <input type="checkbox" name="afdp_enable_bacs" <?php checked($afdp_bacs_enabled, 'yes'); ?>>
                        <span class="afdp-slider"></span>
                    </label>
                </div>
                <div class="afdp-card-body">
                    <div class="afdp-form-row">
                        <div class="afdp-input-label">
                            Transfer Details
                            <small>Provide your Bank Name, Account Number, and IBAN for the customer to see.</small>
                        </div>
                        <textarea name="afdp_bacs_details" class="afdp-field-input" style="height:100px; resize: vertical;" placeholder="e.g. Bank: Chase, Account: 12345678..."><?php echo esc_textarea($afdp_bacs_details); ?></textarea>
                    </div>
                </div>
            </div>

            <div class="afdp-card">
                <div class="afdp-card-header">
                    <h3><span class="dashicons dashicons-money"></span> Cash on Delivery</h3>
                    <label class="afdp-switch">
                        <input type="checkbox" name="afdp_enable_cod" <?php checked($afdp_cod_enabled, 'yes'); ?>>
                        <span class="afdp-slider"></span>
                    </label>
                </div>
                <div class="afdp-card-body">
                    <div class="afdp-form-row">
                        <div class="afdp-input-label">
                            Checkout Note
                            <small>A short message displayed when the customer selects Cash on Delivery.</small>
                        </div>
                        <input type="text" name="afdp_cod_note" class="afdp-field-input" value="<?php echo esc_attr($afdp_cod_note); ?>" placeholder="e.g. Please have exact change ready.">
                    </div>
                </div>
            </div>

            <div class="afdp-action-bar">
                <button type="submit" name="afdp_save_payments" class="afdp-btn-save">
                    <span class="dashicons dashicons-saved"></span> Update Payments
                </button>
            </div>
        </form>
    </div>
    <?php
}