var DocumentPlugin = function () {};

DocumentPlugin.Init = function () {
    DocumentPluginDocumentForm.Init();
    DocumentPlugin.InitEvent();
};

DocumentPlugin.InitEvent = function(){
    Event.AddByClass("removeFile", "click", DocumentPlugin.DeleteFile);
};

DocumentPlugin.DeleteFile = function (e) {

    Animation.Confirm(Language.GetCode("DocumentPlugin.ConfirmRemoveFile"), function () {

        let fileId = e.srcElement.id;
        
         Plugin.SendRequest({"plugin": "DocumentPlugin",
            "method": "RemoveDocument",
            "fileId": fileId
        }, (data) => {

              let container = e.srcElement.parentNode.parentNode;
             container.parentNode.removeChild(container);
        });
    });
};

var DocumentPluginDocumentForm = function () {};


DocumentPluginDocumentForm.Init = function () {
    Event.AddById("btnSendDocumentPluginForm", "click", DocumentPluginDocumentForm.SendDocument);
};

DocumentPluginDocumentForm.SendDocument = function () {

    if (Form.IsValid("DocumentPluginDocumentFormForm"))
    {

        let data = Form.Serialize("DocumentPluginDocumentFormForm");

        Plugin.SendRequest({"plugin": "DocumentPlugin",
            "method": "SendDocument",
            "files": btoa(data),
            "appName": Form.GetValue("DocumentPluginDocumentFormForm", "AppName"),
            "entityName": Form.GetValue("DocumentPluginDocumentFormForm", "EntityName"),
            "entityId": Form.GetValue("DocumentPluginDocumentFormForm", "EntityId"),
        }, (data) => {

            let lstDocumentPlugin = Dom.GetById("lstDocumentPlugin");
            lstDocumentPlugin.innerHTML = data;
            DocumentPlugin.InitEvent();
        });
    }
};