VaKeR CYBER ARMY
Logo of a company Server : Apache/2.4.41 (Ubuntu)
System : Linux absol.cf 5.4.0-198-generic #218-Ubuntu SMP Fri Sep 27 20:18:53 UTC 2024 x86_64
User : www-data ( 33)
PHP Version : 7.4.33
Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Directory :  /var/www/html/tech_preview/hr/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /var/www/html/tech_preview/hr/document_manager.js
hr.document_manager.loadDocumentUnused = function(host){
    return new Promise(function(resolve, reject){
        ModalElement.show_loading();
        FormClass.api_call({
            url: "database_load.php",
            params: [{name: "task", value: "document_manager_load_unused_document"}],
            func: function(success, message){
                ModalElement.close(-1);
                if (success){
                    if (message.substr(0, 2) == "ok"){
                        var st = EncodingClass.string.toVariable(message.substr(2));
                        var documentsDic = contentModule.makeDictionaryIndex(host.database.documents.items);
                        var k;
                        for (var i = 0; i < st.documents.length; i++){
                            k = documentsDic[st.documents[i].id];
                            if (k >= 0) continue;
                            host.database.documents.items.push(st.documents[i]);
                        }
                        host.database.document_properties_links.items = host.database.document_properties_links.items.concat(st.document_properties_links);
                        host.database.document_approvers.items = host.database.document_approvers.items.concat(st.document_approvers);
                        contentModule.makeDocumentsIndex(host);
                        contentModule.makeDocumentsProperties(host);
                        hr.document_manager.makePrivilegeAndApprovalDocument(host);
                        var documentDic = contentModule.makeDictionaryIndex(host.database.documents.items);
                        var k;
                        for (var i = 0; i < host.database.user_marked.items.length; i++){
                            k = documentDic[host.database.user_marked.items[i].taskid];
                            if (k >= 0) host.database.documents.items[k].marked = true;
                        }
                        resolve();
                    }
                    else {
                        ModalElement.alert({message: message});
                    }
                }
                else {
                    ModalElement.alert({message: message});
                }
            }
        });
    });
};

hr.document_manager.approvalDocument = function(host, id, inside){
    var idList = [];
    if (!id){
        host.database.documents.items.forEach(function(item){
            if (host.paramsDataContent.inputidboxes["checkbox_" + item.id] && host.paramsDataContent.inputidboxes["checkbox_" + item.id].checked){
                idList.push(item.id);
            }
        });
    }
    else {
        idList = [id];
    }
    if (idList.length == 0) return;
    var listApproval = [];
    var index, approverIds = [];
    for (var i = 0; i < idList.length; i++){
        approverIds = [];
        index = host.database.documents.getIndex(idList[i]);
        host.database.documents.items[index].approvers.forEach(function(item, stt){
            if (host.employeeOfMe.indexOf(item.employeeid) >= 0){
                approverIds.push(item.id);
            }
        });
        listApproval = [{
            id: idList[i],
            approverIds: approverIds
        }];
    }
    console.log(listApproval);
    ModalElement.show_loading();
    FormClass.api_call({
        url: "document_approval.php",
        params: [
            {name: "listApproval", value: EncodingClass.string.fromVariable(listApproval)}
        ],
        func: function(success, message){
            if (success){
                if (message.substr(0, 2) == "ok"){
                    hr.document_manager.init2(host);
                    if (inside){
                        host.frameList.removeLast();
                        ModalElement.close();
                        hr.document_manager.showDocumentPre(host, host.id);
                    }
                    else {
                        ModalElement.close(-1);
                    }
                }
                else {
                    ModalElement.alert({message: message});
                }
            }
            else {
                ModalElement.alert({message: message});
            }
        }
    });
};

hr.document_manager.denyDocument = function(host, listApproval, comment, inside){
    ModalElement.show_loading();
    FormClass.api_call({
        url: "documents_deny.php",
        params: [
            {name: "listApproval", value: EncodingClass.string.fromVariable(listApproval)},
            {name: "comment", value: comment}
        ],
        func: function(success, message){
            if (success){
                if (message.substr(0, 2) == "ok"){
                    ModalElement.close();
                    hr.document_manager.init2(host);
                    if (inside){
                        host.frameList.removeLast();
                        hr.document_manager.showDocumentPre(host, host.id);
                    }
                    else {
                        ModalElement.close(-1);
                    }
                }
                else {
                    ModalElement.alert({message: message});
                }
            }
            else {
                ModalElement.alert({message: message});
            }
        }
    });
};

hr.document_manager.denyDocumentConfirm = function(host, id, inside){
    var idList = [];
    if (!id){
        host.database.documents.items.forEach(function(item){
            if (host.paramsDataContent.inputidboxes["checkbox_" + item.id] && host.paramsDataContent.inputidboxes["checkbox_" + item.id].checked){
                idList.push(item.id);
            }
        });
    }
    else {
        idList = [id];
    }
    if (idList.length == 0) return;
    var listApproval = [];
    var index, approverIds = [];
    for (var i = 0; i < idList.length; i++){
        approverIds = [];
        index = host.database.documents.getIndex(idList[i]);
        host.database.documents.items[index].approvers.forEach(function(item, stt){
            if (host.employeeOfMe.indexOf(item.employeeid) >= 0){
                approverIds.push(item.id);
            }
        });
        listApproval = [{
            id: idList[i],
            approverIds: approverIds
        }];
    }
    if (listApproval.length == 0) return;
    var comment_input = DOMElement.textarea({
        attrs: {
            className: "cardSimpleTextarea",
            style: {
                minWidth: "250px",
                width: "100%",
                height: "100px"
            }
        }
    });
    ModalElement.showWindow({
        title: LanguageModule.text("txt_deny_request"),
        bodycontent: DOMElement.div({
            children: [
                DOMElement.div({
                    attrs: {
                        style: {
                            height: "var(--control-height)",
                            lineHeight: "var(--control-height)",
                            width: "300px"
                        }
                    },
                    text: LanguageModule.text("txt_reason_deny")
                }),
                comment_input
            ]
        }),
        buttonlist: [
            {
                text: LanguageModule.text("txt_save"),
                typeColor: "light",
                onclick: function(){
                    ModalElement.close();
                    var comment = comment_input.value.trim();
                    hr.document_manager.denyDocument(host, listApproval, comment, inside);
                }
            },
            {
                text: LanguageModule.text("txt_cancel"),
                onclick: function(){
                    ModalElement.close();
                }
            }
        ]
    });
    comment_input.focus();
};

hr.document_manager.deleteFolder = function(host, id){
    return new Promise(function(resolve, reject){
        ModalElement.show_loading();
        FormClass.api_call({
            url: "document_folders_delete.php",
            params: [
                {name: "id", value: id}
            ],
            func: function(success, message){
                ModalElement.close(-1);
                if (success){
                    if (message.substr(0, 2) == "ok"){
                        host.database.document_folders.items = host.database.document_folders.items.filter(function(item){
                            return item.id != id;
                        });
                        contentModule.makeDocumentFoldersIndex(host);
                        resolve();
                    }
                    else {
                        ModalElement.alert({message: message});
                    }
                }
                else {
                    ModalElement.alert({message: message});
                }
            }
        })
    });
};

hr.document_manager.deleteFolderConfirm = function(host, id){
    return new Promise(function(resolve,reject){
        var index = host.database.document_folders.getIndex(id);
        ModalElement.question({
            title: LanguageModule.text("war_title_delete_document_folder"),
            message: LanguageModule.text2("war_txt_detele", [host.database.document_folders.items[index].name]),
            onclick: function(sel){
                if (sel == 0){
                    hr.document_manager.deleteFolder(host, id).then(function(value){
                        resolve(value);
                    });
                }
            }
        });
    });
};

hr.document_manager.addFolderSubmit = function(host, data){
    return new Promise(function(resolve, reject){
        if (data.id > 0){
            var index = host.database.document_folders.getIndex(id);
            data.ver = params.database.document_folders.items[index].ver;
        }
        else data.ver = 1;
        ModalElement.show_loading();
        FormClass.api_call({
            url: "document_folders_save.php",
            params: [
                {name: "data", value: EncodingClass.string.fromVariable(data)}
            ],
            func: function(success, message){
                ModalElement.close(-1);
                if (success){
                    if (message.substr(0, 2) == "ok"){
                        var st = EncodingClass.string.toVariable(message.substr(2));
                        var id = data.id;
                        if (id > 0){
                            var index = host.database.document_folders.getIndex(id);
                            host.database.document_folders.items[index] = st;
                        }
                        else {
                            host.database.document_folders.items.push(st);
                        }
                        contentModule.makeDocumentFoldersIndex(host);
                        contentModule.makePrivilegeDocumentConfigs(host);
                        contentModule.makeDocumentsIndex(host);
                        hr.document_manager.redrawFolderDetails(host);
                        resolve();
                    }
                    else if (message == "failed_name"){
                        ModalElement.alert({
                            message: LanguageModule.text("war_txt_name_is_exist")
                        });
                    }
                    else if (message == "failed_ver" || message == "failed_id"){
                        ModalElement.alert({
                            message: LanguageModule.text("war_txt_failed_ver_reload_data")
                        });
                    }
                    else {
                        ModalElement.alert({message: message});
                    }
                }
                else {
                    ModalElement.alert({message: message});
                }
            }
        });
    });
};

hr.document_manager.addFolder = function(host, parentid, id){
    var name = "";
    if (id > 0){
        var index = host.database.document_folders.getIndex(id);
        name = host.database.document_folders.items[index].name;
    }
    theme.formDocument_managerEditFolder({
        database: host.database,
        frameList: host.frameList,
        title: (id == 0)? LanguageModule.text("txt_add_folder") : LanguageModule.text("txt_edit_folder"),
        data: {
            parentid: parentid,
            name: name,
            id: id
        },
        func: {
            save: function(data){
                return hr.document_manager.addFolderSubmit(host, data);
            }
        }
    });
};

hr.document_manager.addFolderPre = function(host, parentid, id){
    if (host.database.document_folders.getIndex(id) < 0) return;
    ModalElement.show_loading();
    FormClass.api_call({
        url: "database_load.php",
        params: [
            {name: "task", value: "document_manager_load_folder_by_id"},
            {name: "id", value: id}
        ],
        func: function(success, message){
            ModalElement.close(-1);
            if (success){
                if (message.substr(0, 2) == "ok"){
                    var st = EncodingClass.string.toVariable(message.substr(2));
                    var index = host.database.document_folders.getIndex(id);
                    st.childIndexList = host.database.document_folders.items[index].childIndexList;
                    host.database.document_folders.items[index] = st;
                    hr.document_manager.addFolder(host, parentid, id);
                }
                else if (message == "war_txt_failed_data"){
                    ModalElement.alert({
                        message: LanguageModule.text("war_txt_failed_data"),
                        func: function(){
                            hr.document_manager.init(host);
                        }
                    });
                }
                else {
                    ModalElement.alert({message: message});
                }
            }
        }
    });
};

