<?php

include_once("_dirinfo.php");
include_once(PATH_TO_ROOT . "init.php");
include_once(PATH_TO_ROOT . "_class/class.Extjs_tree.php");
include_once(PATH_TO_ROOT . "_class/class.Extjs_grid.php");
include_once(PATH_TO_ROOT . "_support/class.UserSession.php");
include_once(PATH_TO_ROOT . "_support/class.Permissions.php");
include (PATH_TO_ROOT . "/_support/repository.php");
$aLanguage = "si";
//include_once (PATH_TO_ROOT."CRM/_CRM_config.php");
include_once(PATH_TO_ROOT . "capi_manager/class/class.Language-Pack.php");

$db->ChangeDatabase(DB_WARPIT_MAIN);
$language = new LanguagePack($db, $aLanguage);


$treeWebCapiProjects = new MakeTree($_POST['treeName']);
$treeName = $treeWebCapiProjects->getName();
$treeWebCapiProjects->SetTable('_Projects', DB_WARPIT_WEBCATI_BASE);
$treeWebCapiProjects->SetRootVisible('true', 'Projects')
    ->addConfig('updateNode', "function(){}")
    ->region('center')
    ->setFontMedium()
    ->setHeight('200')
    ->addConfig('flex', 0.5)
    ->setReqUrl('capi_manager/_forms/_ajax/ajax.groups.php');
$treeWebCapiProjects->addtBarItem("'->'")
    ->setSearchField('', 'top');

echo $treeWebCapiProjects->CreateTree();

$treeWC = new MakeTree('CapiManagerMenu');
$treeWC->SetTable('_ProgramMenuManager', DB_WARPIT_WEBCATI_BASE);
//->setReqUrl('capi_manager/_ajax/ajax.groups.php');
$treeWC->SetRootVisible("false")
    ->setSort('id ASC')
    ->setLinesVisible(true)
    ->setBoldRoots()
    ->setFontMedium()
//->setTitle($LangPack->GetLabel("CRM_main_panel", "CRM-tools-title","Orodja"))
    ->region('south')
    ->setHeight('200')
    ->addConfig('updateNode', "function(){}")
    ->setTextPadding('1px');

echo $treeWC->CreateTree();


// webcati_tree as first parameter in addTab function (extended addTab function from ExtJS), is meant for title of the tab
// in this case where click on each node transfer different text, this text is used for title
// and first argument is not used at all

$treeProjectName = $treeWebCapiProjects->getName();
$treeWebCapiMenuName = $treeWC->getName();

//$noprojSelMSG = "Ext.Msg.alert('" . $LangPack->GetLabel("CRM_main_panel", "alert-no-project-warning-title", "Opozorilo") . "','" . $LangPack->GetLabel("CRM_main_panel", "alert-no-project-warning-msg", "Prosim izberite projekt!") . "');";
$noprojSelMSG = "Ext.MessageBox.show({
                        title: 'Notice',
                        msg: 'First you must select a project on above',
                        buttons: Ext.MessageBox.OK,
                        icon: Ext.MessageBox.INFO
                    });";
echo "$treeWebCapiMenuName.on('click', function (node) {
        //we changed it that only checked projects count as selected!
        var selectedProject = null;
		selectedProject = $treeProjectName.getSelectionModel().selNode;
		if(node.leaf){
		    if(selectedProject != null){

                if(node.attributes.id == '1' && selectedProject.attributes.isProject == '1' ){
                    //Samplepoints manager
                    addTabAuto('catiShowGroup_'+123,'capi_manager/_forms/interviewers/forms.main_interviewers_samplepoints.php',{
                        group_id:0,project_id:selectedProject.attributes.id,
                        project_text:selectedProject.attributes.text,
                        name:'All',userGroup:'webcati'});

                }
                else if(node.attributes.id == '3' && selectedProject.attributes.isProject == '1' ){
                    //Interviewer control manager
                    addTabAuto('catiShowGroup_'+456,'capi_manager/_forms/control/forms.main_control.php',{
                        group_id:0,
                        project_id:selectedProject.attributes.id,
                        project_text:selectedProject.attributes.text,
                        name:'All',userGroup:'webcati'});

                }
                 else if(node.attributes.id == '2' && selectedProject.attributes.isProject == '1' ){
                    //Interviewer control manager
                    addTabAuto('catiShowGroup_'+789,'capi_manager/_forms/interviewers_nfield/forms.main_interviewers_nfield.php',{
                        group_id:0,
                        project_id:selectedProject.attributes.id,
                        project_text:selectedProject.attributes.text,
                        name:'All',userGroup:'webcati'});

                }
                 else if(node.attributes.id == '6' && selectedProject.attributes.isProject == '1' ){
                    //Interviewer visit manager
                    addTabAuto('catiShowGroup_'+666,'capi_manager/_forms/visit/forms.main_visit.php',{
                        group_id:0,
                        project_id:selectedProject.attributes.id,
                        project_text:selectedProject.attributes.text,
                        name:'All',userGroup:'webcati'});

                }
                else{
                    $noprojSelMSG;
                }
			}
			else{
                $noprojSelMSG;
			}

		}
    }
);"

?>


    var webCapiManagerUniquePanel = new Ext.Panel({
        id:'webCapiManagerUniquePanel',
        title:'Capi manager',
        defaults:{split:true, border:false},
        iconCls:'icon-webcati-tab',
        layout:'border',
        border:false,
        closable:true,
        items:[ <?php echo $treeName . "," . $treeWC->getName();?>]
    });
    Ext.getCmp('west-panel').add(webCapiManagerUniquePanel)
    <?php
    if (true || !$_POST['uniqueNotShow'])
        echo  ".show();";
    ?>

    var fileref = document.createElement('link');

    fileref.setAttribute("rel", "stylesheet")
    fileref.setAttribute("type", "text/css")
    fileref.setAttribute("href", "CRM/_css/mainCRM.css")
    document.getElementsByTagName("head")[0].appendChild(fileref)
