$(document).ready(function () {
    // Datepicker
    $("input[name='start_date']").datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 3,
        format: "yyyy-mm-dd",
        onClose: function (selectedDate) {
            $("#start_date").datepicker("option", "minDate", selectedDate);
        }
    });

    $("input[name='end_date']").datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 3,
        format: "yyyy-mm-dd",
        onClose: function (selectedDate) {
            $("input[name='end_date']").datepicker("option", "minDate", selectedDate);
        }
    });

    setDefaultDate($('#oauth2-tokens-form'));
    setDefaultDate($('#oauth2-pull-jobs-form'));
    setDefaultDate($('#oauth2-pull-threads-form'));

    // Oauth2 Tokens
    function createOauth2TokensTable(result) {

        $('#oauth2-tokens-list tbody').empty();

        for (index in result) {
            let tr =
                `<tr>
                  <td>${result[index].email}</td>
                  <td>${result[index].name}</td>
                  <td>${result[index].provider}</td>
                  <td>${result[index].created}</td>
                  <td>${result[index].updated}</td>
                  <td>${result[index].expires_in}</td>
                  <td>${result[index].access_type}</td>
              </tr>`;

            $('#oauth2-tokens-list tbody').append(tr);
        }
    }
    // Detect pagination click
    $('#pagination-oauth2-tokens').on('click', 'a', function (e) {
        e.preventDefault();
        var pageno = $(this).attr('data-ci-pagination-page');
        loadOAuth2TokensRecord(pageno);
    });

    // Detect search click
    $('#search-oauth2-tokens').on('click', function (e) {
        setDefaultDate($(this).closest('form'));
        loadOAuth2TokensRecord(0);
    });

    // Prevent submit form by press the enter button
    $(window).keydown(function (event) {
        if (event.keyCode == 13) {
            loadPagination(0);
            event.preventDefault();
            return false;
        }
    });

    function loadOAuth2TokensRecord(pagno, btnSearch = null) {
        loadingButton(btnSearch);
        $.ajax({
            url: '/member/loadOAuth2TokensRecord?row_no=' + pagno,
            type: 'get',
            dataType: 'json',
            data: $('#oauth2-tokens-form').serialize(),
            success: function (response) {
                $('#pagination-oauth2-tokens').html(response.pagination);
                createOauth2TokensTable(response.result);
            },
            complete: function () {
                stopLoadingButton(btnSearch);
            }
        });
    }

    loadOAuth2TokensRecord(0);
    // Oauth2 Tokens

    // Oauth2 Pull Jobs
    function createOauth2PullJobsTable(result) {

        $('#oauth2-pull-jobs-list tbody').empty();

        for (index in result) {
            let tr =
                `<tr>
                  <td>${result[index].created}</td>
                  <td>${result[index].started}</td>
                  <td>${result[index].completed}</td>
              </tr>`;

            $('#oauth2-pull-jobs-list tbody').append(tr);
        }
    }
    // Detect pagination click
    $('#pagination-oauth2-pull-jobs').on('click', 'a', function (e) {
        e.preventDefault();
        var pageno = $(this).attr('data-ci-pagination-page');
        loadOAuth2PullJobsRecord(pageno);
    });

    // Detect search click
    $('#search-oauth2-pull-jobs').on('click', function (e) {
        setDefaultDate($(this).closest('form'));
        loadOAuth2PullJobsRecord(0);
    });

    function loadOAuth2PullJobsRecord(pagno, btnSearch = null) {
        loadingButton(btnSearch);
        $.ajax({
            url: '/member/loadOAuth2PullJobsRecord?row_no=' + pagno,
            type: 'get',
            dataType: 'json',
            data: $('#oauth2-pull-jobs-form').serialize(),
            success: function (response) {
                $('#pagination-oauth2-pull-jobs').html(response.pagination);
                createOauth2PullJobsTable(response.result);
            },
            complete: function () {
                stopLoadingButton(btnSearch);
            }
        });
    }

    loadOAuth2PullJobsRecord(0);
    // Oauth2 Pull Jobs

    // Oauth2 Pull Threads
    function createOauth2PullThreadsTable(result) {

        $('#oauth2-pull-threads-list tbody').empty();

        for (index in result) {
            let tr =
                `<tr>
                  <td>${result[index].created}</td>
                  <td>${result[index].started}</td>
                  <td>${result[index].completed}</td>
                  <td>${result[index].item_count}</td>
                  <td>${result[index].search_term}</td>
                  <td>${result[index].search_from}</td>
                  <td>${result[index].failed}</td>
              </tr>`;

            $('#oauth2-pull-threads-list tbody').append(tr);
        }
    }
    // Detect pagination click
    $('#pagination-oauth2-pull-threads').on('click', 'a', function (e) {
        e.preventDefault();
        var pageno = $(this).attr('data-ci-pagination-page');
        loadOAuth2PullThreadsRecord(pageno);
    });

    // Detect search click
    $('#search-oauth2-pull-threads').on('click', function (e) {
        setDefaultDate($(this).closest('form'));
        loadOAuth2PullThreadsRecord(0);
    });

    function loadOAuth2PullThreadsRecord(pagno, btnSearch = null) {
        loadingButton(btnSearch);
        $.ajax({
            url: '/member/loadOAuth2PullThreadsRecord?row_no=' + pagno,
            type: 'get',
            dataType: 'json',
            data: $('#oauth2-pull-threads-form').serialize(),
            success: function (response) {
                $('#pagination-oauth2-pull-threads').html(response.pagination);
                createOauth2PullThreadsTable(response.result);
            },
            complete: function () {
                stopLoadingButton(btnSearch);
            }
        });
    }

    loadOAuth2PullThreadsRecord(0);
    // Oauth2 Pull Threads
})
