<?
/* index.php: $pubmodule, $pubtask, $pubitemid */
/*
$pubsubjectid = $_REQUEST["subjectid"];
$pubtypeid = $_REQUEST["typeid"];
$pubgroupid = $_REQUEST["groupid"];
*/
$pubpersonnelid = $_REQUEST["pid"];

// filter forms..
$filterprojectname=$_POST['projectname'];

$pubpageno = $_REQUEST['pageno'];
if ($pubpageno=="") $pubpageno=1;

?>

<script type="text/javascript">

// ................................................................................
// user-interaction handlers..
// ................................................................................

function project_button_search() {
  document.projectform.job.value='project_search';
  document.projectform.submit();
}

function project_button_select() {
  document.location='index.php?module=project&task=select';
}

function project_button_selectcms() {
  document.location='index.php?module=project&task=selectcms';
}

function project_button_open(projectpkey) {
  document.location='index.php?module=project&task=open&itemid='+projectpkey;
  //document.location='index.php?module=project&task=dashboard;
}

function project_button_import(projectpkey) {
  document.location='index.php?module=project&task=import&itemid='+projectpkey;
}

function project_button_add() {
  document.location='index.php?module=project&task=add';
}

function project_button_edit(projectpkey) {
  document.location='index.php?module=project&task=edit&itemid='+projectpkey;
}

function project_button_personnel() {
  document.location='index.php?module=project&task=personnel';
}

function project_button_view(projectpkey) {
  document.location='index.php?module=project&task=view&itemid='+projectpkey;
}

function project_button_delete(projectpkey) {
  document.location='index.php?module=project&task=delete&itemid='+projectpkey;
}

function project_button_addpersonnel() {
  document.location='index.php?module=project&task=addpersonnel';
}

function project_button_editpersonnel(personnelpkey) {
  document.location='index.php?module=project&task=editpersonnel&itemid='+personnelpkey;
}

function project_button_deletepersonnel(personnelpkey) {
  document.location='index.php?module=project&task=deletepersonnel&itemid='+personnelpkey;
}

function project_button_save() {
  if (validateForm()==true) {
    document.projectform.job.value='project_add'; document.projectform.submit();
  }
}

function project_button_update() {
  if (validateForm()==true) {
    document.projectform.job.value='project_edit'; document.projectform.submit();
  }
}

function project_button_updatefull() {
  if (validateForm()==true) {
    document.projectform.job.value='project_editfull'; document.projectform.submit();
  }
}

function project_button_purge() {
  document.projectform.job.value='project_remove'; document.projectform.submit();
}

function project_button_savepersonnel() {
  if (validateForm()==true) {
    document.projectform.job.value='project_personneladd'; document.projectform.submit();
  }
}

function project_button_updatepersonnel() {
  if (validateForm()==true) {
    document.projectform.job.value='project_personneledit'; document.projectform.submit();
  }
}

function project_button_purgepersonnel() {
  document.projectform.job.value='project_personnelremove'; document.projectform.submit();
}

function project_button_backtolist() {
  document.location='index.php?module=project&task=select';
}

function project_button_back() {
  history.go(-1);
}

</script>

<?

// ................................................................................
// model..
// ................................................................................

include_once('models/projectModel.php');
include_once('models/mainModel.php');
$myprojectModel=new projectModel;
$myprojectModelPMS=new projectModelPMS;
$mymainModel=new mainModel;

?>

<?

// ................................................................................
// default to list view if none specified..
// ................................................................................
if ($pubtask=="") $pubtask="main";

// ................................................................................
// display page header..
// ................................................................................

