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

function elementsell_render_invoice_detail_page() {
    if ( ! current_user_can( 'manage_options' ) ) {
        wp_die( esc_html__( 'Nemáte oprávnění zobrazit tuto stránku.', 'platebni-formular' ) );
    }

    $invoice_id = isset( $_GET['invoice_id'] ) ? intval( $_GET['invoice_id'] ) : 0;
    $invoice    = $invoice_id ? elementsell_get_invoice( $invoice_id ) : null;

    echo '<div class="wrap elementsell-admin elementsell-invoice-detail-wrap">';

    $return = isset( $_GET['return'] ) ? sanitize_key( wp_unslash( (string) $_GET['return'] ) ) : '';
    $back_url = $return === 'issued'
        ? elementsell_get_issued_invoices_admin_url()
        : admin_url( 'admin.php?page=elementsell' );

    echo '<p style="margin-bottom:16px;"><a href="' . esc_url( $back_url ) . '">&larr; Zpět</a></p>';

    if ( ! $invoice ) {
        echo '<p>Faktura nebyla nalezena.</p>';
        echo '</div>';
        return;
    }

    elementsell_sync_invoice_with_order( (int) $invoice->order_id );
    $invoice = elementsell_get_invoice( $invoice_id );

    if ( ! $invoice ) {
        echo '<p>Faktura nebyla nalezena.</p>';
        echo '</div>';
        return;
    }

    $order = elementsell_get_order( (int) $invoice->order_id );

    if ( ! $order ) {
        echo '<p>Objednávka k faktuře nebyla nalezena.</p>';
        echo '</div>';
        return;
    }

    elementsell_render_invoice_detail_styles();

    $supplier_logo = get_option( 'faktury_logo', '' );
    $supplier_sign = get_option( 'faktury_podpis', '' );

    $is_vat_payer = (bool) (int) get_option( 'faktury_platce_dph', 0 );

    $vat_rate = 21.0;
    $stored_vat_rate = get_option( 'faktury_dph_sazba', false );
    if ( false !== $stored_vat_rate ) {
        $vat_rate = (float) $stored_vat_rate;
    }
    $is_demo      = ( $invoice->type === 'demo' );
    $is_paid      = ( $invoice->status === 'paid' );

    $customer_name    = trim( $order->first_name . ' ' . $order->surname );
    $customer_company = elementsell_get_order_field( $order->id, 'company' );
    $customer_address = elementsell_get_order_field( $order->id, 'address' );
    $customer_ico     = elementsell_get_order_field( $order->id, 'ico' );
    $customer_dic     = elementsell_get_order_field( $order->id, 'dic' );

    $price = (float) $invoice->price;
    $total = $price;
    $base  = $price;
    $vat_amount = 0.0;

    if ( $is_vat_payer && $vat_rate > 0 ) {
        $base       = $total / ( 1 + ( $vat_rate / 100 ) );
        $vat_amount = $total - $base;
    }

    $issue_date = $invoice->issue_date
        ? mysql2date( 'j. n. Y', $invoice->issue_date, false )
        : '';

    $paid_label = $is_paid ? 'Zaplaceno' : 'Čeká na úhradu';
    $vat_label  = $is_vat_payer ? 'Plátce DPH' : 'Neplátce DPH';

    if ( ! $is_demo && function_exists( 'elementsell_get_invoice_edit_admin_url' ) ) {
        echo '<p style="margin:0 0 16px;"><a class="button" href="' . esc_url( elementsell_get_invoice_edit_admin_url( $invoice_id, $return === 'issued' ? 'issued' : 'orders' ) ) . '">Upravit fakturu</a></p>';
    }

    echo '<div class="elementsell-invoice-preview">';
    echo '<div class="elementsell-invoice-page">';

    if ( $is_demo ) {
        echo '<div class="elementsell-invoice-watermark" aria-hidden="true">DEMO</div>';
    }

    echo '<div class="elementsell-invoice-content">';
    echo '<div class="elementsell-invoice-top-line"></div>';

    echo '<div class="elementsell-invoice-header">';
    echo '<div class="elementsell-invoice-header-left">';

    if ( ! empty( $supplier_logo ) ) {
        echo '<img src="' . esc_url( $supplier_logo ) . '" alt="" class="elementsell-invoice-logo">';
    }

    echo '</div>';
    echo '<div class="elementsell-invoice-header-right">';
    echo '<div class="elementsell-invoice-number">';
    if ( $is_demo ) {
        echo 'Doklad DEMO č. ' . esc_html( $invoice->invoice_number );
    } else {
        echo 'Faktura č. ' . esc_html( $invoice->invoice_number );
    }
    echo '</div>';
    echo '<div class="elementsell-invoice-vat-label">' . esc_html( $vat_label ) . '</div>';

    if ( $issue_date ) {
        echo '<div class="elementsell-invoice-issue-date">Datum vystavení: ' . esc_html( $issue_date ) . '</div>';
    }

    echo '</div>';
    echo '</div>';

    echo '<hr class="elementsell-invoice-hr">';

    echo '<div class="elementsell-invoice-parties">';
    echo '<div class="elementsell-invoice-party">';
    echo '<h3>Dodavatel</h3>';
    echo '<p class="elementsell-invoice-party-name"><strong>Jméno dodavatele:</strong> ' . esc_html( (string) get_option( 'faktury_jmeno', '' ) ) . '</p>';
    echo '<p class="elementsell-invoice-party-address"><strong>Adresa:</strong> ' . nl2br( esc_html( (string) get_option( 'faktury_adresa', '' ) ) ) . '</p>';
    echo '<p><strong>IČO:</strong> ' . esc_html( (string) get_option( 'faktury_ico', '' ) ) . '</p>';
    echo '<p><strong>DIČ:</strong> ' . esc_html( (string) get_option( 'faktury_dic', '' ) ) . '</p>';
    echo '<p><strong>Email:</strong> ' . esc_html( elementsell_get_invoice_supplier_email() ) . '</p>';
    echo '<p><strong>Web:</strong> ' . esc_html( elementsell_get_invoice_supplier_web() ) . '</p>';
    echo '</div>';

    echo '<div class="elementsell-invoice-party">';
    echo '<h3>Odběratel</h3>';

    if ( $customer_name ) {
        echo '<p class="elementsell-invoice-party-name">' . esc_html( $customer_name ) . '</p>';
    }

    if ( $customer_company ) {
        echo '<p>' . esc_html( $customer_company ) . '</p>';
    }

    if ( $customer_address ) {
        echo '<p class="elementsell-invoice-party-address">' . esc_html( $customer_address ) . '</p>';
    }

    if ( $customer_ico ) {
        echo '<p>IČO: ' . esc_html( $customer_ico ) . '</p>';
    }

    if ( $customer_dic ) {
        echo '<p>DIČ: ' . esc_html( $customer_dic ) . '</p>';
    }

    if ( $order->email ) {
        echo '<p>' . esc_html( $order->email ) . '</p>';
    }

    echo '</div>';
    echo '</div>';

    echo '<hr class="elementsell-invoice-hr">';

    echo '<table class="elementsell-invoice-items">';
    echo '<thead><tr><th>Popis</th><th>Cena</th></tr></thead>';
    echo '<tbody>';
    echo '<tr>';
    echo '<td>' . esc_html( $invoice->product_name ?: '—' ) . '</td>';
    echo '<td>' . esc_html( elementsell_format_invoice_amount( $price ) ) . '</td>';
    echo '</tr>';
    echo '</tbody>';
    echo '</table>';

    echo '<div class="elementsell-invoice-totals">';
    echo '<div class="elementsell-invoice-totals-inner">';

    if ( $is_vat_payer && $vat_rate > 0 ) {
        echo '<div class="elementsell-invoice-total-row"><span>Základ DPH</span><span>' . esc_html( elementsell_format_invoice_amount( $base ) ) . '</span></div>';
        echo '<div class="elementsell-invoice-total-row"><span>DPH ' . esc_html( rtrim( rtrim( number_format( $vat_rate, 2, ',', '' ), '0' ), ',' ) ) . ' %</span><span>' . esc_html( elementsell_format_invoice_amount( $vat_amount ) ) . '</span></div>';
    }

    echo '<div class="elementsell-invoice-total-row elementsell-invoice-total-row--main"><span>Celkem</span><span>' . esc_html( elementsell_format_invoice_amount( $total ) ) . '</span></div>';
    echo '</div>';
    echo '</div>';

    echo '<div class="elementsell-invoice-status ' . ( $is_paid ? 'is-paid' : 'is-unpaid' ) . '">' . esc_html( $paid_label ) . '</div>';

    if ( ! empty( $supplier_sign ) ) {
        echo '<div class="elementsell-invoice-signature">';
        echo '<img src="' . esc_url( $supplier_sign ) . '" alt="">';
        echo '</div>';
    }

    echo '</div>';
    echo '</div>';
    echo '</div>';
    echo '</div>';
}