hr.document_manager.moveFolderSubmit = function(host, id, newparentid){
    return new Promise(function(resolve, reject){
        var index = host.database.document_folders.getIndex(id);
        if (host.database.document_folders.items[index].parentid == newparentid) return;
        ModalElement.show_loading();
        FormClass.api_call({
            url: "document_folders_move_path.php",
            params: [
                {name: "data", value: EncodingClass.string.fromVariable({
                    id: id,
                    parentid: newparentid
                })}
            ],
            func: function(success, message){
                ModalElement.close(-1);
                if (success){
                    if (message.substr(0, 2) == "ok"){
                        var st = EncodingClass.string.toVariable(message.substr(2));
                        host.database.document_folders.items[index] = st;
                        resolve();
                        contentModule.makeDocumentFoldersIndex(host);
                        contentModule.makeDocumentsIndex(host);
                        contentModule.makePrivilegeDocumentConfigs(host);
                        hr.document_manager.redrawFolderDetails(host);
                    }
                    else if (message == "failed_id"){
                        ModalElement.alert({
                            message: LanguageModule.text("war_txt_failed_ver_reload_data")
                        });
                    }
                    else {
                        ModalElement.alert({message: message});
                    }
                }
                else {
                    ModalElement.alert({message: message});
                }
            }
        });
    });
};

hr.document_manager.moveFolder = function(host, id){
    var index = host.database.document_folders.getIndex(id);
    var items = contentModule.getDocumentFoldersSelectTreeMenuMove(host, id);
    theme.formDocument_managerMoveFoler({
        itemsList: items,
        parentid: host.database.document_folders.items[index].parentid,
        func: {
            save: function(value){
                return hr.document_manager.moveFolderSubmit(host, id, value);
            }
        }
    });
};

hr.document_manager.deleteDocument = function(host, id){
    return new Promise(function(resolve, reject){
        ModalElement.show_loading();
        FormClass.api_call({
            url: "documents_delete.php",
            params: [{name: "id", value: id}],
            func: function(success, message){
                ModalElement.close(-1);
                if (success){
                    if (message.substr(0, 2) == "ok"){
                        var index = host.database.documents.getIndex(id);
                        host.database.documents.items.splice(index, 1);
                        contentModule.makeDocumentsIndex(host);
                        resolve();
                    }
                    else if (message == "lock_system"){
                        ModalElement.alert({message: LanguageModule.text("war_txt_system_lock")});
                    }
                    else if (message == "failed_used"){
                        ModalElement.alert({
                            message: LanguageModule.text("war_txt_can_not_delete")
                        });
                    }
                    else {
                        ModalElement.alert({message: message});
                    }
                }
                else {
                    ModalElement.alert({message: message});
                }
            }
        });
    });
};

hr.document_manager.deleteDocumentConfirm = function(host, id){
    return new Promise(function(resolve, reject){
        var index = host.database.documents.getIndex(id);
        ModalElement.question({
            title: LanguageModule.text("war_title_delete_document"),
            message: LanguageModule.text2("war_txt_detele", [host.database.documents.items[index].name]),
            onclick: function(sel){
                if (sel == 0){
                    hr.document_manager.deleteDocument(host, id).then(function(value){
                        resolve(value);
                    });
                }
            }
        });
    });
};

hr.document_manager.addDocumentSubmit = function(host, id, getValueFunction, editApprovalForm, editPropertiesForm){
    return new Promise(function(resolve, reject){
        var data = getValueFunction();
        if (!data) return;
        var oldData;
        if (id > 0){
            oldData = host.dataDocumentEdit;
            var ischange = contentModule.isChanged(oldData, data,
                ["name", "number", "issued_date", "effective_date", "expired_date", "tag", "summary_content", "attachments", "available", "detail_content"]
            );
            if (!ischange){
                resolve();
                return;
            }
        }
        data.id = id;
        data.available = data.available? 1 : 0;
        if (id > 0){
            var index = host.database.documents.getIndex(id);
            data.ver = host.dataDocumentEdit.ver;
        }
        else {
            data.ver = 1;
        }
        if (!data.effective_date) data.effective_date = new Date(0);
        if (!data.expired_date) data.expired_date = new Date(0);
        data.extrainfo = EncodingClass.string.fromVariable(data.extrainfo);
        data.attachments = EncodingClass.string.fromVariable(data.attachments);
        var params = [];
        if (data.id == 0){
            var dataProperties = editPropertiesForm.getValue();
            for (var i = 0; i < dataProperties.length; i++){
                if (dataProperties[i].property_groupid == 'folder'){
                    data.folderid = dataProperties[i].folderid;
                    dataProperties.splice(i, 1);
                    break;
                }
            }
            var dataApproval = editApprovalForm.getValue();
            if (!dataApproval) return;
            params.push({name: "dataApproval", value: EncodingClass.string.fromVariable(dataApproval)});
            params.push({name: "dataProperties", value: EncodingClass.string.fromVariable(dataProperties)});
            console.log(dataApproval);
        }
        else {
            data.folderid = host.dataDocumentEdit.folderid;
        }
        params.push({name: "data", value: EncodingClass.string.fromVariable(data)});
        ModalElement.show_loading();
        FormClass.api_call({
            url: "documents_save.php",
            params: params,
            func: function(success, message){
                ModalElement.close(-1);
                if (success){
                    if (message.substr(0, 2) == "ok"){
                        var content = EncodingClass.string.toVariable(message.substr(2));
                        var st = content.data;
                        st.extrainfo = EncodingClass.string.toVariable(st.extrainfo);
                        st.attachments = EncodingClass.string.toVariable(st.attachments);
                        if (st.effective_date.getTime() == 0) st.effective_date = null;
                        if (st.expired_date.getTime() == 0) st.expired_date = null;
                        st.properties = {};
                        var isNew;
                        if (id > 0){
                            isNew = false;
                            var index = host.database.documents.getIndex(id);
                            st.properties = host.database.documents.items[index].properties;
                            st.marked = host.database.documents.items[index].marked;
                            host.database.documents.items[index] = st;
                            host.dataDocumentEdit = host.database.documents.items[index];
                        }
                        else {
                            isNew = true;
                            for (var i = 0; i < dataProperties.length; i++){
                                st.properties[dataProperties[i].property_groupid] = dataProperties[i].propertyid;
                            }
                            id = st.id;
                            data.id = id;
                            host.id = id;
                            host.database.documents.items.push(st);
                            host.dataDocumentEdit = st;
                        }
                        host.database.document_approvers.items = host.database.document_approvers.items.concat(content.document_approvers);
                        resolve();
                        host.folderid = st.folderid;
                        contentModule.makeDocumentsIndex(host);
                        hr.document_manager.makePrivilegeAndApprovalDocument(host);
                        if (isNew){
                            hr.document_manager.showDocumentPre(host, id);
                        }
                        else {
                            host.drawGeneralInforFunc();
                        }
                        hr.document_manager.redrawFolderDetails(host);
                    }
                    else if (message == "lock_system"){
                        ModalElement.alert({message: LanguageModule.text("war_txt_system_lock")});
                    }
                    else if (message == "deleted" || message == "edited"){
                        ModalElement.alert({
                            message: LanguageModule.text("war_txt_failed_ver_reload_data")
                        });
                    }
                    else if (message.startsWith("approved")){
                        ModalElement.alert({
                            message: "Văn bản đã có người duyệt, không được sửa",
                            func: function(){
                                var st = EncodingClass.string.toVariable(message.substr("approved".length));
                                resolve();
                                host.frameList.removeLast();
                                hr.document_manager.showDocumentPre(host, host.id);
                            }
                        })
                    }
                    else {
                        ModalElement.alert({message: message});
                    }
                }
                else {
                    ModalElement.alert({message: message});
                }
            }
        });
    });
};

hr.document_manager.addDocumentClose = function(host, id, getValueFunction){
    return new Promise(function(resolve, reject){
        var data = getValueFunction();
        if (!data) {
            ModalElement.close();
            resolve();
            return;
        }
        var oldData;
        if (id > 0){
            oldData = host.dataDocumentEdit;
            var ischange = contentModule.isChanged(oldData, data,
                ["name", "number", "issued_date", "effective_date", "expired_date", "tag", "summary_content", "attachments", "available", "detail_content"]
            );
        }
        else {
            oldData = {
                name: "",
                available: 1
            };
            var ischange = contentModule.isChanged(oldData, data, ["name", "available"]);
        }
        if (!ischange){
            resolve();
            return;
        }
        contentModule.questionChange({
            onclick: function(sel){
                if (sel == 0){
                    hr.document_manager.addDocumentSubmit(host, host.id, getValueFunction).then(function(){
                        resolve();
                    });
                }
                else if (sel == 1){
                    resolve();
                }
            }
        });
    });
};

hr.document_manager.editDocument = function(host, id){
    host.id = id;
    var formContainer = DOMElement.div({});
    theme.formDocument_managerEditDocument({
        title: LanguageModule.text("txt_general_information"),
        formContainer: formContainer,
        frameList: host.frameList,
        func: {
            save: function(){
                return hr.document_manager.addDocumentSubmit(host, host.id, getValueFunction);
            },
            cancel: function(){
                return hr.document_manager.addDocumentClose(host, host.id, getValueFunction);
            }
        }
    });
    var getValueFunction;
    contentModule.makeFormData({
        typeid: host.form_config.documents.typeid,
        formid: host.form_config.documents.input_form,
        initialValue: host.dataDocumentEdit,
        formContainer: formContainer,
        typelists: host.database.typelists.items,
        variableType: "system"
    }).then(function(getValue){
        getValueFunction = getValue;
        absol.form.traversal(formContainer.formFragment.view, function(item){
            if (item.node.tag === "TextInput") {
                item.node.domElt.focus();
                item.stop();
                return;
            }
        });
    });
};

