<form class="row g-3 auto-width edit_product" action="{{ route('accounts-import.update', $data->id)}}" method="post" enctype='multipart/form-data'>
    @csrf
    @method('patch')
    <div class="col-md-6">
        <label for="name" class="form-label">Account Name <span class="text-danger"><b>*</b></span></label>
        <input type="text" name="account_name" class="form-control" value="{!! $data->account_name !!}" required>
    </div>

    <div class="col-md-6">
        <label for="name" class="form-label">Print Name<span class="text-danger"><b>*</b></span></label>
        <input type="text" name="print_name" class="form-control" value="{!! $data->print_name !!}" required>
    </div>
    <div class="col-md-6">
        <label for="name" class="form-label">Customer ID</label>
        <input type="text" name="customer_id" class="form-control" value="{!! $data->customer_id !!}" required>
    </div>
    <div class="col-md-6">
        <label for="name" class="form-label">Phone <span class="text-danger"><b>*</b></span></label>
        <input type="text" name="phone" class="form-control" value="{!! $data->phone !!}" required>
    </div>
    <div class="col-md-6">
        <label for="name" class="form-label">Account Group <span class="text-danger"><b>*</b></span></label>
        <select class="single-select form-control" name="accounting_group_id">
            @foreach($accounting_groups as $list)
            <option value="{{ $list->name }}" {!! $data->account_group == $list->name || $data->account_group == $list->code ? 'selected' : '' !!}>{{ $list->name }}</option>
            @endforeach
        </select>
    </div>
    <div class="col-md-6">
        <label for="name" class="form-label">Account Type<span class="text-danger"><b>*</b></span></label>
        <select class="single-select form-control account_type_id" name="account_type_id">
            @foreach($account_types as $list)
            <option value="{{ $list->type_code }}" {!! $data->account_type == $list->type_name || $data->account_type == $list->type_code ? 'selected' : '' !!}>{{ $list->type_name }}</option>
            @endforeach
        </select>
    </div>
    <div class="col-md-6">
        <label for="name" class="form-label">Country <span class="text-danger"><b>*</b></span></label>
        <select class="single-select form-control form-control" id="country" name="country_id" onchange="changecontry()" required>
            <option value=" "> Select Country</option>
            @foreach($country as $list)
            <option value="{{ $list->id}}" {!! $data->country_id == $list->id ? 'selected' : '' !!}>{{$list->name}}</option>
            @endforeach
        </select>
    </div>
    <div class="col-md-6">
        <label for="name" class="form-label">State <span class="text-danger"><b>*</b></span></label>
        <input type="hidden" value="{{ $data->state_id ?? '' }}" id="stateid">
        <select class="single-select form-control" name="state" id="states">
        </select>
    </div>

    <div class="col-md-6">
        <label for="name" class="form-label">Whatsapp</label>
        <input type="text" name="whatsapp" class="form-control" value="{!! $data->whatsapp !!}" required>
    </div>

    <div class="col-md-6">
        <label for="name" class="form-label">Email</label>
        <input type="text" name="email" class="form-control" value="{!! $data->email !!}" required>
    </div>

    <div class="col-md-6">
        <label for="name" class="form-label">Contact Person</label>
        <input type="text" name="contact_person" class="form-control" value="{!! $data->contact_person !!}" required>
    </div>
    <div class="col-md-6">
        <label for="name" class="form-label">Address </label>
        <input type="text" name="address" class="form-control" value="{!! $data->address !!}" required>
    </div>
    <div class="col-md-6">
        <label for="name" class="form-label">City </label>
        <input type="hidden" value="{{ $data->city_id ?? '' }}" name="cityid">
        <select class="single-select" name="city" id="cities">
        </select>
    </div>
    <div class="col-md-6">
        <label for="name" class="form-label">Pin No </label>
        <input type="text" name="pin" class="form-control" value="{!! $data->pin !!}" required>
    </div>

    <div class="col-md-6">
        <label for="name" class="form-label">Marital Status</label>
        <input type="text" name="marital_status" class="form-control" value="{!! $data->marital_status !!}" required>
    </div>

    <div class="col-md-6">
        <label for="name" class="form-label">Gender</label>
        <input type="text" name="gender" class="form-control" value="{!! $data->gender !!}" required>
    </div>

    <div class="col-md-6">
        <label for="name" class="form-label">Blood Group</label>
        <input type="text" name="blood_group" class="form-control" value="{!! $data->blood_group !!}" required>
    </div>

    <div class="col-md-6">
        <label for="name" class="form-label">Bank Account Name</label>
        <input type="text" name="bank_account_name" class="form-control" value="{!! $data->bank_account_name !!}" required>
    </div>

    <div class="col-md-6">
        <label for="name" class="form-label">Account No</label>
        <input type="text" name="account_no" class="form-control" value="{!! $data->account_no !!}" required>
    </div>

    <div class="col-md-6">
        <label for="name" class="form-label">IFSC Code</label>
        <input type="text" name="ifsc_code" class="form-control" value="{!! $data->ifsc_code !!}" required>
    </div>

    <div class="col-md-6">
        <label for="name" class="form-label">Bank Name</label>
        <input type="text" name="bank_name" class="form-control" value="{!! $data->bank_name !!}" required>
    </div>

    @foreach ($licenses as $key => $value)
        @php $license = 'license_' . ($key + 1); @endphp
        <div class="col-md-6">
            <label for="name" class="form-label">{!! $value->name !!}</label>
            <input type="text" name="license_{!! $key + 1 !!}" class="form-control" value="{!! $data->$license !!}" required>
        </div>
    @endforeach
</form>
<script type="text/javascript">
    $('.single-select').select2({
        dropdownParent: $('.edit_product'),
        theme: 'bootstrap4',
        width: $(this).data('width') ? $(this).data('width') : $(this).hasClass('w-100') ? '100%' : 'style',
        placeholder: $(this).data('placeholder'),
        allowClear: Boolean($(this).data('allow-clear')),
    });
    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);
            }
        });
    }
    changecontry();

    $(document).on('change', '#states', function () {
        let states_id=$('#states').val()?$('#states').val():"{{isset($data->state_id) ? $data->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>
