﻿Type.registerNamespace("PalmspringControls");

PalmspringControls.GridControl = function() {
    ///<Summary>
    ///</Summary>
    this.ServicePath = 0;
    this.TotalCount = 0;
    this.PageSize = 10;
    this.CurrentPage = 1;
    this.GridControlId = '';
    this.GridTemplateId = '';
    this.PagerControlId = '';
    this.PagerTemplateId = '';
    PalmspringControls.GridControl.ObjectArgument.apply(this, arguments);
}

PalmspringControls.GridControl.ObjectArgument = function(options) {
    this.TotalCount = 0;
    this.CurrentPage = 1;
    this.PageSize = options.PageSize;
    this.ServicePath = options.ServicePath;
    this.ServicePathCount = options.ServicePathCount;
    this.GridControlId = options.GridControlId;
    this.GridTemplatePath = options.GridTemplatePath;
    this.PagerTemplatePath = options.PagerTemplatePath;
    this.PagerDescTemplatePath = options.PagerDescTemplatePath;
    this.PagerSizeDescTemplatePath = options.PagerSizeDescTemplatePath;
    this.NoDataTemplatePath = options.NoDataTemplate;
    this.SortColumn = options.SortColumn;
    this.LoadingOpts = options.LoadingOpts;
    this.Query = options.Query;
}