hr.document_manager.getEditPropertiesCtn = function(host, id, folderid){
    var listProps;
    var listConfigPrivilegeEditOfMe;
    var getlistPropsInit = function(){
        listProps = {};
        if (systemconfig.privSystem >= 2) listProps["folder"] = [0];
        else listProps["folder"] = [];
        for (var i = 0; i < host.listPropertiesRoot.length; i++){
            if (systemconfig.privSystem >= 2) listProps[host.listPropertiesRoot[i].id] = [0];
            else listProps[host.listPropertiesRoot[i].id] = [];
        }

        listConfigPrivilegeEditOfMe = [];
        for (var i = 0; i < host.listConfigPrivilegeOfMe.length; i++){
            if (!host.listConfigPrivilegeOfMe[i].content.edit) continue;
            listProps["folder"] = listProps["folder"].concat(host.listConfigPrivilegeOfMe[i].folderList);
            listConfigPrivilegeEditOfMe.push(host.listConfigPrivilegeOfMe[i]);
            for (var j = 0; j < host.listPropertiesRoot.length; j++){
                listProps[host.listPropertiesRoot[j].id] = listProps[host.listPropertiesRoot[j].id].concat(host.listConfigPrivilegeOfMe[i].content.properties[host.listPropertiesRoot[j].id]);
            }
        }
    };
    getlistPropsInit();
    var changeProps = function(){
        if (systemconfig.privSystem >= 2) return;
        var listConfig = [];
        var ex;
        for (var i = 0; i < listConfigPrivilegeEditOfMe.length; i++){
            ex = true;
            for (var j = 0; j < host.listPropertiesRoot.length; j++){
                if (combobox[host.listPropertiesRoot[j].id].value != 0){
                    if (listConfigPrivilegeEditOfMe[i].content.properties[host.listPropertiesRoot[j].id].indexOf(0) < 0){
                        if (listConfigPrivilegeEditOfMe[i].content.properties[host.listPropertiesRoot[j].id].indexOf(combobox[host.listPropertiesRoot[j].id].value) < 0){
                            ex = false;
                            break;
                        }
                    }
                }
            }
            if (ex) listConfig.push(listConfigPrivilegeEditOfMe[i]);
        }
        if (listConfig.length == 0){
            folder_select.items = contentModule.makeDocument_foldersSelectItems(host, []);
            folder_select.value = 0;
        }
        else {
            var folderList = [];
            for (var i = 0; i < listConfig.length; i++){
                folderList = folderList.concat(listConfig[i].folderList);
            }
            folder_select.items = contentModule.makeDocument_foldersSelectItems(host, folderList);
        }
        host.listPropertiesRoot.forEach(function(item1){
            listConfig = [];
            for (var i = 0; i < listConfigPrivilegeEditOfMe.length; i++){
                if (folder_select.value != 0 && listConfigPrivilegeEditOfMe[i].folderList.indexOf(folder_select.value) < 0) continue;
                ex = true;
                for (var j = 0; j < host.listPropertiesRoot.length; j++){
                    if (host.listPropertiesRoot[j].id == item1.id) continue;
                    if (combobox[host.listPropertiesRoot[j].id].value != 0){
                        if (listConfigPrivilegeEditOfMe[i].content.properties[host.listPropertiesRoot[j].id].indexOf(0) < 0){
                            if (listConfigPrivilegeEditOfMe[i].content.properties[host.listPropertiesRoot[j].id].indexOf(combobox[host.listPropertiesRoot[j].id].value) < 0){
                                ex = false;
                                break;
                            }
                        }
                    }
                }
                if (ex) listConfig.push(listConfigPrivilegeEditOfMe[i]);
            }
            if (listConfig.length == 0){
                combobox[item1.id].items = contentModule.makeDocument_propertiesSelectItems(host, item1.id, []);
                combobox[item1.id].value = 0;
            }
            else {
                listProps = [];
                for (var i = 0; i < listConfig.length; i++){
                    listProps = listProps.concat(listConfig[i].content.properties[item1.id]);
                    combobox[item1.id].items = contentModule.makeDocument_propertiesSelectItems(host, item1.id, listProps);
                }
            }
        });
    };
    var res = theme.formDocument_managerPropertiesCtn({
        listProps: listProps,
        database: host.database,
        listPropertiesRoot: host.listPropertiesRoot,
        dataDocumentEdit: host.dataDocumentEdit,
        id: id,
        change_func: function(){
            changeProps();
        },
        reset_func: function(){
            getlistPropsInit();
            folder_select.items = contentModule.makeDocument_foldersSelectItems(host, listProps["folder"]);
            folder_select.value = 0;
            for (var i = 0; i < host.listPropertiesRoot.length; i++){
                combobox[host.listPropertiesRoot[i].id].items = contentModule.makeDocument_propertiesSelectItems(host, host.listPropertiesRoot[i].id, listProps[host.listPropertiesRoot[i].id]);
                combobox[host.listPropertiesRoot[i].id].value = 0;
            }
        }
    });
    var folder_select = res.folder_select;
    var combobox = res.combobox;
    if (id > 0) folder_select.value = host.dataDocumentEdit.folderid;
    else folder_select.value = folderid;
    changeProps();
    return res;
};

hr.document_manager.addDocument2 = function(host, folderid){
    var redraw = function(){
        var getValueFunction;
        var formContainer = DOMElement.div({});
        contentModule.makeFormData({
            typeid: host.form_config.documents.typeid,
            formid: host.form_config.documents.input_form,
            initialValue: host.dataDocumentEdit,
            formContainer: formContainer,
            typelists: host.database.typelists.items,
            variableType: "system"
        }).then(function(getValue){
            formContainer.getValue = getValue;
        });
        var editPropertiesForm = hr.document_manager.getEditPropertiesCtn(host, 0, folderid);
        var editApprovalForm = hr.document_manager.getEditApprovalCtn(host, 0);
        theme.formDocument_managerAddDocument({
            frameList: host.frameList,
            formContainer: formContainer,
            editPropertiesForm: editPropertiesForm,
            editApprovalForm: editApprovalForm,
            func: {
                save: function(){
                    return hr.document_manager.addDocumentSubmit(host, 0, formContainer.getValue, editApprovalForm, editPropertiesForm);
                }
            }
        });
    };
    if (!host.employeeViewItems){
        ModalElement.show_loading();
        data_module.loadEmployeeItemsPrivView().then(function(values){
            ModalElement.close(-1);
            host.employeeViewItems = contentModule.getMemberOfEmployeeSelectMenu(values);
            redraw();
        });
    }
    else {
        redraw();
    }
};

hr.document_manager.addDocument = function(host, folderid){
    ModalElement.show_loading();
    FormClass.api_call({
        url: "database_load.php",
        params: [
            {name: "task", value: "document_manager_load_templates_list"}
        ],
        func: function(success, message){
            ModalElement.close(-1)
            if (success){
                if (message.substr(0, 2) == "ok"){
                    var st = EncodingClass.string.toVariable(message.substr(2));
                    contentModule.makeDatabaseContent(host, st);
                    var document_templatesDic = contentModule.makeDictionaryIndex(host.database.document_templates.items);
                    for (var i = 0; i < host.database.document_templates.items.length; i++){
                        host.database.document_templates.items[i].approvers = [];
                    }
                    var k;
                    for (var i = 0; i < host.database.document_approvers2.items.length; i++){
                        k = document_templatesDic[host.database.document_approvers2.items[i].documentid];
                        if (k != undefined){
                            host.database.document_templates.items[k].approvers.push(host.database.document_approvers2.items[i]);
                        }
                    }
                    var templatesList = [{value: 0, text: "Blank"}];
                    for (var i = 0; i < host.database.document_templates.items.length; i++){
                        templatesList.push({value: host.database.document_templates.items[i].id, text: host.database.document_templates.items[i].name});
                    }
                    theme.formDocument_managerAddDocumentChooseTemplate({
                        database: host.database,
                        templatesList: templatesList,
                        frameList: host.frameList,
                        title: LanguageModule.text("txt_add_document"),
                        func: {
                            save: function(templateid){
                                if (templateid != 0){
                                    var index = host.database.document_templates.getIndex(templateid);
                                    host.dataDocumentEdit = host.database.document_templates.items[index];
                                }
                                else host.dataDocumentEdit = undefined;
                                hr.document_manager.addDocument2(host, folderid);
                            }
                        }
                    })
                }
                else {
                    ModalElement.alert({message: message});
                }
            }
            else {
                ModalElement.alert({message: message});
            }
        }
    });
};

hr.document_manager.deletePriv = function(host, configid){
    ModalElement.show_loading();
    FormClass.api_call({
        url: "document_manager_delete_privileges.php",
        params: [{name: "id", value: configid}],
        func: function(success, message){
            ModalElement.close(-1);
            if (success){
                if (message.substr(0, 2) == "ok"){
                    var index = host.database.configs.getIndex(configid);
                    host.database.configs.items.splice(index, 1);
                    var index = host.database.configs_document_privileges.getIndex(configid);
                    host.database.configs_document_privileges.items.splice(index, 1);
                    host.redrawPrivDetails();
                }
                else {
                    ModalElement.alert({message: message});
                }
            }
            else {
                ModalElement.alert({message: message});
            }
        }
    });
};

hr.document_manager.deletePrivConfirm = function(host, configid){
    ModalElement.question({
        title: LanguageModule.text("war_title_delete_permission"),
        message: LanguageModule.text2("war_txt_detele", [""]),
        onclick: function(sel){
            if (sel == 0){
                hr.document_manager.deletePriv(host, configid);
            }
        }
    });
};

hr.document_manager.addPrivSubmit = function(host, id, data){
    if (id > 0){
        data.id = id;
        var index = host.database.configs.getIndex(id);
        data.ver = host.database.configs.items[index].ver;
    }
    else {
        data.id = 0;
        data.ver = 1;
    }
    ModalElement.show_loading();
    FormClass.api_call({
        url: "document_manager_save_privileges.php",
        params: [{name: "data", value: EncodingClass.string.fromVariable(data)}],
        func: function(success, message){
            ModalElement.close(-1);
            if (success){
                if (message.substr(0, 2) == "ok"){
                    var st = EncodingClass.string.toVariable(message.substr(2));
                    st.content = EncodingClass.string.toVariable(st.content);
                    if (id > 0){
                        host.database.configs.items[index] = st;
                    }
                    else {
                        host.database.configs.items.push(st);
                    }
                    ModalElement.close();
                    host.redrawPrivDetails();
                }
                else {
                    ModalElement.alert({message: message});
                }
            }
            else {
                ModalElement.alert({message: message});
            }
        }
    })
};