function elementsell_render_invoice_detail_styles() {
    static $rendered = false;

    if ( $rendered ) {
        return;
    }

    $rendered = true;
    ?>
    <style>
        .elementsell-invoice-detail-wrap {
            max-width: none;
        }

        .elementsell-invoice-preview {
            background: #e8e8e8;
            padding: 32px 24px;
            margin: 0 0 24px;
        }

        .elementsell-invoice-page {
            position: relative;
            width: 794px;
            max-width: 100%;
            margin: 0 auto;
            background: #fff;
            box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
            overflow: hidden;
        }

        .elementsell-invoice-watermark {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%) rotate(-28deg);
            font-size: 140px;
            font-weight: 700;
            letter-spacing: 0.08em;
            color: rgba(0, 0, 0, 0.07);
            pointer-events: none;
            z-index: 1;
            user-select: none;
            white-space: nowrap;
        }

        .elementsell-invoice-content {
            position: relative;
            z-index: 2;
            padding: 28px 36px 40px;
            color: #222;
            font-size: 14px;
            line-height: 1.5;
        }

        .elementsell-invoice-top-line {
            height: 3px;
            background: #222;
            margin: 0 0 24px;
        }

        .elementsell-invoice-header {
            display: flex;
            justify-content: space-between;
            align-items: flex-start;
            gap: 24px;
        }

        .elementsell-invoice-header-left {
            flex: 1;
            min-width: 0;
        }

        .elementsell-invoice-logo {
            max-width: 180px;
            max-height: 80px;
            height: auto;
            display: block;
        }

        .elementsell-invoice-header-right {
            text-align: right;
            flex-shrink: 0;
        }

        .elementsell-invoice-number {
            font-size: 22px;
            font-weight: 700;
            line-height: 1.3;
        }

        .elementsell-invoice-vat-label {
            margin-top: 6px;
            font-size: 14px;
            font-weight: 600;
        }

        .elementsell-invoice-issue-date {
            margin-top: 8px;
            font-size: 13px;
            color: #555;
        }

        .elementsell-invoice-hr {
            border: 0;
            border-top: 2px solid #222;
            margin: 22px 0;
        }

        .elementsell-invoice-parties {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 32px;
        }

        .elementsell-invoice-party h3 {
            margin: 0 0 10px;
            font-size: 15px;
            font-weight: 700;
            text-transform: uppercase;
            letter-spacing: 0.03em;
        }

        .elementsell-invoice-party p {
            margin: 0 0 6px;
        }

        .elementsell-invoice-party-name {
            font-weight: 600;
        }

        .elementsell-invoice-items {
            width: 100%;
            border-collapse: collapse;
        }

        .elementsell-invoice-items th,
        .elementsell-invoice-items td {
            padding: 12px 0;
            border-bottom: 1px solid #ddd;
            text-align: left;
            vertical-align: top;
        }

        .elementsell-invoice-items th {
            font-weight: 700;
            border-bottom: 2px solid #222;
        }

        .elementsell-invoice-items th:last-child,
        .elementsell-invoice-items td:last-child {
            text-align: right;
            width: 160px;
        }

        .elementsell-invoice-totals {
            display: flex;
            justify-content: flex-end;
            margin-top: 18px;
        }

        .elementsell-invoice-totals-inner {
            min-width: 280px;
        }

        .elementsell-invoice-total-row {
            display: flex;
            justify-content: space-between;
            gap: 24px;
            padding: 4px 0;
        }

        .elementsell-invoice-total-row--main {
            margin-top: 8px;
            padding-top: 10px;
            border-top: 2px solid #222;
            font-size: 16px;
            font-weight: 700;
        }

        .elementsell-invoice-status {
            margin-top: 28px;
            font-size: 15px;
            font-weight: 700;
        }

        .elementsell-invoice-status.is-paid {
            color: #1d7a3a;
        }

        .elementsell-invoice-status.is-unpaid {
            color: #b45309;
        }

        .elementsell-invoice-signature {
            margin-top: 36px;
            padding-top: 12px;
        }

        .elementsell-invoice-signature img {
            max-width: 160px;
            max-height: 90px;
            height: auto;
            display: block;
        }
    </style>
    <?php
}
