<script>
    $(function() {
        $(document).on('click', '.idClose', function(e) {
            e.preventDefault();

            if (confirm('Are you sure you want to Close the ID?')) {
                var id = $(this).data('id');
                row = $(this).closest('tr');
                $.ajax({
                    url: "/admin/close/" + id,
                    type: "GET",
                    success: function(data) {
                        if (data.status == 'success') {
                            toastr.success(data.message);
                            //remove current row
                            row.remove();
                        }
                    }
                });
            }
        });
        var token = '';
        $(document).on('click', '.idDestroy', function(e) {
            e.preventDefault();
            var id = $(this).data('id');
            row = $(this);

            jQuery.ajaxSetup({
                async: false
            });
            $.get("{{ route('csrf') }}", function(data) {
                window.token = data;
            });
            jQuery.ajaxSetup({
                async: true
            });

            if (confirm('Are you sure you want to Delete the ID?')) {
                $.ajax({
                    url: "/admin/clients/" + id,
                    type: "DELETE",
                    data: {
                        "_token": window.token,
                        'id': id
                    },
                    success: function(data) {
                        if (data.status == 'success') {
                            toastr.success(data.message);
                            row.closest('tr').remove();
                        } else if (data.status == 'error') {
                            toastr.error(data.message);
                        }
                    }
                });
            }
        });


        $(document).on('click', '.idEnable', function(e) {
            e.preventDefault();

            if (confirm('Are you sure you want to Enable the ID?')) {

                const id = $(this).data('id');
                const row = $(this).closest('tr');

                $.ajax({
                    url: "/admin/idEnable/" + id,
                    type: "GET",
                    success: function(data) {
                        if(data.success != false){
                            toastr.success(data.message = "The Account Has Been Enabled");
                            if (data.clients_status == 'active') {
                                $($(row[0]).find('td')[2]).removeClass("bg-warning").addClass(
                                    "bg-success").text("Active");
                                $($(row[0]).find('td')[3]).removeClass("bg-warning").addClass(
                                    "bg-success");
                            } else {
                                $($(row[0]).find('td')[2]).removeClass("bg-secondary").addClass(
                                    "bg-danger").text("Expired");
                                $($(row[0]).find('td')[3]).removeClass("bg-secondary").addClass(
                                    "bg-danger");
                            }
                        } else {
                            toastr.error(data.message);
                        }
                        const enableId = document.getElementById(id);

                        if (Number(enableId.id) === id) {
                            enableId.innerHTML = "Disable";
                            enableId.classList.remove('idEnable');
                            enableId.classList.add('idDisable');
                        };
                    }

                });

            }
        });


        $(document).on('click', '.idDisable', function(e) {
            e.preventDefault();

            if (confirm('Are you sure you want to Disable the ID?')) {

                const id = $(this).data('id');
                const row = $(this).closest('tr');


                $.ajax({
                    url: "/admin/idDIsable/" + id,
                    type: "GET",
                    success: function(data) {
                        toastr.success(data.message = "The Account Has Been Disabled");
                        $($(row[0]).find('td')[2]).addClass("bg-warning").removeClass(
                            "bg-success").text("Disabled");
                        $($(row[0]).find('td')[3]).addClass("bg-warning").removeClass(
                            "bg-success");

                        // const disableId = document.getElementById(id);

                        // if (Number(disableId.id) === id) {
                        //     disableId.innerHTML = "Enable";
                        //     disableId.classList.remove('idDisable');
                        //     disableId.classList.add('idEnable');

                        // };

                    }

                });
            }
        });

        $(document).on('click', '.id_deactive', function(e) {
            e.preventDefault();

            if (confirm('Are you sure  want to Deactive the ID?')) {
                // alert('ok');
                const id = $(this).data('id');
                const row = $(this).closest('tr');


                $.ajax({
                    url: "/admin/idDeactive/" + id,
                    type: "GET",
                    success: function(data) {
                        toastr.success(data.message = "The Account Has Been Deactivated");
                        $('.cStatus' + id).removeClass("bg-success").addClass(
                            "bg-secondary").text("Deactive");
                        $('.cExpireDate' + id).addClass("bg-secondary").removeClass(
                            "bg-success");
                        $('.cStatus' + id).addClass("bg-secondary").removeClass("bg-danger")
                            .text("Deactive");
                        $('.cExpireDate' + id).addClass("bg-secondary").removeClass(
                            "bg-danger");
                        $('.deactive' + id).addClass("btn-success").removeClass(
                            "btn-danger");
                        // const deactiveId = document.getElementById("deactive" + id);

                        // deactiveId.innerHTML = "Reactive";
                        // deactiveId.classList.remove('id_deactive');
                        // deactiveId.classList.add('id_reactive');
                        // deactiveId.id = 'reactive' + id;



                    }

                });
            }
        });

        $(document).on('click', '.id_reactive', function(e) {
            e.preventDefault();

            if (confirm('Are you sure  want to Reactive the ID?')) {

                const id = $(this).data('id');
                const row = $(this).closest('tr');


                $.ajax({
                    url: "/admin/statusChange/reactive/" + id,
                    type: "GET",
                    success: function(data) {
                        if(data.success != false){
                            toastr.success(data.message = "The Account Has Been Reactivated");
                            if (data.clients_status == 'active') {
                                $('.cStatus' + id).removeClass("bg-warning").addClass(
                                    "bg-success").text("Active");
                                $('.cExpireDate' + id).removeClass("bg-warning").addClass(
                                    "bg-success");
                            }
                            if (data.clients_status == 'expired') {
                                $('.cStatus' + id).removeClass("bg-secondary").addClass(
                                    "bg-danger").text("Expired");
                                $('.cExpireDate' + id).removeClass("bg-secondary").addClass(
                                    "bg-danger");
                            }
                        } else {
                            toastr.error(data.message);
                        }
                        const reactiveId = document.getElementById("reactive" + id);

                        reactiveId.innerHTML = "Deactive";
                        reactiveId.classList.remove('id_reactive');
                        reactiveId.classList.add('id_deactive');
                        reactiveId.id = 'deactive' + id;
                        reactiveId.classList.remove('btn-success');
                        reactiveId.classList.add('btn-danger');
                    }

                });
            }
        });


    });
</script>