hr.document_manager.addPriv = function(host, folderid, id){
    if (id > 0) var index = host.database.configs.getIndex(id);
    var fIndex = host.database.document_folders.getIndex(folderid);
    var folderName = host.database.document_folders.items[fIndex].name;
    var level_select = absol.buildDom({
        tag: "selectmenu",
        style: {
            display: "block",
            width: "100%"
        },
        props: {
            items: [
                {value: 0, text: LanguageModule.text("txt_select_value")},
                {value: 1, text: LanguageModule.text("txt_folder")},
                {value: 2, text: LanguageModule.text("txt_folder_and_sub_folder")}
            ]
        }
    });
    if (id > 0) level_select.value = host.database.configs.items[index].taskdetails;
    var dataView = [
        [{
            attrs: {colSpan: 3, style: {lineHeight: "var(--control-height)"}},
            children: [
                DOMElement.span({text: LanguageModule.text("txt_setting_permission_for_directory") + " "}),
                DOMElement.span({
                    attrs: {style: {fontWeight: "bold"}},
                    text: folderName
                })
            ]
        }],
        [{attrs: {style: {height: "var(--control-verticle-distance-2)"}}}],
        [{
            attrs: {colSpan: 3, style: {fontWeight: "bold", lineHeight: "var(--control-height)"}},
            text: LanguageModule.text("txt_for_what_object")
        }],
        [{attrs: {style: {height: "var(--control-verticle-distance-2)"}}}],
        [
            {text: LanguageModule.text("txt_permission_level")},
            {attrs: {style: {width: "var(--control-horizontal-distance-2)"}}},
            level_select
        ],
        [{attrs: {style: {height: "var(--control-verticle-distance-2)"}}}]
    ];
    var combobox = {};
    for (var i = 0; i < host.listPropertiesRoot.length; i++){
        combobox[host.listPropertiesRoot[i].id] = absol.buildDom({
            tag: "selectbox",
            style: {
                display: "block",
                width: "100%",
                minWidth: "400px"
            },
            props: {
                items: contentModule.makeDocument_propertiesSelectFilterItems(host, host.listPropertiesRoot[i].id),
                enableSearch: true
            }
        });
        if (id > 0){
            if (host.database.configs.items[index].content.properties[host.listPropertiesRoot[i].id]){
                combobox[host.listPropertiesRoot[i].id].values = host.database.configs.items[index].content.properties[host.listPropertiesRoot[i].id];
            }
        }
        dataView.push([
            {
                text: host.listPropertiesRoot[i].name
            },
            {attrs: {style: {width: "var(--control-horizontal-distance-2)"}}},
            combobox[host.listPropertiesRoot[i].id]
        ]);
        dataView.push([{attrs: {style: {height: "var(--control-verticle-distance-2)"}}}]);
    }
    var view_checkbox = absol.buildDom({
        tag: "checkbox",
        style: {
            margin: 0
        }
    });
    var edit_checkbox = absol.buildDom({
        tag: "checkbox",
        style: {
            margin: 0
        }
    });
    var employeeGroupsItems = [{value: 0, text: LanguageModule.text("txt_all_employee_group")}];
    for (var i = 0; i < host.database.employee_groups.items.length; i++){
        if (!host.database.employee_groups.items[i].available) continue;
        employeeGroupsItems.push({
            value: host.database.employee_groups.items[i].id,
            text: host.database.employee_groups.items[i].name
        });
    }
    var employee_group_select = absol.buildDom({
        tag: "selectbox",
        style: {
            width: "100%",
            display: "block"
        },
        props: {
            items: employeeGroupsItems,
            enableSearch: true
        }
    });
    var items = contentModule.getOrgsAndEmployeesListSelectTreeMenuLeaf(host);
    items.unshift({value: 0, text: LanguageModule.text("txt_all_employee"), isLeaf: true});
    var employee_select = absol.buildDom({
        tag: "multichecktreemenu",
        style: {
            width: "100%",
            display: "block"
        },
        props: {
            items: items,
            leafOnly: true
        }
    });
    if (id > 0){
        view_checkbox.checked = host.database.configs.items[index].content.view;
        edit_checkbox.checked = host.database.configs.items[index].content.edit;
        employee_select.values = host.database.configs.items[index].content.employees;
        employee_group_select.values = host.database.configs.items[index].content.employee_groups;
    }
    dataView.push(
        [{attrs: {style: {height: "var(--control-verticle-distance-2)"}}}],
        [{
            attrs: {colSpan: 3, style: {fontWeight: "bold", lineHeight: "var(--control-height)"}},
            text: LanguageModule.text("txt_for_what_permission")
        }],
        [{attrs: {style: {height: "var(--control-verticle-distance-2)"}}}],
        [
            {text: LanguageModule.text("txt_view")},
            {attrs: {style: {width: "var(--control-horizontal-distance-2)"}}},
            view_checkbox
        ],
        [{attrs: {style: {height: "var(--control-verticle-distance-2)"}}}],
        [
            {text: LanguageModule.text("txt_edit")},
            {attrs: {style: {width: "var(--control-horizontal-distance-2)"}}},
            edit_checkbox
        ],
        [{attrs: {style: {height: "20px"}}}],
        [{
            attrs: {colSpan: 3, style: {fontWeight: "bold", lineHeight: "var(--control-height)"}},
            text: LanguageModule.text("txt_for_whom")
        }],
        [
            {text: LanguageModule.text("txt_employee_group")},
            {attrs: {style: {width: "var(--control-horizontal-distance-2)"}}},
            employee_group_select
        ],
        [{attrs: {style: {height: "var(--control-verticle-distance-2)"}}}],
        [
            {text: LanguageModule.text("txt_employee")},
            {attrs: {style: {width: "var(--control-horizontal-distance-2)"}}},
            employee_select
        ]
    );

    var res = DOMElement.table({
        data: dataView
    });
    ModalElement.showWindow({
        title: id == 0? LanguageModule.text("txt_add") : LanguageModule.text("txt_edit"),
        bodycontent: res,
        buttonlist: [
            {
                text: LanguageModule.text("txt_save"),
                onclick: function(){
                    var data = res.getValue();
                    if (!data) return;
                    hr.document_manager.addPrivSubmit(host, id, data);
                }
            },
            {
                text: LanguageModule.text("txt_cancel"),
                onclick: function(){
                    ModalElement.close();
                }
            }
        ]
    });
    res.getValue = function(){
        var values = {
            properties: {}
        };
        var level = level_select.value;
        if (level == 0){
            ModalElement.alert({message: LanguageModule.text("war_txt_level_is_null")});
            return;
        }
        for (type in combobox){
            values.properties[type] = combobox[type].values;
        }
        values.view = view_checkbox.checked;
        values.edit = edit_checkbox.checked;
        values.employee_groups = employee_group_select.values;
        values.employees = employee_select.values;
        return {
            taskid: folderid,
            taskdetails: level,
            content: EncodingClass.string.fromVariable(values)
        };
    };
};

hr.document_manager.addPrivPre = function(host, folderid, id){
    ModalElement.show_loading();
    FormClass.api_call({
        url: "database_load.php",
        params: [
            {name: "task", value: "document_manager_load_privileges_by_id"},
            {name: "id", value: id}
        ],
        func: function(success, message){
            ModalElement.close(-1);
            if (success){
                if (message.substr(0, 2) == "ok"){
                    var st = EncodingClass.string.toVariable(message.substr(2));
                    var index = host.database.configs.getIndex(id);
                    host.database.configs.items[index] = st;
                    hr.document_manager.addPriv(host, folderid, id);
                }
                else {
                    ModalElement.alert({message: message});
                }
            }
            else {
                ModalElement.alert({message: message});
            }
        }
    });
};

