


        $(window).on('load', function() {
            if (feather) {
                feather.replace({
                    width: 14,
                    height: 14
                });
            }
        })
        document.addEventListener('DOMContentLoaded', function() {
          const skypeButton = document.getElementById('skypeButton');
        
          if (skypeButton) {
          const skypeAdd = document.getElementById('skypeButton').innerHTML;
            skypeButton.addEventListener('click', function() {
              const skypeURI = `skype:${skypeAdd}?chat`;
              window.location.href = skypeURI;
            });
          }
        });
        
        var timerID = setInterval(function() {
        now = new Date();
        timezone = now.getTimezoneOffset();
        day = now.getDay();
        hour = now.getUTCHours();
        min = now.getUTCMinutes();
        sec = now.getUTCSeconds();
        ms = now.getUTCMilliseconds();

        displayTime(hour, min, sec, day, "AM", -5, "time-atx", "day-atx");
        }, 1000);

        function displayTime(hour, min, sec, day, meridiem, offset, timeId, dayId) {

        hour = hour + offset;
        if (hour < 0) {
            hour = 24 + hour;
            day--;
        } else if (hour >= 24) {
            hour = hour - 24;
            day++;
        }


        if (hour > 12) {
            hour = hour - 12;
            meridiem = "PM";
        }
        if (hour === 0)
            hour = 12;

        if (hour < 10)
            hour = "0" + hour;
        if (min < 10)
            min = "0" + min;
        if (sec < 10)
            sec = "0" + sec;

        $('#dashHours').html(hour);
        $('#dashMin').html(min);
        $('#dashSec').html(sec);
    }
    


        $(document).ready(function() {
            "use strict"

            //show response message
            function showResponseMessage(data) {

                if (data.status === 'success') {
                    toastr['success'](data.message, 'Success!!', {
                        closeButton: true,
                        positionClass: 'toast-top-right',
                        progressBar: true,
                        newestOnTop: true,
                    });
                    dataListView.draw();
                } else {
                    toastr['warning']("Assign Manager First", 'warning!', {
                        closeButton: true,
                        positionClass: 'toast-top-right',
                        progressBar: true,
                        newestOnTop: true,
                    });
                }
            }
            let search_btn = $("#search_btn");
            search_btn.click(function(e) {
                e.preventDefault();
                dataListView.draw();
            });
            // init table dom
            let Table = $("table");
            // init list view datatable
            let dataListView = $('.datatables-basic').DataTable({
                lengthChange: true,
                processing: true,
                serverSide: true,
                sScrollX: "100%",
                autoWidth: false,
                "serverSide": true,
                "ajax": {
                    "url": "https://trackergenius.runterminal.com/admin/affiliate/search",
                    "dataType": "json",
                    "type": "POST",
                    "data": {
                        _token: "xVg5vlGiMwfiDk04qzHQhHjes4QkM1yoxS7WeOHB",
                        name: function() {
                            return $("#name").val();
                        },
                        email: function() {
                            return $("#email").val();
                        },
                        status: function() {
                            return $("#status").val();
                        },
                        manager_id: function() {
                            return $("#manager_id").val();
                        },
                    }
                },
                "columns": [{
                        "data": 'id',
                        orderable: false,
                        searchable: false
                    },
                    {
                        "data": "name",
                        orderable: false,
                        searchable: false
                    },
                    {
                        "data": "roles",
                        orderable: false,
                        searchable: false
                    },
                    {
                        "data": "manager",
                        orderable: false,
                        searchable: false
                    },
                    {
                        "data": "balance",
                        orderable: false,
                        searchable: false
                    },
                    {
                        "data": "created_at",
                        orderable: false,
                        searchable: false
                    },
                    {
                        "data": "status",
                        orderable: false,
                        searchable: false
                    },
                    {
                        "data": "action",
                        orderable: false,
                        searchable: false
                    }
                ],

                searchDelay: 1500,
                columnDefs: [{
                        // For Responsive
                        className: 'control',
                        orderable: false,
                        responsivePriority: 2,
                        targets: 2
                    },
                    {
                        // For Checkboxes
                        targets: 1,
                        orderable: false,
                        responsivePriority: 3,
                        render: function(data) {
                            return (
                                '<div class="form-check"> <input class="form-check-input dt-checkboxes" type="checkbox" value="" id="' +
                                data +
                                '" /><label class="form-check-label" for="' +
                                data +
                                '"></label></div>'
                            );
                        },
                        checkboxes: {
                            selectAllRender: '<div class="form-check"> <input class="form-check-input" type="checkbox" value="" id="checkboxSelectAll" /><label class="form-check-label" for="checkboxSelectAll"></label></div>',
                            selectRow: true
                        }
                    },
                    {
                        targets: 2,
                        visible: false
                    },
                    {
                        // Avatar image/badge, Name and post
                        targets: 3,
                        responsivePriority: 2,
                        render: function(data, type, full) {
                            var $user_img = full['avatar'],
                                $name = full['name'],
                                $email = full['email'];
                            if ($user_img) {
                                // For Avatar image
                                var $output =
                                    '<img src="' + $user_img +
                                    '" alt="Avatar" width="32" height="32">';
                            } else {
                                // For Avatar badge
                                var stateNum = full['status'];
                                var states = ['success', 'danger', 'warning', 'info', 'dark',
                                    'primary', 'secondary'
                                ];
                                var $state = states[stateNum],
                                    $name = full['name'],
                                    $initials = $name.match(/\b\w/g) || [];
                                $initials = (($initials.shift() || '') + ($initials.pop() || ''))
                                    .toUpperCase();
                                $output = '<span class="avatar-content">' + $initials + '</span>';
                            }

                            var colorClass = $user_img === '' ? ' bg-light-' + $state + ' ' : '';
                            // Creates full output for row
                            return '<div class="d-flex justify-content-left align-items-center">' +
                                '<div class="avatar ' +
                                colorClass +
                                ' me-1">' +
                                $output +
                                '</div>' +
                                '<div class="d-flex flex-column">' +
                                '<span class="emp_name text-truncate fw-bold">' +
                                $name +
                                '</span>' +
                                '<small class="emp_post text-truncate text-muted">' +
                                $email +
                                '</small>' +
                                '</div>' +
                                '</div>';
                        }
                    },
                    {
                        // Actions
                        targets: -1,
                        title: 'Action',
                        orderable: false,
                        render: function(data, type, full) {
                            return (
                                '<span class="d-flex">' +
                                '<span class="action-delete text-danger pe-1 cursor-pointer" data-id=' +
                                full['delete'] + '>' +
                                feather.icons['trash'].toSvg({
                                    class: 'font-medium-4'
                                }) +
                                '</span>' +
                                '<a href="' + full['edit'] + '" class="text-primary">' +
                                feather.icons['edit'].toSvg({
                                    class: 'font-medium-4'
                                }) +
                                '</a>' +
                                '<a href="' + full['login'] +
                                '" class="text-success" style="margin-left: 10px" data-bs-toggle="tooltip" data-bs-placement="top" title="' +
                                "Login" + '">' +
                                feather.icons['log-in'].toSvg({
                                    class: 'font-medium-4'
                                }) +
                                '</a>' +
                                '</span>'
                            );
                        }
                    }
                ],
                dom: '<"d-flex justify-content-between align-items-center mx-0 row"<"col-sm-12 col-md-6"l><"col-sm-12 col-md-6"f>>t<"d-flex justify-content-between mx-0 row"<"col-sm-12 col-md-6 mb-2"i><"col-sm-12 col-md-6"p>>',
                responsive: {
                    details: {
                        display: $.fn.dataTable.Responsive.display.modal({
                            header: function(row) {
                                let data = row.data();
                                return 'Details of ' + data['name'];
                            }
                        }),
                        type: 'column',
                        renderer: function(api, rowIdx, columns) {
                            let data = $.map(columns, function(col) {
                                return col.title !==
                                    '' // ? Do not show row in modal popup if title is blank (for check box)
                                    ?
                                    '<tr data-dt-row="' +
                                    col.rowIdx +
                                    '" data-dt-column="' +
                                    col.columnIndex +
                                    '">' +
                                    '<td>' +
                                    col.title +
                                    ':' +
                                    '</td> ' +
                                    '<td>' +
                                    col.data +
                                    '</td>' +
                                    '</tr>' :
                                    '';
                            }).join('');

                            return data ? $('<table class="table"/>').append('<tbody>' + data +
                                '</tbody>') : false;
                        }
                    }
                },
                aLengthMenu: [
                    [10, 20, 50, 100],
                    [10, 20, 50, 100]
                ],
                select: {
                    style: "multi"
                },
                order: [
                    [2, "desc"]
                ],
                displayLength: 10,
            });


            Table.delegate(".get_status", "click", function() {
                let affiliate_id = $(this).data('id');
                $.ajax({
                    url: "https://trackergenius.runterminal.com/admin/affiliate/active",
                    type: "POST",
                    data: {
                        id: affiliate_id,
                        _token: "xVg5vlGiMwfiDk04qzHQhHjes4QkM1yoxS7WeOHB"
                    },
                    success: function(data) {
                        showResponseMessage(data);
                    }
                });
            });


            // On Delete
            Table.delegate(".action-delete", "click", function(e) {
                e.stopPropagation();
                let id = $(this).data('id');
                Swal.fire({
                    title: "Are you sure?",
                    text: "You won't be able to revert this!",
                    icon: 'warning',
                    showCancelButton: true,
                    confirmButtonText: "Yes, delete it!",
                    customClass: {
                        confirmButton: 'btn btn-primary',
                        cancelButton: 'btn btn-outline-danger ms-1'
                    },
                    buttonsStyling: false,
                }).then(function(result) {
                    if (result.value) {
                        $.ajax({
                            url: "https://trackergenius.runterminal.com/admin/affiliate" + '/' + id,
                            type: "POST",
                            data: {
                                _method: 'DELETE',
                                _token: "xVg5vlGiMwfiDk04qzHQhHjes4QkM1yoxS7WeOHB"
                            },
                            success: function(data) {
                                showResponseMessage(data);
                            },
                            error: function(reject) {
                                if (reject.status === 422) {
                                    let errors = reject.responseJSON.errors;
                                    $.each(errors, function(key, value) {
                                        toastr['warning'](value[0],
                                            "locale.labels.attention", {
                                                closeButton: true,
                                                positionClass: 'toast-top-right',
                                                progressBar: true,
                                                newestOnTop: true,
                                                rtl: isRtl
                                            });
                                    });
                                } else {
                                    toastr['warning'](reject.responseJSON.message,
                                        "locale.labels.attention", {
                                            positionClass: 'toast-top-right',
                                            containerId: 'toast-top-right',
                                            progressBar: true,
                                            closeButton: true,
                                            newestOnTop: true
                                        });
                                }
                            }
                        })
                    }
                })
            });


            //Bulk Enable
            $(".bulk-enable").on('click', function(e) {
                e.preventDefault();
                Swal.fire({
                    title: "Are you sure?",
                    text: "You want to approved selected affiliate!",
                    icon: 'warning',
                    showCancelButton: true,
                    confirmButtonText: "Yes, approved it!",
                    customClass: {
                        confirmButton: 'btn btn-primary',
                        cancelButton: 'btn btn-outline-danger ms-1'
                    },
                    buttonsStyling: false,
                }).then(function(result) {
                    if (result.value) {
                        let affiliate_ids = [];
                        let rows_selected = dataListView.column(1).checkboxes.selected();
                        $.each(rows_selected, function(index, rowId) {
                            affiliate_ids.push(rowId)
                        });

                        if (affiliate_ids.length > 0) {

                            $.ajax({
                                url: "https://trackergenius.runterminal.com/admin/affiliate/bulk-action",
                                type: "POST",
                                data: {
                                    _token: "xVg5vlGiMwfiDk04qzHQhHjes4QkM1yoxS7WeOHB",
                                    action: 'Approved',
                                    ids: affiliate_ids
                                },
                                success: function(data) {
                                    showResponseMessage(data);
                                },
                                error: function(reject) {
                                    if (reject.status === 422) {
                                        let errors = reject.responseJSON.errors;
                                        $.each(errors, function(key, value) {
                                            toastr['warning'](value[0],
                                                "locale.labels.attention", {
                                                    closeButton: true,
                                                    positionClass: 'toast-top-right',
                                                    progressBar: true,
                                                    newestOnTop: true,
                                                    rtl: isRtl
                                                });
                                        });
                                    } else {
                                        toastr['warning'](reject.responseJSON.message,
                                            "locale.labels.attention", {
                                                closeButton: true,
                                                positionClass: 'toast-top-right',
                                                progressBar: true,
                                                newestOnTop: true,
                                                rtl: isRtl
                                            });
                                    }
                                }
                            })
                        } else {
                            toastr['warning']("locale.labels.at_least_one_data",
                                "locale.labels.attention", {
                                    closeButton: true,
                                    positionClass: 'toast-top-right',
                                    progressBar: true,
                                    newestOnTop: true,
                                    rtl: isRtl
                                });
                        }
                    }
                })
            });

            //Bulk disable
            $(".bulk-disable").on('click', function(e) {

                e.preventDefault();

                Swal.fire({
                    title: "Are you sure?",
                    text: "You want to disable selected affiliate!",
                    icon: 'warning',
                    showCancelButton: true,
                    confirmButtonText: "Yes, disable it!",
                    customClass: {
                        confirmButton: 'btn btn-primary',
                        cancelButton: 'btn btn-outline-danger ms-1'
                    },
                    buttonsStyling: false,
                }).then(function(result) {
                    if (result.value) {
                        let affiliate_ids = [];
                        let rows_selected = dataListView.column(1).checkboxes.selected();

                        $.each(rows_selected, function(index, rowId) {
                            affiliate_ids.push(rowId)
                        });

                        if (affiliate_ids.length > 0) {

                            $.ajax({
                                url: "https://trackergenius.runterminal.com/admin/affiliate/bulk-action",
                                type: "POST",
                                data: {
                                    _token: "xVg5vlGiMwfiDk04qzHQhHjes4QkM1yoxS7WeOHB",
                                    action: 'Suspend',
                                    ids: affiliate_ids
                                },
                                success: function(data) {
                                    showResponseMessage(data);
                                },
                                error: function(reject) {
                                    if (reject.status === 422) {
                                        let errors = reject.responseJSON.errors;
                                        $.each(errors, function(key, value) {
                                            toastr['warning'](value[0],
                                                "locale.labels.attention", {
                                                    closeButton: true,
                                                    positionClass: 'toast-top-right',
                                                    progressBar: true,
                                                    newestOnTop: true,
                                                    rtl: isRtl
                                                });
                                        });
                                    } else {
                                        toastr['warning'](reject.responseJSON.message,
                                            "locale.labels.attention", {
                                                closeButton: true,
                                                positionClass: 'toast-top-right',
                                                progressBar: true,
                                                newestOnTop: true,
                                                rtl: isRtl
                                            });
                                    }
                                }
                            })
                        } else {
                            toastr['warning']("locale.labels.at_least_one_data",
                                "locale.labels.attention", {
                                    closeButton: true,
                                    positionClass: 'toast-top-right',
                                    progressBar: true,
                                    newestOnTop: true,
                                    rtl: isRtl
                                });
                        }

                    }
                })
            });


            $(".bulk-delete").on('click', function(e) {
                e.preventDefault();
                Swal.fire({
                    title: "Are you sure?",
                    text: "You want to delete selected affiliate!",
                    icon: 'warning',
                    showCancelButton: true,
                    confirmButtonText: "Yes, delete it!",
                    customClass: {
                        confirmButton: 'btn btn-primary',
                        cancelButton: 'btn btn-outline-danger ms-1'
                    },
                    buttonsStyling: false,
                }).then(function(result) {
                    if (result.value) {
                        let administrator_ids = [];
                        let rows_selected = dataListView.column(1).checkboxes.selected();

                        $.each(rows_selected, function(index, rowId) {
                            administrator_ids.push(rowId)
                        });

                        if (administrator_ids.length > 0) {

                            $.ajax({
                                url: "https://trackergenius.runterminal.com/admin/affiliate/bulk-action",
                                type: "POST",
                                data: {
                                    _token: "xVg5vlGiMwfiDk04qzHQhHjes4QkM1yoxS7WeOHB",
                                    action: 'Delete',
                                    ids: administrator_ids
                                },
                                success: function(data) {
                                    showResponseMessage(data);
                                },
                                error: function(reject) {
                                    if (reject.status === 422) {
                                        let errors = reject.responseJSON.errors;
                                        $.each(errors, function(key, value) {
                                            toastr['warning'](value[0],
                                                "locale.labels.attention", {
                                                    closeButton: true,
                                                    positionClass: 'toast-top-right',
                                                    progressBar: true,
                                                    newestOnTop: true,
                                                    rtl: isRtl
                                                });
                                        });
                                    } else {
                                        toastr['warning'](reject.responseJSON.message,
                                            "locale.labels.attention", {
                                                closeButton: true,
                                                positionClass: 'toast-top-right',
                                                progressBar: true,
                                                newestOnTop: true,
                                                rtl: isRtl
                                            });
                                    }
                                }
                            })
                        } else {
                            toastr['warning']("locale.labels.at_least_one_data",
                                "locale.labels.attention", {
                                    closeButton: true,
                                    positionClass: 'toast-top-right',
                                    progressBar: true,
                                    newestOnTop: true,
                                    rtl: isRtl
                                });
                        }

                    }
                })
            });

            // Select2 basic
            $(".select2").each(function() {
                let $this = $(this);
                $this.wrap('<div class="position-relative"></div>');
                $this.select2({
                    // the following code is used to disable x-scrollbar when click in select input and
                    // take 100% width in responsive also
                    dropdownAutoWidth: true,
                    width: '100%',
                    dropdownParent: $this.parent(),
                    placeholder: 'Select Manager'
                });
            });

            // Select2 multiple
            $(".select2-multiple").each(function() {
                let $this = $(this);
                $this.wrap('<div class="position-relative"></div>');
                $this.select2({
                    // the following code is used to disable x-scrollbar when click in select input and
                    // take 100% width in responsive also
                    dropdownAutoWidth: true,
                    width: '100%',
                    dropdownParent: $this.parent(),
                    placeholder: 'Select Manager'
                });
            });

            // filter
        });
    


        $(document).ready(function () {
            $("#btnExport").click(function () {
                let table = document.getElementsByTagName("table");
                console.log(table);
                debugger;
                TableToExcel.convert(table[0], {
                    name: `UserManagement.xlsx`,
                    sheet: {
                        name: 'Usermanagement'
                    }
                });
            });
        });
    