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

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

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

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

<div class="row g-4">
    <div class="col-12 col-xl-8">
        <div class="card mb-4">
            <div class="card-header fw-semibold">
                <i class="bi bi-building me-2 text-primary"></i>Vendor Information
            </div>
            <div class="card-body">
                <div class="row g-3">
                    <div class="col-12">
                        <label class="form-label fw-semibold">Vendor 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', $vendor->name ?? '') }}" placeholder="Supplier or company name">
                        @error('name')<div class="invalid-feedback">{{ $message }}</div>@enderror
                    </div>
                    <div class="col-12 col-sm-6">
                        <label class="form-label fw-semibold">Vendor Code</label>
                        <input type="text" name="code" class="form-control @error('code') is-invalid @enderror"
                               value="{{ old('code', $vendor->code ?? '') }}" placeholder="e.g. VEND001">
                        @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', $vendor->contact_person ?? '') }}">
                    </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', $vendor->email ?? '') }}" placeholder="vendor@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', $vendor->phone ?? '') }}">
                        </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', $vendor->tax_id ?? '') }}">
                    </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', $vendor->currency_id ?? '') == $cur->id ? 'selected':'' }}>
                                    {{ $cur->code }} — {{ $cur->name }}
                                </option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-12">
                        <label class="form-label fw-semibold">Address</label>
                        <textarea name="address" class="form-control" rows="2"
                                  placeholder="Street, City, Country">{{ old('address', $vendor->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">{{ old('notes', $vendor->notes ?? '') }}</textarea>
                    </div>
                </div>
            </div>
        </div>

        <div class="card mb-4">
            <div class="card-header fw-semibold">
                <i class="bi bi-bank me-2 text-primary"></i>Bank Details <span class="text-muted fw-normal" style="font-size:12px">(Optional)</span>
            </div>
            <div class="card-body">
                <div class="row g-3">
                    <div class="col-12 col-sm-6">
                        <label class="form-label fw-semibold">Bank Name</label>
                        <input type="text" name="bank_name" class="form-control"
                               value="{{ old('bank_name', $vendor->bank_name ?? '') }}" placeholder="e.g. Bank of America">
                    </div>
                    <div class="col-12 col-sm-6">
                        <label class="form-label fw-semibold">Account Number</label>
                        <input type="text" name="bank_account" class="form-control"
                               value="{{ old('bank_account', $vendor->bank_account ?? '') }}">
                    </div>
                    <div class="col-12 col-sm-6">
                        <label class="form-label fw-semibold">Routing / SWIFT / IBAN</label>
                        <input type="text" name="bank_routing" class="form-control"
                               value="{{ old('bank_routing', $vendor->bank_routing ?? '') }}">
                    </div>
                </div>
            </div>
        </div>
    </div>

    <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">Payment Terms</label>
                        <div class="input-group">
                            <input type="number" name="payment_terms_days" class="form-control"
                                   value="{{ old('payment_terms_days', $vendor->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">AP Account</label>
                        <select name="account_payable_id" class="form-select select2">
                            <option value="">— Use Default AP Account —</option>
                            @foreach($apAccounts as $acc)
                                <option value="{{ $acc->id }}" {{ old('account_payable_id', $vendor->account_payable_id ?? '') == $acc->id ? 'selected':'' }}>
                                    {{ $acc->code ? "[{$acc->code}] " : "" }}{{ $acc->name }}
                                </option>
                            @endforeach
                        </select>
                    </div>
                    @if(isset($vendor))
                    <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', $vendor->is_active) ? 'checked':'' }}>
                                <label class="form-check-label fw-semibold" for="isActive">Active Vendor</label>
                                <div class="text-muted" style="font-size:12px">Inactive vendors cannot be used in new transactions</div>
                            </div>
                        </div>
                    </div>
                    @endif
                </div>
            </div>
        </div>

        <div class="d-flex gap-2">
            <a href="{{ route('masters.vendors.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($vendor) ? 'Save' : 'Create' }}
            </button>
        </div>
    </div>
</div>
</form>
@endsection

@push('scripts')
<script>
document.getElementById('vendorForm').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
