@extends('layouts.app')

@section('title', 'Prices - Harshana Systems')
@section('page-title', 'Prices')

@section('content')
    <div class="d-flex justify-content-between mb-3">
        <h1 class="h3 mb-0 page-title-theme">Prices</h1>
        @if(auth()->user() && auth()->user()->hasAccessTo('prices.create'))
            <a href="{{ route('prices.create') }}" class="btn btn-primary">Create Price</a>
        @endif
    </div>

    <div class="card">
        <div class="card-body">
            <!-- Search Form -->
            <form method="GET" action="{{ route('prices.index') }}" class="mb-3">
                <div class="row">
                    <div class="col-md-6">
                        <div class="input-group">
                            <input type="text" name="search" class="form-control" placeholder="Search by product name or remark..." value="{{ $search ?? '' }}">
                            <button class="btn btn-outline-secondary" type="submit">
                                <i class="fas fa-search"></i>
                            </button>
                            @if($search)
                                <a href="{{ route('prices.index') }}" class="btn btn-outline-danger">
                                    <i class="fas fa-times"></i>
                                </a>
                            @endif
                        </div>
                    </div>
                </div>
            </form>

            <div class="table-responsive">
                <table class="table table-striped table-hover mb-0">
                    <thead>
                        <tr>
                            <th>#</th>
                            <th>Product</th>
                            <th>Remark</th>
                            <th>Selling</th>
                            <th>Market</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($prices as $price)
                            <tr>
                                <td>{{ $price->id }}</td>
                                <td>{{ $price->product->product_name ?? '-' }}</td>
                                <td>{{ Str::limit($price->remark, 50) }}</td>
                                <td>{{ $price->selling_price }}</td>
                                <td>{{ $price->market_price }}</td>
                                <td>
                                    @if(auth()->user() && auth()->user()->hasAccessTo('prices.view'))
                                        <a href="{{ route('prices.show', $price) }}" class="btn btn-sm btn-info me-1">View</a>
                                    @endif
                                    @if(auth()->user() && auth()->user()->hasAccessTo('prices.edit'))
                                        <a href="{{ route('prices.edit', $price) }}" class="btn btn-sm btn-warning me-1">Edit</a>
                                    @endif
                                    @if(auth()->user() && auth()->user()->hasAccessTo('prices.delete'))
                                        <form method="POST" action="{{ route('prices.destroy', $price) }}" style="display:inline">
                                            @csrf
                                            @method('DELETE')
                                            <button class="btn btn-sm btn-danger" onclick="return confirm('Delete price?')">Delete</button>
                                        </form>
                                    @endif
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="7" class="text-center">No prices found.</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            <div class="mt-3 d-flex justify-content-center">
                {{ $prices->appends(request()->except('page'))->links() }}
            </div>
        </div>
    </div>
@endsection