hr.document_manager.viewPrivList = function(host, id){
    return new Promise(function(resolve, reject){
        var document_propertiesDic = contentModule.makeDictionaryIndex(host.database.document_properties.items);
        var employeesDic = contentModule.makeDictionaryIndex(host.database.employees.items);
        var employee_groupsDic = contentModule.makeDictionaryIndex(host.database.employee_groups.items);
        var getLevelText = function(level, taskid){
            var res;
            switch (level) {
                case 1:
                res = LanguageModule.text("txt_folder");
                break;
                case 2:
                res = LanguageModule.text("txt_folder_and_sub_folder");
                break;
                default:
                res = "-";
                break;
            }
            if (taskid != id) {
                var foIndex = host.database.document_folders.getIndex(taskid);
                if (foIndex >= 0) res += " (" + host.database.document_folders.items[foIndex].name + ")";
                else res += " (-)";
            }
            return res;
        };
        host.redrawPrivDetails = function(){
            var employeeFvalue = employee_select.value;
            var employee_groupFvalue = employee_group_select.value;
            DOMElement.removeAllChildren(data_ctn);
            var data = [], row, objectElt, content;
            for (var i = 0; i < host.database.configs.items.length; i++){
                content = host.database.configs.items[i].content;
                if (employeeFvalue != 0){
                    var view = true;
                    if (content.employees.indexOf(0) < 0 && content.employees.indexOf(employeeFvalue) < 0){
                        view = false;
                        if (content.employee_groups.indexOf(0) >= 0){
                            for (j = 0; j < host.database.employee_groups.items.length; j++){
                                if (host.database.employee_groups.items[j].members.indexOf(employeeFvalue) >= 0){
                                    view = true;
                                    break
                                }
                            }
                        }
                        else {
                            for (var j = 0; j < content.employee_groups.length; j++){
                                var gIndex = employee_groupsDic[content.employee_groups[j]];
                                if (host.database.employee_groups.items[gIndex].members.indexOf(employeeFvalue) >= 0){
                                    view = true;
                                    break
                                }
                            };
                        }
                    }
                    if (!view) continue;
                }
                if (employee_groupFvalue != 0 && content.employee_groups.indexOf(0) < 0){
                    if (content.employee_groups.indexOf(employee_groupFvalue) < 0) continue;
                }
                var view = true;
                for (type in filters){
                    if (filters[type].value != 0){
                        if (content.properties[type]){
                            if (content.properties[type].indexOf(0) < 0){
                                if (content.properties[type].indexOf(filters[type].value) < 0) view = false;
                            }
                        }
                        else {
                            view = false;
                        }
                    }
                }
                if (!view) continue;
                objectElt = DOMElement.div({
                    children: [
                        DOMElement.div({
                            text: getLevelText(host.database.configs.items[i].taskdetails, host.database.configs.items[i].taskid)
                        })
                    ]
                });
                host.listPropertiesRoot.forEach(function(item1){
                    var pid;
                    var x = document_propertiesDic[item1.id];
                    if (x >= 0){
                        var propsType = host.database.document_properties.items[x].name + ": ";
                        var details = "";
                        if (!content.properties[item1.id] || content.properties[item1.id].length == 0){
                            details = "-";
                        }
                        else {
                            for (var j = 0; j < content.properties[item1.id].length; j++){
                                pid = content.properties[item1.id][j];
                                if (pid == 0){
                                    details += "; " + LanguageModule.text("txt_all");
                                    break;
                                }
                                else {
                                    var p = document_propertiesDic[pid];
                                    if (p >= 0){
                                        details += "; " + host.database.document_properties.items[p].name;
                                    }
                                }
                            }
                            details = details.substr(2);
                        }
                        objectElt.appendChild(DOMElement.div({
                            children: [
                                DOMElement.span({text: propsType}),
                                DOMElement.span({
                                    attrs: {style: {fontWeight: "bold"}},
                                    text: details
                                })
                            ]
                        }));
                    }
                });
                row = [
                    {style: {textAlign: "center"}},
                    {
                        element: objectElt
                    },
                    {
                        element: DOMElement.table({
                            data: [
                                [
                                    {attrs: {style: {border: 0}}, text: LanguageModule.text("txt_view")},
                                    {attrs: {style: {width: "var(--control-horizontal-distance-1)", border: 0}}},
                                    {
                                        attrs: {style: {border: 0}},
                                        children: [absol.buildDom({
                                            tag: "checkbox",
                                            props: {
                                                checked: content.view,
                                                disabled: true
                                            }
                                        })]
                                    }
                                ],
                                [{attrs: {style: {height: "var(--control-horizontal-distance-2)", border: 0}}}],
                                [
                                    {attrs: {style: {border: 0}}, text: LanguageModule.text("txt_edit")},
                                    {attrs: {style: {width: "var(--control-horizontal-distance-1)", border: 0}}},
                                    {
                                        attrs: {style: {border: 0}},
                                        children: [absol.buildDom({
                                            tag: "checkbox",
                                            props: {
                                                checked: content.edit,
                                                disabled: true
                                            }
                                        })]
                                    }
                                ]
                            ]
                        })
                    }
                ];
                var whomElt = DOMElement.div({});
                var eIndex, eText = "";
                for (var j = 0; j < content.employees.length; j++){
                    if (content.employees[j] == 0){
                        eText += ", " + LanguageModule.text("txt_all_employee");
                    }
                    else {
                        eIndex = employeesDic[content.employees[j]];
                        if (eIndex >= 0) eText += "; " + host.database.employees.items[eIndex].fullname;
                    }
                }
                eText = eText.substr(2);
                whomElt.appendChild(DOMElement.div({
                    children: [
                        DOMElement.span({
                            text: LanguageModule.text("txt_employee") + ": "
                        }),
                        DOMElement.span({
                            attrs: {style: {fontWeight: "bold"}},
                            text: eText
                        })
                    ]
                }));
                var gIndex, gText = "";
                for (var j = 0; j < content.employee_groups.length; j++){
                    if (content.employee_groups[j] == 0){
                        gText += ", " + LanguageModule.text("txt_all_employee_group");
                    }
                    else {
                        gIndex = employee_groupsDic[content.employee_groups[j]];
                        if (gIndex >= 0) gText += "; " + host.database.employee_groups.items[gIndex].name;
                    }
                }
                gText = gText.substr(2);
                whomElt.appendChild(DOMElement.div({
                    children: [
                        DOMElement.span({
                            text: LanguageModule.text("txt_employee_group") + ": "
                        }),
                        DOMElement.span({
                            attrs: {style: {fontWeight: "bold"}},
                            text: gText
                        })
                    ]
                }));
                row.push({
                    element: whomElt
                });
                if (host.database.configs.items[i].taskid != id){
                    row.push({});
                }
                else {
                    var qmenuButton = DOMElement.div({
                        attrs: {
                            className: "card-icon-cover"
                        },
                        children: [DOMElement.i({
                            attrs: {
                                className: "material-icons bsc-icon-hover-black"
                            },
                            text: "more_vert"
                        })]
                    });
                    var quickMenuItems = [
                        {
                            text: LanguageModule.text("txt_edit"),
                            icon: {tag: 'i', class: 'material-icons', child: {text: "create"}},
                            extendClasses: ['bsc-quickmenu'],
                            cmd: function (configid) {
                                return function(){
                                    hr.document_manager.addPrivPre(host, id, configid);
                                }
                            }(host.database.configs.items[i].id)
                        }
                    ];
                    quickMenuItems.push({
                        text: LanguageModule.text("txt_delete"),
                        icon: {tag: 'i', class: 'material-icons', child: {text: "delete"}},
                        extendClasses: ['bsc-quickmenu', 'red'],
                        cmd: function (configid) {
                            return function(){
                                hr.document_manager.deletePrivConfirm(host, configid);
                            }
                        }(host.database.configs.items[i].id)
                    });
                    absol.QuickMenu.showWhenClick(qmenuButton, {items: quickMenuItems}, [3, 4], function (menuItem) {
                        if (menuItem.cmd) menuItem.cmd();
                    });
                    row.push({
                        element: qmenuButton
                    });
                }
                data.push(row);
            }
            var header = [
                {value: LanguageModule.text("txt_index"), type: "increase"},
                {value: LanguageModule.text("txt_object")},
                {value: LanguageModule.text("txt_authority")},
                {value: LanguageModule.text("txt_employee") + "/ " + LanguageModule.text("txt_employee_group")},
                {}
            ];
            var tableView = pizo.tableView(
                header,
                data
            );
            data_ctn.appendChild(tableView);
        };
        var buttonPanel = DOMElement.div({
            attrs: {className: 'button-panel-header absol-single-page-header'},
            children: [
                DOMElement.div({
                    attrs: {className: "single-button-header"},
                    children: [theme.backButton({
                        onclick: function(){
                            host.frameList.removeLast();
                            resolve();
                        }
                    })]
                }),
                DOMElement.div({
                    attrs: {className: "single-button-header"},
                    children: [theme.addButton({
                        onclick: function(){
                            hr.document_manager.addPriv(host, id, 0);
                        }
                    })]
                })
            ]
        });
        var filters = {};
        for (var i = 0; i < host.listPropertiesRoot.length; i++){
            filters[host.listPropertiesRoot[i].id] = absol.buildDom({
                tag: "selectmenu",
                props: {
                    items: contentModule.makeDocument_propertiesSelectFilterItems(host, host.listPropertiesRoot[i].id),
                    enableSearch: true
                },
                on: {
                    change: function(){
                        host.redrawPrivDetails();
                    }
                }
            });
            buttonPanel.appendChild(DOMElement.div({
                attrs: {className: "single-button-header"},
                children: [filters[host.listPropertiesRoot[i].id]]
            }));
        }
        var employeeGroupsItems = [{value: 0, text: LanguageModule.text("txt_all_employee_group")}];
        for (var i = 0; i < host.database.employee_groups.items.length; i++){
            if (!host.database.employee_groups.items[i].available) continue;
            employeeGroupsItems.push({
                value: host.database.employee_groups.items[i].id,
                text: host.database.employee_groups.items[i].name
            });
        }
        var employee_group_select = absol.buildDom({
            tag: "selectmenu",
            props: {
                items: employeeGroupsItems,
                enableSearch: true
            },
            on: {
                change: function(){
                    host.redrawPrivDetails();
                }
            }
        });
        buttonPanel.appendChild(DOMElement.div({
            attrs: {className: "single-button-header"},
            children: [employee_group_select]
        }));
        var employeeItems = contentModule.getMemberOfEmployeeSelectMenu(host);
        employeeItems[0].text = LanguageModule.text("txt_all_employee");
        var employee_select = absol.buildDom({
            tag: "selecttreeleafmenu",
            props: {
                items: employeeItems,
                enableSearch: true
            },
            on: {
                change: function(){
                    host.redrawPrivDetails();
                }
            }
        });
        buttonPanel.appendChild(DOMElement.div({
            attrs: {className: "single-button-header"},
            children: [employee_select]
        }));
        var folderName = "";
        var fIndex = host.database.document_folders.getIndex(id);
        if (fIndex >= 0) folderName = host.database.document_folders.items[fIndex].name;
        var data_ctn = DOMElement.div({attrs: {style: {paddingBottom: "var(--footer-margin)"}}});
        host.redrawPrivDetails();
        var singlePage = absol.buildDom({
            tag: "singlepage",
            child: [
                buttonPanel,
                DOMElement.div({
                    attrs: {
                        style: {
                            height: "var(--control-height)",
                            lineHeight: "var(--control-height)"
                        }
                    },
                    children: [
                        DOMElement.span({
                            text: LanguageModule.text("txt_permission_to_folder") + " "
                        }),
                        DOMElement.span({
                            attrs: {style: {fontWeight: "bold"}},
                            text: folderName
                        })
                    ]
                }),
                data_ctn
            ]
        });
        host.frameList.addChild(singlePage);
        singlePage.requestActive();
    });
};

hr.document_manager.viewPrivListPre = function(host, id){
    return new Promise(function(resolve, reject){
        var loadPriv = function(){
            var folderparentlist = [], pid;
            var fIndex = host.database.document_folders.getIndex(id);
            pid = host.database.document_folders.items[fIndex].parentid;
            while (pid != 0){
                folderparentlist.push(pid);
                fIndex = host.database.document_folders.getIndex(pid);
                pid = host.database.document_folders.items[fIndex].parentid;
            }
            FormClass.api_call({
                url: "database_load.php",
                params: [
                    {name: "task", value: "document_manager_load_privileges"},
                    {name: "folderid", value: id},
                    {name: "folderparentlist", value: EncodingClass.string.fromVariable(folderparentlist)}
                ],
                func: function(success, message){
                    ModalElement.close(-1);
                    if (success){
                        if (message.substr(0, 2) == "ok"){
                            var st = EncodingClass.string.toVariable(message.substr(2));
                            contentModule.makeDatabaseContent(host, st);
                            hr.document_manager.viewPrivList(host, id).then(function(){
                                resolve();
                            });
                        }
                        else {
                            ModalElement.alert({message: message});
                        }
                    }
                    else {
                        ModalElement.alert({message: message});
                    }
                }
            });
        };
        ModalElement.show_loading();
        if (host.quickOpen){
            FormClass.api_call({
                url: "database_load.php",
                params: [{name: "task", value: "document_manager_load_quick"}],
                func: function(success, message){
                    if (success){
                        if (message.substr(0, 2) == "ok"){
                            var content = EncodingClass.string.toVariable(message.substr(2));
                            host.employeeOfMe = content.employeeOfMe;
                            delete content.employeeOfMe;
                            contentModule.makeDatabaseContent(host, content);
                            host.listPropertiesRoot = [];
                            for (var i = 0; i < host.database.document_properties.items.length; i++){
                                if (!host.database.document_properties.items[i].available) continue;
                                if (host.database.document_properties.items[i].parentid != 0) continue;
                                host.listPropertiesRoot.push(host.database.document_properties.items[i]);
                            }
                            if (!host.loadedOrgs){
                                hr.document_manager.loadOrgsData(host).then(function(value){
                                    loadPriv();
                                });
                            }
                            else {
                                loadPriv();
                            }
                        }
                        else {
                            ModalElement.alert({message: message});
                        }
                    }
                    else {
                        ModalElement.alert({message: message});
                    }
                }
            });
        }
        else {
            if (!host.loadedOrgs){
                hr.document_manager.loadOrgsData(host).then(function(value){
                    loadPriv();
                });
            }
            else {
                loadPriv();
            }
        }
    });
};

hr.document_manager.getCellFolder = function(host, content){
    var func = {
        edit: function(){
            hr.document_manager.addFolderPre(host, content.parentid, content.id);
        },
        add_folder: function(){
            hr.document_manager.addFolder(host, content.id, 0);
        },
        move: function(){
            hr.document_manager.moveFolder(host, content.id);
        },
        delete: function(){
            return new Promise(function(resolve,reject){
                hr.document_manager.deleteFolderConfirm(host, content.id).then(function(value){
                    resolve(value);
                });
            });
        },
        edit_priv: function(){
            hr.document_manager.viewPrivListPre(host, content.id);
        },
        clickRowFunc: function(folderid){
            host.folderid = folderid;
            hr.document_manager.redrawFolderDetails(host);
        }
    };
    var priv_add_document = false;
    if (systemconfig.privSystem >= 2) priv_add_document = true;
    else {
        for (var i = 0; i < host.listConfigPrivilegeOfMe.length; i++){
            if (host.listConfigPrivilegeOfMe[i].folderList.indexOf(content.id) < 0) continue;
            if (host.listConfigPrivilegeOfMe[i].content.edit){
                priv_add_document = true;
                break;
            }
        }
    }
    if (priv_add_document) func.add_document = function(){
        hr.document_manager.addDocument(host, content.id);
    };
    return {
        id: content.id,
        name: content.name,
        func: func
    };
};

hr.document_manager.storageDocument = function(host, id){
    return new Promise(function(resolve, reject){
        var index = host.database.documents.getIndex(id);
        if (index < 0){
            resolve();
            return;
        }
        var data = {
            id: id,
            unused: 1
        };
        var expired_time = host.database.documents.items[index].expired_date.getTime();
        if (expired_time > 0){
            if (expired_time > new Date().getTime()){
                ModalElement.alert({message: LanguageModule.text("war_txt_can_not_archive_document")});
                return;
            }
        }
        else {
            data.expired_date = new Date();
        }
        ModalElement.show_loading();
        FormClass.api_call({
            url: "documents_change_to_unused.php",
            params: [{name: "data", value: EncodingClass.string.fromVariable(data)}],
            func: function(success, message){
                ModalElement.close(-1);
                if (success){
                    if (message.substr(0, 2) == "ok"){
                        host.database.documents.items[index].unused = 1;
                        if (data.expired_date) host.database.documents.items[index].expired_date = data.expired_date;
                        resolve(data);
                    }
                    else {
                        ModalElement.alert({message: message});
                    }
                }
                else {
                    ModalElement.alet({message: message});
                }
            }
        });
    });
};

