@extends('layouts.app')

@section('title', 'Create Account')

@section('content')
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h4 class="card-title">Create Account</h4>
                    <div class="card-tools">
                        <a href="{{ route('accounts.index') }}" class="btn btn-secondary">
                            <i class="fas fa-arrow-left"></i> Back to List
                        </a>
                    </div>
                </div>
                <div class="card-body">
                    <form action="{{ route('accounts.store') }}" method="POST">
                        @csrf

                        <div class="row">
                            <div class="col-md-6">
                                <div class="mb-3">
                                    <label for="name" class="form-label">Account Name *</label>
                                    <input type="text" class="form-control @error('name') is-invalid @enderror"
                                           id="name" name="name" value="{{ old('name') }}" required>
                                    @error('name')
                                        <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>
                            </div>

                            <div class="col-md-6">
                                <div class="mb-3">
                                    <label for="model_type" class="form-label">Account Type *</label>
                                    <select class="form-select @error('model_type') is-invalid @enderror"
                                            id="model_type" name="model_type" required>
                                        <option value="">Select Account Type</option>
                                        <option value="contact" {{ old('model_type') == 'contact' ? 'selected' : '' }}>Contact</option>
                                        <option value="user" {{ old('model_type') == 'user' ? 'selected' : '' }}>User</option>
                                    </select>
                                    @error('model_type')
                                        <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>
                            </div>
                        </div>

                        <div class="mb-3">
                            <label for="model_id" class="form-label">Select {{ ucfirst(old('model_type', 'Account Type')) }} *</label>
                            <select class="form-select @error('model_id') is-invalid @enderror"
                                    id="model_id" name="model_id" required>
                                <option value="">Select {{ ucfirst(old('model_type', 'Account Type')) }}</option>
                            </select>
                            @error('model_id')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>

                        <div class="d-flex justify-content-end">
                            <button type="submit" class="btn btn-primary">
                                <i class="fas fa-save"></i> Create Account
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@push('scripts')
<script>
$(document).ready(function() {
    // Store the original data
    var contacts = @json($contacts);
    var users = @json($users);

    $('#model_type').change(function() {
        var modelType = $(this).val();
        var modelSelect = $('#model_id');

        // Clear current options
        modelSelect.empty();
        modelSelect.append('<option value="">Select ' + modelType.charAt(0).toUpperCase() + modelType.slice(1) + '</option>');

        if (modelType === 'contact') {
            contacts.forEach(function(contact) {
                modelSelect.append('<option value="' + contact.id + '">' + contact.name + '</option>');
            });
        } else if (modelType === 'user') {
            users.forEach(function(user) {
                modelSelect.append('<option value="' + user.id + '">' + user.name + '</option>');
            });
        }
    });

    // Trigger change if model_type is already selected
    if ($('#model_type').val()) {
        $('#model_type').trigger('change');
    }
});
</script>
@endpush
