$(document).ready(function () {
    loadDataTable();

    function cleanBtnFun() {
        if (confirm('確定刪除資料?')) {
            $.ajax({
                url: "/BackEndApi/clean_vote",
                type: "POST",
                data: null,
                success: function (data, textStatus, jqXHR) {
                    if (data.ret == "yes") {

                        $('#myTable').DataTable().ajax.reload();
                        alert('刪除成功');
                    } else {
                        alert(data.message);

                        if (data.err_code == "9999") {
                            location.href = "/BackEnd/Index";
                        }
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert('網路或伺服器發生錯誤，請稍後重試!');
                }
            });
        }
    }

    function loadDataTable() {
        var dataTables = {
            init: function init() {

                this.bindUIActions();
            },
            bindUIActions: function bindUIActions() {

                // event handlers
                this.table = this.handleDataTables();

                // add buttons
                this.table.buttons().container().appendTo('#dt-buttons').unwrap();
            },
            handleDataTables: function handleDataTables() {
                //$('#myTable').append("<tfoot><tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr></tfoot>");
                return $('#myTable').DataTable({
                    dom: '<\'text-muted\'Bif>\n        <\'table-responsive\'trl>\n        <\'mt-4\'p>',
                    lengthChange: true,
                    lengthMenu: [[25, 50, 100, -1], [25, 50, 100, "All"]],
                    pageLength: 100,
                    buttons: [
                        //{
                        //    text: '休假設定',
                        //    action: function (e, dt, node, config) {
                        //        vacationBtnFun();

                        //    }
                        //},
                        {
                            text: '清除10/10(含)之前紀錄',
                            action: function (e, dt, node, config) {
                                cleanBtnFun();

                            }
                        }
                    ],
                    language: {
                        paginate: {
                            previous: '<i class="fa fa-lg fa-angle-left"></i>',
                            next: '<i class="fa fa-lg fa-angle-right"></i>'
                        },
                        buttons: {
                            copyTitle: 'Data copied',
                            copyKeys: 'Use your keyboard or menu to select the copy command'
                        }
                    },
                    autoWidth: false,
                    ajax: {
                        url: '/BackEndApi/voted_list',
                        type: 'POST',
                        data: function (d) {
                            Object.assign(d, {
                                ticket_type: ''
                            });

                            return d;
                        },
                        dataSrc: 'data'
                    },
                    rowId: 'vote_sn',
                    deferRender: true,
                    initComplete: function () {
                        $('#myTable').on('click', 'a', function () {
                            buttonClick(this);
                        });

                        $('#myTable').on('click', 'input[name="selectedRow[]"]', function () {
                            checkboxClick(this);
                        });
                    },
                    order: [[1, 'desc']],
                    info: true,
                    search: "搜尋:",
                    searching: true,
                    columns: [{ data: 'dj_name', className: 'align-middle', orderable: false, searchable: true }, { data: 'vote_createdatestr', className: 'align-middle text-left', orderable: true, searchable: true },
                        { data: 'vote_line_uid', className: 'align-middle text-left', orderable: false, searchable: true }, { data: 'vote_point', className: 'align-middle text-right' },
                    { data: 'vote_token', className: 'align-middle text-left' }],
                    columnDefs: [
                        //{
                        //    targets: 0,
                        //    className: 'align-middle',
                        //    orderable: false,
                        //    searchable: false,
                        //    render: function render(data, type, row, meta) {

                        //        return "";
                        //        //var editRet = '<a id="table-btn" class="btn btn-sm btn-secondary" href="javascript: void(0);" data-method="edit" data-uid="' + row.quotation_serial + '"><i class="fa fa-pencil-alt"></i></a>          <a id="table-btn" class="btn btn-sm btn-secondary" href="javascript: void(0);" data-method="setting" data-uid="' + row.quotation_serial + '"><i class="fas fa-cog"></i></a>';

                        //        //if (row.quotationUser_perm == "A" || row.quotationUser_perm == "D" || row.quotationUser_perm == "admin" || row.quotationUser_perm == "pro") {
                        //        //    editRet = editRet + '          <a  id="table-btn" class="btn btn-sm btn-secondary" href="javascript: void(0);" data-method="del" data-uid="' + row.quotation_serial + '"><i class="far fa-trash-alt"></i></a>';
                        //        //}
                        //        //return editRet;
                        //    }
                        //}, {
                        //    targets: 1,
                        //    orderable: false,
                        //    searchable: false,
                        //    render: function render(data, type, row, meta) {
                        //        return '<a href="javascript: void(0); " data-method="edit" data-uid="' + row.company_uid + '">' + row.company_name + '</a>';
                        //    }
                        //}, {
                        //    targets: 2,
                        //    orderable: false,
                        //    searchable: false,
                        //    render: function render(data, type, row, meta) {
                        //        return row.company_serial;
                        //    }
                        //}, {
                        //    targets: 3,
                        //    orderable: false,
                        //    searchable: false,
                        //    render: function render(data, type, row, meta) {

                        //        if (row.company_enabled == "Y") {
                        //            return '<label class="switcher-control switcher-control-lg"><input type="checkbox" class="switcher-input" checked disabled> <span class="switcher-indicator"></span> <span class="switcher-label-on">啟用</span> <span class="switcher-label-off">停用</span></label>';
                        //        } else {
                        //            return '<label class="switcher-control switcher-control-lg"><input type="checkbox" class="switcher-input" disabled> <span class="switcher-indicator"></span> <span class="switcher-label-on">啟用</span> <span class="switcher-label-off">停用</span></label>';
                        //        }
                        //    }
                        //}, {
                        //    targets: 4,
                        //    orderable: false,
                        //    searchable: false,
                        //    render: function render(data, type, row, meta) {
                        //        return (new Date(row.company_createdate)).format("yyyy/MM/dd hh:mm:ss");
                        //    }
                        //}, {
                        //    targets: 5,
                        //    orderable: false,
                        //    searchable: false,
                        //    render: function render(data, type, row, meta) {
                        //        var editRet = '<a class="btn btn-sm btn-secondary" href="javascript: void(0);" data-method="edit" data-uid="' + row.company_uid + '" ><i class="fa fa-pencil-alt"></i></a>          <a  class="btn btn-sm btn-secondary" style="display: none;" href="javascript: void(0);" data-method="del" data-uid="' + row.company_uid + '" ><i class="far fa-trash-alt"></i></a>';


                        //        return editRet;
                        //    }
                        //}

                    ],

                });
            },
            handleSearchRecords: function handleSearchRecords() {
                var self = this;

                $('#table-search, #filterBy').on('keyup change focus', function (e) {
                    var filterBy = $('#filterBy').val();
                    var hasFilter = filterBy !== '';
                    var value = $('#table-search').val();

                    self.table.search('').columns().search('').draw();

                    if (hasFilter) {
                        self.table.columns(filterBy).search(value).draw();
                    } else {
                        self.table.search(value).draw();
                    }

                });
            }
        }

        dataTables.init();
    }
});