hr.document_manager.storageDocumentConfirm = function(host, id){
    return new Promise(function(resolve, reject){
        var index = host.database.documents.getIndex(id);
        ModalElement.question({
            title: LanguageModule.text("txt_warning"),
            message: LanguageModule.text2("war_title_archive_document", [host.database.documents.items[index].name]),
            onclick: function(sel){
                if (sel == 0){
                    hr.document_manager.storageDocument(host, id).then(function(value){
                        resolve(value);
                    });
                }
            }
        });
    });
};

hr.document_manager.editApprovalSubmit = function(host, data){
    return new Promise(function(resolve, reject){
        data.id = host.id;
        ModalElement.show_loading();
        FormClass.api_call({
            url: "documents_save_approval.php",
            params: [
                {name: "data", value: EncodingClass.string.fromVariable(data)},
            ],
            func: function(success, message){
                ModalElement.close(-1);
                if (success){
                    if (message.substr(0, 2) == "ok"){
                        resolve();
                        var approvers = EncodingClass.string.toVariable(message.substr(2));
                        host.dataDocumentEdit.approvers = approvers;
                        host.dataDocumentEdit.approval_method = data.approval_method;
                        host.dataDocumentEdit.time_hours_needed_for_approval = data.time_hours_needed_for_approval;
                        host.drawApprovalFunc();
                    }
                    else if (message.startsWith("approved")){
                        ModalElement.alert({
                            message: "Văn bản đã có người duyệt, không được sửa",
                            func: function(){
                                var st = EncodingClass.string.toVariable(message.substr("approved".length));
                                resolve();
                                host.frameList.removeLast();
                                hr.document_manager.showDocumentPre(host, host.id);
                            }
                        })
                    }
                    else {
                        ModalElement.alert({message: message});
                    }
                }
                else {
                    ModalElement.alert({message: message});
                }
            }
        });
    });
};

hr.document_manager.getEditApprovalCtn = function(host, id){
    return theme.formDocument_managerApprovalCtn({
        employeeViewItems: host.employeeViewItems,
        id: id,
        dataDocumentEdit: host.dataDocumentEdit
    });
};

hr.document_manager.editApproval = function(host){
    var redraw = function(){
        var elt = hr.document_manager.getEditApprovalCtn(host, host.id);
        theme.formDocument_managerEditApproval({
            title: LanguageModule.text("txt_approval"),
            elt: elt,
            frameList: host.frameList,
            func: {
                save: function(){
                    return new Promise(function(resolve, reject){
                        var data = elt.getValue();
                        if (!data) return;
                        hr.document_manager.editApprovalSubmit(host, data).then(function(){
                            resolve();
                        });
                    });
                }
            }
        });
    };
    if (host.employeeViewItems){
        redraw();
    }
    else {
        ModalElement.show_loading();
        data_module.loadEmployeeItemsPrivView().then(function(values){
            ModalElement.close(-1);
            host.employeeViewItems = contentModule.getMemberOfEmployeeSelectMenu(values);
            redraw();
        });
    }
};

hr.document_manager.editApprovalPre = function(host){
    ModalElement.show_loading();
    FormClass.api_call({
        url: "database_load.php",
        params: [
            {name: "task", value: "documents_approvers_load_by_id"},
            {name: "id", value: host.id}
        ],
        func: function(success, message){
            ModalElement.close(-1);
            if (success){
                if(message.substr(0, 2) == "ok"){
                    host.dataDocumentEdit = EncodingClass.string.toVariable(message.substr(2));
                    var index = host.database.documents.getIndex(host.id);
                    if (index >= 0) host.dataDocumentEdit.marked = host.database.documents.items[index].marked;
                    if (host.dataDocumentEdit.effective_date.getTime() == 0) host.dataDocumentEdit.effective_date = null;
                    if (host.dataDocumentEdit.expired_date.getTime() == 0) host.dataDocumentEdit.expired_date = null;
                    host.dataDocumentEdit.properties = {};
                    var document_properties_links = host.dataDocumentEdit.document_properties_links;
                    for (var i = 0; i < document_properties_links.length; i++){
                        host.dataDocumentEdit.properties[document_properties_links[i].property_groupid] = document_properties_links[i].propertyid;
                    }
                    if (host.dataDocumentEdit.extrainfo == "") host.dataDocumentEdit.extrainfo = {};
                    else host.dataDocumentEdit.extrainfo = EncodingClass.string.toVariable(host.dataDocumentEdit.extrainfo);
                    if (host.dataDocumentEdit.attachments == "") host.dataDocumentEdit.attachments = [];
                    else host.dataDocumentEdit.attachments = EncodingClass.string.toVariable(host.dataDocumentEdit.attachments);
                    hr.document_manager.editApproval(host);
                }
                else {
                    ModalElement.alert({message: message});
                }
            }
            else {
                ModalElement.alert({message: message});
            }
        }
    });
};

hr.document_manager.saveAsTemplateSubmit = function(host, id, name){
    return new Promise(function(resolve, reject){
        ModalElement.show_loading();
        FormClass.api_call({
            url: "document_save_as_template.php",
            params: [
                {name: "id", value: id},
                {name: "name", value: name}
            ],
            func: function(success, message){
                ModalElement.close(-1);
                if (success){
                    if (message.substr(0, 2) == "ok"){
                        resolve();
                    }
                    else {
                        ModalElement.alert({message: message});
                    }
                }
                else {
                    ModalElement.alert({message: message});
                }
            }
        });
    });
};

hr.document_manager.saveAsTemplate = function(host, id){
    var index = host.database.documents.getIndex(id);
    theme.formDocument_managerSaveAsTemplate({
        title: LanguageModule.text("txt_save_as_template"),
        name: host.database.documents.items[index].name,
        frameList: host.frameList,
        func: {
            save: function(name){
                return hr.document_manager.saveAsTemplateSubmit(host, id, name);
            }
        }
    });
};

hr.document_manager.editPropertiesSubmit = function(host, data){
    return new Promise(function(resolve, reject){
        for (var i = 0; i < data.length; i++){
            data[i].documentid = host.id;
        }
        if (data.length == 0){
            resolve();
            return;
        }
        ModalElement.show_loading();
        FormClass.api_call({
            url: "documents_save_properties.php",
            params: [{name: "data", value: EncodingClass.string.fromVariable(data)}],
            func: function(success, message){
                ModalElement.close(-1);
                if (success){
                    if (message.substr(0, 2) == "ok"){
                        host.dataDocumentEdit.properties = {};
                        for (var i = 0; i < data.length; i++){
                            if (data[i].property_groupid == "folder") host.dataDocumentEdit.folderid = data[i].folderid;
                            else host.dataDocumentEdit.properties[data[i].property_groupid] = data[i].propertyid;
                        }
                        var index = host.database.documents.getIndex(host.id);
                        if (index >= 0){
                            host.database.documents.items[index].folderid = host.dataDocumentEdit.folderid;
                            host.database.documents.items[index].properties = host.dataDocumentEdit.properties;
                        }
                        host.drawPropertiesFunc();
                        host.folderid = host.dataDocumentEdit.folderid;
                        contentModule.makeDocumentsIndex(host);
                        hr.document_manager.makePrivilegeAndApprovalDocument(host);
                        hr.document_manager.redrawFolderDetails(host);
                        resolve();
                    }
                    else {
                        ModalElement.alert({message: message});
                    }
                }
                else {
                    ModalElement.alert({message: message});
                }
            }
        });
    });
};

hr.document_manager.editProperties = function(host, id){
    var elt = hr.document_manager.getEditPropertiesCtn(host, id);
    theme.formDocument_managerEditProperties({
        title: LanguageModule.text("txt_folder_and_properties"),
        elt: elt,
        frameList: host.frameList,
        func: {
            save: function(){
                return new Promise(function(resolve, reject){
                    var data = elt.getValue();
                    if (!data) return;
                    hr.document_manager.editPropertiesSubmit(host, data).then(function(){
                        resolve();
                    });
                });
            }
        }
    });
};

hr.document_manager.editPropertiesPre = function(host, id){
    ModalElement.show_loading();
    FormClass.api_call({
        url: "database_load.php",
        params: [
            {name: "task", value: "documents_approvers_load_by_id"},
            {name: "id", value: host.id}
        ],
        func: function(success, message){
            ModalElement.close(-1);
            if (success){
                if(message.substr(0, 2) == "ok"){
                    host.dataDocumentEdit = EncodingClass.string.toVariable(message.substr(2));
                    var index = host.database.documents.getIndex(host.id);
                    if (index >= 0) host.dataDocumentEdit.marked = host.database.documents.items[index].marked;
                    if (host.dataDocumentEdit.effective_date.getTime() == 0) host.dataDocumentEdit.effective_date = null;
                    if (host.dataDocumentEdit.expired_date.getTime() == 0) host.dataDocumentEdit.expired_date = null;
                    host.dataDocumentEdit.properties = {};
                    var document_properties_links = host.dataDocumentEdit.document_properties_links;
                    for (var i = 0; i < document_properties_links.length; i++){
                        host.dataDocumentEdit.properties[document_properties_links[i].property_groupid] = document_properties_links[i].propertyid;
                    }
                    if (host.dataDocumentEdit.extrainfo == "") host.dataDocumentEdit.extrainfo = {};
                    else host.dataDocumentEdit.extrainfo = EncodingClass.string.toVariable(host.dataDocumentEdit.extrainfo);
                    if (host.dataDocumentEdit.attachments == "") host.dataDocumentEdit.attachments = [];
                    else host.dataDocumentEdit.attachments = EncodingClass.string.toVariable(host.dataDocumentEdit.attachments);
                    hr.document_manager.editProperties(host, id);
                }
                else {
                    ModalElement.alert({message: message});
                }
            }
            else {
                ModalElement.alert({message: message});
            }
        }
    });
};

