//------------------------------------------------------------------------------
// PPM - Hauptfunktion
//------------------------------------------------------------------------------

class PPM
{
    constructor ()
    {
        this.BodyDataAction();

        this.TemplateManager_o = new TemplateManager_cl();

        console.log("PPM LOADED");
    }

    BodyDataAction ()
    {
        $('body').on('click', '[data-action]', function() {
            var action = $(this).data('action');
            
            PPM_o.Menu(action);
            return false;
        });
    }

    BodyTableSelect ()
    {
        $('#idList tr').click(function(){
            $(".selected").removeClass('selected');
            $(this).addClass('selected');
            this.selectedId = $(this).attr('id');
            console.log("SELECT ID: ", this.selectedId);
            return false;
        });
    }

    Menu (message)
    {
        var selectedId = $(".selected").attr('id');
        switch (message)
        {
            case 'index':

                this.LoadContent('/category/?cat=Index&view=List', 'index.tpl');
                break;
            
            case 'categoryStudenten':

                this.LoadContent('/category/?cat=Studenten&view=List', 'list.tpl');
                break
            
            case 'categoryLehrender':

                this.LoadContent('/category/?cat=Lehrender&view=List', 'list.tpl');
                break
            
            case 'categoryFirmen':

                this.LoadContent('/category/?cat=Firmen&view=List', 'list.tpl');
                break

            case 'categoryAngebote':

                this.LoadContent('/category/?cat=Angebote&view=List', 'list.tpl');
                break
            
            case 'choice':

                this.LoadContent('/choice/?view=List', 'listChoice.tpl');
                break
            
            case 'evalStudenten':

                this.LoadContent('/eval/?cat=Studenten', 'listEval.tpl');
                break
            
            case 'evalLehrender':

                this.LoadContent('/eval/?cat=Lehrender', 'listEval.tpl');
                break
            
            case 'evalFirmen':

                this.LoadContent('/eval/?cat=Firmen', 'listEval.tpl');
                break
            
            case 'newStudenten':

                console.log("NewStudenten");
                this.LoadContent('/category/?cat=Studenten&view=Detail', 'detail.tpl');
                break

            case 'newLehrender':

                console.log("NewLehrender");
                this.LoadContent('/category/?cat=Lehrender&view=Detail', 'detail.tpl');
                break

            case 'newFirmen':

                console.log("NewFirmen");
                this.LoadContent('/category/?cat=Firmen&view=Detail', 'detail.tpl');
                break

            case 'newAngebote':

                console.log("NewAngebote");
                this.LoadContent('/category/?cat=Angebote&view=Detail', 'detail.tpl');
                break

            case 'modifyStudenten':
                console.log("modifyStudenten: ", selectedId);
                this.LoadContent('/category/?cat=Studenten&view=Detail&id='+ selectedId, 'detail.tpl');
                break

            case 'modifyLehrender':

                console.log("modifyLehrender", selectedId);
                this.LoadContent('/category/?cat=Lehrender&view=Detail&id='+ selectedId, 'detail.tpl');
                break

            case 'modifyFirmen':

                console.log("modifyFirmen", selectedId);
                this.LoadContent('/category/?cat=Firmen&view=Detail&id='+ selectedId, 'detail.tpl');
                break

            case 'modifyAngebote':

                console.log("modifyAngebote", selectedId);
                this.LoadContent('/category/?cat=Angebote&view=Detail&id='+ selectedId, 'detail.tpl');
                break

            case 'choiceAnnehmen':

                console.log("choiceAnnehmen", selectedId);
                this.LoadContent('/choice/?view=Detail&id='+ selectedId, 'detailChoice.tpl');
                break

            case 'choiceNeu':

                console.log("choiceNeu");
                this.LoadContent('/choice/?view=Detail', 'detailChoice.tpl');
                break

            case 'deleteStudenten':

                console.log("deleteStudenten", selectedId);
                if (confirm("Soll der Eintrag wirklich geloescht werden?")) {
                    console.log("DELETE");
                    this.DeleteContent('/category/?cat=Studenten&id='+selectedId, 'categoryStudenten');
                }
                break

            case 'deleteLehrender':

                console.log("deleteLehrender", selectedId);
                if (confirm("Soll der Eintrag wirklich geloescht werden?")) {
                    console.log("DELETE");
                    this.DeleteContent('/category/?cat=Lehrender&id='+selectedId, 'categoryLehrender');
                }
                break

            case 'deleteFirmen':

                console.log("DeleteFirmen", selectedId);
                if (confirm("Soll der Eintrag wirklich geloescht werden?")) {
                    console.log("DELETE");
                    this.DeleteContent('/category/?cat=Firmen&id='+selectedId, 'categoryFirmen');
                }
                break
            
            case 'saveCategoryStudenten':

                var data = $("#idForm").serialize();
                this.SaveContent('/category/?cat=Studenten&'+data, 'categoryStudenten');
                break

            case 'saveCategoryLehrende':

                var data = $("#idForm").serialize();
                this.SaveContent('/category/?cat=Lehrender&'+data, 'categoryLehrender');
                break

            case 'saveCategoryFirmen':

                var data = $("#idForm").serialize();
                this.SaveContent('/category/?cat=Firmen&'+data, 'categoryFirmen');
                break

            case 'saveCategoryAngebote':

                var data = $("#idForm").serialize();
                this.SaveContent('/category/?cat=Angebote&'+data, 'categoryAngebote');
                break

            case 'saveChoice':

                var data = $("#idForm").serialize();
                this.SaveContent('/choice/?'+data, 'choice');
                break

            default:

                alert ('[PPM] Unbekannte Anfrage: ' + message);
        }
    }

    LoadContent ( path, template )
    {
        $.ajax({
            dataType: "json",
            url: path,
            type: 'GET',
            context: this
        })
        .done(function (json) {
            //console.log("JSON: ", json);

            var result = this.TemplateManager_o.execute_px( template, json );
            //console.log("RESULT: ", result);

            //$( ".clContent" ).replaceWith(result);
            $( ".clContent" ).empty();
            $( ".clContent" ).append(result);
            this.BodyTableSelect();
            console.log("TM DONE");              
        })
        .fail(function(jqXHR_opl, textStatus) {
            alert( "[PPM] Fehler bei Anforderung: " + textStatus );
        });
    }

    SaveContent( path, menu )
    {
        $.ajax({
            dataType: "json",
            url: path,
            type: 'POST',
            context: this
        })
        .done(function (json) {
            this.Menu(menu);         
        })
        .fail(function(jqXHR_opl, textStatus) {
            alert( "[PPM] Fehler bei Anforderung: " + textStatus );
        });   
    }

    DeleteContent( path, menu )
    {
        $.ajax({
            dataType: "json",
            url: path,
            type: 'DELETE',
            context: this
        })
        .done(function (json) {
            this.Menu(menu);         
        })
        .fail(function(jqXHR_opl, textStatus) {
            alert( "[PPM] Fehler bei Anforderung: " + textStatus );
        });   
    }
}


$(function() {
    console.log("ONLOAD");
    PPM_o = new PPM();
    PPM_o.Menu("index");
});