{# app/templates/admin/vendor-detail.html #}
{% extends "admin/base.html" %}

{% block title %}Vendor Details{% endblock %}

{% block alpine_data %}adminVendorDetail(){% endblock %}

{% block content %}
<!-- Page Header -->
<div class="flex items-center justify-between my-6">
    <div>
        <h2 class="text-2xl font-semibold text-gray-700 dark:text-gray-200" x-text="vendor?.name || 'Vendor Details'">
            Vendor Details
        </h2>
        <p class="text-sm text-gray-600 dark:text-gray-400 mt-1" x-show="vendor">
            <span x-text="vendorCode"></span>
            <span class="text-gray-400 mx-2">•</span>
            <span x-text="vendor?.subdomain"></span>
        </p>
    </div>
    <a href="/admin/vendors"
       class="flex items-center px-4 py-2 text-sm font-medium leading-5 text-gray-700 transition-colors duration-150 bg-white border border-gray-300 rounded-lg dark:text-gray-400 dark:border-gray-600 dark:bg-gray-800 hover:border-gray-400 focus:outline-none">
        <span x-html="$icon('arrow-left', 'w-4 h-4 mr-2')"></span>
        Back
    </a>
</div>

<!-- Loading State -->
<div x-show="loading" class="text-center py-12">
    <span x-html="$icon('spinner', 'inline w-8 h-8 text-purple-600')"></span>
    <p class="mt-2 text-gray-600 dark:text-gray-400">Loading vendor details...</p>
</div>

<!-- Error State -->
<div x-show="error && !loading" class="mb-6 p-4 bg-red-100 border border-red-400 text-red-700 rounded-lg flex items-start">
    <span x-html="$icon('exclamation', 'w-5 h-5 mr-3 mt-0.5 flex-shrink-0')"></span>
    <div>
        <p class="font-semibold">Error loading vendor</p>
        <p class="text-sm" x-text="error"></p>
    </div>
</div>

<!-- Vendor Details -->
<div x-show="!loading && vendor">
    <!-- Quick Actions Card -->
    <div class="px-4 py-3 mb-6 bg-white rounded-lg shadow-md dark:bg-gray-800">
        <h3 class="mb-4 text-lg font-semibold text-gray-700 dark:text-gray-200">
            Quick Actions
        </h3>
        <div class="flex flex-wrap items-center gap-3">
            <a
                :href="`/admin/vendors/${vendorCode}/edit`"
                class="flex items-center px-4 py-2 text-sm font-medium leading-5 text-white transition-colors duration-150 bg-purple-600 border border-transparent rounded-lg hover:bg-purple-700 focus:outline-none focus:shadow-outline-purple">
                <span x-html="$icon('edit', 'w-4 h-4 mr-2')"></span>
                Edit Vendor
            </a>
            <button
                @click="deleteVendor()"
                class="flex items-center px-4 py-2 text-sm font-medium leading-5 text-white transition-colors duration-150 bg-red-600 border border-transparent rounded-lg hover:bg-red-700 focus:outline-none focus:shadow-outline-red">
                <span x-html="$icon('delete', 'w-4 h-4 mr-2')"></span>
                Delete Vendor
            </button>
        </div>
    </div>

    <!-- Status Cards -->
    <div class="grid gap-6 mb-8 md:grid-cols-4">
        <!-- Verification Status -->
        <div class="flex items-center p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
            <div class="p-3 mr-4 rounded-full" 
                 :class="vendor?.is_verified ? 'text-green-500 bg-green-100 dark:text-green-100 dark:bg-green-500' : 'text-orange-500 bg-orange-100 dark:text-orange-100 dark:bg-orange-500'">
                <span x-html="$icon(vendor?.is_verified ? 'badge-check' : 'clock', 'w-5 h-5')"></span>
            </div>
            <div>
                <p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">
                    Verification
                </p>
                <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="vendor?.is_verified ? 'Verified' : 'Pending'">
                    -
                </p>
            </div>
        </div>

        <!-- Active Status -->
        <div class="flex items-center p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
            <div class="p-3 mr-4 rounded-full"
                 :class="vendor?.is_active ? 'text-green-500 bg-green-100 dark:text-green-100 dark:bg-green-500' : 'text-red-500 bg-red-100 dark:text-red-100 dark:bg-red-500'">
                <span x-html="$icon(vendor?.is_active ? 'check-circle' : 'x-circle', 'w-5 h-5')"></span>
            </div>
            <div>
                <p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">
                    Status
                </p>
                <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="vendor?.is_active ? 'Active' : 'Inactive'">
                    -
                </p>
            </div>
        </div>

        <!-- Created Date -->
        <div class="flex items-center p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
            <div class="p-3 mr-4 text-blue-500 bg-blue-100 rounded-full dark:text-blue-100 dark:bg-blue-500">
                <span x-html="$icon('calendar', 'w-5 h-5')"></span>
            </div>
            <div>
                <p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">
                    Created
                </p>
                <p class="text-sm font-semibold text-gray-700 dark:text-gray-200" x-text="formatDate(vendor?.created_at)">
                    -
                </p>
            </div>
        </div>

        <!-- Updated Date -->
        <div class="flex items-center p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
            <div class="p-3 mr-4 text-purple-500 bg-purple-100 rounded-full dark:text-purple-100 dark:bg-purple-500">
                <span x-html="$icon('refresh', 'w-5 h-5')"></span>
            </div>
            <div>
                <p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">
                    Last Updated
                </p>
                <p class="text-sm font-semibold text-gray-700 dark:text-gray-200" x-text="formatDate(vendor?.updated_at)">
                    -
                </p>
            </div>
        </div>
    </div>

    <!-- Main Info Cards -->
    <div class="grid gap-6 mb-8 md:grid-cols-2">
        <!-- Basic Information -->
        <div class="px-4 py-3 bg-white rounded-lg shadow-md dark:bg-gray-800">
            <h3 class="mb-4 text-lg font-semibold text-gray-700 dark:text-gray-200">
                Basic Information
            </h3>
            <div class="space-y-3">
                <div>
                    <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase">Vendor Code</p>
                    <p class="text-sm text-gray-700 dark:text-gray-300" x-text="vendor?.vendor_code || '-'">-</p>
                </div>
                <div>
                    <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase">Name</p>
                    <p class="text-sm text-gray-700 dark:text-gray-300" x-text="vendor?.name || '-'">-</p>
                </div>
                <div>
                    <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase">Subdomain</p>
                    <p class="text-sm text-gray-700 dark:text-gray-300" x-text="vendor?.subdomain || '-'">-</p>
                </div>
                <div>
                    <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase">Description</p>
                    <p class="text-sm text-gray-700 dark:text-gray-300" x-text="vendor?.description || 'No description provided'">-</p>
                </div>
            </div>
        </div>

        <!-- Contact Information -->
        <div class="px-4 py-3 bg-white rounded-lg shadow-md dark:bg-gray-800">
            <h3 class="mb-4 text-lg font-semibold text-gray-700 dark:text-gray-200">
                Contact Information
            </h3>
            <div class="space-y-3">
                <div>
                    <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase">Owner Email</p>
                    <p class="text-sm text-gray-700 dark:text-gray-300" x-text="vendor?.owner_email || '-'">-</p>
                    <p class="text-xs text-gray-500 dark:text-gray-400">Owner's authentication email</p>
                </div>
                <div>
                    <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase">Contact Email</p>
                    <p class="text-sm text-gray-700 dark:text-gray-300" x-text="vendor?.contact_email || '-'">-</p>
                    <p class="text-xs text-gray-500 dark:text-gray-400">Public business contact</p>
                </div>
                <div>
                    <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase">Phone</p>
                    <p class="text-sm text-gray-700 dark:text-gray-300" x-text="vendor?.contact_phone || '-'">-</p>
                </div>
                <div>
                    <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase">Website</p>
                    <a 
                        x-show="vendor?.website"
                        :href="vendor?.website" 
                        target="_blank"
                        class="text-sm text-purple-600 hover:text-purple-700 dark:text-purple-400" 
                        x-text="vendor?.website">
                    </a>
                    <span x-show="!vendor?.website" class="text-sm text-gray-700 dark:text-gray-300">-</span>
                </div>
            </div>
        </div>
    </div>

    <!-- Business Details -->
    <div class="px-4 py-3 mb-8 bg-white rounded-lg shadow-md dark:bg-gray-800">
        <h3 class="mb-4 text-lg font-semibold text-gray-700 dark:text-gray-200">
            Business Details
        </h3>
        <div class="grid gap-6 md:grid-cols-2">
            <div>
                <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase mb-2">Business Address</p>
                <p class="text-sm text-gray-700 dark:text-gray-300 whitespace-pre-line" x-text="vendor?.business_address || 'No address provided'">-</p>
            </div>
            <div>
                <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase mb-2">Tax Number</p>
                <p class="text-sm text-gray-700 dark:text-gray-300" x-text="vendor?.tax_number || 'Not provided'">-</p>
            </div>
        </div>
    </div>

    <!-- Owner Information -->
    <div class="px-4 py-3 mb-8 bg-white rounded-lg shadow-md dark:bg-gray-800">
        <h3 class="mb-4 text-lg font-semibold text-gray-700 dark:text-gray-200">
            Owner Information
        </h3>
        <div class="grid gap-6 md:grid-cols-3">
            <div>
                <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase mb-2">Owner User ID</p>
                <p class="text-sm text-gray-700 dark:text-gray-300" x-text="vendor?.owner_user_id || '-'">-</p>
            </div>
            <div>
                <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase mb-2">Owner Username</p>
                <p class="text-sm text-gray-700 dark:text-gray-300" x-text="vendor?.owner_username || '-'">-</p>
            </div>
            <div>
                <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase mb-2">Owner Email</p>
                <p class="text-sm text-gray-700 dark:text-gray-300" x-text="vendor?.owner_email || '-'">-</p>
            </div>
        </div>
    </div>

    <!-- Marketplace URLs -->
    <div class="px-4 py-3 mb-8 bg-white rounded-lg shadow-md dark:bg-gray-800" x-show="vendor?.letzshop_csv_url_fr || vendor?.letzshop_csv_url_en || vendor?.letzshop_csv_url_de">
        <h3 class="mb-4 text-lg font-semibold text-gray-700 dark:text-gray-200">
            Marketplace CSV URLs
        </h3>
        <div class="space-y-3">
            <div x-show="vendor?.letzshop_csv_url_fr">
                <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase mb-1">French (FR)</p>
                <a 
                    :href="vendor?.letzshop_csv_url_fr" 
                    target="_blank"
                    class="text-sm text-purple-600 hover:text-purple-700 dark:text-purple-400 break-all" 
                    x-text="vendor?.letzshop_csv_url_fr">
                </a>
            </div>
            <div x-show="vendor?.letzshop_csv_url_en">
                <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase mb-1">English (EN)</p>
                <a 
                    :href="vendor?.letzshop_csv_url_en" 
                    target="_blank"
                    class="text-sm text-purple-600 hover:text-purple-700 dark:text-purple-400 break-all" 
                    x-text="vendor?.letzshop_csv_url_en">
                </a>
            </div>
            <div x-show="vendor?.letzshop_csv_url_de">
                <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase mb-1">German (DE)</p>
                <a 
                    :href="vendor?.letzshop_csv_url_de" 
                    target="_blank"
                    class="text-sm text-purple-600 hover:text-purple-700 dark:text-purple-400 break-all" 
                    x-text="vendor?.letzshop_csv_url_de">
                </a>
            </div>
        </div>
    </div>

    <!-- More Actions -->
    <div class="px-4 py-3 bg-white rounded-lg shadow-md dark:bg-gray-800">
        <h3 class="mb-4 text-lg font-semibold text-gray-700 dark:text-gray-200">
            More Actions
        </h3>
        <div class="flex flex-wrap gap-3">
            <!-- View Parent Company -->
            <a
                :href="'/admin/companies/' + vendor?.company_id + '/edit'"
                class="inline-flex items-center px-4 py-2 text-sm font-medium text-white transition-colors duration-150 bg-blue-600 border border-transparent rounded-lg hover:bg-blue-700 focus:outline-none focus:shadow-outline-blue"
            >
                <span x-html="$icon('office-building', 'w-4 h-4 mr-2')"></span>
                View Parent Company
            </a>

            <!-- Customize Theme -->
            <a
                :href="`/admin/vendors/${vendorCode}/theme`"
                class="inline-flex items-center px-4 py-2 text-sm font-medium text-white transition-colors duration-150 bg-purple-600 border border-transparent rounded-lg hover:bg-purple-700 focus:outline-none focus:shadow-outline-purple"
            >
                <span x-html="$icon('color-swatch', 'w-4 h-4 mr-2')"></span>
                Customize Theme
            </a>
        </div>
        <p class="mt-3 text-xs text-gray-500 dark:text-gray-400">
            <span x-html="$icon('information-circle', 'w-4 h-4 inline mr-1')"></span>
            This vendor belongs to company: <strong x-text="vendor?.company_name"></strong>.
            Contact info and ownership are managed at the company level.
        </p>
    </div>
</div>
{% endblock %}

{% block extra_scripts %}
<script src="{{ url_for('static', path='admin/js/vendor-detail.js') }}"></script>
{% endblock %}