var Reward = function () {};

/*
 * Add Event when the App loaded
 */
Reward.Load = function (parameter)
{
    this.LoadEvent();

    Reward.InitTabReward();
    Reward.InitTabGift();
    Reward.InitTabRewardUser();
};

/*
 * Add event
 */
Reward.LoadEvent = function ()
{
    Dashboard.AddEventAppMenu(Reward.Execute, "", "Reward");
    Dashboard.AddEventWindowsTool("Reward");
};

/***
 * Start the Event for Member 
 */
Reward.LoadMember = function () {

    Event.AddByClass("btnSendResponseReward", "click", Reward.ShowAddResponseReward);
    Event.AddById("btnClaim", "click", Reward.Claim);
};

/*
 * Execute a function
 */
Reward.Execute = function (e)
{
    //Call the function
    Dashboard.Execute(this, e, "Reward");
    return false;
};

/**
 * Tab des catgéogrie
 * @returns {undefined}
 */
Reward.InitTabReward = function () {
    //Catégory
    Event.AddById("btnAddReward", "click", () => {
        Reward.ShowAddReward("")
    });
    EntityGrid.Initialise('gdReward');
};

/**
 * Edit Reward
 * @param {type} RewardId
 * @returns {undefined}
 */
Reward.EditReward = function (RewardId) {
    Reward.ShowAddReward(RewardId);
};

/**
 * Dialogue d'ajout de catégorie
 */
Reward.ShowAddReward = function (RewardId) {

    Dialog.open('', {"title": Dashboard.GetCode("Reward.AddReward"),
        "app": "Reward",
        "class": "DialogAdminReward",
        "method": "AddReward",
        "params": RewardId
    });
};

/***
 * Rafraichit la tab des catégorie
 * @returns {undefined}
 */
Reward.ReloadReward = function (data) {
    let gdReward = Dom.GetById("gdReward");
    gdReward.parentNode.innerHTML = data.data;
    Reward.InitTabReward();
};

/***
 * Supprime une catégorie
 * @param {type} RewardId
 * @returns {undefined}
 */
Reward.DeleteReward = function (RewardId) {

    Animation.Confirm(Language.GetCode("Reward.ConfirmRemoveCategorie"), () => {

        let data = "Class=Reward&Methode=DeleteReward&App=Reward";
        data += "&RewardId=" + RewardId;

        Request.Post("Ajax.php", data).then(data => {

            data = JSON.parse(data);

            Reward.ReloadReward(data);
        });
    });
};

/***
 * Translate dialogue
 */
Reward.TranslateReward = function (RewardId) {

    Dialog.open('', {"title": Dashboard.GetCode("Reward.TranslateReward"),
        "app": "Lang",
        "class": "DialogAdminLang",
        "method": "Translate",
        "params": "Reward-RewardReward-" + RewardId,
        "type": "full",
    });
};

/**
 * Tab des catgéogrie
 * @returns {undefined}
 */
Reward.InitTabGift = function () {
    //Catégory
    Event.AddById("btnAddGift", "click", () => {
        Reward.ShowAddGift("")
    });
    EntityGrid.Initialise('gdGifts');
};

/**
 * Edit Reward
 * @param {type} RewardId
 * @returns {undefined}
 */
Reward.EditGift = function (GiftId) {
    Reward.ShowAddGift(GiftId);
};

/**
 * Dialogue d'ajout de catégorie
 */
Reward.ShowAddGift = function (GiftId) {

    Dialog.open('', {"title": Dashboard.GetCode("Reward.AddGift"),
        "app": "Reward",
        "class": "DialogAdminReward",
        "method": "AddGift",
        "params": GiftId
    });
};

/***
 * Rafraichit la tab des catégorie
 * @returns {undefined}
 */
Reward.ReloadGift = function (data) {
    let gdGift = Dom.GetById("gdGifts");
    gdGift.parentNode.innerHTML = data.data;
    Reward.InitTabGift();
};

/***
 * Supprime une catégorie
 * @param {type} RewardId
 * @returns {undefined}
 */
Reward.DeleteGift = function (RewardId) {

    Animation.Confirm(Language.GetCode("Reward.ConfirmRemoveGift"), () => {

        let data = "Class=Reward&Methode=DeleteReward&App=Reward";
        data += "&RewardId=" + RewardId;

        Request.Post("Ajax.php", data).then(data => {

            data = JSON.parse(data);

            Reward.ReloadGift(data);
        });
    });
};

/***
 * Translate dialogue
 */
Reward.TranslateReward = function (RewardId) {

    Dialog.open('', {"title": Dashboard.GetCode("Reward.TranslateReward"),
        "app": "Lang",
        "class": "DialogAdminLang",
        "method": "Translate",
        "params": "Reward-RewardReward-" + RewardId,
        "type": "full",
    });
};


/***
 * Add Response
 */
Reward.ShowAddResponseReward = function (e) {

    Dialog.open('', {"title": Dashboard.GetCode("Reward.AddUserResponse"),
        "app": "Reward",
        "class": "DialogReward",
        "method": "ShowAddResponseReward",
        "params": e.srcElement.id});
};

/****
 * Valid response
 */
Reward.ValidResponseUser = function (responseId) {

    Animation.Confirm(Language.GetCode("Reward.ConfirmValidResponse"), () => {

        let data = "Class=Reward&Methode=ValidResponseUser&App=Reward";
        data += "&ResponseUserId=" + responseId;

        Request.Post("Ajax.php", data).then(data => {

            data = JSON.parse(data);

            Reward.ReloadRewardUser(data);
        });
    });
};

/***
 * 
 */
Reward.InitTabRewardUser = function () {
    EntityGrid.Initialise('gdRewardUser');
};

/***
 * Rafraichit la tab des rewardUser
 */
Reward.ReloadRewardUser = function (data) {
    let gdReward = Dom.GetById("gdRewardUser");
    gdReward.parentNode.innerHTML = data.data;
    Reward.InitTabRewardUser();
};

/****
 * Reclamer une récompense
 * @returns {undefined}
 */
Reward.Claim = function(){
    
     Dialog.open('', {"title": Dashboard.GetCode("Reward.ClaimReward"),
        "app": "Reward",
        "class": "DialogReward",
        "method": "Claim",
        "params": "" });
    
    
};


/***
 * Init the app front page
 */
Reward.Init = function (app, method) {
    if (app == "Reward") {
        switch (method) {
            default :
                break;
        }
    }
};

/***
 * Init the Admin Widget
 */
Reward.InitAdminWidget = function () {

};