<style type="text/css">
</style>
<form class="row g-3 auto-width create_customer" id="create_customer_form" enctype='multipart/form-data' action="{!! route('supplier-account-store') !!}">
    <!-- For validation comment on -->
    <!-- <form class="row g-3 was-validated"> -->
    @csrf
    <div class="col-md-12">
        <div>
            <label for="name" class="form-label">Supplier 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="Full Name">
        </div>
        <span class="text-danger error_name" style="display:none;">This field is required.</span>
    </div>
    <div class="col-md-6 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="" id="phone_no" />
            <input type="hidden" name="phone_no">
        </div>
        <span class="text-danger error_phone" style="display:none;">This field is required.</span>
    </div>

    <div class="col-md-6 whatsapp_no_col">
        <div>
            <label class="form-label">Whatsapp no </label>
        </div>
        <div class="input-group">
            <input type="tel" class="form-control" placeholder="" id="whatsapp_no" />
            <input type="hidden" name="whatsapp_no">
        </div>
    </div>
    <div class="col-md-6">
        <label class="form-label">Country</label>
        <select class="single-select 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">
        <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-12">
        <label class="form-label">Address </label>
        <textarea name="address" class="form-control" placeholder="Enter Address"></textarea>
    </div>
    <div class="col-md-4">
        <label class="form-label">State</label>
        <input type="hidden" value="{!! $state_id !!}" id="stateid">
        <select class=" form-control single-select" 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-4">
        <label class="form-label">City</label>
        <input type="hidden" value="{!! $city_id !!}" name="cityid">
        <select class="single-select form-control" name="city_id" id="cities">
        </select>
        <span class="text-danger error_city_id" style="display:none;">This field is required.</span>
    </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>


    @foreach($license_types as $key => $value)
    <div class="col-md-6 ">
        <label for="license_number" class="form-label">{{ $value->name }} <span class="text-danger"><b>*</b></span></label>
        <input type="text" class="form-control license_type_validation" id="license_number[{{$value->code}}][license_number]" name="license_number[{{$value->code}}][license_number]" aria-describedby="inputGroupPrepend" placeholder="Enter {{$value->code}} Number" required>
        <input type="hidden" name="license_number[{{$value->code}}][license_type_id]" value="{{ $value->id }}">
        <span class="text-danger error_license_number" style="display:none;">This field is required.</span>
    </div>
    @endforeach

</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;
    }
    $("[name='year']").on('input', function() {
        var onlyNumbers = allNumeric($(this).val());

        if (onlyNumbers.length > 4) {
            onlyNumbers = onlyNumbers.substring(0, 4);
        }

        $("[name='year']").val(onlyNumbers);
    });

    $("[name='month']").on('input', function() {
        var month = allNumeric($(this).val());

        if (month > 12) {
            month = 12;
        }

        $("[name='month']").val(month);
    });

    $("[name='day']").on('input', function() {
        var day = parseInt($(this).val());
        if (isNaN(day) || day < 1) {
            day = 1;
        }

        if (day > 31) {
            day = 31;
        }

        $("[name='day']").val(day);
    });
    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>

@if($country_id)
    <script>
        changecontry();

        $('#states').trigger('change');
    </script>
@endif
