@extends('layouts.app')
@section('title', isset($customer) ? 'Edit Customer' : 'Add Customer')

@section('breadcrumb')
    <li class="breadcrumb-item"><a href="{{ route('dashboard') }}">Dashboard</a></li>
    <li class="breadcrumb-item"><a href="{{ route('masters.customers.index') }}">Customers</a></li>
    <li class="breadcrumb-item active">{{ isset($customer) ? 'Edit' : 'Add Customer' }}</li>
@endsection

@section('content')
<div class="page-header">
    <div>
        <h1 class="page-title">{{ isset($customer) ? 'Edit Customer' : 'Add Customer' }}</h1>
        @if(isset($customer))
        <p class="page-subtitle">{{ $customer->name }}</p>
        @endif
    </div>
    <a href="{{ route('masters.customers.index') }}" class="btn btn-outline-secondary">
        <i class="bi bi-arrow-left me-1"></i>Back
    </a>
</div>

<form method="POST"
      action="{{ isset($customer) ? route('masters.customers.update',$customer) : route('masters.customers.store') }}"
      id="custForm">
@csrf
@if(isset($customer)) @method('PUT') @endif

<div class="row g-4">
    {{-- Left: Basic Info --}}
    <div class="col-12 col-xl-8">

        {{-- Basic Info --}}
        <div class="card mb-4">
            <div class="card-header fw-semibold">
                <i class="bi bi-person me-2 text-primary"></i>Customer Information
            </div>
            <div class="card-body">
                <div class="row g-3">
                    <div class="col-12">
                        <label class="form-label fw-semibold">Customer Name <span class="text-danger">*</span></label>
                        <input type="text" name="name" required autofocus
                               class="form-control form-control-lg @error('name') is-invalid @enderror"
                               value="{{ old('name', $customer->name ?? '') }}" placeholder="Full company or person name">
                        @error('name')<div class="invalid-feedback">{{ $message }}</div>@enderror
                    </div>
                    <div class="col-12">
                        <label class="form-label fw-semibold">Job of</label>
                        @if(!($canSelectJobOf ?? true))
                            <input type="hidden" name="parent_id" value="">
                            <select class="form-select" disabled>
                                <option selected>— Not available (this customer has jobs) —</option>
                            </select>
                            <div class="form-text">Top-level customers with jobs cannot be assigned to another customer.</div>
                        @else
                            <select name="parent_id" class="form-select select2 @error('parent_id') is-invalid @enderror">
                                <option value="">— None (top-level customer) —</option>
                                @foreach($jobOfCustomers as $parentCustomer)
                                    <option value="{{ $parentCustomer->id }}" {{ old('parent_id', $customer->parent_id ?? '') == $parentCustomer->id ? 'selected' : '' }}>
                                        {{ $parentCustomer->code ? "[{$parentCustomer->code}] " : '' }}{{ $parentCustomer->name }}
                                    </option>
                                @endforeach
                            </select>
                            <div class="form-text">Select a customer to create this as a job (sub-customer), like QuickBooks Desktop.</div>
                            @error('parent_id')<div class="invalid-feedback d-block">{{ $message }}</div>@enderror
                        @endif
                    </div>
                    <div class="col-12 col-sm-6">
                        <label class="form-label fw-semibold">Customer Code</label>
                        <input type="text" name="code"
                               class="form-control @error('code') is-invalid @enderror"
                               value="{{ old('code', $customer->code ?? '') }}" placeholder="e.g. CUST001">
                        <div class="form-text">Optional unique identifier</div>
                        @error('code')<div class="invalid-feedback">{{ $message }}</div>@enderror
                    </div>
                    <div class="col-12 col-sm-6">
                        <label class="form-label fw-semibold">Contact Person</label>
                        <input type="text" name="contact_person" class="form-control"
                               value="{{ old('contact_person', $customer->contact_person ?? '') }}"
                               placeholder="Main contact name">
                    </div>
                    <div class="col-12 col-sm-6">
                        <label class="form-label fw-semibold">Email</label>
                        <div class="input-group">
                            <span class="input-group-text"><i class="bi bi-envelope text-muted"></i></span>
                            <input type="email" name="email" class="form-control"
                                   value="{{ old('email', $customer->email ?? '') }}" placeholder="customer@email.com">
                        </div>
                    </div>
                    <div class="col-12 col-sm-6">
                        <label class="form-label fw-semibold">Phone</label>
                        <div class="input-group">
                            <span class="input-group-text"><i class="bi bi-telephone text-muted"></i></span>
                            <input type="text" name="phone" class="form-control"
                                   value="{{ old('phone', $customer->phone ?? '') }}" placeholder="+1 555 0100">
                        </div>
                    </div>
                    <div class="col-12 col-sm-6">
                        <label class="form-label fw-semibold">Tax / VAT ID</label>
                        <input type="text" name="tax_id" class="form-control"
                               value="{{ old('tax_id', $customer->tax_id ?? '') }}" placeholder="Tax registration number">
                    </div>
                    <div class="col-12 col-sm-6">
                        <label class="form-label fw-semibold">Currency</label>
                        <select name="currency_id" class="form-select select2">
                            <option value="">— Use Company Default —</option>
                            @foreach($currencies as $cur)
                                <option value="{{ $cur->id }}" {{ old('currency_id', $customer->currency_id ?? '') == $cur->id ? 'selected':'' }}>
                                    {{ $cur->code }} — {{ $cur->name }}
                                </option>
                            @endforeach
                        </select>
                    </div>
                </div>
            </div>
        </div>

        {{-- Addresses --}}
        <div class="card mb-4">
            <div class="card-header fw-semibold">
                <i class="bi bi-geo-alt me-2 text-primary"></i>Addresses
            </div>
            <div class="card-body">
                <div class="row g-3">
                    <div class="col-12 col-md-6">
                        <label class="form-label fw-semibold">Billing Address</label>
                        <textarea name="billing_address" class="form-control" rows="3"
                                  placeholder="Street, City, State, Zip, Country">{{ old('billing_address', $customer->billing_address ?? '') }}</textarea>
                    </div>
                    <div class="col-12 col-md-6">
                        <label class="form-label fw-semibold">Shipping Address</label>
                        <textarea name="shipping_address" class="form-control" rows="3"
                                  placeholder="Leave blank if same as billing">{{ old('shipping_address', $customer->shipping_address ?? '') }}</textarea>
                    </div>
                    <div class="col-12">
                        <label class="form-label fw-semibold">Notes</label>
                        <textarea name="notes" class="form-control" rows="2"
                                  placeholder="Internal notes about this customer">{{ old('notes', $customer->notes ?? '') }}</textarea>
                    </div>
                </div>
            </div>
        </div>
    </div>

    {{-- Right: Financial Settings --}}
    <div class="col-12 col-xl-4">
        <div class="card mb-4">
            <div class="card-header fw-semibold">
                <i class="bi bi-cash-stack me-2 text-primary"></i>Financial Settings
            </div>
            <div class="card-body">
                <div class="row g-3">
                    <div class="col-12">
                        <label class="form-label fw-semibold">Price Level</label>
                        <select name="price_level_id" class="form-select">
                            <option value="">— Default Pricing —</option>
                            @foreach($priceLevels as $pl)
                                <option value="{{ $pl->id }}" {{ old('price_level_id', $customer->price_level_id ?? '') == $pl->id ? 'selected':'' }}>
                                    {{ $pl->name }}
                                </option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-12">
                        <label class="form-label fw-semibold">Credit Limit</label>
                        <div class="input-group">
                            <span class="input-group-text text-muted">Limit</span>
                            <input type="number" name="credit_limit" class="form-control text-end"
                                   value="{{ old('credit_limit', $customer->credit_limit ?? 0) }}"
                                   min="0" step="0.01" placeholder="0.00">
                        </div>
                        <div class="form-text">Enter 0 for no credit limit</div>
                    </div>
                    <div class="col-12">
                        <label class="form-label fw-semibold">Payment Terms</label>
                        <div class="input-group">
                            <input type="number" name="payment_terms_days" class="form-control"
                                   value="{{ old('payment_terms_days', $customer->payment_terms_days ?? 30) }}"
                                   min="0" max="365">
                            <span class="input-group-text text-muted">days</span>
                        </div>
                    </div>
                    <div class="col-12">
                        <label class="form-label fw-semibold">AR Account</label>
                        <select name="account_receivable_id" class="form-select select2">
                            <option value="">— Use Default AR Account —</option>
                            @foreach($arAccounts as $acc)
                                <option value="{{ $acc->id }}" {{ old('account_receivable_id', $customer->account_receivable_id ?? '') == $acc->id ? 'selected':'' }}>
                                    {{ $acc->code ? "[{$acc->code}] " : "" }}{{ $acc->name }}
                                </option>
                            @endforeach
                        </select>
                    </div>
                    @if(isset($customer))
                    <div class="col-12">
                        <div class="p-3 rounded-2 border" style="background:#f8f9fa">
                            <div class="form-check form-switch mb-0">
                                <input class="form-check-input" type="checkbox" name="is_active" value="1"
                                       id="isActive" {{ old('is_active', $customer->is_active) ? 'checked':'' }}>
                                <label class="form-check-label fw-semibold" for="isActive">Active Customer</label>
                                <div class="text-muted" style="font-size:12px">Inactive customers cannot be selected in new transactions</div>
                            </div>
                        </div>
                    </div>
                    @endif
                </div>
            </div>
        </div>

        <div class="d-flex gap-2">
            <a href="{{ route('masters.customers.index') }}" class="btn btn-outline-secondary flex-fill">Cancel</a>
            <button type="submit" class="btn btn-primary flex-fill" id="saveBtn">
                <i class="bi bi-check-circle me-1"></i>{{ isset($customer) ? 'Save' : 'Create' }}
            </button>
        </div>
    </div>
</div>
</form>
@endsection

@push('scripts')
<script>
document.getElementById('custForm').addEventListener('submit', function() {
    const b = document.getElementById('saveBtn');
    b.innerHTML = '<span class="spinner-border spinner-border-sm me-2"></span>Saving...';
    b.disabled = true;
});
</script>
@endpush
