@extends('layouts.master')
@section('content')
<div class="container-fluid py-3">
    <div class="d-flex justify-content-end mb-3">
        <a href="{{ route('home') }}" class="btn-back">
            <i class="fas fa-arrow-left me-2"></i> Back to Dashboard
        </a>
    </div>

    <div class="row">
        <div class="col-lg-5">
            <div class="card shadow-sm">
                <div class="card-header bg-primary text-white py-3">
                    <h5 class="mb-0"><i class="fas fa-tags me-2"></i> Add Price Listing</h5>
                </div>
                <div class="card-body">
                    <form action="{{ route('supplier.price_listings.store') }}" method="POST" id="priceListingForm">
                        @csrf
                        <input type="hidden" name="id" id="listingId">
                        
                        <div class="mb-3">
                            <label class="form-label"><strong>Item:</strong></label>
                            <select name="item_id" id="item_id" class="form-select" required>
                                <option value="">-- Select Item --</option>
                                @foreach($items as $item)
                                    <option value="{{ $item->id }}">{{ $item->code }} - {{ $item->description }}</option>
                                @endforeach
                            </select>
                        </div>

                        <div class="mb-3">
                            <label class="form-label"><strong>Price List Name:</strong></label>
                            <select name="price_list_id" id="price_list_id" class="form-select" required>
                                <option value="">-- Select Price List --</option>
                                @foreach($priceLists as $list)
                                    <option value="{{ $list->id }}">{{ $list->name }}</option>
                                @endforeach
                            </select>
                        </div>

                        <div class="mb-3">
                            <label class="form-label"><strong>Unit:</strong></label>
                            <input type="text" class="form-control" name="unit" id="unit" required placeholder="e.g., EA">
                        </div>

                        <div class="mb-3">
                            <label class="form-label"><strong>Currency:</strong></label>
                            <select name="currency_id" id="currency_id" class="form-select" required>
                                <option value="">-- Select Currency --</option>
                                @foreach($currencies as $currency)
                                    <option value="{{ $currency->id }}">{{ $currency->code }}</option>
                                @endforeach
                            </select>
                        </div>

                        <div class="mb-3">
                            <label class="form-label"><strong>Price:</strong></label>
                            <input type="number" step="0.01" class="form-control" name="price" id="price" required>
                        </div>
                        
                        <div class="d-flex justify-content-end mt-4">
                            <input type="submit" class="btn btn-primary me-2" value="SUBMIT">
                            <input type="reset" class="btn btn-outline-secondary" onclick="resetForm()" value="REFRESH">
                        </div>
                    </form>
                </div>
            </div>
        </div>

        <div class="col-lg-7 mt-4 mt-lg-0">
            <div class="card shadow-sm">
                <div class="card-header bg-primary text-white py-3">
                    <h5 class="mb-0"><i class="fas fa-list me-2"></i> Price Listings</h5>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-bordered table-hover">
                            <thead class="table-light">
                                <tr>
                                    <th>Item Code</th>
                                    <th>Description</th>
                                    <th>Price List</th>
                                    <th>Price</th>
                                    <th>Unit</th>
                                    <th>Currency</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($listings as $listing)
                                    <tr>
                                        <td>{{ $listing->item->code ?? '' }}</td>
                                        <td>{{ $listing->item->description ?? '' }}</td>
                                        <td>{{ $listing->priceList->name ?? '' }}</td>
                                        <td>{{ number_format($listing->price, 2) }}</td>
                                        <td>{{ $listing->unit }}</td>
                                        <td>{{ $listing->currency->code ?? '' }}</td>
                                        <td class="text-center">
                                            <a href="#" class="btn btn-sm btn-primary" onclick="editListing(event, {{ $listing->toJson() }})">
                                                <i class="fas fa-edit"></i>
                                            </a>
                                            <a href="#" class="btn btn-sm btn-danger" onclick="confirmDelete('{{ route('supplier.price_listings.delete', $listing->id) }}')">
                                                <i class="fas fa-trash-alt"></i>
                                            </a>
                                        </td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    function editListing(event, listing) {
        event.preventDefault();
        document.querySelector('.card-header h5').innerHTML = '<i class="fas fa-tags me-2"></i> Edit Price Listing';
        document.getElementById('priceListingForm').action = '{{ route("supplier.price_listings.edit") }}';
        document.getElementById('listingId').value = listing.id;
        document.getElementById('item_id').value = listing.item_id;
        document.getElementById('price_list_id').value = listing.price_list_id;
        document.getElementById('unit').value = listing.unit;
        document.getElementById('currency_id').value = listing.currency_id;
        document.getElementById('price').value = listing.price;
    }

    function resetForm() {
        document.querySelector('.card-header h5').innerHTML = '<i class="fas fa-tags me-2"></i> Add Price Listing';
        document.getElementById('priceListingForm').action = '{{ route("supplier.price_listings.store") }}';
        document.getElementById('listingId').value = '';
    }

    function confirmDelete(deleteUrl) {
        if (confirm('Are you sure you want to delete this price listing?')) {
            window.location.href = deleteUrl;
        }
    }
</script>

<style>
.btn-back {
    text-decoration: none;
    color: #1AB9D9;
    font-weight: 500;
    display: inline-flex;
    align-items: center;
    padding: 5px 15px;
    border-radius: 4px;
    transition: all 0.3s;
}
.btn-back:hover {
    background-color: rgba(26, 185, 217, 0.1);
    text-decoration: none;
}
.card {
    border-radius: 8px;
    overflow: hidden;
    border: none;
    box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.1);
}
.card-header {
    border-bottom: 1px solid rgba(0,0,0,0.1);
    padding: 12px 20px;
}
.table th {
    background-color: #f8f9fa;
}
</style>
@endsection