<?php
if (!defined('ABSPATH')) exit;

class VIMAKAVA_GBC_Settings {

    public function __construct() {
        add_action('admin_menu', array($this, 'add_admin_menu'));
        add_action('admin_init', array($this, 'register_settings'));
    }

    public function add_admin_menu() {
        add_menu_page(
            'Gold Calculator Settings',
            'Gold Calc',
            'manage_options',
            'vimakava-gbc-settings',
            array($this, 'settings_page_html'),
            'dashicons-chart-line',
            80
        );
    }

    public function register_settings() {
        register_setting('vimakava_gbc_settings_group', 'vimakava_gbc_manual_gold_rate', array('sanitize_callback' => 'sanitize_text_field'));
        register_setting('vimakava_gbc_settings_group', 'vimakava_gbc_buyback_margin', array('sanitize_callback' => 'absint'));
        register_setting('vimakava_gbc_settings_group', 'vimakava_gbc_enable_lead_capture', array('sanitize_callback' => 'absint'));
        register_setting('vimakava_gbc_settings_group', 'vimakava_gbc_currency', array('sanitize_callback' => 'sanitize_text_field'));
        register_setting('vimakava_gbc_settings_group', 'vimakava_gbc_smtp_enable', array('sanitize_callback' => 'absint'));
        register_setting('vimakava_gbc_settings_group', 'vimakava_gbc_smtp_host', array('sanitize_callback' => 'sanitize_text_field'));
        register_setting('vimakava_gbc_settings_group', 'vimakava_gbc_smtp_port', array('sanitize_callback' => 'absint'));
        register_setting('vimakava_gbc_settings_group', 'vimakava_gbc_smtp_username', array('sanitize_callback' => 'sanitize_text_field'));
        register_setting('vimakava_gbc_settings_group', 'vimakava_gbc_smtp_password', array('sanitize_callback' => 'sanitize_text_field'));
        register_setting('vimakava_gbc_settings_group', 'vimakava_gbc_whatsapp_enable', array('sanitize_callback' => 'absint'));
        register_setting('vimakava_gbc_settings_group', 'vimakava_gbc_whatsapp_number', array('sanitize_callback' => 'sanitize_text_field'));
    }