hr.document_manager.showDocument = function(host, id){
    host.id = id;
    var privEdit = false;
    if (host.dataDocumentEdit.approverIds.length == 0 && !host.dataDocumentEdit.unused){
        if (systemconfig.privSystem >= 2){
            privEdit = true;
        }
        else {
            if (contentModule.getPrivDocument(host, host.dataDocumentEdit, "edit")) privEdit = true;
        }
    }
    var viewGeneralInformation = DOMElement.div({});
    host.drawGeneralInforFunc = function(){
        contentModule.makeOutputFormData({
            typeid: host.form_config.documents.typeid,
            formid: host.form_config.documents.output_form,
            initialValue: host.dataDocumentEdit,
            formContainer: viewGeneralInformation,
            typelists: host.database.typelists.items,
            variableType: "system"
        }).then(function(getValue){});
    };
    var viewProperties = DOMElement.div({});
    host.drawPropertiesFunc = function(){
        DOMElement.removeAllChildren(viewProperties);
        var value, propertyid, pIndex, nameProps;
        var folderIndex = host.database.document_folders.getIndex(host.dataDocumentEdit.folderid);
        var folderName = host.database.document_folders.items[folderIndex].name;
        var propertiesList = [];
        for (var i = 0; i < host.listPropertiesRoot.length; i++){
            propertyid = host.dataDocumentEdit.properties[host.listPropertiesRoot[i].id];
            pIndex = host.database.document_properties.getIndex(propertyid);
            if (pIndex >= 0){
                nameProps = host.database.document_properties.items[pIndex].name;
            }
            else {
                nameProps = "-";
            }
            propertiesList.push({
                name: host.listPropertiesRoot[i].name,
                value: nameProps
            });
        }
        viewProperties.appendChild(theme.formDocument_managerDrawPropertiesView({
            folderName: folderName,
            propertiesList: propertiesList
        }));
    };
    var getApprovalMethod = function(value){
        var list = contentModule.getListApprovalMethod();
        for (var i = 0; i < list.length; i++){
            if (list[i].value == value) return list[i].text;
        }
        return "";
    };
    var viewApproval = DOMElement.div({});
    host.drawApprovalFunc = function(){
        var approval_method = getApprovalMethod(host.dataDocumentEdit.approval_method);
        var over_due = false;
        var time_hours_needed_for_approval = host.dataDocumentEdit.time_hours_needed_for_approval;
        if (host.dataDocumentEdit.issued_date.getTime() - time_hours_needed_for_approval*60*60*1000 < new Date().getTime()){
            over_due = true;
        }
        var direct_done = true;
        var row, employeeid, eIndex, dIndex, orgIndex, approversItem;
        var prev_done = true;
        var approvers = [];
        for (var i = 0; i < host.dataDocumentEdit.approvers.length; i++){
            employeeid = host.dataDocumentEdit.approvers[i].employeeid;
            eIndex = host.database.employees.getIndex(employeeid);
            row = {
                fullname: host.database.employees.items[eIndex].fullname
            };
            switch (host.dataDocumentEdit.approvers[i].status){
                case 0:
                    switch (host.dataDocumentEdit.approval_method){
                        case "one_approver":
                            if (host.dataDocumentEdit.status == 0){
                                if (true){//if (!over_due){
                                    row.status = DOMElement.span({
                                        attrs: {
                                            className: "table-list-view-status waiting-approval"
                                        },
                                        text: LanguageModule.text("txt_pending_approval")
                                    });
                                }
                                else {
                                    row.status = DOMElement.span({
                                        attrs: {
                                            className: "table-list-view-status over-due"
                                        },
                                        text: LanguageModule.text("txt_over_due")
                                    });
                                }
                            }
                            else {
                                row.status = DOMElement.span({});
                            }
                            break;
                        case "parallel":
                            if (host.dataDocumentEdit.status == 0){
                                if (true){//if (!over_due){
                                    row.status = DOMElement.span({
                                        attrs: {
                                            className: "table-list-view-status waiting-approval"
                                        },
                                        text: LanguageModule.text("txt_pending_approval")
                                    });
                                }
                                else {
                                    row.status = DOMElement.span({
                                        attrs: {
                                            className: "table-list-view-status over-due"
                                        },
                                        text: LanguageModule.text("txt_over_due")
                                    });
                                }
                            }
                            else {
                                row.status = DOMElement.span({});
                            }
                            break;
                        case "sequence":
                            if (host.dataDocumentEdit.status == 0 && prev_done){
                                if (true){//if (!over_due){
                                    row.status =  DOMElement.span({
                                        attrs: {
                                            className: "table-list-view-status waiting-approval"
                                        },
                                        text: LanguageModule.text("txt_pending_approval")
                                    });
                                }
                                else {
                                    row.status = DOMElement.span({
                                        attrs: {
                                            className: "table-list-view-status over-due"
                                        },
                                        text: LanguageModule.text("txt_over_due")
                                    });
                                }
                            }
                            else {
                                row.status = DOMElement.span({});
                            }
                    }
                    row.time = DOMElement.span({});
                    row.note = DOMElement.span({});
                    prev_done = false;
                    break;
                case 1:
                    row.status = DOMElement.span({
                        attrs: {
                            className: "table-list-view-status approved"
                        },
                        text: LanguageModule.text("txt_approved")
                    });
                    row.time = DOMElement.span({text: contentModule.getDateTimeView(host.dataDocumentEdit.approvers[i].time)});
                    row.note = DOMElement.span({text: host.dataDocumentEdit.approvers[i].comment});
                    break;
                case 2:
                    row.status = DOMElement.span({
                        attrs: {
                            className: "table-list-view-status deny"
                        },
                        text: LanguageModule.text("txt_rejected")
                    });
                    row.time = DOMElement.span({text: contentModule.getDateTimeView(host.dataDocumentEdit.approvers[i].time)});
                    row.note = DOMElement.span({text: host.dataDocumentEdit.approvers[i].comment});
                    prev_done = false;
                    break;
            }
            approvers.push(row);
        }
        DOMElement.removeAllChildren(viewApproval);
        viewApproval.appendChild(theme.formDocument_managerDrawApproveView({
            time_hours_needed_for_approval: time_hours_needed_for_approval,
            approval_method: approval_method,
            approvers: approvers
        }));
    };
    var pIndex = host.database.documents.getIndex(host.id);
    var privApproval;
    if (host.dataDocumentEdit.status == 0){
        if (!privApproval){
            var employeeid;
            var prev_done = true;
            for (var i = 0; i < host.dataDocumentEdit.approvers.length; i++){
                employeeid = host.dataDocumentEdit.approvers[i].employeeid;
                if (host.employeeOfMe.indexOf(employeeid) >= 0){
                    if (host.dataDocumentEdit.approvers[i].status == 0){
                        if (host.dataDocumentEdit.approval_method != "sequence") privApproval = true;
                        else if (host.dataDocumentEdit.approval_method == "sequence" && prev_done) privApproval = true;
                    }
                    break;
                }
                else {
                    if (host.dataDocumentEdit.approval_method == "sequence" && host.dataDocumentEdit.approvers[i].status == 0){
                        prev_done = false;
                        break;
                    }
                }
            }
        }
    }
    var func = {
        markedFunc: function(marked){
            return new Promise(function(rs, rj){
                contentModule.markedFunc(host, {
                    id: host.id,
                    marked: marked,
                    task: "documents",
                    tablename: "documents"
                }).then(function(v){
                    host.dataDocumentEdit.marked = marked;
                    hr.document_manager.redrawFolderDetails(host);
                    rs(v);
                });
            });
        },
        edit_document: function(){
            hr.document_manager.editDocument(host, host.id);
        },
        edit_properties: function(){
            hr.document_manager.editPropertiesPre(host, host.id);
        },
        edit_approve: function(){
            hr.document_manager.editApprovalPre(host);
        },
        approval: function(){
             hr.document_manager.approvalDocument(host, host.id, true);
        },
        deny: function(){
            hr.document_manager.denyDocumentConfirm(host, host.id, true);
        }
    };
    if (privEdit && !host.dataDocumentEdit.unused){
        func.change_to_unused = function(){
            hr.document_manager.storageDocumentConfirm(host, host.id).then(function(){
                hr.document_manager.redrawFolderDetails(host);
                host.frameList.removeLast();
                hr.document_manager.showDocumentPre(host, host.id);
            });
        }
    }
    if (host.dataDocumentEdit.status == 1){
        func.save_as_template = function(){
            hr.document_manager.saveAsTemplate(host, host.id);
        };
    }
    theme.formDocument_managerShowDocument({
        id: id,
        holder: host.holder,
        frameList: host.frameList,
        dataDocumentEdit: host.dataDocumentEdit,
        drawGeneralInforFunc: host.drawGeneralInforFunc,
        viewGeneralInformation: viewGeneralInformation,
        drawPropertiesFunc: host.drawPropertiesFunc,
        viewProperties: viewProperties,
        drawApprovalFunc: host.drawApprovalFunc,
        viewApproval: viewApproval,
        privEdit: privEdit,
        privApproval: privApproval,
        func: func
    });
};

hr.document_manager.showDocumentPre = function(host, id){
    var index = host.database.documents.getIndex(id);
    if (index < 0 || !host.database.documents.items[index].privView) return;
    var loadDocumentsById = function(){
        ModalElement.show_loading();
        FormClass.api_call({
            url: "database_load.php",
            params: [
                {name: "task", value: "documents_approvers_load_by_id"},
                {name: "id", value: id}
            ],
            func: function(success, message){
                ModalElement.close(-1);
                if (success){
                    if (message.substr(0, 2) == "ok"){
                        host.dataDocumentEdit = EncodingClass.string.toVariable(message.substr(2));
                        var index = host.database.documents.getIndex(host.id);
                        if (index >= 0) host.dataDocumentEdit.marked = host.database.documents.items[index].marked;
                        if (host.dataDocumentEdit.effective_date.getTime() == 0) host.dataDocumentEdit.effective_date = null;
                        if (host.dataDocumentEdit.expired_date.getTime() == 0) host.dataDocumentEdit.expired_date = null;
                        host.dataDocumentEdit.approverIds = [];
                        for (var i = 0; i < host.dataDocumentEdit.approvers.length; i++){
                            if (host.dataDocumentEdit.approvers[i].status != 0){
                                host.dataDocumentEdit.approverIds.push(host.dataDocumentEdit.approvers[i].employeeid);
                            }
                        }
                        host.dataDocumentEdit.properties = {};
                        var document_properties_links = host.dataDocumentEdit.document_properties_links;
                        for (var i = 0; i < document_properties_links.length; i++){
                            host.dataDocumentEdit.properties[document_properties_links[i].property_groupid] = document_properties_links[i].propertyid;
                        }
                        if (host.dataDocumentEdit.extrainfo == "") host.dataDocumentEdit.extrainfo = {};
                        else host.dataDocumentEdit.extrainfo = EncodingClass.string.toVariable(host.dataDocumentEdit.extrainfo);
                        if (host.dataDocumentEdit.attachments == "") host.dataDocumentEdit.attachments = [];
                        else host.dataDocumentEdit.attachments = EncodingClass.string.toVariable(host.dataDocumentEdit.attachments);
                        hr.document_manager.showDocument(host, id);
                    }
                    else if (message == "war_txt_failed_data"){
                        ModalElement.alert({
                            message: LanguageModule.text("war_txt_failed_data"),
                            func: function(){
                                hr.document_manager.init2(host);
                            }
                        });
                    }
                    else {
                        ModalElement.alert({message: message});
                    }
                }
                else {
                    ModalElement.alert({message: message});
                }
            }
        });
    };
    if (!host.loadedOrgs){
        hr.document_manager.loadOrgsData(host).then(function(value){
            loadDocumentsById();
        });
    }
    else {
        loadDocumentsById();
    }
};

hr.document_manager.loadOrgsData = function(host){
    return new Promise(function(resolve, reject){
        ModalElement.show_loading();
        FormClass.api_call({
            url: "database_load.php",
            params: [
                {name: "task", value: "load_data_orgs"}
            ],
            func: function(success, message){
                if (success){
                    if (message.substr(0, 2) == "ok"){
                        var st = EncodingClass.string.toVariable(message.substr(2));
                        contentModule.makeDatabaseContent(host, st);
                        contentModule.makeOrgsIndex(host);
                        contentModule.makeEmployeeData(host);
                        contentModule.makeEmployeesIndex(host);
                        contentModule.makeEmployeePositionData(host);
                        host.loadedOrgs = true;
                        resolve();
                    }
                    else {
                        ModalElement.alert({message: message});
                    }
                }
                else {
                    ModalElement.alert({message: message});
                }
            }
        });
    });
};

