<form class="row g-3 auto-width" action="{{ route('booking.storeconsignor')}}" method="post" enctype='multipart/form-data' id="quality_form">
    @csrf
    <style type="text/css">
    .intl-tel-input .country-list {
        z-index: 8;
    }
</style>

    <div class="col-md-12 mt-1 consignor_columns">
        <div>
            <label for="name" class="form-label">Consignor Name <span class="text-danger"><b>*</b></span></label>
        </div>
        <div class="input-group">
            <input type="text" name="name" class="form-control" value="{!! old('name') !!}" placeholder="Consignor Name">
        </div>
        <span class="text-danger error_name" style="display:none;">This field is required.</span>
    </div>
    <div class="col-md-12 mt-1 consignee_columns">
        <div>
            <label for="name" class="form-label">Consignee Name <span class="text-danger"><b>*</b></span></label>
        </div>
        <div class="input-group">
            <input type="text" name="name" class="form-control" value="{!! old('name') !!}" placeholder="Consignee Name">
        </div>
        <span class="text-danger error_name" style="display:none;">This field is required.</span>
    </div>

    <div class="col-md-12 mt-1 vendor_columns ">
        <div>
            <label for="name" class="form-label">Vendor Name <span class="text-danger"><b>*</b></span></label>
        </div>
        <div class="input-group">
            <input type="text" name="name" class="form-control" value="{!! old('name') !!}" placeholder="Vendor Name">
        </div>
        <span class="text-danger error_name" style="display:none;">This field is required.</span>
    </div>

    <div class="col-md-12 mt-1 lc_issued_by_columns ">
        <div>
            <label for="name" class="form-label">LC Issued By Name <span class="text-danger"><b>*</b></span></label>
        </div>
        <div class="input-group">
            <input type="text" name="name" class="form-control" value="{!! old('name') !!}" placeholder="LC Issued By Name">
        </div>
        <span class="text-danger error_name" style="display:none;">This field is required.</span>
    </div>
     <div class="col-md-12 mt-1 cfs_agent_columns ">
        <div>
            <label for="name" class="form-label">CFS Agent Name <span class="text-danger"><b>*</b></span></label>
        </div>
        <div class="input-group">
            <input type="text" name="name" class="form-control" value="{!! old('name') !!}" placeholder="CFS Agent Name">
        </div>
        <span class="text-danger error_name" style="display:none;">This field is required.</span>
    </div>

 <div class="col-md-12 mt-1 broker_columns ">
        <div>
            <label for="name" class="form-label">Broker Name <span class="text-danger"><b>*</b></span></label>
        </div>
        <div class="input-group">
            <input type="text" name="name" class="form-control" value="{!! old('name') !!}" placeholder="Broker Name">
        </div>
        <span class="text-danger error_name" style="display:none;">This field is required.</span>
    </div>

    @foreach($license_types as $key => $value)
    <div class="col-md-6 mt-1 ">
        <label for="license_number" class="form-label">{{ $value->name }}</label>
        <input type="text" class="form-control" id="license_number[{{$value->code}}][license_number]" name="license_number[{{$value->code}}][license_number]" aria-describedby="inputGroupPrepend" required>
        <input type="hidden" name="license_number[{{$value->code}}][license_type_id]" value="{{ $value->id }}">
    </div>
   @endforeach
    <div class="col-md-6 mt-1 phone_no_col ">
        <div>
            <label class="form-label">Phone  No.<span class="text-danger"><b>*</b></span></label>
        </div>
        <div class="input-group">
            <input type="tel" class="form-control w-100" placeholder="111 2223333" id="phone_no" />
            <input type="hidden" name="phone_no">
        </div>
        <span class="text-danger error_phone_no" style="display:none;">This field is required.</span>
    </div>

    <div class="col-md-6 mt-1 whatsapp_no_col">
        <div>
            <label class="form-label">Whatsapp No. </label>
        </div>
        <div class="input-group">
            <input type="tel" class="form-control" placeholder="111 2223333" id="whatsapp_no" />
            <input type="hidden" name="whatsapp_no">
        </div>
    </div>
    <div class="col-md-6 mt-1">
        <div>
            <label class="form-label">Email </label>
        </div>
        <div class="input-group">
            <input type="text" class="form-control" placeholder="Email" name="email" />
        </div>
    </div>
    <div class="col-md-6 mt-1">
        <label class="form-label">Country</label>
        <select class="form-control" id="country" name="country_id" onchange="changecontry()" required>
            <option value=" "> Select Country</option>
            @foreach($country as $list)
            <option value="{{ $list->id}}" {!! matchSelected(old('country_id', $country_id), $list->id) !!}>{{$list->name}}</option>
            @endforeach
        </select>
    </div>

    <div class="col-md-6 mt-1">
        <label class="form-label">State</label>
        <input type="hidden" value="{!! $state_id !!}" id="stateid">
        <select class=" form-control" name="state_id" id="states">
        </select>
        <span class="text-danger error_state_id" style="display:none;">This field is required.</span>
    </div>
    <div class="col-md-6 mt-1">
        <label class="form-label">City</label>
        <div class="input-group">
            <input type="hidden" value="{!! $city_id !!}" name="cityid">
            <select class="form-control" name="city_id" id="cities">
            </select>
            <div class="input-group-append" style="margin: auto 10px auto 0;">
                <button class="btn btn-secondary add_btn" id="create_city" type="button">+</button>
            </div>
            <span class="text-danger error_city_id" style="display:none;">This field is required.</span>
        </div>
    </div>

    <div class="col-md-8">
        <label class="form-label">Address </label>
        <textarea name="address" class="form-control"></textarea>
    </div>
    <div class="col-md-4">
        <div>
            <label class="form-label">PIN/Postal code </label>
        </div>
        <div class="input-group">
            <input type="text" class="form-control" placeholder="PIN/Postal code" name="postal_code" />
        </div>
    </div>
