@extends('dashboard')
@section('title', 'Add New OMS Client')
@section('page_content')
<div class="container py-4">
    <div class="row justify-content-center">
        <div class="col-md-10">
            <div class="card shadow-sm">
                <div class="card-header">
                    <h5 class="mb-0">Add New OMS Client</h5>
                </div>
                <div class="card-body">
                    <form action="{{ route('oms_clients.store') }}" method="POST">
                        @csrf
                        <div class="row g-3">
                            <div class="col-md-4">
                                <label class="form-label">Date <span class="text-danger">*</span></label>
                                <input type="date" name="date" class="form-control" value="{{ date('Y-m-d') }}" required>
                            </div>
                            <div class="col-md-4">
                                <label class="form-label">Client Name <span class="text-danger">*</span></label>
                                <input type="text" name="client_name" class="form-control" required>
                            </div>
                            <div class="col-md-4">
                                <label class="form-label">Guardian Name <span class="text-danger">*</span></label>
                                <input type="text" name="guardian_name" class="form-control" required>
                            </div>
                            <div class="col-md-4">
                                <label class="form-label">NID Number <span class="text-danger">*</span></label>
                                <input type="text" name="nid_number" class="form-control" required>
                            </div>
                            <div class="col-md-4">
                                <label class="form-label">Mobile Number</label>
                                <input type="text" name="mobile_number" class="form-control">
                            </div>
                            <div class="col-md-4">
                                <label class="form-label">Product Name</label>
                                <input type="text" name="product_name" class="form-control">
                            </div>
                            
                            <div class="col-md-6">
                                <label class="form-label">Dealer Name <span class="text-danger">*</span></label>
                                <select name="dealer_id" id="dealer_id" class="form-select" required>
                                    <option value="">-- Select Dealer --</option>
                                    @foreach($dealers as $dealer)
                                        <option value="{{ $dealer->id }}">{{ $dealer->dealer_name }}</option>
                                    @endforeach
                                </select>
                            </div>
                            <div class="col-md-6">
                                <label class="form-label">Word No</label>
                                <input type="text" name="word_no" id="word_no" class="form-control " readonly required>
                            </div>
                            <div class="col-md-6">
                                <label class="form-label">Institute Name</label>
                                <input type="text" name="institute_name" id="institute_name" class="form-control " readonly required>
                            </div>
                            <div class="col-md-6">
                                <label class="form-label">Sale Point</label>
                                <input type="text" name="sale_point" id="sale_point" class="form-control " readonly required>
                            </div>

                            <div class="col-md-12">
                                <label class="form-label">Address</label>
                                <textarea name="address" class="form-control" rows="2"></textarea>
                            </div>
                        </div>
                        <div class="mt-4 text-end">
                            <a href="{{ route('oms_clients.index') }}" class="btn btn-secondary">Cancel</a>
                            <button type="submit" class="btn btn-primary">Save Client</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    document.getElementById('dealer_id').addEventListener('change', function() {
        let dealerId = this.value;
        if(dealerId) {
            fetch(`/oms-clients/dealer-details/${dealerId}`)
                .then(response => response.json())
                .then(data => {
                    if(data) {
                        document.getElementById('word_no').value = data.word_number || '';
                        document.getElementById('institute_name').value = data.institute_name || '';
                        document.getElementById('sale_point').value = data.sale_point || '';
                    }
                });
        } else {
            document.getElementById('word_no').value = '';
            document.getElementById('institute_name').value = '';
            document.getElementById('sale_point').value = '';
        }
    });
</script>
@endsection