<div>
    <x-slot name="title">Create Invoice from Estimate</x-slot>

    <div class="py-12">
        <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
            <div class="bg-white dark:bg-zinc-800 overflow-hidden shadow-sm sm:rounded-lg">
                <div class="p-6 text-zinc-900 dark:text-zinc-100">
                    <div class="flex justify-between items-center mb-6">
                        <div>
                            <h2 class="text-2xl font-semibold">Create Invoice from Estimate #{{ $estimate->estimate_number }}</h2>
                            <p class="text-sm text-zinc-600 dark:text-zinc-400 mt-1">Converting estimate to invoice</p>
                        </div>
                        <a href="{{ route('invoices.index') }}" class="text-zinc-600 hover:text-zinc-900 dark:text-zinc-400 dark:hover:text-zinc-100">
                            ← Back to Invoices
                        </a>
                    </div>

                    @if ($errors->any())
                        <div class="mb-4 p-4 bg-red-100 border border-red-400 text-red-700 rounded">
                            <ul>
                                @foreach ($errors->all() as $error)
                                    <li>{{ $error }}</li>
                                @endforeach
                            </ul>
                        </div>
                    @endif

                    <form wire:submit="save" class="space-y-6">
        <!-- Basic Information -->
                        <!-- Customer Information (Read-only) -->
                        <div class="bg-zinc-50 dark:bg-zinc-700 p-4 rounded-lg">
                            <h3 class="text-lg font-medium mb-4">Customer Information</h3>
                            <div class="space-y-2">
                                <p><strong>Name:</strong> {{ $estimate->customer->first_name }} {{ $estimate->customer->last_name }}</p>
                                <p><strong>Email:</strong> {{ $estimate->customer->email }}</p>
                                <p><strong>Phone:</strong> {{ $estimate->customer->phone }}</p>
                            </div>
                        </div>

                        <!-- Estimate Information (Read-only) -->
                        <div class="bg-zinc-50 dark:bg-zinc-700 p-4 rounded-lg">
                            <h3 class="text-lg font-medium mb-4">Original Estimate</h3>
                            <div class="space-y-2">
                                <p><strong>Estimate #:</strong> {{ $estimate->estimate_number }}</p>
                                <p><strong>Date:</strong> {{ $estimate->estimate_date->format('Y-m-d') }}</p>
                                <p><strong>Total:</strong> ${{ number_format($estimate->total_amount, 2) }}</p>
                                <p><strong>Status:</strong> {{ ucfirst($estimate->status) }}</p>
                            </div>
                        </div>
                    </div>

                    <!-- Invoice Details -->
                    <div class="bg-white dark:bg-zinc-800 shadow rounded-lg p-6">
                        <h3 class="text-lg font-medium text-zinc-900 dark:text-zinc-100 mb-4">Invoice Details</h3>
                        
                        <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                            <div>
                                <label for="subject" class="block text-sm font-medium text-zinc-700 dark:text-zinc-300 mb-2">
                                    Invoice Subject *
                                </label>
                                <input type="text" 
                                       id="subject" 
                                       wire:model="subject" 
                                       placeholder="Invoice subject"
                                       class="w-full px-3 py-2 border border-zinc-300 dark:border-zinc-600 rounded-md shadow-sm focus:outline-none focus:ring-orange-500 focus:border-orange-500 dark:bg-zinc-700 dark:text-zinc-100">
                                @error('subject') 
                                    <span class="text-red-500 text-sm">{{ $message }}</span> 
                                @enderror
                            </div>

                            <div>
                                <label for="branch_id" class="block text-sm font-medium text-zinc-700 dark:text-zinc-300 mb-2">
                                    Branch *
                                </label>
                                <select id="branch_id" 
                                        wire:model="branch_id" 
                                        class="w-full px-3 py-2 border border-zinc-300 dark:border-zinc-600 rounded-md shadow-sm focus:outline-none focus:ring-orange-500 focus:border-orange-500 dark:bg-zinc-700 dark:text-zinc-100">
                                    <option value="">Select branch</option>
                                    @foreach($branches as $branch)
                                        <option value="{{ $branch->id }}">{{ $branch->name }} ({{ $branch->code }})</option>
                                    @endforeach
                                </select>
                                @error('branch_id') 
                                    <span class="text-red-500 text-sm">{{ $message }}</span> 
                                @enderror
                            </div>

                            <div>
                                <label for="invoice_date" class="block text-sm font-medium text-zinc-700 dark:text-zinc-300 mb-2">
                                    Invoice Date *
                                </label>
                                <input type="date" 
                                       id="invoice_date" 
                                       wire:model="invoice_date" 
                                       class="w-full px-3 py-2 border border-zinc-300 dark:border-zinc-600 rounded-md shadow-sm focus:outline-none focus:ring-orange-500 focus:border-orange-500 dark:bg-zinc-700 dark:text-zinc-100">
                                @error('invoice_date') 
                                    <span class="text-red-500 text-sm">{{ $message }}</span> 
                                @enderror
                            </div>

                            <div>
                                <label for="due_date" class="block text-sm font-medium text-zinc-700 dark:text-zinc-300 mb-2">
                                    Due Date *
                                </label>
                                <input type="date" 
                                       id="due_date" 
                                       wire:model="due_date" 
                                       class="w-full px-3 py-2 border border-zinc-300 dark:border-zinc-600 rounded-md shadow-sm focus:outline-none focus:ring-orange-500 focus:border-orange-500 dark:bg-zinc-700 dark:text-zinc-100">
                                @error('due_date') 
                                    <span class="text-red-500 text-sm">{{ $message }}</span> 
                                @enderror
                            </div>

                            <div class="md:col-span-2">
                                <label for="description" class="block text-sm font-medium text-zinc-700 dark:text-zinc-300 mb-2">
                                    Description
                                </label>
                                <textarea id="description" 
                                          wire:model="description" 
                                          rows="3" 
                                          placeholder="Additional notes or description for this invoice..."
                                          class="w-full px-3 py-2 border border-zinc-300 dark:border-zinc-600 rounded-md shadow-sm focus:outline-none focus:ring-orange-500 focus:border-orange-500 dark:bg-zinc-700 dark:text-zinc-100"></textarea>
                                @error('description') 
                                    <span class="text-red-500 text-sm">{{ $message }}</span> 
                                @enderror
                            </div>
                        </div>
                    </div>

                    <!-- Line Items Preview -->
                    <div class="bg-white dark:bg-zinc-800 shadow rounded-lg p-6">
                        <h3 class="text-lg font-medium text-zinc-900 dark:text-zinc-100 mb-4">Line Items to be Converted</h3>

                                                <div class="overflow-x-auto">
                            <table class="min-w-full divide-y divide-zinc-200 dark:divide-zinc-600">
                                <thead class="bg-zinc-50 dark:bg-zinc-700">
                                    <tr>
                                        <th class="px-6 py-3 text-left text-xs font-medium text-zinc-500 dark:text-zinc-300 uppercase tracking-wider">Type</th>
                                        <th class="px-6 py-3 text-left text-xs font-medium text-zinc-500 dark:text-zinc-300 uppercase tracking-wider">Item</th>
                                        <th class="px-6 py-3 text-left text-xs font-medium text-zinc-500 dark:text-zinc-300 uppercase tracking-wider">Description</th>
                                        <th class="px-6 py-3 text-left text-xs font-medium text-zinc-500 dark:text-zinc-300 uppercase tracking-wider">Qty</th>
                                        <th class="px-6 py-3 text-left text-xs font-medium text-zinc-500 dark:text-zinc-300 uppercase tracking-wider">Unit Price</th>
                                        <th class="px-6 py-3 text-left text-xs font-medium text-zinc-500 dark:text-zinc-300 uppercase tracking-wider">Total</th>
                                    </tr>
                                </thead>
                                <tbody class="bg-white dark:bg-zinc-800 divide-y divide-zinc-200 dark:divide-zinc-600">
                                    @foreach($estimate->lineItems as $item)
                                        <tr>
                                            <td class="px-6 py-4 whitespace-nowrap text-sm text-zinc-900 dark:text-zinc-100">
                                                {{ ucfirst($item->type) }}
                                            </td>
                                            <td class="px-6 py-4 whitespace-nowrap text-sm text-zinc-900 dark:text-zinc-100">
                                                @if($item->type === 'parts' && $item->part)
                                                    {{ $item->part->name }} ({{ $item->part->part_number }})
                                                @else
                                                    {{ $item->description }}
                                                @endif
                                            </td>
                                            <td class="px-6 py-4 text-sm text-zinc-900 dark:text-zinc-100">
                                                {{ $item->description }}
                                            </td>
                                            <td class="px-6 py-4 whitespace-nowrap text-sm text-zinc-900 dark:text-zinc-100">
                                                {{ $item->quantity }}
                                            </td>
                                            <td class="px-6 py-4 whitespace-nowrap text-sm text-zinc-900 dark:text-zinc-100">
                                                ${{ number_format($item->unit_price, 2) }}
                                            </td>
                                            <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-zinc-900 dark:text-zinc-100">
                                                ${{ number_format($item->total_amount, 2) }}
                                            </td>
                                        </tr>
                                    @endforeach
                                </tbody>
                                <tfoot class="bg-zinc-50 dark:bg-zinc-700">
                                    <tr>
                                        <td colspan="5" class="px-6 py-4 text-right text-sm font-medium text-zinc-900 dark:text-zinc-100">
                                            Total Amount:
                                        </td>
                                        <td class="px-6 py-4 whitespace-nowrap text-sm font-bold text-zinc-900 dark:text-zinc-100">
                                            ${{ number_format($estimate->total_amount, 2) }}
                                        </td>
                                    </tr>
                                </tfoot>
                            </table>
                        </div>
                    </div>

                    <div class="flex justify-end space-x-4">
                        <a href="{{ route('invoices.index') }}" 
                           class="px-4 py-2 border border-zinc-300 dark:border-zinc-600 rounded-md shadow-sm text-sm font-medium text-zinc-700 dark:text-zinc-300 bg-white dark:bg-zinc-800 hover:bg-zinc-50 dark:hover:bg-zinc-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-orange-500">
                            Cancel
                        </a>
                        <button type="submit" 
                                wire:loading.attr="disabled"
                                class="px-4 py-2 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-orange-600 hover:bg-orange-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-orange-500 disabled:opacity-50">
                            <span wire:loading.remove>Create Invoice</span>
                            <span wire:loading>Creating...</span>
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
                    </div>
                @endforeach
            </div>
        </div>

        <!-- Invoice Summary -->
        <div class="bg-white shadow rounded-lg p-6">
            <h3 class="text-lg font-medium text-gray-900 mb-4">Invoice Summary</h3>
            
            <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                <div>
                    <flux:field>
                        <flux:label>Tax Rate (%)</flux:label>
                        <flux:input type="number" wire:model="tax_rate" step="0.01" min="0" max="100" />
                        <flux:error name="tax_rate" />
                    </flux:field>
                </div>
                
                <div class="space-y-2">
                    <div class="flex justify-between">
                        <span class="text-sm text-gray-600">Subtotal:</span>
                        <span class="text-sm font-medium">${{ number_format($this->subtotal, 2) }}</span>
                    </div>
                    <div class="flex justify-between">
                        <span class="text-sm text-gray-600">Tax ({{ $tax_rate }}%):</span>
                        <span class="text-sm font-medium">${{ number_format($this->taxAmount, 2) }}</span>
                    </div>
                    <div class="flex justify-between border-t pt-2">
                        <span class="text-base font-semibold">Total:</span>
                        <span class="text-base font-semibold">${{ number_format($this->total, 2) }}</span>
                    </div>
                </div>
            </div>
        </div>

        <!-- Action Buttons -->
        <div class="flex justify-end space-x-3">
            <flux:button type="button" variant="outline" href="{{ route('estimates.show', $estimate) }}">
                Cancel
            </flux:button>
            <flux:button type="submit" variant="primary">
                Create Invoice
            </flux:button>
        </div>
    </form>
</div>
