<script>
    $(function() {

        var timeout = null;


        $('#reset').click(function() {
            $('#result').html('');
            $('#pop').html('<option value="all">ALL POP</option>');
        });

        $('#searchBtn').on('click', function(e) {
            e.preventDefault();

            $('#processing').show();
            $.ajax({
                type: 'get',
                url: '<?php echo e(route('newClientSearchReport')); ?>',
                data: $('#search').serialize(),
                success: function(result) {
                    $('#result').html(result);
                    $('#processing').hide();
                    $('#itest').css('display', 'block');
                }
            });
        });


        $('body').on('click', '.addOtc', function() {
            var id = $(this).attr('data-id');
            console.log(id,'penidng');
            $('#addOtc .client_id').val(id);
        });


        $(document).on('click', '.clientBillCycleChange', function() {
            console.log('comes');
            var id = $(this).attr('data-id');
            var billcycle = $(this).attr('data-curdate');

            console.log(billcycle);
            $("select#allBillCycle option").each(function() {

                if ($(this).val() == billcycle) { // EDITED THIS LINE
                    $(this).attr("selected", "selected");
                }
                $('.billingCycleChange').show();
            });


            $('#bill_client_id').val(id);

        });

        $(document).on('click', '.changeTemporaryExtend', function() {
            var id = $(this).attr('data-id');
            var extendedDay = $(this).attr('data-tmpExtend');
            $('#extend').val(extendedDay);
            $('#client_id').val(id);
        });


    })



    // $('#searchbtn').on('click', function() {
    //     let name = $(this).val();
    //     $('#processing').show();

    //     if (name.length >= 3 || name.length == 0) {
    //         $.ajax({
    //             type: 'get',
    //             url: '<?php echo e(route('customer_search')); ?>',
    //             data: {
    //                 search: name
    //             },
    //             success: function(result) {

    //                 $('#customer_list_body').html(result);
    //                 $('#processing').hide();
    //                 //checkExpire();
    //             }
    //         });
    //     } else {
    //         alert('Please tryp 3 character');
    //         $('#processing').hide();
    //     }
    // })

    $('#reseller').on('change', function(e) {
        e.preventDefault();
        $('body').append('<div class="hidden" id="loading">Loading.....</div>');

        $.ajax({
            type: 'get',
            url: '<?php echo e(route('getResellerPopList')); ?>',
            data: {
                id: $(this).val()
            },
            success: function(result) {
                $('#pop').html(result);
                $('#loading').remove();

            }
        });
    });

    // search section

    var typingTimer; //timer identifier
    var doneTypingInterval = 500; //time in ms (5 seconds)



    $('#customer_search').on('keyup', function() {
        clearTimeout(typingTimer);
        typingTimer = setTimeout(doneTyping, doneTypingInterval);

    });

    function doneTyping() {
        customersearch();
        clearTimeout(typingTimer);
    }

    $('#contact').on('keyup', function() {
        clearTimeout(typingTimer);
        typingTimer = setTimeout(doneTyping, doneTypingInterval);
    });

    $('#area').on('keyup', function() {
        clearTimeout(typingTimer);
        typingTimer = setTimeout(doneTyping, doneTypingInterval);
    });

    $('#box').on('keyup', function() {
        clearTimeout(typingTimer);
        typingTimer = setTimeout(doneTyping, doneTypingInterval);
    });

    $('#customername').on('keyup', function() {
        clearTimeout(typingTimer);
        typingTimer = setTimeout(doneTyping, doneTypingInterval);
    });

    $('#customerCode').on('keyup', function() {
        clearTimeout(typingTimer);
        typingTimer = setTimeout(doneTyping, doneTypingInterval);
    });

    $('#id').on('keyup', function() {
        clearTimeout(typingTimer);
        typingTimer = setTimeout(doneTyping, doneTypingInterval);
    });

    function customersearch() {

        let name = $('#customer_search').val();
        let contact = $('#contact').val();
        let area = $('#area').val();
        let box = $('#box').val();
        let id = $('#id').val();
        let customername = $('#customername').val();
        let customerCode = $('#customerCode').val();


        // $('#processing').show();
        if (contact.length > 3 || area.length > 2 || box.length > 1 || customername.length > 2 || id.length > 1 || name
            .length > 0 || customerCode.length > 0) {

            $.ajax({
                type: 'get',
                url: '<?php echo e(route('customer_search')); ?>',
                data: {
                    username: name,
                    contact: contact,
                    area: area,
                    box: box,
                    id: id,
                    customername: customername,
                    customerCode: customerCode
                },
                success: function(result) {
                    $('#result').html(result);
                    $('#processing').hide();
                }
            });


        } else {
            // alert('Please tryp 3 character');
            $('#processing').hide();
        }
    }

    $(document).on("click", ".package_change_without_cost", function() {

        let client_id = $(this).data('clientid');

        $.ajax({
            type: 'get',
            url: '<?php echo e(route('getClientDetailsForPackageChange')); ?>',
            data: {
                client_id: client_id
            },
            success: function(result) {
                $('#package_change_without_cost_client_id').val(result.client.id);
                $('#package_change_without_cost_client_name').val(result.client.clientsinfo
                    .clients_name);
                $('#package_change_without_cost_package').empty();
                $('#package_change_without_cost_package').append(
                    '<option value="">Select Package</option>');

                if (result.client.pop.subreseller == 'yes') {
                    let client_packages = result.client.sub_package_id;
                    $.each(result.packages, function(key, value) {
                        if (client_packages == value.id) {
                            $('#package_change_without_cost_package').append(
                                '<option value="' + value.id + '" selected>' + value
                                .name + '</option>');
                        } else {

                            $('#package_change_without_cost_package').append(
                                '<option value="' + value.id + '">' + value.name +
                                '</option>');
                        }
                    });
                } else {
                    let client_packages = result.client.package_id;
                    $.each(result.packages, function(key, value) {
                        if (client_packages == value.id) {
                            $('#package_change_without_cost_package').append(
                                '<option value="' + value.id + '" selected>' + value
                                .package_name + '</option>');
                        } else {

                            $('#package_change_without_cost_package').append(
                                '<option value="' + value.id + '">' + value
                                .package_name + '</option>');
                        }
                    });
                }
            }
        });
    });


    $(document).on("click", ".package_change_action_btn", function() {

        let client_id = $(this).data('clientid');

        $.ajax({
            type: 'get',
            url: '<?php echo e(route('getClientDetailsForPackageChange')); ?>',
            data: {
                client_id: client_id
            },
            success: function(result) {

                $('#package_change_client_name').val(result.client.clientsinfo.clients_name);
                $('#package_change_client_id').val(result.client.id);
                $('#package_change_package').empty();
                $('#package_change_package').append('<option value="">Select Package</option>');

                if (result.client.pop.subreseller == 'yes') {
                    let client_packages = result.client.sub_package_id;
                    $.each(result.packages, function(key, value) {
                        if (client_packages == value.id) {
                            $('#package_change_package').append('<option value="' + value
                                .id + '" selected>' + value.name + '</option>');
                        } else {

                            $('#package_change_package').append('<option value="' + value
                                .id + '">' + value.name + '</option>');
                        }
                    });
                } else {
                    let client_packages = result.client.package_id;
                    $.each(result.packages, function(key, value) {
                        if (client_packages == value.id) {
                            $('#package_change_package').append('<option value="' + value
                                .id + '" selected>' + value.package_name + '</option>');
                        } else {

                            $('#package_change_package').append('<option value="' + value
                                .id + '">' + value.package_name + '</option>');
                        }
                    });
                }

            }
        });

    });



    $(document).ready(function() {
        $('#order-select').on('change', function() {
            updateTable();
        });

        function updateTable() {
            var selectedOption = $('#order-select').val();
            const $tbody = $('.product-table tbody');
            var rows = $tbody.find('tr').get();

            rows.sort(function(a, b) {
                var aVal, bVal;

                if (selectedOption === 'id_asc' || selectedOption === 'id_desc') {
                    aVal = $(a).find('td:first').text();
                    bVal = $(b).find('td:first').text();
                } else {
                    aVal = $(a).find('td:nth-child(2)').text();
                    bVal = $(b).find('td:nth-child(2)').text();
                }

                if (selectedOption === 'id_asc' || selectedOption === 'name_asc') {
                    return (aVal > bVal) ? 1 : -1;
                } else {
                    return (aVal < bVal) ? 1 : -1;
                }
            });

            $.each(rows, function(index, row) {
                $tbody.append(row);
            });
        }
    });

    $(document).on("click", ".schedule_package_change", function() {

        let client_id = $(this).data('clientid');

        $.ajax({
            type: 'get',
            url: '<?php echo e(route('getClientDetailsForPackageChange')); ?>',
            data: {
                client_id: client_id
            },
            success: function(result) {

                $('#schedule_package_change_client_name').val(result.client.clientsinfo.clients_name);
                $('#schedule_package_change_client_id').val(result.client.id);
                $('#schedule_package_change_package').empty();
                $('#schedule_package_change_package').append('<option value="">Select Package</option>');

                if (result.client.pop.subreseller == 'yes') {
                    let client_packages = result.client.sub_package_id;
                    $.each(result.packages, function(key, value) {
                        if (client_packages == value.id) {
                            $('#schedule_package_change_package').append(
                                '<option value="' + value.id +
                                '" selected>' + value.name + '</option>');
                        } else {

                            $('#schedule_package_change_package').append(
                                '<option value="' + value.id + '">' + value
                                .name + '</option>');
                        }
                    });
                } else {
                    let client_packages = result.client.package_id;
                    $.each(result.packages, function(key, value) {
                        if (client_packages == value.id) {
                            $('#schedule_package_change_package').append(
                                '<option value="' + value.id +
                                '" selected>' + value.package_name +
                                '</option>');
                        } else {

                            $('#schedule_package_change_package').append(
                                '<option value="' + value.id + '">' + value.package_name + '</option>');
                        }
                    });
                }

            }
        });

    });





    /*auto deactive date change start*/
    // const autoDeactiveDateApply = function(cid){
    // const cidOfClient = document.getElementById(`autoDeactiveId-${cid}`);
    // const autoDeactiveDate =  cidOfClient.getAttribute('data-autodeactive');
    // const deactiveModalPropertiesCid = document.getElementById("deactive_client_id");
    // const deactiveModalPropertiesAutoDate = document.getElementById("deactiveDate");

    //     if(autoDeactiveDate !=='01-Jan-1970'){
    //     deactiveModalPropertiesAutoDate.setAttribute("value",autoDeactiveDate);
    //     }
    //     deactiveModalPropertiesCid.setAttribute("value",cid);

    //   }
    /*auto deactive date chnage end*/
</script>
<?php /**PATH /var/www/html/trustnet/radius-circle/resources/views/clients/JsFile/Customer.blade.php ENDPATH**/ ?>