var accounting = {
    settings: {
        dashboardUrl: ''
    },
    init: function() {
        accounting.dashboardContents();
    },
    dashboardContents: function() {
        if(jQ("#dashboard-counter").length > 0) {
            global.ajax({
                url: accounting.settings.dashboardUrl,
                type: 'GET',
                success: function(json) {
                    jQ('#dashboard-pending-encode-list').empty();
                    jQ('#dashboard-pending-audit-list').empty();

                    jQ('#invoice-dashboard').empty();
                    jQ('#payment-dashboard').empty();
                    jQ('#refund-dashboard').empty();

                    accounting.listPendingEncodeDashboard(json);
                    accounting.listPendingAuditDashboard(json);
                    accounting.accountingBilling(json);
                }
            }, 'dashboardCount');
        }
    },
    listPendingEncodeDashboard: function(json){
        jQ('#dashboard-pending-encode-list').removeClass('has-loading');
        accounting.animateCount(jQ("#encoding-ctr"),json.overview.encoded.length);
        for(let i =0; i < json.overview.encoded.length; i++){
            let pending_encode = json.overview.encoded[i];
            let li = jQ('<li>').attr({
                    onclick: `window.open('${global.settings.url}/main/accounting/encoding/form/u/${pending_encode.idEncoded}', '_blank')`
                }),
                head = jQ('<div>').attr({
                    class: "list-head",
                }),
                headTitle = jQ('<span>').attr({
                    class: 'list-head-title'
                }).html(pending_encode.id),
                headSub = jQ('<span>').attr({
                    class: "list-head-subtitle",
                    title: "Accounting File ID",
                }).html(' ' + pending_encode.accounting_file_id),
                body = jQ('<div>').attr({
                    class: "list-body",
                    title: "",
                }).html(pending_encode.file_desc);
            headTitle.appendTo(head);
            headSub.appendTo(head);
            head.appendTo(li);
            body.appendTo(li);
            jQ('#dashboard-pending-encode-list').append(li);
        }
        if(json.overview.encoded.length <= 0) jQ('#dashboard-pending-encode-list').html("<li><small style='color:grey; margin-left: 6px'>No pending encode.</small></li>")


    },
    listPendingAuditDashboard: function(json){
        jQ('#dashboard-pending-audit-list').removeClass('has-loading');
        accounting.animateCount(jQ("#audit-ctr"),json.overview.check.length);
        for(let i =0; i < json.overview.check.length; i++){
            let pending_check = json.overview.check[i];
            let li = jQ('<li>').attr({
                    onclick: `window.open('${global.settings.url}/main/accounting/encoding/form/u/${pending_check.idEncoded}', '_blank')`
                }),
                head = jQ('<div>').attr({
                    class: "list-head",
                }),
                headTitle = jQ('<span>').attr({
                    class: 'list-head-title'
                }).html(pending_check.id),
                headSub = jQ('<span>').attr({
                    class: "list-head-subtitle",
                    title: "Accounting File ID",
                }).html(' ' + pending_check.accounting_file_id),
                body = jQ('<div>').attr({
                    class: "list-body",
                    title: "",
                }).html(pending_check.file_desc);
            headTitle.appendTo(head);
            headSub.appendTo(head);
            head.appendTo(li);
            body.appendTo(li);
            jQ('#dashboard-pending-audit-list').append(li);
        }
        if(json.overview.check.length <= 0) jQ('#dashboard-pending-audit-list').html("<li><small style='color:grey; margin-left: 6px'>No pending audit.</small></li>")
    },
    accountingBilling: function(json) {
        jQ('#invoice-dashboard-data-container').removeClass('has-loading');
        jQ('#invoice-dashboard-data-loading').removeClass('hide');

        jQ('#payment-dashboard-data-container').removeClass('has-loading');
        jQ('#payment-dashboard-data-loading').removeClass('hide');

        jQ('#refund-dashboard-data-container').removeClass('has-loading');
        jQ('#refund-dashboard-data-loading').removeClass('hide');

        jQ('.invoice-dashboard').append('  <ul>\n' +
            '                                <div>Pending Encode : '+json.accounting_billing.invoice.pendingInvoiceEncode+'</div>\n' +
            '                                <div>Pending Audit : '+json.accounting_billing.invoice.pendingInvoiceAudit+'</div>\n' +
            '                            </ul>');

        jQ('.payment-dashboard').append('  <ul>\n' +
            '                                <div>Pending Encode : '+json.accounting_billing.payment.pendingInvoiceEncode+'</div>\n' +
            '                                <div>Pending Audit : '+json.accounting_billing.payment.pendingInvoiceAudit+'</div>\n' +
            '                            </ul>');

        jQ('.refund-dashboard').append('  <ul>\n' +
            '                                <div>Pending Encode : '+json.accounting_billing.refund.pendingInvoiceEncode+'</div>\n' +
            '                                <div>Pending Audit : '+json.accounting_billing.refund.pendingInvoiceAudit+'</div>\n' +
            '                            </ul>');
    },
    animateCount: function(element, value){
        jQuery({ Counter: 0 }).animate({ Counter: value }, {
            duration: 1000,
            easing: 'swing',
            step: function () {
                element.text(Math.ceil(this.Counter));
            }
        });
    },

};