@extends('layouts.app')
@section('title','Price Books')
@section('content')
<main class="nxl-container"><div class="nxl-content"><div class="page-header"><h5>Price Books</h5></div><div class="main-content">
@if(session('success'))<div class="alert alert-success">{{ session('success') }}</div>@endif
@if($errors->any())<div class="alert alert-danger">{{ $errors->first() }}</div>@endif
<div class="card mb-4"><div class="card-header">Create Price Book</div><div class="card-body"><form class="row g-2" method="POST" action="{{ route('price-books.store') }}">@csrf
<div class="col-md-6"><input class="form-control" name="name" placeholder="Price Book name" required></div><div class="col-md-3"><input class="form-control" name="currency" value="USD" maxlength="8" required></div>
<div class="col-md-2"><label><input type="checkbox" name="active" value="1" checked> Active</label></div><div class="col-auto"><button class="btn btn-primary">Create</button></div></form></div></div>
@foreach($books as $book)<div class="card mb-4"><div class="card-header"><form method="POST" action="{{ route('price-books.update',$book) }}" class="row g-2 align-items-center">@csrf @method('PUT')
<div class="col"><input class="form-control" name="name" value="{{ $book->name }}"></div><div class="col-2"><input class="form-control" name="currency" value="{{ $book->currency }}"></div>
<div class="col-2"><label><input type="checkbox" name="active" value="1" @checked($book->active)> Active</label></div><div class="col-auto"><button class="btn btn-light">Save</button></div></form></div>
<div class="card-body"><form method="POST" action="{{ route('price-books.items.store',$book) }}" class="row g-2 mb-3">@csrf
<div class="col"><select class="form-select" name="product_id">@foreach($products as $product)<option value="{{ $product->id }}">{{ $product->product_name }}</option>@endforeach</select></div>
<div class="col-3"><input class="form-control" type="number" step=".01" name="unit_price" placeholder="Unit price" required></div><div class="col-3"><input class="form-control" type="number" step=".01" name="max_discount_percent" placeholder="Maximum discount %" required></div><div class="col-auto"><button class="btn btn-primary">Add / Update</button></div></form>
<table class="table"><thead><tr><th>Product</th><th>Price</th><th>Maximum discount</th><th></th></tr></thead><tbody>@forelse($book->items as $item)<tr><td>{{ $item->product?->product_name }}</td><td>{{ $book->currency }} {{ number_format($item->unit_price,2) }}</td><td>{{ $item->max_discount_percent }}%</td><td><form method="POST" action="{{ route('price-books.items.destroy',[$book,$item]) }}">@csrf @method('DELETE')<button class="btn btn-sm btn-outline-danger">Remove</button></form></td></tr>@empty<tr><td colspan="4" class="text-muted">No products.</td></tr>@endforelse</tbody></table>
<form method="POST" action="{{ route('price-books.destroy',$book) }}">@csrf @method('DELETE')<button class="btn btn-sm btn-outline-danger">Archive Price Book</button></form></div></div>@endforeach
</div></div></main>
@endsection