$pagetitle="Project";
if (($pubtask=="main")||($pubtask=="submenu")) {
  $subtitle="Projects List";
} else if ($pubtask=="view") {
  $subtitle="Project Information";
} else if ($pubtask=="select") {
  $subtitle="Select Project";
} else if ($pubtask=="selectpms") {
  $subtitle="Select Project";
} else if ($pubtask=="selectcms") {
  $subtitle="Import Project from CMS";
} else if ($pubtask=="open") {
  $subtitle="Open Project";
} else if ($pubtask=="import") {
  $subtitle="Import Project";
} else if ($pubtask=="add") {
  $subtitle="Add Project";
} else if ($pubtask=="edit") {
  $subtitle="Edit Project";
} else if ($pubtask=="personnel") {
  $subtitle="Project Personnel";
} else if ($pubtask=="addpersonnel") {
  $subtitle="Add Project Personnel";
} else if ($pubtask=="editpersonnel") {
  $subtitle="Edit Project Personnel";
} else if ($pubtask=="deletepersonnel") {
  $subtitle="Delete Project Personnel";
} else if ($pubtask=="dashboard") {
  $subtitle="Dashboard";
} else {
  $subtitle="Error: Invalid Option";
}

$headercaption=$pagetitle." ··» ".$subtitle;
include_once('header.php');

include_once('views/general-header.tpl');


// TODO: Later to show list of available variables here if in DEBUG mode!!

// ................................................................................
// display main area..
// ................................................................................

