@extends('layouts.app')
@section('title',($party->exists?'Edit':'Add').' Party')
@section('page_title',($party->exists?'Edit':'Add').' Party')
@section('content')
<div class="page-head"><div><h1>{{ $party->exists?'Edit':'Add' }} Party</h1><p>Create one party record and select every applicable business role.</p></div><a class="btn btn-secondary" href="{{ route('parties.index') }}">Back</a></div>
<form method="post" action="{{ $party->exists?route('parties.update',$party):route('parties.store') }}">@csrf @if($party->exists)@method('put')@endif
<div class="card"><div class="card-head"><strong>Identity & Roles</strong></div><div class="card-body"><div class="form-grid">
<div class="field"><label>Legal Name *</label><input class="control" name="legal_name" value="{{ old('legal_name',$party->legal_name) }}" required></div>
<div class="field"><label>Trade Name</label><input class="control" name="trade_name" value="{{ old('trade_name',$party->trade_name) }}"></div>
<div class="field"><label>Entity Type</label><select class="control" name="entity_type"><option value="company" @selected(old('entity_type',$party->entity_type ?? 'company')==='company')>Company</option><option value="individual" @selected(old('entity_type',$party->entity_type)==='individual')>Individual</option></select></div>
<div class="field"><label>Roles *</label><div style="display:flex;flex-wrap:wrap;gap:10px">@php($selected=old('roles',$party->exists?$party->roles->pluck('role')->map(fn($r)=>$r->value ?? $r)->all():[]))@foreach($roleOptions as $role)<label class="badge" style="cursor:pointer"><input type="checkbox" name="roles[]" value="{{ $role->value }}" @checked(in_array($role->value,$selected))> {{ ucwords(str_replace('_',' ',$role->value)) }}</label>@endforeach</div></div>
<div class="field"><label>TRN</label><input class="control" name="trn" value="{{ old('trn',$party->trn) }}"></div><div class="field"><label>Trade Licence Number</label><input class="control" name="trade_license_number" value="{{ old('trade_license_number',$party->trade_license_number) }}"></div>
<div class="field"><label>Licence Expiry</label><input class="control" type="date" name="license_expiry_date" value="{{ old('license_expiry_date',optional($party->license_expiry_date)->format('Y-m-d')) }}"></div><div class="field"><label>ID / Registration Number</label><input class="control" name="identification_number" value="{{ old('identification_number',$party->identification_number) }}"></div>
</div></div></div>
<div class="card" style="margin-top:18px"><div class="card-head"><strong>Contact & Address</strong></div><div class="card-body"><div class="form-grid">
<div class="field"><label>Contact Person</label><input class="control" name="contact_person" value="{{ old('contact_person',$party->contact_person) }}"></div><div class="field"><label>Phone</label><input class="control" name="phone" value="{{ old('phone',$party->phone) }}"></div><div class="field"><label>Email</label><input class="control" type="email" name="email" value="{{ old('email',$party->email) }}"></div><div class="field"><label>Emirate</label><input class="control" name="emirate" value="{{ old('emirate',$party->emirate) }}"></div><div class="field"><label>Country Code</label><input class="control" name="country_code" value="{{ old('country_code',$party->country_code ?? 'AE') }}" maxlength="2"></div><div class="field"><label>Assigned Branch</label><select class="control" name="assigned_branch_id"><option value="">Shared / Unassigned</option>@foreach($branches as $branch)<option value="{{ $branch->id }}" @selected(old('assigned_branch_id',$party->assigned_branch_id)==$branch->id)>{{ $branch->code }} - {{ $branch->name }}</option>@endforeach</select></div><div class="field"><label>Billing Address</label><textarea class="control" name="billing_address">{{ old('billing_address',$party->billing_address) }}</textarea></div><div class="field"><label>Operational Address</label><textarea class="control" name="operational_address">{{ old('operational_address',$party->operational_address) }}</textarea></div>
</div></div></div>
<div class="card" style="margin-top:18px"><div class="card-head"><strong>Accounting Defaults</strong></div><div class="card-body"><div class="form-grid">
<div class="field"><label>Credit Limit</label><input class="control" type="number" step="0.01" name="credit_limit" value="{{ old('credit_limit',$party->credit_limit ?? 0) }}"></div><div class="field"><label>Credit Period (Days)</label><input class="control" type="number" name="credit_period_days" value="{{ old('credit_period_days',$party->credit_period_days ?? 0) }}"></div><div class="field"><label>Currency</label><input class="control" name="default_currency" value="{{ old('default_currency',$party->default_currency ?? 'AED') }}"></div>
<div class="field"><label>Default VAT Code</label><select class="control" name="default_tax_code_id"><option value="">Select</option>@foreach($taxCodes as $tax)<option value="{{ $tax->id }}" @selected(old('default_tax_code_id',$party->default_tax_code_id)==$tax->id)>{{ $tax->code }} - {{ $tax->name }}</option>@endforeach</select></div>
@foreach(['default_receivable_account_id'=>'Receivable Account','default_payable_account_id'=>'Payable Account','default_income_account_id'=>'Income Account','default_expense_account_id'=>'Expense Account'] as $field=>$label)<div class="field"><label>{{ $label }}</label><select class="control" name="{{ $field }}"><option value="">Use system default</option>@foreach($accounts as $account)<option value="{{ $account->id }}" @selected(old($field,$party->{$field})==$account->id)>{{ $account->code }} - {{ $account->name }}</option>@endforeach</select></div>@endforeach
<div class="field"><label>E-Invoice Electronic Address</label><input class="control" name="einvoice_address" value="{{ old('einvoice_address',$party->einvoice_address) }}"></div><div class="field"><label>E-Invoice Scheme</label><input class="control" name="einvoice_scheme" value="{{ old('einvoice_scheme',$party->einvoice_scheme) }}"></div><div class="field full"><label>Notes</label><textarea class="control" name="notes">{{ old('notes',$party->notes) }}</textarea></div><div class="checkbox"><input type="checkbox" name="is_active" value="1" @checked(old('is_active',$party->exists?$party->is_active:true))><label>Active</label></div>
</div></div><div class="card-head" style="justify-content:flex-end"><a class="btn btn-secondary" href="{{ route('parties.index') }}">Cancel</a><button class="btn btn-primary">Save Party</button></div></div>
</form>
@endsection