hr.document_manager.makePrivilegeAndApprovalDocument = function(host){
    var documentDic = contentModule.makeDictionaryIndex(host.database.documents.items);
    for (var i = 0; i < host.database.documents.items.length; i++){
        host.database.documents.items[i].approverIds = [];
        host.database.documents.items[i].approvers = [];
        host.database.documents.items[i].employeeHasPrivApproval = [];
    }
    var k;
    for (var i = 0; i < host.database.document_approvers.items.length; i++){
        k = documentDic[host.database.document_approvers.items[i].documentid];
        if (k != undefined){
            if (host.database.document_approvers.items[i].status != 0){
                host.database.documents.items[k].approverIds.push(host.database.document_approvers.items[i].employeeid);
            }
            host.database.documents.items[k].employeeHasPrivApproval.push(host.database.document_approvers.items[i].employeeid);
            host.database.documents.items[k].approvers.push(host.database.document_approvers.items[i]);
        }
    }
    host.database.documents.items.forEach(function(item){
        switch (item.status) {
            case 0:
            case 2:
                if (item.status == 0){
                    var time_hours_needed_for_approval = item.time_hours_needed_for_approval;
                    if (item.issued_date.getTime() - time_hours_needed_for_approval*60*60*1000 < new Date().getTime()){
                        status = "over_due";
                    }
                    else {
                        status = "waiting_approval";
                    }
                }
                else {
                    status = "deny";
                }
                if (systemconfig.privSystem >= 2){
                    if (item.approverIds.length == 0){
                        item.privEdit = true;
                    }
                    item.privView = true;
                }
                else {
                    if (contentModule.getPrivDocument(host, item, "edit")){
                        if (item.approverIds.length == 0) item.privEdit = true;
                        item.privView = true;
                    }
                    else if (contentModule.getPrivDocument(host, item, "view")){
                        item.privView = true;
                    }
                }
                item.employeeHasPrivApproval.forEach(function(eid, stt){
                    if (host.employeeOfMe.indexOf(eid) >= 0){
                        switch (item.approval_method){
                            case "one_approver":
                                if (status == "waiting_approval") item.privApproval = true;
                                item.privView = true;
                                item.i_approve = true;
                                item.employeeidApproval = eid;
                                break;
                            case "parallel":
                                if (item.approverIds.indexOf(eid) < 0){
                                    if (status == "waiting_approval" || status == "over_due") item.privApproval = true;
                                    item.employeeidApproval = eid;
                                }
                                item.privView = true;
                                item.i_approve = true;
                                break;
                            case "sequence":
                                var checkMe = true;
                                for (i = 0; i < stt; i++){
                                    if (item.approverIds.indexOf(item.employeeHasPrivApproval[i]) < 0){
                                        checkMe = false;
                                    }
                                }
                                if (checkMe){
                                    if (item.approverIds.indexOf(eid) < 0){
                                        if (status == "waiting_approval" || status == "over_due") item.privApproval = true;
                                        item.employeeidApproval = eid;
                                    }
                                    item.privView = true;
                                    item.i_approve = true;
                                }
                                break;
                        }
                    }
                });
                if (item.userid == systemconfig.userid){
                    item.i_request = true;
                }
                break;
            case 1:
                status = "approved";
                if (systemconfig.privSystem >= 2) item.privView = true;
                if (item.userid == systemconfig.userid){
                    item.i_request = true;
                }
                item.employeeHasPrivApproval.forEach(function(eid){
                    if (host.employeeOfMe.indexOf(eid) >= 0){
                        item.i_approve = true;
                        item.privView = true;
                    }
                });
                if (!item.privView){
                    if (contentModule.getPrivDocument(host, item, "view")) item.privView = true;
                }
                break;
            default:
        }
        item.statusType = status;
    });
};

hr.document_manager.redrawFolderDetails = function(host){
    host.paramsDataContent = {
        viewStatus: host.viewStatus,
        folderid: host.folderid,
        database: host.database,
        form_config: host.form_config,
        listPropertiesRoot: host.listPropertiesRoot,
        approval_btn_ctn: host.approval_btn_ctn,
        deny_btn_ctn: host.deny_btn_ctn,
        user_column_configsDic: host.user_column_configsDic,
        status_select: host.status_select,
        filters: host.filters,
        holder: host.holder,
        data_container: host.data_container,
        inputsearchbox: host.inputsearchbox,
        pathElt: host.pathElt,
        getRowFolder: function(content){
            return hr.document_manager.getCellFolder(host, content);
        },
        document_func: {
            view: function(id){
                hr.document_manager.showDocumentPre(host, id);
            },
            approval: function(id){
                hr.document_manager.approvalDocument(host, id);
            },
            deny: function(id){
                hr.document_manager.denyDocumentConfirm(host, id);
            },
            delete: function(id){
                return hr.document_manager.deleteDocumentConfirm(host, id);
            },
            markedFunc: function(id, marked){
                return contentModule.markedFunc(host, {
                    id: id,
                    marked: marked,
                    task: "documents",
                    tablename: "documents"
                });
            }
        }
    };
    theme.formDocument_managerContentData(host.paramsDataContent);
};

hr.document_manager.redraw = function(host){
    hr.document_manager.makePrivilegeAndApprovalDocument(host);
    hr.document_manager.redrawFolderDetails(host);
};

hr.document_manager.init2 = function(host){
    return new Promise(function(resolveMn, rejectMn){
        ModalElement.show_loading();
        FormClass.api_call({
            url: "database_load.php",
            params: [
                {name: "task", value: "document_manager_load_document"}
            ],
            func: function(success, message){
                ModalElement.close(-1);
                if (success){
                    if (message.substr(0, 2) == "ok"){
                        var st = EncodingClass.string.toVariable(message.substr(2));
                        if (host.loadUnused){
                            var x = host.database.documents.items.filter(function(item){
                                return item.unused;
                            });
                            var dic = contentModule.makeDictionaryIndex(x);
                            var y = host.database.document_properties_links.items.filter(function(item){
                                return dic[item.documentid] >= 0;
                            });
                            var z = host.database.document_approvers.items.filter(function(item){
                                return dic[item.documentid] >= 0;
                            });
                            st.documents = st.documents.concat(x);
                            st.document_properties_links = st.document_properties_links.concat(y);
                            st.document_approvers = st.document_approvers.concat(z);
                        }
                        contentModule.makeDatabaseContent(host, st);
                        contentModule.makeDocumentsIndex(host);
                        contentModule.makeDocumentsProperties(host);
                        var documentDic = contentModule.makeDictionaryIndex(host.database.documents.items);
                        var k;
                        for (var i = 0; i < host.database.user_marked.items.length; i++){
                            k = documentDic[host.database.user_marked.items[i].taskid];
                            if (k >= 0) host.database.documents.items[k].marked = true;
                        }
                        hr.document_manager.redraw(host);
                        resolveMn(host);
                    }
                    else {
                        ModalElement.alert({message: message});
                    }
                }
                else {
                    ModalElement.alert({message: message});
                }
            }
        });
    });
};

hr.document_manager.init = function(host){
    return new Promise(function(resolveMn, rejectMn){
        host.folderid = 0;
        hr.menu.changeCurrentUrlTab(host, "document_manager");
        host.database = {};
        host.viewStatus = "tree";
        ModalElement.show_loading();
        FormClass.api_call({
            url: "database_load.php",
            params: [
                {name: "task", value: "document_manager_load_init"}
            ],
            func: function(success, message){
                if (success){
                    if (message.substr(0, 2) == "ok"){
                        var content = EncodingClass.string.toVariable(message.substr(2));
                        host.employeeOfMe = content.employeeOfMe;
                        delete content.employeeOfMe;
                        contentModule.makeDatabaseContent(host, content);
                        host.listPropertiesRoot = [];
                        for (var i = 0; i < host.database.document_properties.items.length; i++){
                            if (!host.database.document_properties.items[i].available) continue;
                            if (host.database.document_properties.items[i].parentid != 0) continue;
                            host.listPropertiesRoot.push(host.database.document_properties.items[i]);
                        }
                        contentModule.makeEmployeeGroupMemberIndex(host);
                        contentModule.makePrivilegeDocumentConfigs(host);
                        host.form_config = contentModule.configVariable(host);
                        host.user_column_configsDic = contentModule.getUser_column_configsDic(host);
                        var cmdbutton = {
                            close: function (event, me) {
                                if (hr.isMobile){
                                    // host.holder.selfRemove();
                                    hr.menu.loadPage(1000);
                                }
                                else {
                                    hr.menu.tabPanel.removeTab(host.holder.id);
                                }
                            },
                            add_folder: function(){
                                hr.document_manager.addFolder(host, 0, 0);
                            },
                            add_document: function(){
                                hr.document_manager.addDocument(host, 0);
                            },
                            approval: function(){
                                hr.document_manager.approvalDocument(host);
                            },
                            deny: function(){
                                hr.document_manager.denyDocumentConfirm(host);
                            }
                        };
                        host.data_container = DOMElement.div({});
                        host.holder.addChild(host.frameList);
                        host.pathElt = absol.buildDom({
                            tag: 'pathmenu',
                            style: {
                                height: "30px",
                                marginBottom: "var(--control-verticle-distance-2)"
                            },
                            props: {
                                path: []
                            },
                            on: {
                                change: function (event) {
                                    if (event.item.id != host.folderid){
                                        host.folderid = event.item.id;
                                        hr.document_manager.redrawFolderDetails(host);
                                    }
                                },
                                press: function (event){
                                    if (event.pathItem.id != host.folderid){
                                        host.folderid = event.pathItem.id;
                                        hr.document_manager.redrawFolderDetails(host);
                                    }
                                }
                            }
                        });
                        var singlePage = theme.formDocument_managerInit({
                            cmdbutton: cmdbutton,
                            data_container: host.data_container,
                            pathElt: host.pathElt,
                            listPropertiesRoot: host.listPropertiesRoot,
                            database: host.database,
                            viewStatus: host.viewStatus,
                            filter_change_func: function(){
                                hr.document_manager.redrawFolderDetails(host);
                            },
                            status_change_func: function(){
                                if (host.loadUnused){
                                    hr.document_manager.redrawFolderDetails(host);
                                }
                                else {
                                    ModalElement.show_loading();
                                    hr.document_manager.loadDocumentUnused(host).then(function(){
                                        ModalElement.close(-1);
                                        host.loadUnused = true;
                                        hr.document_manager.redrawFolderDetails(host);
                                    });
                                }
                            },
                            type_view_change_func: function(viewStatus){
                                host.viewStatus = viewStatus;
                                hr.document_manager.redrawFolderDetails(host);
                            }
                        });
                        host.approval_btn_ctn = singlePage.approval_btn_ctn;
                        host.deny_btn_ctn = singlePage.deny_btn_ctn;
                        host.inputsearchbox = singlePage.inputsearchbox;
                        host.filters = singlePage.filters;
                        host.status_select = singlePage.status_select;
                        host.frameList.addChild(singlePage);
                        singlePage.requestActive();
                        hr.document_manager.init2(host).then(function(value){
                            resolveMn(value);
                        });
                    }
                    else {
                        ModalElement.alert({message: message});
                    }
                }
                else {
                    ModalElement.alert({message: message});
                }
            }
        });
    });
};

VaKeR 2022