PalmspringControls.GridControl.prototype = {
    initialize: function() {
        //var $GridControl = $("#" + this.GridControlId);

        this.GridTemplate = this.ReadTemplate(this.GridTemplatePath);
        this.PagerTemplate = this.ReadTemplate(this.PagerTemplatePath);
        this.PagerSizeTemplate = this.ReadTemplate(this.PagerSizeDescTemplatePath);
        this.PagerDescTemplate = this.ReadTemplate(this.PagerDescTemplatePath);
        this.NoDataTemplate = this.ReadTemplate(this.NoDataTemplatePath);
        this.$Grid = $("#" + this.GridControlId);
        this.LoadData(1, true);


    },
    LoadData: function(page, initialize) {
        var grid = this;
        //        var template = $("#" + this.GridTemplateId).html();
        if (IsEmpty(page))
            page = 1;
        var take = this.PageSize;
        var skip = (page - 1) * take;
        var res = "";
        var now = new Date();

        now = now.format("yyyy-MM-dd");
        dtStDate = new Date();
        var dd = dtStDate.getDate();
        var mm = dtStDate.getMonth();
        var yy = dtStDate.getFullYear() + 1;
        var dtEndDate = yy + "-" + mm + "-" + dd;

        var scheduleQuery = "schedule <> " + now + ".." + dtEndDate;

        var postURL = this.ServicePathCount;
        $.ajax({
            cache: false,
            async: false,
            type: "GET",
            contentType: "application/json; charset=utf-8",
            dataType: 'json',
            url: postURL,
            timeout: 10000,
            success: function(msg) {
                grid.TotalCount = msg.d
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert('load postschema: ' + textStatus);
            }
        });

        var postURL = this.ServicePath + "&SkipCount=" + skip + "&TakeCount=" + take + "&OrderByClause=" + this.SortColumn;
        if (!IsEmpty(this.LoadingOpts))
            if (!IsEmpty(this.LoadingOpts.loadstart))
            this.LoadingOpts.loadstart.call(this.$Grid);
        $.ajax({
            cache: false,
            async: false,
            type: "GET",
            contentType: "application/json; charset=utf-8",
            dataType: 'json',
            url: postURL,
            timeout: 10000,
            success: function(msg) {
                res = msg.d;
                if (IsEmpty(res) || res.length == 0) {
                    grid.ClearGrid();
                    $(grid.NoDataTemplate).bindTo({}, { appendTo: $("#ulGrid", grid.$Grid), onBound: grid.formatData });
                } else {
                    $("#ulGrid", grid.$Grid).text('');
                    $(grid.GridTemplate).bindTo(res, { appendTo: $("#ulGrid", grid.$Grid), onBound: grid.formatData });

                    if (initialize) {
                        grid.BindPager();
                        grid.BindPagerDescription(grid.TotalCount, grid.PageSize, page);
                    }
                }
                //                for (var i = 1; i < 1000; i++) {
                ////                    if (i % 100 == 0)
                ////                        grid.$Grid.append("<label>" + i + "</label>");
                //                    //nothing to do;
                //                }
                //                // alert('1');
                //                                while (IsEmpty(grid.$Grid.data('loading'))) {
                //                                    //wait;
                //                                    var t = "vandana";
                //                                }

                if (!IsEmpty(grid.LoadingOpts))
                    if (!IsEmpty(grid.LoadingOpts.loadend))
                        grid.LoadingOpts.loadend.call(grid.$Grid);
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert('load postschema: ' + textStatus);
                grid.$Grid.loading(false);
            }
        });

        if (initialize) {
            //Bind PageSizer
            ps = new PalmspringControls.PageSize({
                PageSize: this.PageSize,
                GridControlId: this.GridControlId,
                Template: this.PagerSizeTemplate
            });
            ps.OnPageSizeChange = function(NewSize) {
                grid.PageSizeChange.call(grid, NewSize);
            }

            ps.initialize();
            this.PageSizeCtrl = ps;
        }
    },
    BindPager: function() {
      var  grid = this;
        var pager = new PalmspringControls.Pager({
            TotalCount: grid.TotalCount,
            PageSize: grid.PageSize,
            CurrentPage: 1,
            GridControlId: grid.GridControlId,
            Template: grid.PagerTemplate
        });
        pager.OnPageChange = function(NewPage) {
            grid.PageChange.call(grid, NewPage);
        }

        pager.initialize();
        grid.Pager = pager;


    },
    BindPagerDescription: function(totalCount, pageSize, currentPage) {
        var toVal = (parseInt((currentPage - 1) * pageSize) + parseInt(pageSize)) > totalCount ? totalCount : parseInt((currentPage - 1) * pageSize) + parseInt(pageSize);
        var Desc = { From: (currentPage - 1) * pageSize + 1, To: toVal, Total: totalCount };
        var template = this.PagerDescTemplate;
        $("#divPagerDesc", this.$Grid).text('');
        $(template).bindTo(Desc, { appendTo: $("#divPagerDesc", this.$Grid) });

    },
    PageChange: function(CurrentPage) {
        this.LoadData(CurrentPage);

        this.BindPagerDescription(this.TotalCount, this.PageSize, CurrentPage);
        return false;
    },
    PageSizeChange: function(NewSize) {
        this.PageSize = NewSize;
        this.CurrentPage = 1;
        if (this.Pager)
            this.Pager.dispose();
        this.LoadData(1);
        this.BindPager();
        this.BindPagerDescription(this.TotalCount, this.PageSize, this.CurrentPage);
        return false;
    },
    SortBy: function(param) {
        this.SortColumn = param;
        this.CurrentPage = 1;
        this.StartPage = 1;
        if (this.Pager)
            this.Pager.dispose();
        this.LoadData(this.CurrentPage);
        this.BindPager();
        this.BindPagerDescription(this.TotalCount, this.PageSize, this.CurrentPage);

    },
    ReadTemplate: function(path) {
        var res = null;
        $.ajax({
            type: "GET",
            url: path,
            async: false,
            dataType: "text/html",
            async: false,
            success: function(data) {
                res = data;
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
            }
        });      //close $.ajax(
        return res;
    },
    ClearGrid: function() {
        var grid = this;
        $("#ulGrid", grid.$Grid).text('');
        $("#divPagerDesc", grid.$Grid).text('');
        $("#pager", grid.$Grid).text('');
        //  $("", grid.$Grid)
    }

}

PalmspringControls.GridControl.registerClass("PalmspringControls.GridControl", null, Sys.IDisposable);
