@extends('layouts.app')
@section('title', 'Tax Codes')

@section('breadcrumb')
    <li class="breadcrumb-item"><a href="{{ route('dashboard') }}">Dashboard</a></li>
    <li class="breadcrumb-item active">Tax Codes</li>
@endsection

@section('content')
<div class="page-header">
    <div>
        <h1 class="page-title">Tax Codes</h1>
        <p class="page-subtitle">Configure sales and purchase tax rates</p>
    </div>
</div>

<div class="row g-4">
    {{-- Tax Codes Table --}}
    <div class="col-12 col-xl-8">
        <div class="card">
            <div class="card-header fw-semibold">
                <i class="bi bi-receipt-cutoff me-2 text-primary"></i>Tax Codes
            </div>
            <div class="card-body p-0">
                <div class="table-responsive">
                    <table class="table table-hover mb-0" style="min-width:400px">
                        <thead class="table-light">
                            <tr>
                                <th>Code / Name</th>
                                <th class="text-end" style="width:80px">Rate</th>
                                <th class="d-none d-sm-table-cell">Type</th>
                                <th class="d-none d-md-table-cell">Applies To</th>
                                <th>Status</th>
                                <th class="text-end pe-3" style="width:80px">Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse($taxCodes as $tax)
                            <tr>
                                <td>
                                    <div class="fw-semibold" style="font-size:13.5px">{{ $tax->name }}</div>
                                    <code style="font-size:11.5px">{{ $tax->code }}</code>
                                </td>
                                <td class="text-end">
                                    <span class="fw-semibold" style="font-size:14px">{{ number_format($tax->rate, 2) }}%</span>
                                </td>
                                <td class="d-none d-sm-table-cell">
                                    <span class="badge {{ $tax->type==='inclusive' ? '' : 'bg-light text-dark border' }}"
                                          style="{{ $tax->type==='inclusive' ? 'background:#cfe2ff;color:#084298' : '' }};font-size:11px">
                                        {{ ucfirst($tax->type) }}
                                    </span>
                                </td>
                                <td class="d-none d-md-table-cell">
                                    @php $on=['sales'=>['#d1e7dd','#0a3622'],'purchase'=>['#fff3cd','#664d03'],'both'=>['#EBF3FB','#1B4F8A']]; [$bg,$fg]=$on[$tax->applicable_on]; @endphp
                                    <span class="badge" style="background:{{ $bg }};color:{{ $fg }};font-size:11px">
                                        {{ ucfirst($tax->applicable_on) }}
                                    </span>
                                </td>
                                <td>
                                    <span class="badge" style="background:{{ $tax->is_active ? '#d1e7dd':'#f8d7da' }};color:{{ $tax->is_active ? '#0a3622':'#842029' }};font-size:11px">
                                        {{ $tax->is_active ? 'Active' : 'Inactive' }}
                                    </span>
                                </td>
                                <td class="text-end pe-2">
                                    @if(auth()->user()->hasPermission('settings','edit'))
                                    <div class="d-flex justify-content-end gap-1">
                                        <button class="btn btn-outline-primary btn-sm btn-edit-tax"
                                            data-url="{{ route('masters.tax-codes.update', $tax) }}"
                                            data-name="{{ $tax->name }}" data-rate="{{ $tax->rate }}"
                                            data-type="{{ $tax->type }}" data-on="{{ $tax->applicable_on }}"
                                            data-active="{{ $tax->is_active ? '1':'0' }}"
                                            title="Edit">
                                            <i class="bi bi-pencil"></i>
                                        </button>
                                        <form method="POST" action="{{ route('masters.tax-codes.destroy', $tax) }}" id="dt-{{ $tax->id }}">
                                            @csrf @method('DELETE')
                                            <button type="button" class="btn btn-outline-danger btn-sm"
                                                data-confirm data-form="#dt-{{ $tax->id }}"
                                                data-confirm-title="Delete Tax Code?"
                                                data-confirm-text="Delete '{{ $tax->code }}'? This cannot be undone."
                                                data-confirm-icon="warning" data-confirm-color="#E74C3C" data-confirm-btn="Delete"
                                                title="Delete">
                                                <i class="bi bi-trash"></i>
                                            </button>
                                        </form>
                                    </div>
                                    @endif
                                </td>
                            </tr>
                            @empty
                            <tr>
                                <td colspan="6" class="text-center py-5 text-muted">
                                    <i class="bi bi-receipt display-5 d-block mb-2"></i>
                                    No tax codes yet. Add your first tax code using the form.
                                </td>
                            </tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

    {{-- Add Tax Code Form --}}
    <div class="col-12 col-xl-4">
        @if(auth()->user()->hasPermission('settings','edit'))
        <div class="card mb-4">
            <div class="card-header fw-semibold">
                <i class="bi bi-plus-circle me-2 text-primary"></i>Add Tax Code
            </div>
            <div class="card-body">
                <form method="POST" action="{{ route('masters.tax-codes.store') }}">
                @csrf
                <div class="row g-3">
                    <div class="col-5">
                        <label class="form-label fw-semibold">Code <span class="text-danger">*</span></label>
                        <input type="text" name="code"
                               class="form-control @error('code') is-invalid @enderror"
                               value="{{ old('code') }}" placeholder="VAT15" required>
                        @error('code')<div class="invalid-feedback">{{ $message }}</div>@enderror
                    </div>
                    <div class="col-7">
                        <label class="form-label fw-semibold">Rate % <span class="text-danger">*</span></label>
                        <div class="input-group">
                            <input type="number" name="rate"
                                   class="form-control @error('rate') is-invalid @enderror"
                                   value="{{ old('rate', 0) }}" step="0.01" min="0" max="100" required>
                            <span class="input-group-text text-muted">%</span>
                        </div>
                        @error('rate')<div class="invalid-feedback">{{ $message }}</div>@enderror
                    </div>
                    <div class="col-12">
                        <label class="form-label fw-semibold">Name <span class="text-danger">*</span></label>
                        <input type="text" name="name"
                               class="form-control @error('name') is-invalid @enderror"
                               value="{{ old('name') }}" placeholder="e.g. VAT 15%" required>
                        @error('name')<div class="invalid-feedback">{{ $message }}</div>@enderror
                    </div>
                    <div class="col-6">
                        <label class="form-label fw-semibold">Tax Type</label>
                        <select name="type" class="form-select">
                            <option value="exclusive" {{ old('type')==='exclusive' ? 'selected':'' }}>Exclusive</option>
                            <option value="inclusive" {{ old('type')==='inclusive' ? 'selected':'' }}>Inclusive</option>
                        </select>
                    </div>
                    <div class="col-6">
                        <label class="form-label fw-semibold">Applies To</label>
                        <select name="applicable_on" class="form-select">
                            <option value="both"     {{ old('applicable_on','both')==='both'     ? 'selected':'' }}>Both</option>
                            <option value="sales"    {{ old('applicable_on')==='sales'    ? 'selected':'' }}>Sales</option>
                            <option value="purchase" {{ old('applicable_on')==='purchase' ? 'selected':'' }}>Purchase</option>
                        </select>
                    </div>
                    <div class="col-12">
                        <button type="submit" class="btn btn-primary w-100">
                            <i class="bi bi-plus-circle me-1"></i>Add Tax Code
                        </button>
                    </div>
                </div>
                </form>
            </div>
        </div>
        @endif

        <div class="card">
            <div class="card-header fw-semibold">
                <i class="bi bi-info-circle me-2 text-primary"></i>Tax Type Guide
            </div>
            <div class="card-body" style="font-size:13px">
                <div class="mb-3">
                    <strong>Exclusive (most common)</strong>
                    <div class="text-muted">Tax is added on top of the price.<br>
                        Price: $100 + 15% = <strong>$115 total</strong>
                    </div>
                </div>
                <div>
                    <strong>Inclusive</strong>
                    <div class="text-muted">Tax is included in the price.<br>
                        Price: $115 includes 15% = <strong>$15 tax</strong>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