    public function settings_page_html() {
        ?>
        <div class="wrap">
            <h1>Daily Gold Rate Settings</h1>
            <p>Set the gold rate manually here to be used by the frontend calculator.</p>
            <form method="post" action="options.php">
                <?php
                settings_fields('vimakava_gbc_settings_group');
                do_settings_sections('vimakava_gbc_settings_group');
                ?>
                <table class="form-table">
                    <tr valign="top">
                        <th scope="row">Today's Gold Rate (per gram)</th>
                        <td>
                            <input type="number" step="0.01" name="vimakava_gbc_manual_gold_rate" value="<?php echo esc_attr(get_option('vimakava_gbc_manual_gold_rate', '75.00')); ?>" class="regular-text" />
                        </td>
                    </tr>
                    <tr valign="top">
                        <th scope="row">Your Payout Percentage (%)</th>
                        <td>
                            <input type="number" name="vimakava_gbc_buyback_margin" value="<?php echo esc_attr(get_option('vimakava_gbc_buyback_margin', '95')); ?>" class="regular-text" />
                            <p class="description">Example: 95 means you pay the customer 95% of market value. Your profit is 5%.</p>
                        </td>
                    </tr>
                    <tr valign="top">
                        <th scope="row">Enable Lead Capture</th>
                        <td>
                            <input type="checkbox" name="vimakava_gbc_enable_lead_capture" value="1" <?php checked(get_option('vimakava_gbc_enable_lead_capture', '1'), 1); ?> />
                            <p class="description">If checked, shows the "Lock this Price" email form to visitors.</p>
                        </td>
                    </tr>
                    
                    <tr>
                        <th colspan="2" style="padding-top:20px;">
                            <hr>
                            <h2>WhatsApp Integration</h2>
                        </th>
                    </tr>
                    <tr valign="top">
                        <th scope="row">Enable WhatsApp Share</th>
                        <td>
                            <input type="checkbox" name="vimakava_gbc_whatsapp_enable" value="1" <?php checked(get_option('vimakava_gbc_whatsapp_enable'), 1); ?> />
                            <p class="description">If enabled, customers get a button to instantly forward their quote to your WhatsApp.</p>
                        </td>
                    </tr>
                    <tr valign="top">
                        <th scope="row">Admin WhatsApp Number</th>
                        <td>
                            <input type="text" name="vimakava_gbc_whatsapp_number" value="<?php echo esc_attr(get_option('vimakava_gbc_whatsapp_number')); ?>" class="regular-text" placeholder="e.g. 15551234567" />
                            <p class="description">Include country code. Do not use + or - or spaces. Example for India: 919876543210</p>
                        </td>
                    </tr>

                    <tr>
                        <th colspan="2" style="padding-top:20px;">
                            <hr>
                            <h2>Localization</h2>
                        </th>
                    </tr>
                    <tr valign="top">
                        <th scope="row">Currency</th>
                        <td>
                            <?php $currency = get_option('vimakava_gbc_currency', '$'); ?>
                            <select name="vimakava_gbc_currency">
                                <option value="$" <?php selected($currency, '$'); ?>>USD ($)</option>
                                <option value="€" <?php selected($currency, '€'); ?>>EUR (€)</option>
                                <option value="£" <?php selected($currency, '£'); ?>>GBP (£)</option>
                                <option value="₹" <?php selected($currency, '₹'); ?>>INR (₹)</option>
                                <option value="A$" <?php selected($currency, 'A$'); ?>>AUD (A$)</option>
                                <option value="C$" <?php selected($currency, 'C$'); ?>>CAD (C$)</option>
                            </select>
                            <p class="description">Select the currency symbol to display on the calculator.</p>
                        </td>
                    </tr>
                    
                    <tr>
                        <th colspan="2" style="padding-top:30px;">
                            <hr>
                            <h2>Email Deliverability (SMTP)</h2>
                            <p>Enable and configure these settings if your local server (XAMPP) cannot send lead emails.</p>
                        </th>
                    </tr>
                    <tr valign="top">
                        <th scope="row">Enable Custom SMTP</th>
                        <td>
                            <input type="checkbox" name="vimakava_gbc_smtp_enable" value="1" <?php checked(get_option('vimakava_gbc_smtp_enable'), 1); ?> />
                            <span class="description">Check to route emails through an external SMTP server.</span>
                        </td>
                    </tr>
                    <tr valign="top">
                        <th scope="row">SMTP Host</th>
                        <td>
                            <input type="text" name="vimakava_gbc_smtp_host" value="<?php echo esc_attr(get_option('vimakava_gbc_smtp_host', 'smtp.gmail.com')); ?>" class="regular-text" />
                        </td>
                    </tr>
                    <tr valign="top">
                        <th scope="row">SMTP Port</th>
                        <td>
                            <input type="number" name="vimakava_gbc_smtp_port" value="<?php echo esc_attr(get_option('vimakava_gbc_smtp_port', '587')); ?>" class="regular-text" />
                            <p class="description">Commonly 587 (TLS) or 465 (SSL)</p>
                        </td>
                    </tr>
                    <tr valign="top">
                        <th scope="row">SMTP Username / Email</th>
                        <td>
                            <input type="text" name="vimakava_gbc_smtp_username" value="<?php echo esc_attr(get_option('vimakava_gbc_smtp_username')); ?>" class="regular-text" />
                        </td>
                    </tr>
                    <tr valign="top">
                        <th scope="row">SMTP Password</th>
                        <td>
                            <input type="password" name="vimakava_gbc_smtp_password" value="<?php echo esc_attr(get_option('vimakava_gbc_smtp_password')); ?>" class="regular-text" />
                            <p class="description">If using Gmail, generate an <a href="https://myaccount.google.com/apppasswords" target="_blank">App Password</a> to use here.</p>
                        </td>
                    </tr>
                </table>
                <?php submit_button('Update Settings'); ?>
            </form>
        </div>
        <?php
    }
}
