@php($isEdit = isset($customer))
<form action="{{ $isEdit ? route('customers.update', $customer) : route('customers.store') }}" method="POST" id="customerForm">
    @csrf
    <div id="formErrors" class="alert alert-danger" style="display: none;"></div>
    @if($isEdit)
        @method('PUT')
    @endif

    <div class="row g-3">
        <div class="col-md-6">
            <label class="form-label">Customer Name *</label>
            <input type="text" name="name" class="form-control @error('name') is-invalid @enderror"
                   value="{{ old('name', $isEdit ? $customer->name : '') }}" required>
            @error('name')
                <div class="invalid-feedback">{{ $message }}</div>
            @enderror
        </div>
        <div class="col-md-6">
            <label class="form-label">Email</label>
            <input type="text" name="email" class="form-control @error('email') is-invalid @enderror"
                   value="{{ old('email', $isEdit ? $customer->email : '') }}" placeholder="Enter email address (optional)">
            @error('email')
                <div class="invalid-feedback">{{ $message }}</div>
            @enderror
        </div>
    </div>

    <div class="row g-3 mt-2">
        <div class="col-md-6">
            <label class="form-label">Phone</label>
            <input type="text" name="phone" class="form-control @error('phone') is-invalid @enderror"
                   value="{{ old('phone', $isEdit ? $customer->phone : '') }}" placeholder="Enter phone number" required>
            @error('phone')
                <div class="invalid-feedback">{{ $message }}</div>
            @enderror
        </div>
        <div class="col-md-6">
            <label class="form-label">Tax Number</label>
            <input type="text" name="tax_number" class="form-control @error('tax_number') is-invalid @enderror"
                   value="{{ old('tax_number', $isEdit ? $customer->tax_number : '') }}" placeholder="Enter tax number">
            @error('tax_number')
                <div class="invalid-feedback">{{ $message }}</div>
            @enderror
        </div>
    </div>

    <div class="row g-3 mt-2">
        <div class="col-12">
            <label class="form-label">Address</label>
            <textarea name="address" rows="3" class="form-control @error('address') is-invalid @enderror"
                      placeholder="Enter customer address">{{ old('address', $isEdit ? $customer->address : '') }}</textarea>
            @error('address')
                <div class="invalid-feedback">{{ $message }}</div>
            @enderror
        </div>
    </div>

    <div class="row g-3 mt-3">
        <div class="col-md-4">
            <label class="form-label">Opening Balance</label>
            <input type="number" step="0.0001" name="opening_balance" class="form-control @error('opening_balance') is-invalid @enderror"
                   value="{{ old('opening_balance', $isEdit ? $customer->opening_balance : '0') }}" placeholder="0.0000">
            @error('opening_balance')
                <div class="invalid-feedback">{{ $message }}</div>
            @enderror
            <small class="form-text text-muted">Positive for credit, negative for debit</small>
        </div>
        <div class="col-md-4">
            <label class="form-label">Credit Limit</label>
            <input type="number" step="0.0001" name="credit_limit" class="form-control @error('credit_limit') is-invalid @enderror"
                   value="{{ old('credit_limit', $isEdit ? $customer->credit_limit : '0') }}" placeholder="0.0000">
            @error('credit_limit')
                <div class="invalid-feedback">{{ $message }}</div>
            @enderror
        </div>
        <div class="col-md-4 d-flex align-items-end">
            <div class="form-check">
                <input type="hidden" name="is_active" value="0">
                <input type="checkbox" name="is_active" class="form-check-input @error('is_active') is-invalid @enderror"
                       value="1" {{ old('is_active', $isEdit ? (int)$customer->is_active : 1) ? 'checked' : '' }}>
                <label class="form-check-label">Active</label>
                @error('is_active')
                    <div class="invalid-feedback d-block">{{ $message }}</div>
                @enderror
            </div>
        </div>
    </div>

    <div class="row g-3 mt-3">
        <div class="col-md-4">
            <label class="form-label">Payment Term Number</label>
            <input type="number" name="pay_term_no" class="form-control @error('pay_term_no') is-invalid @enderror"
                   value="{{ old('pay_term_no', $isEdit ? $customer->pay_term_no : '') }}" placeholder="e.g., 30" min="1" max="999">
            @error('pay_term_no')
                <div class="invalid-feedback">{{ $message }}</div>
            @enderror
        </div>
        <div class="col-md-4">
            <label class="form-label">Payment Term Type</label>
            <select name="pay_term_type" class="form-select @error('pay_term_type') is-invalid @enderror">
                <option value="">Select Type</option>
                <option value="days" {{ old('pay_term_type', $isEdit ? $customer->pay_term_type : '') === 'days' ? 'selected' : '' }}>Days</option>
                <option value="month" {{ old('pay_term_type', $isEdit ? $customer->pay_term_type : '') === 'month' ? 'selected' : '' }}>Month</option>
            </select>
            @error('pay_term_type')
                <div class="invalid-feedback">{{ $message }}</div>
            @enderror
        </div>
        <div class="col-md-4">
            <!-- Empty column for alignment -->
        </div>
    </div>

    <div class="row g-3 mt-3">
        <div class="col-md-4">
            <label class="form-label">Commission Rate (%)</label>
            <input type="number" step="0.01" name="commission_rate" class="form-control @error('commission_rate') is-invalid @enderror"
                   value="{{ old('commission_rate', $isEdit ? $customer->commission_rate : '0') }}" placeholder="0.00" min="0" max="100">
            @error('commission_rate')
                <div class="invalid-feedback">{{ $message }}</div>
            @enderror
        </div>
        <div class="col-md-4">
            <label class="form-label">Security Deposit</label>
            <input type="number" step="0.01" name="security_deposit" class="form-control @error('security_deposit') is-invalid @enderror"
                   value="{{ old('security_deposit', $isEdit ? $customer->security_deposit : '0') }}" placeholder="0.00" min="0">
            @error('security_deposit')
                <div class="invalid-feedback">{{ $message }}</div>
            @enderror
        </div>
        <div class="col-md-4 d-flex align-items-end">
            <div class="form-check">
                <input type="hidden" name="is_security_account" value="0">
                <input type="checkbox" name="is_security_account" class="form-check-input @error('is_security_account') is-invalid @enderror"
                       value="1" {{ old('is_security_account', $isEdit ? (int)$customer->is_security_account : 0) ? 'checked' : '' }}>
                <label class="form-check-label">Is Security Account</label>
                @error('is_security_account')
                    <div class="invalid-feedback d-block">{{ $message }}</div>
                @enderror
            </div>
        </div>
    </div>

    <div class="row g-3 mt-4">
        <div class="col-12">
            <div class="d-flex gap-2">
                <button type="submit" class="btn btn-primary">
                    <i class="fas fa-save me-2"></i>
                    {{ $isEdit ? 'Update Customer' : 'Create Customer' }}
                </button>
                {{-- <a href="{{ route('customers.index') }}" class="btn btn-secondary">
                    <i class="fas fa-times me-2"></i>
                    Cancel
                </a> --}}
            </div>
        </div>
    </div>
</form>
