<?php
/**
 * Plugin Name: WooCommerce SaaS Premium Delivery (Serson Final)
 * Description: Entrega de PDF e Links SaaS com Scroll Horizontal Fluido e Design Serson.
 * Version: 3.0
 */

if ( ! defined( 'ABSPATH' ) ) exit;

// 1. CAMPOS NO ADMIN
add_action( 'woocommerce_product_options_general_product_data', 'serson_admin_fields' );
function serson_admin_fields() {
    echo '<div class="options_group" style="border-top: 2px solid #811d1d; padding: 10px;">';
    echo '<strong style="color:#811d1d; margin-left:10px;">Configurações de Entrega SaaS</strong>';
    woocommerce_wp_text_input( array( 'id' => '_saas_link', 'label' => 'Link da App' ) );
    woocommerce_wp_text_input( array( 'id' => '_saas_pdf', 'label' => 'Link do PDF' ) );
    echo '</div>';
}

add_action( 'woocommerce_process_product_meta', 'serson_save_fields' );
function serson_save_fields( $post_id ) {
    update_post_meta( $post_id, '_saas_link', esc_url_raw( $_POST['_saas_link'] ) );
    update_post_meta( $post_id, '_saas_pdf', esc_url_raw( $_POST['_saas_pdf'] ) );
}

// 2. EXIBIÇÃO NO FRONT-END (CORRIGIDA)
add_action( 'woocommerce_order_details_after_order_table', 'serson_display_links_ui', 10, 1 );

function serson_display_links_ui( $order ) {
    $order = wc_get_order( $order );
    if ( ! $order->is_paid() ) return;

    ?>
    <style>
        .serson-delivery-section {
            margin: 40px 0;
            background: #fff;
            border-radius: 12px;
            border: 1px solid #ddd;
            overflow: hidden; /* Importante para não quebrar layout */
            position: relative;
            z-index: 10; /* Menor que o menu para não o tapar */
        }
        .serson-header {
            background: #f8f8f8;
            padding: 15px 20px;
            border-bottom: 1px solid #eee;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .serson-header h3 {
            margin: 0 !important;
            font-size: 16px;
            color: #333;
            text-transform: uppercase;
        }
        /* ÁREA DE SCROLL HORIZONTAL */
        .serson-scroll-container {
            display: flex; /* Garante que os cards ficam lado a lado */
            flex-wrap: nowrap; /* Impede que os cards saltem para baixo */
            overflow-x: auto;
            -webkit-overflow-scrolling: touch;
            padding: 20px;
            gap: 20px;
        }
        .serson-scroll-container::-webkit-scrollbar { height: 8px; }
        .serson-scroll-container::-webkit-scrollbar-thumb { background: #811d1d; border-radius: 10px; }
        
        /* CARD INDIVIDUAL */
        .serson-card {
            flex: 0 0 280px; /* Largura fixa para cada card */
            background: #ffffff;
            border: 1px solid #eee;
            border-radius: 8px;
            padding: 20px;
            box-shadow: 0 4px 12px rgba(0,0,0,0.05);
            text-align: center;
        }
        .serson-card strong {
            display: block;
            margin-bottom: 15px;
            font-size: 14px;
            color: #811d1d;
            height: 40px; /* Alinha títulos diferentes */
            overflow: hidden;
        }
        .serson-btn {
            display: block;
            padding: 12px;
            margin-top: 10px;
            border-radius: 6px;
            text-decoration: none !important;
            font-weight: bold;
            font-size: 13px;
            transition: 0.3s;
        }
        .btn-app { background: #811d1d; color: #fff !important; }
        .btn-pdf { background: #fff; color: #811d1d !important; border: 1px solid #811d1d; }
        
        .serson-hint {
            font-size: 11px;
            color: #999;
            text-align: center;
            padding-bottom: 10px;
        }
    </style>

    <div class="serson-delivery-section">
        <div class="serson-header">
            <h3>🔑 Seus Acessos SaaS</h3>
            <span style="color:#811d1d;">↔️ Deslize</span>
        </div>

        <div class="serson-scroll-container">
            <?php
            $has_items = false;
            foreach ( $order->get_items() as $item ) {
                $product_id = $item->get_product_id();
                $link = get_post_meta( $product_id, '_saas_link', true );
                $pdf = get_post_meta( $product_id, '_saas_pdf', true );

                if ( $link || $pdf ) {
                    $has_items = true;
                    echo '<div class="serson-card">';
                    echo '<strong>' . esc_html( $item->get_name() ) . '</strong>';
                    
                    if ( $link ) {
                        echo '<a href="'.esc_url($link).'" target="_blank" class="serson-btn btn-app">ACEDER À PLATAFORMA</a>';
                    }
                    if ( $pdf ) {
                        echo '<a href="'.esc_url($pdf).'" target="_blank" class="serson-btn btn-pdf">DOWNLOAD MANUAL PDF</a>';
                    }
                    echo '</div>';
                }
            }
            if (!$has_items) echo '<p style="padding:20px;">Nenhum ficheiro digital disponível para esta encomenda.</p>';
            ?>
        </div>
        <div class="serson-hint">← Arraste para os lados para ver mais produtos →</div>
    </div>
    <?php
}