define([], function () {
    var gs = {};
    gs.title = GetTextByKey("P_IPT_FUELLOG", 'Fuel Log');
    gs.description = GetTextByKey("P_IPT_FUELLOG", 'Fuel Log');
    gs.version = '1.0';
    gs.status = 0;
    var datacontent = null;
    var startdateinputcontrol = undefined;
    var enddateinputcontrol = undefined;
    gs.createContent = function () {
        var _this = this;
        var content = $('
');
        function createHeader() {
            var header = $('');
            header.append($('').text(gs.title));
            setPageTitle(gs.title, true);
            var search_bar = $('');
            header.append(search_bar);
            search_bar.append('');
            search_bar.append('' + GetTextByKey("P_IPT_BEGINDATE_COLON", "Begin Date:") + '');
            startdateinputcontrol = $('').val(begindate);
            search_bar.append($('').append(startdateinputcontrol));
            startdateinputcontrol.datetimepicker({
                timepicker: false,
                format: 'm/d/Y'
            });
            search_bar.append('' + GetTextByKey("P_IPT_ENDDATE_COLON", "End Date:") + '');
            enddateinputcontrol = $('').val(enddate);
            search_bar.append($('').append(enddateinputcontrol));
            enddateinputcontrol.datetimepicker({
                timepicker: false,
                format: 'm/d/Y'
            });
            var btnRefresh = $('');
            search_bar.append(btnRefresh);
            btnRefresh.click(function () {
                gs.refresh();
            });
            var func = $('');
            var iconRefresh = $('' + GetTextByKey("P_IPT_REFRESH", "Refresh") + '').click(function () {
                gs.refresh();
            });
            func.append(iconRefresh);
            var iconSettings = $('' + GetTextByKey("P_MAIN_SETTINGS", "Settings") + '').click(function () {
                gs.createSettings();
            });
            func.append(iconSettings);
            header.append(func)
            return header;
        }
        content.append(createHeader());
        var dataheader = $('');
        dataheader.append('');
        dataheader.append('' + GetTextByKey("P_IPT_DATE", "Date") + '
');
        dataheader.append('' + GetTextByKey("P_IPT_ASSETNAME", "Asset Name") + '
');
        dataheader.append('' + GetTextByKey("P_IPT_VIN", "VIN") + '
');
        dataheader.append('' + GetTextByKey("P_IPT_EMPLOYEENAME", "Employee Name") + '
');
        dataheader.append('' + GetTextByKey("P_IPT_CHECKEDIN", "Checked In") + '
');
        dataheader.append('' + GetTextByKey("P_IPT_CHECKEDOUT", "Checked Out") + '
');
        dataheader.append('');
        content.append(dataheader);
        datacontent = $('');
        content.append(datacontent);
        _this.refresh();
        return content;
    }
    gs.refresh = function () {
        var _this = this;
        datacontent.empty();
        var startydate = startdateinputcontrol.val();
        var enddate = enddateinputcontrol.val();
        var p = JSON.stringify([teamintelligence, startydate, enddate]);
        inspectionrequest("GetFuelReportItems", htmlencode(p), function (data) {
            datacontent.empty();
            if (typeof (data) === "string") {
                showAlert(data, GetTextByKey("P_IPT_ERROR", 'Error'));
                return;
            }
            if (data) {
                for (var i = 0; i < data.length; i++) {
                    _this.createFuelLog(data[i]);
                }
                //showFuelLogs(data);
            }
        }, function (err) {
        });
    }
    gs.createFuelLog = function (fuellog) {
        var _this = this;
        var content = $('');
        var holder = $('');
        content.append(holder);
        var btnfuleitem = $('
');
        btnfuleitem.click(function () {
            var icon = btnfuleitem.find('.spanbtn');
            if (icon.hasClass('iconangleright')) {
                icon.removeClass('iconangleright').addClass('iconangledown');
                if (fuellog.FuelReportItems)
                    _this.createFuelItem(content, fuellog.FuelReportItems);
            }
            else {
                icon.removeClass('iconangledown').addClass('iconangleright');
                btnfuleitem.parents().parents().children('.questionitem').remove();
            }
        });
        holder.append(btnfuleitem);
        var spantime = $('').text(GetTextByKey("P_IPT_DATE", "Date:") + fuellog.LocalCalendarDateStr);
        var spancount = $('').text("(" + fuellog.FuelReportCount + ")");
        holder.append($('').append(spantime).append(spancount));
        datacontent.append(content);
    }
    gs.createFuelItem = function (content, fuelitem) {
        var _this = this;
        for (var i = 0; i < fuelitem.length; i++) {
            var fuel = fuelitem[i];
            var holder = $('');
            var qholder = $('');//question holder
            holder.append(qholder);
            if (_this.index % 2 == 1)
                qholder.addClass('holder-even');
            qholder.append('
');
            var span = $('').text(fuel.LocalCalendarDateStr);
            qholder.append($('').append(span));
            span = $('').text(fuel.AssetName);
            qholder.append($('').append(span));
            span = $('').text(fuel.VIN);
            qholder.append($('').append(span));
            span = $('').text(fuel.EmployeeName);
            qholder.append($('').append(span));
            span = $('').text(fuel.CheckInTimeLocalStr);
            qholder.append($('').append(span));
            span = $('').text(fuel.CheckOutTimeLocalStr);
            qholder.append($('').append(span));
            holder.dblclick(fuel, function (e) {
                window.open("fuelreport.aspx?rid=" + e.data.Id + "&team=" + (teamintelligence ? 1 : 0), "_blank");
            });
            holder.find('.question-name span').click(fuel, function (e) {
                window.open("fuelreport.aspx?rid=" + e.data.Id + "&team=" + (teamintelligence ? 1 : 0), "_blank");
            });
            content.append(holder);
        }
    }
    gs.createSettings = function () {
        var loading = $('');
        loading.append('');
        function onSave(exit) {
            var alerttitle = GetTextByKey("P_IPT_FUELRPT_SETTING", 'Fuel Report Page Settings');
            loading.fadeIn(100);
            var headerLeft = $('#fuelrpt_headers_left').val();
            var headerMiddle = $('#fuelrpt_headers_middle').val();
            var headerRight = $('#fuelrpt_headers_right').val();
            var footerLeft = $('#fuelrpt_footers_left').val();
            var footerMiddle = $('#fuelrpt_footers_middle').val();
            var footerRight = $('#fuelrpt_footers_right').val();
            var item = {
                '$type': 'Foresight.Fleet.Services.Inspection.FuelReportHeaderFooterItem, FleetServiceClient',
                HeaderLeft: headerLeft,
                HeaderMiddle: headerMiddle,
                HeaderRight: headerRight,
                FooterLeft: footerLeft,
                FooterMiddle: footerMiddle,
                FooterRight: footerRight
            };
            var p = [false, htmlencode(JSON.stringify(item))];
            inspectionrequest('UpdateFuelReportHeaderFooter', JSON.stringify(p), function (r) {
                loading.hide();
                if (r == 'OK') {
                    showAlert(GetTextByKey("P_MV_SAVSUCCESSFULLY", 'Saved successfully.'), alerttitle, null, function () {
                        if (exit) {
                            showRightPopup(false);
                        }
                    });
                } else {
                    showAlert(GetTextByKey("P_IPT_FUELRPT_SAVEERROR", 'Failed to save fuel report page settings.'), alerttitle, null, function () {
                    });
                }
            }, 1000);
        }
        var content = $('');
        var funcs = $('');
        var btn = $('' + GetTextByKey("P_IPT_SAVE", "Save") + '').click(function () {
            onSave();
        });
        funcs.append(btn);
        btn = $('' + GetTextByKey("P_IPT_SAVE1", "Save and Exit") + '').click(function () {
            onSave(true);
        });
        funcs.append(btn);
        btn = $('' + GetTextByKey("P_IPT_SAVE2", "Exit Without Saving") + '').click(function () {
            showRightPopup(false);
        });
        funcs.append(btn);
        content.append(funcs);
        content.append($('').text(GetTextByKey('P_IPT_FUELRPT_HEADER', 'Page Headers')));
        var line = $('');
        var label = $('').text(GetTextByKey('P_IPT_FUELRPT_LEFT', 'Left'));
        line.append(label);
        var field = $('');
        line.append(field);
        content.append(line);
        line = $('');
        label = $('').text(GetTextByKey('P_IPT_FUELRPT_MIDDLE', 'Middle'));
        line.append(label);
        field = $('');
        line.append(field);
        content.append(line);
        line = $('');
        label = $('').text(GetTextByKey('P_IPT_FUELRPT_RIGHT', 'Right'));
        line.append(label);
        field = $('');
        line.append(field);
        content.append(line);
        content.append($('').text(GetTextByKey('P_IPT_FUELRPT_FOOTER', 'Page Footers')));
        line = $('');
        label = $('').text(GetTextByKey('P_IPT_FUELRPT_LEFT', 'Left'));
        line.append(label);
        field = $('');
        line.append(field);
        content.append(line);
        line = $('');
        label = $('').text(GetTextByKey('P_IPT_FUELRPT_MIDDLE', 'Middle'));
        line.append(label);
        field = $('');
        line.append(field);
        content.append(line);
        line = $('');
        label = $('').text(GetTextByKey('P_IPT_FUELRPT_RIGHT', 'Right'));
        line.append(label);
        field = $('');
        line.append(field);
        content.append(line);
        content.append(loading);
        $('#right_popup').empty().append(content);
        showRightPopup(true);
        loading.fadeIn(100);
        inspectionrequest('GetFuelReportHeaderFooter', 'false', function (r) {
            //console.log(r);
            gs.fuelRptSettings = r;
            content.find('#fuelrpt_headers_left').val(r.HeaderLeft);
            content.find('#fuelrpt_headers_middle').val(r.HeaderMiddle);
            content.find('#fuelrpt_headers_right').val(r.HeaderRight);
            content.find('#fuelrpt_footers_left').val(r.FooterLeft);
            content.find('#fuelrpt_footers_middle').val(r.FooterMiddle);
            content.find('#fuelrpt_footers_right').val(r.FooterRight);
            loading.fadeOut(100);
        });
    };
    return gs;
});