﻿function Crud(holderID, cntrlrUrl) {
    this.holderID = holderID;
    this.cnUrl = cntrlrUrl;
    this.view = 'browse';
    this.page = 0;
    this.sort = 0;
    this.rpp = 10;
    this.fltr = {};
    this.masterFltr = {};
    this.fltrFormID = null;
    this.editFormOptions = {
        url: cntrlrUrl + 'SubmitEdit',
        type: 'POST'
    };
    this.deleteFormOptions = {
        url: cntrlrUrl + 'SubmitDelete',
        type: 'POST'
    };
    this.beforeChangePage = null;
    this.beforeSetSort = null;
    this.browseAction = 'Browse';
    this.onChahgeState = function (event) {
        this.fltr = event.parameters;
        if (this.fltr.view) this.view = this.fltr.view;
        if (this.fltr.page) this.view = this.fltr.page;
        if (this.fltr.sort) this.view = this.fltr.sort;
        if (this.fltr.rpp) this.view = this.fltr.rpp;
    }
   // jQuery.appstate(this.onChahgeState)
}

Crud.prototype.renewList = function() {
    if (this.beforeChangePage)
        this.beforeChangePage();
    var onLoad = this.onListLoaded ? this.onListLoaded : null;
    this.setView(this.browseAction, onLoad)
    return false;
}

Crud.prototype.setPage = function(page) {
    this.page = page;
    this.renewList();
    return false;
}

Crud.prototype.setSort = function(sort) {
    if (this.beforeSetSort)
        this.beforeSetSort(sort);
    this.sort = sort;
    this.renewList();
    return false;
}

Crud.prototype.setRPP = function(rpp) {
    this.rpp = rpp;
    this.renewList();
    return false;
}

Crud.prototype.setView = function (view, callback) {
    this.view = view;
    var prm = !this.fltr ? {} : this.fltr;
    prm.page = this.page;
    prm.sort = this.sort;
    prm.rpp = this.rpp;

    if (this.masterFltr)
        for (var i in this.masterFltr)
            prm[i] = this.masterFltr[i];

    jQuery('#' + this.holderID).load(this.cnUrl + view, prm, callback)
    return false;
}

Crud.prototype.getFilterValues = function (form) {
    var qry = jQuery(form).formToArray();
    var prm = {};
    if (qry.length && qry.length > 0) {
        for (var i = 0; i < qry.length; i++) {
            if (qry[i].name)
                prm[qry[i].name] = qry[i].value;
        }
    }
    this.fltr = prm;
}

Crud.prototype.applyFilter = function (form) {
    this.getFilterValues(form);
    this.renewList();
    return false;
}

Crud.prototype.clearFilter = function() {
    if (this.fltrFormID && this.fltrFormID.length > 0)
        jQuery('#' + this.fltrFormID).resetForm();
    this.fltr = {};
    this.renewList();
    return false;
}

Crud.prototype.edit = function (id) {
    var onLoad = this.onEditLoaded ? this.onEditLoaded : null;
    this.fltr.id = id;
    this.setView('edit', function (resp) {
        if (onLoad) onLoad(id, resp);
    });

}

Crud.prototype.submitEdit = function(form) {
    var onLoad = this.onEditLoaded ? this.onEditLoaded : null;
    if (this.onSubmitEdit) this.onSubmitEdit();
    
    var jqf = jQuery(form);
    var th = this;
    var options = {
        url: th.cnUrl + 'SubmitEdit',
        success: function(resp) {
            if (resp == 'ok')
                th.renewList()
            else {
                jQuery('#' + th.holderID).html(resp);
                if (onLoad) onLoad(th.item_id);
            }
        }
    };
    jqf.attr('action', options.url);
    jqf.ajaxSubmit(options);
    return false;
}

Crud.prototype.del = function (id) {
    if (this.beforeChangePage)
        this.beforeChangePage();
    this.fltr.id = id;
    this.setView('delete');
}

Crud.prototype.submitDelete = function(form) {
    var jqf = jQuery(form);
    var th = this;
    var options = {
        url: this.cnUrl + 'SubmitDelete',
        success: function(resp) {
            if (resp == 'ok')
                th.renewList()
            else
                jQuery('#' + th.holderID).html(resp);
        }
    };
    jqf.attr('action', options.url);
    jqf.ajaxSubmit(options);
    return false;
}