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

add_action('admin_menu', function() {
    add_submenu_page(
        'woocommerce',
        __('Awesome Invoice Settings', 'awesome-invoice-delivery-notes-packing-slips'),
        __('Invoice Settings', 'awesome-invoice-delivery-notes-packing-slips'),
        'manage_options',
        'aipd-settings',
        'aipd_general_page'
    );
});

add_action('admin_enqueue_scripts', function($hook){
    if ($hook !== 'woocommerce_page_aipd-settings') return;
    wp_enqueue_style('wp-color-picker');
    wp_enqueue_script('wp-color-picker');
});

function aipd_general_page() {

    if (isset($_POST['aipd_nonce']) && wp_verify_nonce($_POST['aipd_nonce'], 'aipd_save_general')) {
        $fields = [
            'company_name','company_email','company_phone','company_address','company_website','company_logo',
            'invoice_prefix','invoice_start','invoice_footer','invoice_terms_conditions',
            'style_font_family','style_font_size','style_text_color','style_header_color','style_background','logo_placement'
        ];
        $values = [];
        foreach ($fields as $f) {
            $values[$f] = isset($_POST['aipd_'.$f]) ? sanitize_text_field($_POST['aipd_'.$f]) : '';
        }
        update_option('aipd_general_settings', $values);
        echo '<div class="updated"><p>'.esc_html__('Settings saved','awesome-invoice-delivery-notes-packing-slips').'</p></div>';
    }

    $defaults = [
        'company_name' => get_bloginfo('name'),
        'company_email'=> get_option('admin_email'),
        'company_phone'=> '',
        'company_address'=> '',
        'company_website'=> home_url(),
        'company_logo'=> '',
        'invoice_prefix'=>'INV-',
        'invoice_start'=>'1001',
        'invoice_footer'=>'Thank you for your business',
        'invoice_terms_conditions'=>'',
        'style_font_family'=>'Arial, sans-serif',
        'style_font_size'=>'14px',
        'style_text_color'=>'#333333',
        'style_header_color'=>'#0073aa',
        'style_background'=>'#ffffff',
        'logo_placement'=>'left',
    ];

    $get = wp_parse_args(get_option('aipd_general_settings', []), $defaults);
    ?>
    <style>
        .aipd-wrap { max-width:960px; margin:20px auto; background:#fff; border-radius:12px; padding:30px; font-family:"Segoe UI", Roboto, sans-serif; }
        .aipd-wrap h1 { text-align:center; font-size:28px; margin-bottom:25px; }
        .aipd-tabs { margin-top:10px; }
        .aipd-tab-buttons { text-align:center; margin-bottom:20px; }
        .aipd-tab-buttons button { margin:0 5px; padding:8px 18px; cursor:pointer; border:1px solid #ccc; border-radius:30px; background:#f7f7f7; transition:.25s; }
        .aipd-tab-buttons button.active { background:#0073aa; color:#fff; border-color:#0073aa; }
        .aipd-tab-content { display:none; }
        .aipd-tab-content.active { display:block; }
        .aipd-grid { display:grid; grid-template-columns:1fr 1fr; gap:20px; margin-bottom:20px; }
        .aipd-grid label { font-weight:600; display:block; margin-bottom:6px; }
        .aipd-grid input[type="text"], .aipd-grid input[type="email"], .aipd-grid input[type="url"], .aipd-grid textarea, .aipd-grid select { width:100%; padding:8px 10px; border:1px solid #ccc; border-radius:6px; }
        .aipd-color-wrap { display:flex; align-items:center; gap:8px; }
        .aipd-color-swatch { width:24px; height:24px; border-radius:50%; border:1px solid #ccc; }
        .aipd-logo-preview img { max-width:160px; margin-top:8px; border-radius:6px; border:1px solid #ddd; }
        .aipd-submit { margin-top:30px; text-align:center; }
        .aipd-submit input[type="submit"] { background:#0073aa; color:#fff; padding:12px 40px; font-size:16px; border:0; border-radius:30px; cursor:pointer; transition:.25s; }
        .aipd-submit input[type="submit"]:hover { background:#005a87; }
        @media (max-width:700px) { .aipd-grid { grid-template-columns:1fr; } }
    </style>

    <div class="wrap aipd-wrap">
        <h1><?php esc_html_e('Awesome Invoice Settings','awesome-invoice-delivery-notes-packing-slips'); ?></h1>

        <div class="aipd-tabs">
            <div class="aipd-tab-buttons">
                <button class="active" data-tab="company"><?php esc_html_e('Company Info','awesome-invoice-delivery-notes-packing-slips'); ?></button>
                <button data-tab="invoice"><?php esc_html_e('Invoice Settings','awesome-invoice-delivery-notes-packing-slips'); ?></button>
                <button data-tab="style"><?php esc_html_e('Style Settings','awesome-invoice-delivery-notes-packing-slips'); ?></button>
            </div>

            <form method="post">
                <?php wp_nonce_field('aipd_save_general','aipd_nonce'); ?>

                <!-- Company Info -->
                <div id="company" class="aipd-tab-content active">
                    <div class="aipd-grid">
                        <div>
                            <label><?php esc_html_e('Company Name','awesome-invoice-delivery-notes-packing-slips'); ?></label>
                            <input type="text" name="aipd_company_name" value="<?php echo esc_attr($get['company_name']); ?>" required>
                        </div>
                        <div>
                            <label><?php esc_html_e('Company Email','awesome-invoice-delivery-notes-packing-slips'); ?></label>
                            <input type="email" name="aipd_company_email" value="<?php echo esc_attr($get['company_email']); ?>" required>
                        </div>
                        <div>
                            <label><?php esc_html_e('Company Phone','awesome-invoice-delivery-notes-packing-slips'); ?></label>
                            <input type="text" name="aipd_company_phone" value="<?php echo esc_attr($get['company_phone']); ?>" required>
                        </div>
                        <div>
                            <label><?php esc_html_e('Company Address','awesome-invoice-delivery-notes-packing-slips'); ?></label>
                            <textarea rows="3" name="aipd_company_address" required><?php echo esc_textarea($get['company_address']); ?></textarea>
                        </div>
                        <div>
                            <label><?php esc_html_e('Company Website URL (optional)','awesome-invoice-delivery-notes-packing-slips'); ?></label>
                            <input type="url" name="aipd_company_website" value="<?php echo esc_url($get['company_website']); ?>">
                        </div>
                        <div>
                            <label><?php esc_html_e('Company Logo URL','awesome-invoice-delivery-notes-packing-slips'); ?></label>
                            <input type="text" name="aipd_company_logo" value="<?php echo esc_url($get['company_logo']); ?>" placeholder="https://example.com/logo.png">
                            <?php if($get['company_logo']): ?>
                                <div class="aipd-logo-preview">
                                    <img src="<?php echo esc_url($get['company_logo']); ?>" alt="Logo Preview">
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>

                <!-- Invoice Settings -->
                <div id="invoice" class="aipd-tab-content">
                    <div class="aipd-grid">
                        <div>
                            <label><?php esc_html_e('Invoice Prefix','awesome-invoice-delivery-notes-packing-slips'); ?></label>
                            <input type="text" name="aipd_invoice_prefix" value="<?php echo esc_attr($get['invoice_prefix']); ?>">
                        </div>
                        <div>
                            <label><?php esc_html_e('Starting Number','awesome-invoice-delivery-notes-packing-slips'); ?></label>
                            <input type="text" name="aipd_invoice_start" value="<?php echo esc_attr($get['invoice_start']); ?>">
                        </div>
                        <div style="grid-column:1 / -1;">
                            <label><?php esc_html_e('Invoice Footer Notes','awesome-invoice-delivery-notes-packing-slips'); ?></label>
                            <textarea rows="2" name="aipd_invoice_footer"><?php echo esc_textarea($get['invoice_footer']); ?></textarea>
                        </div>
                        <div style="grid-column:1 / -1;">
                            <label><?php esc_html_e('Terms & Conditions','awesome-invoice-delivery-notes-packing-slips'); ?></label>
                            <textarea rows="4" name="aipd_invoice_terms_conditions"><?php echo esc_textarea($get['invoice_terms_conditions']); ?></textarea>
                        </div>
                    </div>
                </div>

                <!-- Style Settings -->
                <div id="style" class="aipd-tab-content">
                    <div class="aipd-grid">
                        <div>
                            <label><?php esc_html_e('Font Family','awesome-invoice-delivery-notes-packing-slips'); ?></label>
                            <select name="aipd_style_font_family">
                                <?php
                                $fonts = ['Arial, sans-serif','Tahoma, sans-serif','Verdana, sans-serif','Helvetica, sans-serif','Georgia, serif'];
                                foreach($fonts as $font){
                                    echo '<option value="'.esc_attr($font).'" '.selected($get['style_font_family'],$font,false).'>'.esc_html($font).'</option>';
                                }
                                ?>
                            </select>
                        </div>
                        <div>
                            <label><?php esc_html_e('Font Size','awesome-invoice-delivery-notes-packing-slips'); ?></label>
                            <input type="text" name="aipd_style_font_size" value="<?php echo esc_attr($get['style_font_size']); ?>" placeholder="14px">
                        </div>
                        <div>
                            <label><?php esc_html_e('Text Color','awesome-invoice-delivery-notes-packing-slips'); ?></label>
                            <input type="text" class="aipd-color" name="aipd_style_text_color" value="<?php echo esc_attr($get['style_text_color']); ?>">
                        </div>
                        <div>
                            <label><?php esc_html_e('Header Color / Background Color','awesome-invoice-delivery-notes-packing-slips'); ?></label>
                            <input type="text" class="aipd-color" name="aipd_style_header_color" value="<?php echo esc_attr($get['style_header_color']); ?>">
                        </div>
                        <div>
                            <label><?php esc_html_e('Logo Placement','awesome-invoice-delivery-notes-packing-slips'); ?></label>
                            <select name="aipd_logo_placement">
                                <option value="left" <?php selected($get['logo_placement'],'left'); ?>>Left</option>
                                <option value="center" <?php selected($get['logo_placement'],'center'); ?>>Center</option>
                                <option value="right" <?php selected($get['logo_placement'],'right'); ?>>Right</option>
                            </select>
                        </div>
                    </div>
                </div>

                <div class="aipd-submit">
                    <?php submit_button(__('Save Settings','awesome-invoice-delivery-notes-packing-slips')); ?>
                </div>
            </form>
        </div>
    </div>

    <script>
    jQuery(document).ready(function($){
        // Tabs
        $('.aipd-tab-buttons button').on('click', function(){
            var tab = $(this).data('tab');
            $('.aipd-tab-buttons button').removeClass('active');
            $(this).addClass('active');
            $('.aipd-tab-content').removeClass('active');
            $('#'+tab).addClass('active');
            localStorage.setItem('aipd_active_tab', tab);
        });
        var activeTab = localStorage.getItem('aipd_active_tab') || 'company';
        $('.aipd-tab-buttons button[data-tab="'+activeTab+'"]').click();

        // Color Pickers
        $('.aipd-color').wpColorPicker();
    });
    </script>
<?php
} // End function
