@extends('layouts.app')
@section('content')
<main class="main-content"><div class="container-fluid"><div class="d-flex justify-content-between align-items-center mb-3"><h4>{{ $definition['title'] }}</h4></div>
@if(session('success'))<div class="alert alert-success">{{ session('success') }}</div>@endif
@if($type==='sales-orders')<div class="card mb-3"><div class="card-body"><form method="POST" action="{{ route('operational-documents.sales-orders.from-quotation') }}" class="row g-2 align-items-end">@csrf<div class="col-md-5"><label class="form-label">Customer-accepted quotation ID</label><input name="trade_document_id" type="number" class="form-control" required></div><div class="col-auto"><button class="btn btn-outline-primary">Create Sales Order from Quotation</button></div></form></div></div>@endif
@if($type==='deliveries')<div class="card mb-4"><div class="card-body"><form method="GET" class="row g-2 align-items-end"><div class="col-md-2"><label class="form-label">Manager</label><select name="delivery_manager_id" class="form-select"><option value="">All</option>@foreach($deliveryManagers as $manager)<option value="{{ $manager->id }}" @selected(($deliveryFilters['delivery_manager_id']??null)==$manager->id)>{{ $manager->name }}</option>@endforeach</select></div><div class="col-md-2"><label class="form-label">Team</label><input name="delivery_team" class="form-control" value="{{ $deliveryFilters['delivery_team']??'' }}"></div><div class="col-md-2"><label class="form-label">Release</label><input name="version_release" class="form-control" value="{{ $deliveryFilters['version_release']??'' }}"></div><div class="col-md-2"><label class="form-label">Environment</label><input name="environment" class="form-control" value="{{ $deliveryFilters['environment']??'' }}"></div><div class="col-md-2"><label class="form-label">Status</label><select name="delivery_status" class="form-select"><option value="">All</option>@foreach($definition['status'] as $status)<option value="{{ $status }}" @selected(($deliveryFilters['delivery_status']??null)===$status)>{{ ucwords(str_replace('_',' ',$status)) }}</option>@endforeach</select></div><div class="col-md-2"><label class="form-label">Completion date</label><input type="date" name="completion_date" class="form-control" value="{{ $deliveryFilters['completion_date']??'' }}"></div><div class="col-12"><button class="btn btn-outline-primary">Apply Filters</button><a class="btn btn-outline-secondary ms-1" href="{{ route('operational-documents.index',$type) }}">Reset Filters</a></div></form></div></div>@endif
<div class="card mb-3"><div class="card-body"><form method="POST" action="{{ route('operational-documents.store',$type) }}" class="row g-3">@csrf
<div class="col-md-4"><label class="form-label">{{ $definition['reference'] }} *</label><input name="reference_number" class="form-control" required value="{{ old('reference_number') }}"></div>
@foreach($definition['fields'] as $name=>$kind)@php $isRequired=$type!=='deliveries'; @endphp<div class="col-md-4"><label class="form-label">{{ ucwords(str_replace('_',' ',$name)) }} @if($isRequired)*@endif</label>
@if($kind==='textarea')<textarea name="{{ $name }}" class="form-control" @if($isRequired) required @endif>{{ old($name) }}</textarea>
@elseif(str_starts_with($kind,'select:'))<select name="{{ $name }}" class="form-select" @if($isRequired) required @endif><option value="">Select</option>@foreach(explode(',',substr($kind,7)) as $option)<option value="{{ $option }}">{{ ucwords(str_replace('_',' ',$option)) }}</option>@endforeach</select>
@else<input name="{{ $name }}" type="{{ $kind==='date'?'date':($kind==='number'?'number':'text') }}" step="{{ $kind==='number'?'.01':'any' }}" class="form-control" @if($isRequired) required @endif value="{{ old($name) }}">@endif</div>@endforeach
<div class="col-md-4"><label class="form-label">Status *</label><select name="status" class="form-select" required><option value="">Select</option>@foreach($definition['status'] as $status)<option value="{{ $status }}">{{ ucwords(str_replace('_',' ',$status)) }}</option>@endforeach</select></div>
<div class="col-12"><button class="btn btn-primary">Create {{ rtrim($definition['title'],'s') }}</button></div></form></div></div>
<div class="card"><div class="card-body table-responsive"><table class="table"><thead><tr><th>Reference</th><th>Status</th><th>Created</th><th>Workflow</th></tr></thead><tbody>@forelse($records as $record)<tr><td>{{ $record->reference_number }}</td><td>{{ ucwords(str_replace('_',' ',$record->status)) }}</td><td>{{ $record->created_at }}</td><td><form method="POST" action="{{ route('operational-documents.transition',$record) }}" class="d-flex gap-1">@csrf<select name="status" class="form-select form-select-sm"><option value="">Next status</option>@foreach($definition['status'] as $status)@if($status!==$record->status)<option value="{{ $status }}">{{ ucwords(str_replace('_',' ',$status)) }}</option>@endif @endforeach</select><input name="comment" class="form-control form-control-sm" placeholder="Comment"><button class="btn btn-sm btn-primary">Update</button></form>@if($record->type==='contracts' && $record->status==='sent_to_customer')<form method="POST" action="{{ route('operational-documents.contracts.sign',$record) }}" class="mt-1 d-flex gap-1">@csrf<input name="customer_signatory" class="form-control form-control-sm" placeholder="Customer signatory" required><input name="company_signatory" class="form-control form-control-sm" placeholder="Company signatory" required><input name="signed_date" type="date" class="form-control form-control-sm" required><button class="btn btn-sm btn-success">Record signature</button></form>@endif @if(in_array($record->type,['sales-orders','contracts','handovers','deliveries']))<form method="POST" action="{{ route('operational-documents.next',$record) }}" class="mt-1">@csrf<button class="btn btn-sm btn-outline-success">Create Next Workflow Record</button></form>@endif
@if($record->type==='deliveries' && in_array($record->status,['customer_approval','delivered']))<details class="mt-2"><summary class="small text-danger">Record customer rejection</summary><form method="POST" action="{{ route('operational-documents.deliveries.reject',$record) }}" class="row g-2 mt-1">@csrf<input class="form-control" name="reason" placeholder="Required rejection reason" required><textarea class="form-control" name="affected_deliverables" placeholder="Required affected deliverables" required></textarea><button class="btn btn-sm btn-danger">Reject & create corrective action</button></form></details>@endif
@if($record->type==='deliveries')<x-delivery-corrective-actions :delivery="$record" />@endif
@if($record->type==='sales-orders')<x-sales-order-payment-summary :sales-order="$record" />@endif
<x-conversion-override-audits :document="$record" />
<x-commercial-workflow-trace :document="$record" />
@if(in_array($record->status,['draft','returned']) || ($record->type==='deliveries' && $record->status!=='closed'))<details class="mt-2"><summary class="small text-primary">Edit delivery and completion details</summary><form method="POST" action="{{ route('operational-documents.details.update',$record) }}" class="row g-2 mt-1">@csrf @method('PUT')
@foreach($definition['fields'] as $name=>$kind)<div class="col-md-6"><label class="form-label small">{{ ucwords(str_replace('_',' ',$name)) }}</label>
@if($kind==='textarea')<textarea name="{{ $name }}" class="form-control form-control-sm">{{ $record->details[$name] ?? '' }}</textarea>
@elseif(str_starts_with($kind,'select:'))<select name="{{ $name }}" class="form-select form-select-sm"><option value="">Select</option>@foreach(explode(',',substr($kind,7)) as $option)<option value="{{ $option }}" @selected(($record->details[$name] ?? '')===$option)>{{ ucwords(str_replace('_',' ',$option)) }}</option>@endforeach</select>
@else<input name="{{ $name }}" type="{{ $kind==='date'?'date':($kind==='number'?'number':'text') }}" step="{{ $kind==='number'?'.01':'any' }}" class="form-control form-control-sm" value="{{ $record->details[$name] ?? '' }}">@endif</div>@endforeach
<div class="col-12"><button class="btn btn-sm btn-outline-primary">Save draft details</button></div></form></details>@endif</td></tr>@empty<tr><td colspan="4" class="text-muted">No records.</td></tr>@endforelse</tbody></table>{{ $records->links() }}</div></div>
</div></main>
@endsection
