@extends('layouts.admin')

@section('title', 'Tax Settings')

@section('content')
<div class="admin-page-header">
    <h1>Tax Settings</h1>
    @can('taxes.create')
    <a href="{{ route('admin.taxes.create') }}" class="admin-btn-primary">
        <i class="bi bi-plus"></i> Add New Tax
    </a>
    @endcan
</div>

<div class="admin-card">
    <div class="card-body">
        @if($taxes->count() > 0)
            <div class="table-responsive">
                <table class="admin-table table table-hover">
                    <thead>
                        <tr>
                            <th>Name</th>
                            <th>Type</th>
                            <th>Rate (%)</th>
                            <th>Components</th>
                            <th>Status</th>
                            <th>Products Using</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($taxes as $tax)
                            <tr>
                                <td>
                                    <strong>{{ $tax->name }}</strong>
                                </td>
                                <td>
                                    <span class="badge-modern {{ $tax->type === 'single' ? 'badge-modern-primary' : 'badge-modern-info' }}">
                                        {{ ucfirst($tax->type) }}
                                    </span>
                                </td>
                                <td>
                                    <strong>{{ number_format($tax->rate, 2) }}%</strong>
                                </td>
                                <td>
                                    @if($tax->type === 'multiple' && !empty($tax->components))
                                        <div class="d-flex flex-wrap gap-1">
                                            @foreach($tax->components as $component)
                                                <span class="badge-modern badge-modern-primary">
                                                    {{ $component['name'] }}: {{ number_format($component['rate'], 2) }}%
                                                </span>
                                            @endforeach
                                        </div>
                                    @else
                                        <span class="text-muted">-</span>
                                    @endif
                                </td>
                                <td>
                                    @if($tax->is_active)
                                        <span class="badge-modern badge-modern-success">Active</span>
                                    @else
                                        <span class="badge-modern badge-modern-primary">Inactive</span>
                                    @endif
                                </td>
                                <td>
                                    <span class="badge-modern badge-modern-info">{{ $tax->products()->count() }}</span>
                                </td>
                                <td>
                                    <div class="d-flex gap-2">
                                        @can('taxes.update')
                                        <a href="{{ route('admin.taxes.edit', $tax->id) }}" 
                                           class="admin-action-btn admin-action-btn-sm admin-btn-primary"
                                           title="Edit">
                                            <i class="bi bi-pencil"></i>
                                        </a>
                                        @endcan
                                        @can('taxes.delete')
                                        <form action="{{ route('admin.taxes.destroy', $tax->id) }}" 
                                              method="POST" 
                                              class="d-inline" 
                                              onsubmit="return confirm('Are you sure you want to delete this tax? It will be removed from all products using it.')">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit" 
                                                    class="admin-action-btn admin-action-btn-sm"
                                                    style="background: #ef4444; height: -webkit-fill-available; color: white;"
                                                    title="Delete">
                                                <i class="bi bi-trash"></i>
                                            </button>
                                        </form>
                                        @endcan
                                    </div>
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        @else
            <div class="admin-empty-state">
                <i class="bi bi-percent"></i>
                <h3>No Taxes Found</h3>
                @can('taxes.create')
                <p>Get started by creating your first tax.</p>
                <a href="{{ route('admin.taxes.create') }}" class="admin-btn-primary">
                    <i class="bi bi-plus"></i> Add New Tax
                </a>
                @endcan
            </div>
        @endif
    </div>
</div>
@endsection