if (($pubtask=="main")||($pubtask=="submenu")) {

  $project_list = $myprojectModelPMS->getlist_projects();
  $cindex=0;
  foreach ($project_list as $mysupp) {
    $approval_list[$cindex] = $myprojectModel->getlist_approvals($mysupp['project_pkey']);
    $bg_list[$cindex] = $myprojectModel->getlist_bankguarantees($mysupp['project_pkey']);
    foreach ($bg_list[$cindex] as $mybg) {
      $bgtotal[$cindex]['bg_banktotal']=0;
      $bgtotal[$cindex]['bg_personaltotal']=0;
      $bgtotal[$cindex]['bg_banktotal']=$bgtotal[$cindex]['bg_banktotal']+$mybg['bg_bankamount'];
      $bgtotal[$cindex]['bg_personaltotal']=$bgtotal[$cindex]['bg_personaltotal']+$mybg['bg_personalamount'];
      $bgtotal[$cindex]['bg_fullremarks']=$bgtotal[$cindex]['bg_fullremarks'].$mybg['bg_startdate']." - ".$mybg['bg_enddate']." ".$mybg['bg_projectname']." ".($mybg['bg_bankamount']/1000)."K,";
    }
    $bgtotal[$cindex]['bg_fulltotal']=($bgtotal[$cindex]['bg_banktotal']*$mysupp['project_bgratio'])+$bgtotal[$cindex]['bg_personaltotal'];
    $bgtotal[$cindex]['bg_fullremarks']=substr($bgtotal[$cindex]['bg_fullremarks'],0,strlen($bgtotal[$cindex]['bg_fullremarks'])-1);
    $cindex++;
  }
  include_once('views/project-list.tpl');

} else if ($pubtask=="select") {

  $project_list = $myprojectModelPMS->getlist_projects();
  $access_list = $myprojectModelPMS->getlist_useraccess($pubuserpkey);
  include_once('views/project-select.tpl');

} else if ($pubtask=="selectpms") {

  $project_list = $myprojectModelPMS->getlist_projects();
  include_once('views/project-select.tpl');

} else if ($pubtask=="selectcms") {

  $project_list = $myprojectModel->getlist_projects();
  include_once('views/project-selectCMS.tpl');

} else if ($pubtask=="open") {

  $project_data = $myprojectModelPMS->getdata_project($pubitemid);
  $_SESSION['Xprojectpkey']=$pubitemid;
  $_SESSION['Xprojectid']=$project_data['projectid'];
  $_SESSION['Xprojectname']=$project_data['projectname'];
  $_SESSION['Xprojectdesc']=$project_data['projectdescription'];
  $g=goto('index.php?module=project&task=dashboard');	// dulu dashboard
  echo $g;
  exit;

} else if ($pubtask=="import") {

  $project_data = $myprojectModel->getdata_project($pubitemid);
  $result = $myprojectModelPMS->insertdata_project($project_data['projectpkey'], $project_data['projectid'], $project_data['projectname'], $project_data['projectdescription'], $project_data['clientname'], $project_data['clientphone1'], $project_data['projectcommencementdate'], $project_data['projectcompletiondate'], $project_data['projectqsname'], $project_data['projectqsphone'], $project_data['projectpmname'], $project_data['projectpmphone'], 0, '');
  if ($result!=false) {
    $_SESSION['Xprojectpkey']=$pubitemid;
    $_SESSION['Xprojectid']=$project_data['projectid'];
    $_SESSION['Xprojectname']=$project_data['projectname'];
    $_SESSION['Xprojectdesc']=$project_data['projectdescription'];
    $g=goto('index.php?module=project&task=view');
    echo $g;
    exit;
  }

//} else if (($pubtask=="view")&&(($pubuseraccess['access_project_view']==1)||($pubuserrole>=2))) {	// TODO
} else if ($pubtask=="view") {

  $project_data = $myprojectModelPMS->getdata_project($pubprojectpkey);
  $consultant_list = $myprojectModel->getlist_consultants($pubprojectpkey);
  $personnel_list = $myprojectModelPMS->getlist_personnel($pubprojectpkey);
  include_once('views/project-view.tpl');

} else if ($pubtask=="add") {

  $project_data = $myprojectModelPMS->getdata_project($pubitemid);
  $personnel_list = $myprojectModelPMS->getlist_personnel($pubitemid);
  $project_data['projectmanual']="1";
  include_once('views/project-editfull.tpl');

//} else if (($pubtask=="edit")&&(($pubuseraccess['access_project_edit']==1)||($pubuserrole>=2))) {	// TODO
} else if ($pubtask=="edit") {

  $project_data = $myprojectModelPMS->getdata_project($pubitemid);
  $personnel_list = $myprojectModelPMS->getlist_personnel($pubitemid);
  if ($project_data['projectmanual']=="0") {
    include_once('views/project-edit.tpl');
  } else {
    include_once('views/project-editfull.tpl');
  }

} else if ($pubtask=="close") {

  $_SESSION['Xprojectpkey']="";
  $_SESSION['Xprojectid']="";
  $_SESSION['Xprojectname']="";
  $_SESSION['Xprojectdesc']="";
  $g=goto('index.php');
  echo $g;
  exit;

} else if ($pubtask=="delete") {

  $project_data = $myprojectModelPMS->getdata_project($pubitemid);
  $consultant_list = $myprojectModel->getlist_consultants($pubitemid);
  $personnel_list = $myprojectModelPMS->getlist_personnel($pubitemid);
  include_once('views/project-view.tpl');

} else if ($pubtask=="personnel") {

  $personnel_list = $myprojectModelPMS->getlist_personnel($pubprojectpkey);
  $project_data = $myprojectModelPMS->getdata_project($pubprojectpkey);
  include_once('views/project-personnel.tpl');

//} else if ((($pubtask=="addpersonnel")||($pubtask=="editpersonnel"))&&(($pubuseraccess['access_project_edit']==1)||($pubuserrole>=2))) {
} else if (($pubtask=="addpersonnel")||($pubtask=="editpersonnel")) {

  $personnel_data = $myprojectModelPMS->getdata_personnel($pubitemid);
  include_once('views/project-editpersonnel.tpl');

//} else if (($pubtask=="deletepersonnel")&&(($pubuseraccess['access_project_delete']==1)||($pubuserrole>=2))) {
} else if ($pubtask=="deletepersonnel") {

  $personnel_data = $myprojectModelPMS->getdata_personnel($pubitemid);
  include_once('views/project-viewpersonnel.tpl');

} else if ($pubtask=="dashboard") {

  //$mro_list = $mymainModel->getlist_mroapprovals($pubprojectpkey);
  include_once('views/project-dashboard.tpl');
  //include_once('views/general-invalidtask.php');

} else {
  include_once('views/general-invalidtask.php');
}

// ...............................................................................
// display button options area..
// ...............................................................................

$prevurl = explode('/',$_SESSION['Xpreviousurl']);
$prevfile = $prevurl[count($prevurl)-1];
if ($prevfile=='inputController.php') $backstatus="style='display: none;'";

echo "<br>";