</form>

<script>

    $('.details-toggle').hide();
    var toggle = true;

    $("#phone_no").on('input', function() {
        const onlyNumbers = allNumeric($(this).val());
        $("#phone_no").val(onlyNumbers);
    });
    $("#whatsapp_no").on('input', function() {
        const onlyNumbers = allNumeric($(this).val());
        $("#whatsapp_no").val(onlyNumbers);
    });
    $("#alternative_no").on('input', function() {
        const onlyNumbers = allNumeric($(this).val());
        $("#alternative_no").val(onlyNumbers);
    });
    function allNumeric(inputtxt) {
        onlyNumbers = inputtxt.replace(/[^0-9]/g, '');
        if (onlyNumbers.length > 16) {
            onlyNumbers = onlyNumbers.substring(0, 16);
        }
        return onlyNumbers;
    }
   
    function changecontry()
    {
        $.ajaxSetup({
            headers : {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });

        let country=$('#country').val();
        let stateid=$('#stateid').val();

        $.ajax({
            type: "POST",
            url: "{{ url('companies/getstate') }}",
            data: {country:country,stateid:stateid},
            success: function (data) {
                $('#states').html(data);
                $('#states').trigger('change');
            },
            error: function (data) {
                console.log(data);
            }
        });
        $.ajax({
            type: "POST",
            url: "{{ url('companies/getcountrycode') }}",
            data: {
                country: country
            },
            success: function (countryCode) {
                var telInput = $('#phone_no, #whatsapp_no, #alternative_no');

                // Destroy the existing instances
                telInput.intlTelInput('destroy');

                // Reinitialize the input fields with the new country code
                telInput.intlTelInput({
                    utilsScript: 'https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.13/js/utils.js',
                    initialCountry: countryCode, // Set the received country code directly
                    separateDialCode: true
                });
            },
            error: function (data) {
                // Handle error
            }
        });
    }

    $(document).on('change', '#states', function () {
        let states_id=$('#states').val()?$('#states').val():"{{isset($state_id) ? $state_id : ''}}";
        let cityid=$('#cityid').val();

        $.ajaxSetup({
            
            headers : {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });
        $.ajax({
            type: "POST",
            url: "{{ url('companies/getcity') }}",
            data: {states_id:states_id,cityid:cityid},
            success: function (data) {
                $('#cities').html(data);
            },
            error: function (data) {
                console.log(data);
            }
        });
    })

</script>
</script>
@if($country_id)
    <script>
        changecontry();
        $('#states').trigger('change');
    </script>
  <script>

</script>
@endif

</form>