{{-- Edit Tax Modal --}}
<div class="modal fade" id="editTaxModal" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header border-0 pb-0">
                <h5 class="modal-title fw-semibold"><i class="bi bi-pencil me-2 text-primary"></i>Edit Tax Code</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body pt-2">
                <form method="POST" id="editTaxForm">
                @csrf @method('PUT')
                <div class="row g-3">
                    <div class="col-8">
                        <label class="form-label fw-semibold">Name <span class="text-danger">*</span></label>
                        <input type="text" name="name" id="etName" class="form-control" required>
                    </div>
                    <div class="col-4">
                        <label class="form-label fw-semibold">Rate %</label>
                        <div class="input-group">
                            <input type="number" name="rate" id="etRate" class="form-control" step="0.01" min="0" max="100">
                            <span class="input-group-text">%</span>
                        </div>
                    </div>
                    <div class="col-6">
                        <label class="form-label fw-semibold">Type</label>
                        <select name="type" id="etType" class="form-select">
                            <option value="exclusive">Exclusive</option>
                            <option value="inclusive">Inclusive</option>
                        </select>
                    </div>
                    <div class="col-6">
                        <label class="form-label fw-semibold">Applies To</label>
                        <select name="applicable_on" id="etOn" class="form-select">
                            <option value="both">Both</option>
                            <option value="sales">Sales</option>
                            <option value="purchase">Purchase</option>
                        </select>
                    </div>
                    <div class="col-12">
                        <div class="form-check form-switch">
                            <input class="form-check-input" type="checkbox" name="is_active" value="1" id="etActive">
                            <label class="form-check-label" for="etActive">Active</label>
                        </div>
                    </div>
                </div>
                </form>
            </div>
            <div class="modal-footer border-0 pt-0">
                <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Cancel</button>
                <button type="button" class="btn btn-primary px-4" onclick="document.getElementById('editTaxForm').submit()">
                    <i class="bi bi-check-circle me-1"></i>Save Changes
                </button>
            </div>
        </div>
    </div>
</div>
@endsection

@push('scripts')
<script>
document.querySelectorAll('.btn-edit-tax').forEach(btn => {
    btn.addEventListener('click', function() {
        document.getElementById('editTaxForm').action = this.dataset.url;
        document.getElementById('etName').value  = this.dataset.name;
        document.getElementById('etRate').value  = this.dataset.rate;
        document.getElementById('etType').value  = this.dataset.type;
        document.getElementById('etOn').value    = this.dataset.on;
        document.getElementById('etActive').checked = this.dataset.active === '1';
        new bootstrap.Modal(document.getElementById('editTaxModal')).show();
    });
});
</script>
@endpush