if (($pubtask=="main")||($pubtask=="submenu")) {

  echo "<br><hr size=1>";
  //echo "<button name=\"print\" id=\"print\" onClick=\"project_button_printlist();\">&nbsp;<img src='gfx/options/b_print1.png' class='actionbutton'>&nbsp;&nbsp;Print&nbsp;</button>&nbsp;";
  echo "<button name=\"add\" id=\"add\" onClick=\"project_button_add();\"><img src='gfx/options/b_add.png' class='actionbutton'>&nbsp;&nbsp;Add New Project</button>&nbsp;";
  echo "<button name=\"back\" id=\"back\" ".$backstatus." onClick=\"project_button_back();\">&nbsp;<img src='gfx/options/b_back.png' class='actionbutton'>&nbsp;&nbsp;Back&nbsp;</button>&nbsp;";
  echo "<br><hr size=1><br>";

} else if ($pubtask=="select") {

  echo "<br><hr size=1>";
  if (($pubuseraccess['access_project_edit']==1)||($pubuserrole>=2)) {
    echo "<button name=\"addnew\" id=\"addnew\" onClick=\"project_button_add();\"><img src='gfx/options/b_add.png' class='actionbutton'>&nbsp;&nbsp;Add New Project</button>&nbsp;";
  }
  //echo "<button name=\"print\" id=\"print\" onClick=\"project_button_printlist();\">&nbsp;<img src='gfx/options/b_print1.png' class='actionbutton'>&nbsp;&nbsp;Print List&nbsp;</button>&nbsp;";
  //echo "<button name=\"selectcms\" id=\"selectcms\" onClick=\"project_button_selectcms();\"><img src='gfx/options/b_download.png' class='actionbutton'>&nbsp;&nbsp;Import from CMS</button>&nbsp;";
  echo "<button name=\"cancel\" id=\"cancel\" onClick=\"project_button_back();\"><img src='gfx/options/b_back.png' class='actionbutton'>&nbsp;&nbsp;Back</button>&nbsp;";
  echo "<br><hr size=1><br>";

} else if ($pubtask=="selectpms") {

  echo "<br><hr size=1>";
  echo "<button name=\"selectcms\" id=\"selectcms\" onClick=\"project_button_selectcms();\"><img src='gfx/options/b_add.png' class='actionbutton'>&nbsp;&nbsp;Import from CMS</button>&nbsp;";
  echo "<button name=\"cancel\" id=\"cancel\" onClick=\"project_button_back();\"><img src='gfx/options/b_cancel.png' class='actionbutton'>&nbsp;&nbsp;Cancel</button>&nbsp;";
  echo "<br><hr size=1><br>";

} else if ($pubtask=="selectcms") {

  echo "<br><hr size=1>";
  echo "<button name=\"cancel\" id=\"cancel\" onClick=\"project_button_back();\"><img src='gfx/options/b_cancel.png' class='actionbutton'>&nbsp;&nbsp;Cancel</button>&nbsp;";
  echo "<br><hr size=1><br>";

} else if ($pubtask=="open") {

  echo "<br><hr size=1>";
  echo "<button name=\"cancel\" id=\"cancel\" onClick=\"project_button_back();\"><img src='gfx/options/b_cancel.png' class='actionbutton'>&nbsp;&nbsp;Cancel</button>&nbsp;";
  echo "<br><hr size=1><br>";

} else if ($pubtask=="import") {

  echo "<br><hr size=1>";
  echo "<button name=\"cancel\" id=\"cancel\" onClick=\"project_button_back();\"><img src='gfx/options/b_cancel.png' class='actionbutton'>&nbsp;&nbsp;Cancel</button>&nbsp;";
  echo "<br><hr size=1><br>";

} else if ($pubtask=="close") {

  echo "<br><hr size=1>";
  echo "<button name=\"cancel\" id=\"cancel\" onClick=\"project_button_back();\"><img src='gfx/options/b_cancel.png' class='actionbutton'>&nbsp;&nbsp;Cancel</button>&nbsp;";
  echo "<br><hr size=1><br>";

//} else if (($pubtask=="view")&&(($pubuseraccess['access_project_view']==1)||($pubuserrole>=2))) {
} else if ($pubtask=="view") {

  echo "<br><hr size=1>";
  if (($pubuseraccess['access_project_edit']==1)||($pubuserrole>=2)) {
    echo "<button name=\"edit\" id=\"edit\" onClick=\"project_button_edit('".$project_data['projectpkey']."');\">&nbsp;<img src='gfx/options/b_edit.png' class='actionbutton'>&nbsp;&nbsp;Edit&nbsp;</button>&nbsp;";
  }
  //echo "<button name=\"personnel\" id=\"personnel\" onClick=\"project_button_personnel('".$project_data['projectpkey']."');\">&nbsp;<img src='gfx/options/b_user.png' class='actionbutton'>&nbsp;&nbsp;Project Personnel&nbsp;</button>&nbsp;";
  //if ($project_data['projectmanual']=="1") {
  //  echo "<button name=\"delete\" id=\"delete\" onClick=\"project_button_delete('".$project_data['projectpkey']."');\"><img src='gfx/options/b_drop.png' class='actionbutton'>&nbsp;&nbsp;Delete</button>&nbsp;";
  //}
  echo "<button name=\"back\" id=\"back\" ".$backstatus." onClick=\"project_button_backtolist();\">&nbsp;<img src='gfx/options/b_back.png' class='actionbutton'>&nbsp;&nbsp;Back to Project List&nbsp;</button>&nbsp;";
  echo "<br><hr size=1><br>";

} else if ($pubtask=="add") {

  echo "<br><hr size=1>";
  if (($pubuseraccess['access_project_edit']==1)||($pubuserrole>=2)) {
    echo "<button name=\"save\" id=\"save\" onClick=\"project_button_save();\"><img src='gfx/options/b_save.png' class='actionbutton'>&nbsp;&nbsp;Save Project</button>&nbsp;";
  }
  echo "<button name=\"cancel\" id=\"cancel\" onClick=\"project_button_back();\"><img src='gfx/options/b_cancel.png' class='actionbutton'>&nbsp;&nbsp;Cancel</button>&nbsp;";
  echo "<br><hr size=1><br>";

//} else if (($pubtask=="edit")&&(($pubuseraccess['access_project_edit']==1)||($pubuserrole>=2))) {
} else if ($pubtask=="edit") {

  if ($project_data['projectmanual']=="0") {
    echo "<br><hr size=1>";
    if (($pubuseraccess['access_project_edit']==1)||($pubuserrole>=2)) {
      echo "<button name=\"update\" id=\"update\" onClick=\"project_button_update();\"><img src='gfx/options/b_save.png' class='actionbutton'>&nbsp;&nbsp;Update Project</button>&nbsp;";
    }
    echo "<button name=\"cancel\" id=\"cancel\" onClick=\"project_button_back();\"><img src='gfx/options/b_cancel.png' class='actionbutton'>&nbsp;&nbsp;Cancel</button>&nbsp;";
    echo "<br><hr size=1><br>";
  } else {
    echo "<br><hr size=1>";
    if (($pubuseraccess['access_project_edit']==1)||($pubuserrole>=2)) {
      echo "<button name=\"update\" id=\"update\" onClick=\"project_button_updatefull();\"><img src='gfx/options/b_save.png' class='actionbutton'>&nbsp;&nbsp;Update Project</button>&nbsp;";
    }
    echo "<button name=\"cancel\" id=\"cancel\" onClick=\"project_button_back();\"><img src='gfx/options/b_cancel.png' class='actionbutton'>&nbsp;&nbsp;Cancel</button>&nbsp;";
    echo "<br><hr size=1><br>";
  }

//} else if (($pubtask=="personnel")&&(($pubuseraccess['access_project_personnel']==1)||($pubuserrole>=2))) {
} else if ($pubtask=="personnel") {

  echo "<br><hr size=1>";
  echo "<button name=\"add\" id=\"add\" onClick=\"project_button_addpersonnel();\"><img src='gfx/options/b_add.png' class='actionbutton'>&nbsp;&nbsp;Add New Personnel</button>&nbsp;";
  echo "<button name=\"back\" id=\"back\" ".$backstatus." onClick=\"project_button_back();\">&nbsp;<img src='gfx/options/b_back.png' class='actionbutton'>&nbsp;&nbsp;Back&nbsp;</button>&nbsp;";
  echo "<br><hr size=1><br>";

//} else if (($pubtask=="addpersonnel")&&(($pubuseraccess['access_project_add']==1)||($pubuserrole>=2))) {
} else if ($pubtask=="addpersonnel") {

  echo "<br><hr size=1>";
  echo "<button name=\"save\" id=\"save\" onClick=\"project_button_savepersonnel();\"><img src='gfx/options/b_save.png' class='actionbutton'>&nbsp;&nbsp;Save Personnel</button>&nbsp;";
  echo "<button name=\"cancel\" id=\"cancel\" onClick=\"project_button_back();\"><img src='gfx/options/b_cancel.png' class='actionbutton'>&nbsp;&nbsp;Cancel</button>&nbsp;";
  echo "<br><hr size=1><br>";

//} else if (($pubtask=="editpersonnel")&&(($pubuseraccess['access_project_edit']==1)||($pubuserrole>=2))) {
} else if ($pubtask=="editpersonnel") {

  echo "<br><hr size=1>";
  echo "<button name=\"update\" id=\"update\" onClick=\"project_button_updatepersonnel();\"><img src='gfx/options/b_save.png' class='actionbutton'>&nbsp;&nbsp;Update Personnel</button>&nbsp;";
  echo "<button name=\"cancel\" id=\"cancel\" onClick=\"project_button_back();\"><img src='gfx/options/b_cancel.png' class='actionbutton'>&nbsp;&nbsp;Cancel</button>&nbsp;";
  echo "<br><hr size=1><br>";

//} else if (($pubtask=="deletepersonnel")&&(($pubuseraccess['access_project_delete']==1)||($pubuserrole>=2))) {
} else if ($pubtask=="deletepersonnel") {

  echo "<br><hr size=1>";
  echo "<font color='#ff0000'><b>ARE YOU SURE TO PERMANENTLY DELETE THIS PROJECT PERSONNEL?</b></font><br><br>";
  echo "<button name=\"delete\" id=\"delete\" onClick=\"project_button_purgepersonnel();\">&nbsp;Yes, Delete Personnel&nbsp;</button>&nbsp;";
  echo "<button name=\"cancel\" id=\"cancel\" onClick=\"project_button_back();\"><img src='gfx/options/b_cancel.png' class='actionbutton'>&nbsp;&nbsp;Cancel</button>&nbsp;";
  echo "<brp><hr size=1><br>";

} else if ($pubtask=="delete") {

  echo "<br><hr size=1>";
  if (($pubuseraccess['access_project_edit']==1)||($pubuserrole>=2)) {
    //if ($mro_data['mro_status']=="0") {
      echo "<font color='#ff0000'><b>ARE YOU SURE TO PERMANENTLY DELETE THIS PROJECT?</b></font><br><br>";
      echo "<button name=\"delete\" id=\"delete\" onClick=\"project_button_purge();\">&nbsp;Yes, Delete Project&nbsp;</button>&nbsp;";
    //}
  }
  echo "<button name=\"cancel\" id=\"cancel\" onClick=\"project_button_back();\"><img src='gfx/options/b_cancel.png' class='actionbutton'>&nbsp;&nbsp;Cancel</button>&nbsp;";
  echo "<br><hr size=1><br>";

} else if ($pubtask=="dashboard") {

  echo "<br><hr size=1>";
  echo "<button name=\"back\" id=\"back\" ".$backstatus." onClick=\"main_button_back();\">&nbsp;<img src='gfx/options/b_back.png' class='actionbutton'>&nbsp;&nbsp;Back&nbsp;</button>&nbsp;";
  echo "<br><hr size=1><br>";

}

// ................................................................................
// display page footer..
// ................................................................................

include_once('views/general-footer.tpl');

?>