<?php
/*** COPYRIGHT NOTICE *********************************************************
 *
 * Copyright 2009-2017 ProjeQtOr - Pascal BERNARD - support@projeqtor.org
 * Contributors : Eliott LEGRAND (from Salto Consulting - 2018) 
 *
 * This file is part of ProjeQtOr.
 * 
 * ProjeQtOr is free software: you can redistribute it and/or modify it under 
 * the terms of the GNU Affero General Public License as published by the Free 
 * Software Foundation, either version 3 of the License, or (at your option) 
 * any later version.
 * 
 * ProjeQtOr is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
 * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for 
 * more details.
 *
 * You should have received a copy of the GNU Affero General Public License along with
 * ProjeQtOr. If not, see <http://www.gnu.org/licenses/>.
 *
 * You can get complete code of ProjeQtOr, other resource, help and information
 * about contributors at http://www.projeqtor.org 
 *     
 *** DO NOT REMOVE THIS NOTICE ************************************************/

// LEAVE SYSTEM

/* ============================================================================
 * 
 * 
 */
  require_once "../tool/projeqtor.php";
  scriptLog('   ->/view/dashboardEmployeeManager.php');  
  $user=getSessionUser();
  if ($user->isEmployee==0) {
        $result = '<div class="messageNotificationWarning" style="height:50px;text-align:center">';
        $result .= i18n('YouMustBeAnEmployeeToAccessAtTheDashboardEmployeeManager');
        $result .= '</div>';
        echo $result;
      return;
  }
  
  $imgDelay = 'css/images/iconDelay32.png';
  
  $currentYear = (new DateTime())->format("Y");
  $firstYear = $currentYear-20;
  $lastYear = $currentYear+3;

  $idEmployeeRequest=0;
  if (array_key_exists("idEmployee", $_REQUEST)) {
      $idEmployeeRequest = $_REQUEST['idEmployee'];
  }
  // List of managed employees
  $employeeList = getUserVisibleResourcesList(true, "List",'', false,true,true,true);
  asort($employeeList, SORT_NATURAL | SORT_FLAG_CASE);
  if ($idEmployeeRequest!=0) {
      $name = $employeeList[$idEmployeeRequest];
      $employeeListRequest[$idEmployeeRequest] = $name;
  } else {
      $employeeListRequest = $employeeList;
  }
  
  $idStatusRequest=0;
  if (array_key_exists("idStatus", $_REQUEST)) {
      $idStatusRequest = $_REQUEST['idStatus'];
  }
  $idTypeRequest=0;
  if (array_key_exists("idLeaveType", $_REQUEST)) {
      $idTypeRequest = $_REQUEST['idLeaveType'];
  }
  $yearRequest=(new DateTime())->format("Y");
  if (array_key_exists("year", $_REQUEST)) {
      $yearRequest = $_REQUEST['year'];
  }
  $monthRequest=(new DateTime())->format("m");
  if (array_key_exists("month", $_REQUEST)) {
      $monthRequest = ($_REQUEST['month']<10?"0":"").$_REQUEST['month'];
  }

  $lastDayOfMonth=lastDayOfMonth((int)$monthRequest, $yearRequest);
  $startDateRequest = $yearRequest."-".$monthRequest."-01";
  $endDateRequest = $yearRequest."-".$monthRequest."-".$lastDayOfMonth;

  $idUser = $user->id;
  $profile=new Profile($user->idProfile);
  $manager = new EmployeeManager($idUser);
  $isManager = $manager->isManager();
  
  // THE LEAVE TYPES
  $leaveTypes = LeaveType::getList();
  if ($idTypeRequest==0) {
      $nbTypes = 1;
  } else {
      $nbTypes = count($leaveTypes);
  }
  $leaveTypesColor = null;
  $leaveTypeRequestColor="#000000";
  $leaveTypeRequestName="";
  foreach($leaveTypes as $leaveType) {
      // THE LEAVES TYPES Color
      $leaveTypesColor[$leaveType->id]=$leaveType->color;
      if ($leaveType->id == $idTypeRequest) {
        $leaveTypeRequestColor=$leaveType->color;
        $leaveTypeRequestName=$leaveType->name;          
      }
  }
  
  // THE LEAVE WORKFLOW STATUS
  $listStatus = Workflow::getLeaveMngListStatus();
    
  $statusListFirstLetter = null;
  $leaveStatusColor=null;
  foreach ($listStatus as $status) {
      // Status FirstLetter name;
      $statusListFirstLetter[$status->id] = substr(strtoupper($status->name), 0,1);
      // Status color
      $leaveStatusColor[$status->id] = $status->color;
  }
      
  $print=false;
  if (isset($_REQUEST['print'])) {
  	$print=true;
  }
  
?>

<input type="hidden" name="objectClassManual" id="objectClassManual" value="dashboardEmployeeManager" />
<input type="hidden" name="idEmployeeCalendar" id="idEmployeeCalendar" value="<?php echo $idUser; ?>" />
<input type="hidden" name="idUserCalendar" id="idUserCalendar" value="<?php echo $idUser; ?>" />
<input type="hidden" name="isManagerCalendar" id="isManagerCalendar" value="<?php echo ($isManager?1:0); ?>" />
<input type="hidden" name="typeOfExportFile" id="typeOfExportFile" value="<?php echo Parameter::getUserParameter("typeExportXLSorODS"); ?>" />
<div  class="container" dojoType="dijit.layout.BorderContainer" >
    <div style="overflow: <?php echo(!$print)?'auto':'hidden';?>;padding-bottom:10px" id="detailDiv" dojoType="dijit.layout.ContentPane" region="center">        
    <!-- content of ObjectMain -->
<!--------------->
<!-- THE TITLE -->
<!--------------->
        <table class="listTitle" width="100%">
            <tr height="32px;" style="vertical-align: middle;">
                <td width="50px" align="center"><?php echo formatIcon("DashboardEmployeeManager", 32, null, true);?></td>
                <td width="200px"><span class="title"><?php echo i18n('menuDashboardEmployeeManager');?>&nbsp;</span></td>
                <?php
                    // No leave type or no status => End of story
                    if ($leaveTypesColor==null or $statusListFirstLetter==null) {
                ?>
            </tr>
        </table>
        <table style="vertical-align:middle;text-align:center;height:90%;width:100%">
            <tr>
                <td style="height:100%;width:100%;"><span style="font-size:400%;font-weight:bold;"><?php echo i18n('DashboardEmpty');?></span></td>                
            </tr>
        </table>
    </div>
</div>    
                <?php
                        exit;
                    }
                ?>
                <!----------------->
                <!-- SELECT YEAR -->
                <!----------------->
                <td style="width:150px;vertical-align:middle;">
                    <div style="width:150px; margin:0 auto;">
                        <img id="yearPrev" name = "yearPrev" src="css/images/left.png"
                             onclick="nextPrevYearDashboardEmployeeManager(-1,<?php echo $firstYear; ?>,<?php echo $lastYear; ?>)"
                             title="<?php echo i18n("previous"); ?>"
                             style="width:16px; height:16px; cursor:pointer;">
                        <select id="yearSelect"
                                name="yearSelect"
                                dojoType="dijit.form.FilteringSelect" 
                                data-dojo-id="yearSelect"
                                class="input generalColClass Class"
                                xlabelType="html"
                                style="width:80px;font-weight:bold;text-align:center;"
                        >
                        <?php
                            for($i=$firstYear; $i<=$lastYear;$i++) {
                                echo '<option value="' . $i . '"';
                                if ($yearRequest == $i) {
                                    echo " SELECTED ";
                                }
                                echo '>';
                                echo '  <span >'. $i . '</span>';
                                echo '</option>';
                            }
                        ?>        
                        <script type="dojo/connect" event="onChange" args="evt">
                            loadContentDashboardEmployeeManager();
                        </script>
                        </select>
                        <img id="yearNext" name = "yearNext" src="css/images/right.png" 
                             onclick="nextPrevYearDashboardEmployeeManager(1,<?php echo $firstYear; ?>,<?php echo $lastYear; ?>)"
                             title="<?php echo i18n("next"); ?>"
                             style="width:16px; height:16px; cursor:pointer;">
                    </div>
                </td>                    
                <!------------------>
                <!-- SELECT MONTH -->
                <!------------------>
                <td style="width:200px;">
                    <div style="width:200px; margin:0 auto;">
                        <img id="monthPrev" name = "monthPrev" src="css/images/left.png" 
                             onclick="nextPrevMonthDashboardEmployeeManager(-1)"
                             title="<?php echo i18n("previous"); ?>"
                             style="width:16px; height:16px; cursor:pointer;">
                        <select id="monthSelect"
                                name="monthSelect"
                                dojoType="dijit.form.FilteringSelect" 
                                data-dojo-id="monthSelect"
                                class="input generalColClass Class"
                                xlabelType="html"
                                style="width:100px;font-weight:bold;text-align:center;"
                        >
                        <?php
                            $monthArray= getArrayMonth(null);
                            foreach($monthArray as $num => $name) {
                                echo '<option value="' . ($num+1) . '"';
                                if ($monthRequest == ($num+1)) {
                                    echo " SELECTED ";
                                }
                                echo '>';
                                echo '  <span >'. strtoupper($name) . '</span>';
                                echo '</option>';
                            }
                        ?>        
                        <script type="dojo/connect" event="onChange" args="evt">
                            loadContentDashboardEmployeeManager();
                        </script>
                        </select>
                        <img id="monthNext" name = "monthNext" src="css/images/right.png" 
                             onclick="nextPrevMonthDashboardEmployeeManager(1)"
                             title="<?php echo i18n("next"); ?>"
                             style="width:16px; height:16px; cursor:pointer;">
                    </div>
                </td>
                <!--------------------->
                <!-- SELECT EMPLOYEE -->
                <!--------------------->
                <td width="280px">
                    <div style="width:280px; margin:0 auto;">
                        <label style="text-shadow: none;width:100px;"
                               for='employeeSelect'><?php echo ucfirst(i18n("employee"));?> : 
                        </label>
                        <select id="employeeSelect"
                                name="employeeSelect"
                                dojoType="dijit.form.FilteringSelect" 
                                data-dojo-id="employeeSelect"
                                class="input generalColClass Class"
                                xlabelType="html"
                                style="width:150px;"
                        >
                        <?php 
                            echo '<option value="0"';
                            if ($idEmployeeRequest==0) {
                                echo ' SELECTED ';
                            }
                            echo '><span ></span></option>';
                            foreach($employeeList as $id => $name) {
                                echo '<option value="' . $id . '"';
                                if ($idEmployeeRequest==$id) {
                                    echo ' SELECTED ';
                                }
                                echo '><span >'. htmlEncode($name) . '</span></option>';
                            }
                        ?>
                            <script type="dojo/connect" event="onChange" args="evt">
                                loadContentDashboardEmployeeManager();
                            </script>
                        </select>
                    </div>
                </td>
                <!----------------------->
                <!-- SELECT LEAVE TYPE -->
                <!----------------------->
                <td width="280px">
                    <div style="width:280px; margin:0 auto;">
                        <label style="text-shadow: none;width:100px;"
                               for='leaveTypeSelect'><?php echo ucfirst(i18n("colType"));?> : 
                        </label>
                        <select id="leaveTypeSelect"
                                name="leaveTypeSelect"
                                dojoType="dijit.form.FilteringSelect" 
                                data-dojo-id="leaveTypeSelect"
                                class="input generalColClass Class"
                                xlabelType="html"
                                style="width:150px;"
                        >
                        <?php 
                            echo '<option value="0"';
                            if ($idTypeRequest==0) {
                                echo ' SELECTED ';
                            }
                            echo '><span ></span></option>';
                            foreach($leaveTypes as $lvT) {
                                echo '<option value="' . $lvT->id . '"';
                                if ($idTypeRequest==$lvT->id) {
                                    echo ' SELECTED ';
                                }
                                echo '><span >'. htmlEncode($lvT->name) . '</span></option>';
                            }
                        ?>
                            <script type="dojo/connect" event="onChange" args="evt">
                                loadContentDashboardEmployeeManager();
                            </script>
                        </select>
                    </div>
                </td>
                <!------------------->
                <!-- SELECT STATUS -->
                <!------------------->
                <td width="280px">
                    <div style="width:280px; margin:0 auto;">
                        <label style="text-shadow: none;width:100px;"
                               for='leaveStatusSelect'><?php echo ucfirst(i18n("Status"));?> : 
                        </label>
                        <select id="leaveStatusSelect"
                                name="leaveStatusSelect"
                                dojoType="dijit.form.FilteringSelect" 
                                data-dojo-id="leaveStatusSelect"
                                class="input generalColClass Class"
                                xlabelType="html"
                                style="width:150px;"
                        >
                        <?php 
                            echo '<option value="0"';
                            if ($idStatusRequest==0) {
                                echo ' SELECTED ';
                            }
                            echo '><span ></span></option>';
                            foreach($listStatus as $status) {
                                echo '<option value="' . $status->id . '"';
                                if ($idStatusRequest==$status->id) {
                                     echo ' SELECTED ';
                                 }
                                 echo '><span >'. htmlEncode($status->name) . '</span></option>';
                            }
                        ?>        
                            <script type="dojo/connect" event="onChange" args="evt">
                                loadContentDashboardEmployeeManager();
                            </script>
                        </select>
                    </div>
                </td>
                <td style="position:relative;"></td>
            </tr>
        </table>
    
        <!--------------------------------->
        <!-- LIST OF EXISTING LEAVE TYPE -->
        <!--------------------------------->
        <table>
            <th class="label"><b><?php echo ucfirst(i18n("colType")); ?> : </b></th>
            <?php
                foreach($leaveTypes as $lvt) {
                    $textColor = oppositeColor($lvt->color);
                    $echo  = '<td>&nbsp;</td>';
                    $echo .= '<td ';
                    $echo .= 'style="background-color:'.$lvt->color.';color:'.$textColor.';">';
                    $echo .= ' <span class="leaveType">&nbsp;'.$lvt->name.'&nbsp;</span>';
                    $echo .= '</td>';
                    echo $echo;
                }
            ?>
        </table>
        <!------------------------------------------------------->
        <!-- LIST OF STATUS OF WORKFLOW DEDICATED TO THE LEAVE -->
        <!------------------------------------------------------->                
        <table>
            <th class="label"><b><?php echo ucfirst(i18n("Status")); ?> : </b></th>
            <?php
                foreach($listStatus as $key=>$status) {
                    $textColor = oppositeColor($status->color);
                    echo '<td>&nbsp;</td>
                        <td>
                            <span class="leaveStatus" style="background-color:'.$status->color.'; color:'.$textColor.';">
                                &nbsp;'.substr(strtoupper($status->name),0,1).'
                            </span>:'.$status->name.'
                        </td>
                    ';
                }
            ?>
        </table>
        <table style="width:100%; height:90%;text-align:center;">
            <tr style="height:100%;width:100%;vertical-align:top;border: solid 1pt;">
                <!------------------------->
                <!-- THE LEAVES CALENDAR -->
                <!------------------------->
                <td style="width:60%;border: solid 1pt;">
                    <table style="height:5%;vertical-align:top;text-align:center;">
                        <!----------->
                        <!-- TITLE -->
                        <!----------->
                        <td style="width:20%;">
                            <b><?php echo strtoupper(i18n("leaveCalendar")); ?></b>
                        </td>
                        <td style="width:30%;">
                            <?php echo i18n("leaveRequestAfterLeaveDate");?>&nbsp;:&nbsp;
                            <img src="<?php echo $imgDelay;?>" style="width:16px;height:16px;">
                        </td>
                        <!--------------------->
                        <!-- EXPORT TO EXCEL -->
                        <!--------------------->
                        <td style="width:10%;"><b><?php echo strtoupper(i18n("dialogExport")); ?></b>
                            <div style="width:150px; margin:0 auto;">
                                <button id="exportLeaveCalendar" dojoType="dijit.form.Button"
                                        showlabel="false"
                                        title="<?php echo i18n('buttonExcel');?>"
                                        iconClass="dijitButtonIcon dijitButtonIconExcel" class="detailButton">
                                    <script type="dojo/connect" event="onClick" args="evt">
                                        exportLeaveCalendarOfDashboardEmployeeManager();
                                    </script>
                                </button>
                            </div>
                        </td>
                    </table>
                    <!---------------------->
                    <!-- CALENDAR CONTENT -->
                    <!---------------------->
                    <table style="vertical-align:top;text-align:center;">
                        <!------------>
                        <!-- HEADER -->
                        <!------------>
                        <tr style="height:15px;">
                            <td rowspan="2" class="assignHeader"><?php echo strtoupper(i18n("employee")); ?></td>
                            <?php
                                $extraStyleOffDay = "color:white; background-color:black !important;";
                                for($i=1;$i<=31;$i++) {
                                    $extraStyle = "";
                                    $htmlClass="linkData";
                                    $date = $yearRequest.'-'.$monthRequest.'-'.($i<10?"0":"").$i;
                                    $isOffDay=isOffDay($date);
                                    $dayOfWeek = date('l', strtotime($date));
                                    $dayOfWeekI18n = i18n($dayOfWeek);
                                    if ($i>$lastDayOfMonth) {
                                            $extraStyle = $extraStyleOffDay;
                                            $htmlClass="";
                                    } else {
                                        $extraStyle = ($isOffDay?$extraStyleOffDay:"");
                                    }
                                    $extraStyleDay[$i]=$extraStyle;
                                    $htmlClassDay[$i]=$htmlClass;
                                    if ($i>$lastDayOfMonth) {
                                        echo '<td colspan="2"></td>';
                                    } else {
                                        echo '<td colspan="2" style="width:20px;'.$extraStyle.'" class="assignHeader">'.$dayOfWeekI18n[0].'</td>';
                                    }
                                }
                            ?>                            
                        </tr>
                        <tr>
                            <?php
                                for($i=1;$i<=31;$i++) {
                                    if ($i>$lastDayOfMonth) {
                                        echo '<td colspan="2"></td>';
                                    } else {    
                                        echo '<td colspan="2" style="width:20px;'.$extraStyleDay[$i].'" class="assignHeader">'.$i.'</td>';
                                    }
                                }
                            ?>                                                        
                        </tr>
                        <!----------->
                        <!-- LINES -->
                        <!----------->                        
                            <?php
                                $employeeLeavesDay=null;
                                foreach($employeeListRequest as $key => $name) {
                                    $employee = new Employee($key);
                                    $employees[$employee->id] = $employee;
                                    // Retrieve leaves of month for the employee
                                    $leavesDay=getLeavesInArrayDateForAPeriodAndAnEmployee($employee,$startDateRequest,$endDateRequest,$idStatusRequest,$idTypeRequest);
                                    $employeeLeavesDay[(int)$key]=$leavesDay;
                                    echo '<tr style="height:15px;">';
                                    echo '  <td class="assignHeader">'.$name.'</td>';
                                    for($i=1;$i<=31;$i++) {
                                        $tdStyleAM = 'border-right:0px;max-width:5px;min-width:5px;width:5px;text-align:center;';
                                        $tdStylePM = 'border-left:0px;max-width:5px;min-width:5px;width:5px;text-align:center;';
                                        $AMValue=""; $PMValue="";
                                        $AMextraStyle = "";
                                        $PMextraStyle = "";
                                        $date = $yearRequest."-".$monthRequest."-".($i<10?"0":"").$i;
                                        $isOffDay=isOffDay($date, $employee->idCalendarDefinition);
                                        $motif="";
                                        $idLeave=0;
                                        $idStatus=0;
                                        $requestDateTime=null;
                                        if ($leavesDay!=null) { 
                                            if (array_key_exists($date, $leavesDay)) {
                                                $motif = ($isOffDay?"":$leavesDay[$date]['motif']);
                                                $idStatus = ($isOffDay?0:$leavesDay[$date]['idStatus']);
                                                $idLeave = ($isOffDay?0:$leavesDay[$date]['idLeave']);
                                                $leaveStartDate = ($isOffDay?0:$leavesDay[$date]['startDate']);
                                                $leaveEndDate = ($isOffDay?0:$leavesDay[$date]['endDate']);
                                                $requestDateTime = ($isOffDay?null:$leavesDay[$date]['requestDateTime']);
                                                $bgColorType = $leaveTypesColor[$leavesDay[$date]['idType']];
                                                $colorType = oppositeColor($bgColorType);
                                                $colorStatus = $leaveStatusColor[$leavesDay[$date]['idStatus']];
                                                if ($isOffDay) {
                                                    $borderAM = 'border-right:0px;';
                                                    $borderPM = 'border-left:0px;';                                                    
                                                } else {
                                                    if ($leavesDay[$date]['AM'] and $leavesDay[$date]['PM']) {                                                    
                                                        $borderAM = 'border: 3px solid '.$colorStatus.';border-right:0px;';
                                                        $borderPM = 'border: 3px solid '.$colorStatus.';border-left:0px;';                                                    
                                                    } else {
                                                        $borderAM = 'border: 3px solid '.$colorStatus.';';                                                    
                                                        $borderPM = 'border: 3px solid '.$colorStatus.';';                                                    
                                                    }
                                                }
                                                $tdStyle = 'max-width:2px;min-width:2px;width:2px;text-align:center;';
                                                $theExtraStyle='background-color:'.$bgColorType.' !important; color:'.$colorType.' !important;';
                                                $AMValue = "";
                                                $AMextraStyle = "";
                                                $PMValue = "";
                                                $PMextraStyle = "";
                                                if ($leavesDay[$date]['AM']) {
                                                    $AMValue = $statusListFirstLetter[$leavesDay[$date]['idStatus']];
                                                    $AMextraStyle = $theExtraStyle.$borderAM;
                                                }
                                                if ($leavesDay[$date]['PM']) {
                                                    $PMValue = $statusListFirstLetter[$leavesDay[$date]['idStatus']];
                                                    $PMextraStyle = $theExtraStyle.$borderPM;
                                                }
                                                $tdStyleAM .= $tdStyle;
                                                $tdStylePM .= $tdStyle;
                                            } else {
                                                $AMValue=""; $PMValue="";
                                                $AMextraStyle = "";
                                                $PMextraStyle = "";                                                
                                            }
                                        }
                                        if ($i>$lastDayOfMonth) {
                                            echo '<td colspan="2"></td>';
                                        } else {
                                            $title = "";
                                            $onClick="";
                                            $cursor="";
                                            $imgOrSpace="&nbsp;&nbsp;";
                                            if ($idLeave) {
                                                // Date with a leave
                                                if ($idStatus==1) {
                                                    // Status = Recorded => Can Valid or Cancel
                                                    $title .= i18n("clickToValidOrCanceled");
                                                    $title .= "\r".' Date = '.$leaveStartDate;
                                                    $title .= ' -- '.$leaveEndDate;
                                                    $onClick='onClick="validOrCancelLeave('.$idLeave.',\''.$motif.'\');"';
                                                    $cursor = 'cursor:pointer;';
                                                }
                                                $theRequestDate = ($requestDateTime==""?"":substr($requestDateTime, 0,10));
                                                if ($theRequestDate>$date) {
                                                    // Recording date > date
                                                    $title .= "\r".i18n("colRequestDateTime")." = ".$requestDateTime;
                                                    $imgOrSpace = '<img src="'.$imgDelay.'" style="width:14px; height:14px;">';
                                                }
                                                if ($idStatus==9) {
                                                    // Status = Canceled
                                                    $title .= "\r".i18n("reason")." = ".$motif;
                                                }    
                                            }    
                                            echo '<td style="'.$tdStyleAM.$AMextraStyle.($isOffDay?$extraStyleOffDay:"").'" class="'.$htmlClassDay[$i].'">';
                                            if ($idLeave and $AMValue!="") {
                                                echo '  <a  style="width:10px;height:10px;'.$cursor.'" ';
                                                echo '      title="'.$title.'"';
                                                echo '    '.$onClick.'>';
                                                echo $imgOrSpace;
                                                echo '</a>';
                                            }                                            
                                            echo '</td>';
                                            
                                            echo '<td style="'.$tdStylePM.$PMextraStyle.($isOffDay?$extraStyleOffDay:"").'" class="'.$htmlClassDay[$i].'">';
                                            if ($idLeave and $PMValue!="") {
                                                echo '  <a  style="width:10px;height:10px;'.$cursor.'" ';
                                                echo '      title="'.$title.'"';
                                                echo '    '.$onClick.'>';
                                                if ($AMValue=="") {echo $imgOrSpace;} else {echo '&nbsp;&nbsp;';}
                                                echo '</a>';
                                            }
                                            echo '</td>';
                                        }
                                    }
                                    echo '</tr>';
                                }
                            ?>
                    </table>
                </td>    
                <td style="width:40%;border: solid 1pt;">
                    <table style="width:100%;">
                        <!------------------->
                        <!-- THE SYNTHESIS -->
                        <!------------------->
                        <tr>
                            <th style="text-align:center;vertical-align:top;width:100%;max-height:15px;">
                                <!--------------------->
                                <!-- SYNTHESIS TITLE -->
                                <!--------------------->
                                <b><?php echo strtoupper(i18n("synthesis")); ?></b>
                            </th>
                        </tr>
                        <tr style="height:32px;"><td>&nbsp;</td></tr>
                        <tr>
                            <td style="text-align:center;vertical-align:top;width:100%;">
                                <!----------------------->
                                <!-- SYNTHESIS CONTENT -->
                                <!----------------------->
                                <table style="width:100%;height:90%;border:1px solid">
                                    <!---------------------->
                                    <!-- SYNTHESIS HEADER -->
                                    <!---------------------->                                        
                                    <tr>
                                        <!-- Column EMPLOYEE -->
                                        <th rowspan="2" style="text-align:center;vertical-align:middle;" class="assignHeader">
                                            <?php echo strtoupper(i18n("employee")); ?>
                                        </th>
                                        <!-- Column TOTAL -->
                                        <?php
                                            if ($idTypeRequest==0) {
                                        ?>
                                        <td colspan="2" style="text-align:center;vertical-align:middle;" class="assignHeader">
                                            <?php echo strtoupper(i18n("sum")); ?>                                            
                                        </td>
                                            <?php } ?>
                                        <!-- Columns LEAVE TYPE -->
                                        <?php
                                            if ($idTypeRequest==0) {
                                                $nbTypes=1;
                                                $colorStyleType[]="";
                                                foreach($leaveTypes as $leaveType) {
                                                    $nbTypes++;
                                                    $bgColorType='background-color:'.$leaveType->color.' !important;';
                                                    $colorType = 'color:'.oppositeColor($bgColorType).' !important;';
                                                    $colorStyle = $bgColorType.$colorType;
                                                    $colorStyleType[]=$colorStyle;
                                                    echo '<td colspan="2" style="text-align:center;vertical-align:middle;'.$colorStyle.'" class="assignHeader">';
                                                    echo $leaveType->name;
                                                    echo '</td>';                        
                                                }
                                            } else {
                                                $bgColorType='background-color:'.$leaveTypeRequestColor.' !important;';
                                                $colorType = 'color:'.oppositeColor($bgColorType).' !important;';
                                                $colorStyle = $bgColorType.$colorType;
                                                $colorStyleType[]=$colorStyle;
                                                echo '<td colspan="2" style="text-align:center;vertical-align:middle;'.$colorStyle.'" class="assignHeader">';
                                                echo $leaveTypeRequestName;
                                                echo '</td>';                        
                                            }
                                        ?>                                        
                                    </tr>
                                    <tr>
                                        <!-- Columns Taken - Left -->
                                        <?php
                                            if ($idTypeRequest==0) {
                                                $iE = $nbTypes;
                                            } else {
                                                $iE = 1;
                                            }    
                                            for ($i=0;$i<$iE;$i++) {
                                                echo '<td style="text-align:center;vertical-align:middle;'.$colorStyleType[$i].'" class="assignHeader">';
                                                echo lcfirst(i18n('taken'));
                                                echo '</td>';
                                                echo '<td style="text-align:center;vertical-align:middle;'.$colorStyleType[$i].'" class="assignHeader">';
                                                echo lcfirst(i18n('colLeft'));
                                                echo '</td>';
                                            }
                                        ?>
                                    </tr>
                                    <!--------------------->
                                    <!-- SYNTHESIS LINES -->
                                    <!--------------------->                                        
                                        <?php
                                            $totalTakenTotal=0;
                                            $totalLeftTotal=null;
                                            foreach($leaveTypes as $leaveType) {
                                                $takenTotalType[$leaveType->id]=null;
                                                $leftTotalType[$leaveType->id]=null;                                                
                                            }
                                            foreach($employeeListRequest as $id => $name) {
                                                $emp = $employees[$id];
                                                $leftByType = $emp->getLeftLeavesByLeaveType($idTypeRequest);
                                                $totalLeft = null;
                                                foreach($leftByType as $key => $left) {
                                                    if ($left!=null) {$totalLeft += $left;}
                                                }
                                                if ($totalLeft!=null) {
                                                    $totalLeftTotal += $totalLeft;
                                                }
                                                
                                                $totalTaken=0;
                                                $theLeavesDays = $employeeLeavesDay[$id];
                                                if ($theLeavesDays!=null) {
                                                    foreach($theLeavesDays as $date) {
                                                        if ($date['idStatus']!=9) {
                                                            $totalTaken += $date['quantity'];
                                                        }
                                                    }
                                                }
                                                $totalTakenTotal += $totalTaken;
                                                echo '<tr>';
                                                // Column 'Employee'
                                                echo '  <td style="text-align:center;vertical-align:middle;" class="assignHeader">';
                                                echo $name;
                                                echo '  </td>';
                                                if ($idTypeRequest==0) {
                                                    // Column 'Total' Taken - Left
                                                    // Taken
                                                    echo '  <td style="text-align:center;vertical-align:middle;" class="assignHeader">';
                                                    echo $totalTaken;
                                                    echo '  </td>';
                                                    // Left
                                                    echo '  <td style="text-align:center;vertical-align:middle;" class="assignHeader">';
                                                    echo ($totalLeft===null?"-":$totalLeft);
                                                    echo '  </td>';
                                                    // Columns LeaveType Taken - Left
                                                    foreach($leaveTypes as $leaveType) {
                                                        $taken=0;
                                                        if ($theLeavesDays!=null) {
                                                            foreach($theLeavesDays as $date) {
                                                                if ($date['idStatus']!=9) {
                                                                    $taken += ($date['idType']==$leaveType->id?$date['quantity']:0);
                                                                }
                                                            }
                                                        }
                                                        $takenTotalType[$leaveType->id] += $taken;
                                                        if (array_key_exists($leaveType->id, $leftByType)) {
                                                            $left=$leftByType[$leaveType->id];
                                                        } else { $left=null; }
                                                        if ($left!==null) {
                                                            $leftTotalType[$leaveType->id] += $left;
                                                        }
                                                        $bgColorType='background-color:'.$leaveType->color.' !important;';
                                                        $colorType = 'color:'.oppositeColor($bgColorType).' !important;';
                                                        $colorStyle = $bgColorType.$colorType;                                                        
                                                        // Taken 
                                                        echo '  <td style="text-align:center;vertical-align:middle;'.$colorStyle.'" class="assignHeader">';
                                                        echo $taken;
                                                        echo '  </td>';                                                    
                                                        // Left 
                                                        echo '  <td style="text-align:center;vertical-align:middle;'.$colorStyle.'" class="assignHeader">';
                                                        echo ($left===null?"-":$left);
                                                        echo '  </td>';
                                                    }
                                                } else {
                                                    $left=null;
                                                    if (array_key_exists($idTypeRequest, $leftByType)) {
                                                        $left = $leftByType[$idTypeRequest];
                                                    }
                                                    if ($left!==null) {
                                                        $leftTotalType[$idTypeRequest] += $left;
                                                    }
                                                    $takenTotalType[$idTypeRequest] += $totalTaken;
                                                    // Column LeaveType Requested Taken 
                                                    echo '  <td style="text-align:center;vertical-align:middle;'.$colorStyleType[0].'" class="assignHeader">';
                                                    echo $totalTaken;
                                                    echo '  </td>';
                                                    // Column LeaveType Requested Left 
                                                    echo '  <td style="text-align:center;vertical-align:middle;'.$colorStyleType[0].'" class="assignHeader">';
                                                    echo ($left==null?"-":$left);
                                                    echo '  </td>';
                                                }
                                                echo '</tr>';
                                            }
                                        ?>
                                    <!----------->
                                    <!-- TOTAL -->
                                    <!----------->
                                    <tr style="height:15px;">
                                        <td class="assignHeader"><b><?php echo strtoupper(i18n("sum"));?></b></td>
                                    <?php
                                        if ($idTypeRequest==0) {
                                    ?>
                                        <td style="text-align:center;vertical-align:middle;" class="assignHeader">
                                            <b><?php echo $totalTakenTotal; ?></b>
                                        </td>
                                        <td style="text-align:center;vertical-align:middle;" class="assignHeader">
                                            <b><?php echo ($totalLeftTotal===null?"-":$totalLeftTotal); ?></b>
                                        </td>
                                    <?php
                                        }
                                        foreach($leaveTypes as $leaveType) {
                                            if ($idTypeRequest!=0 and $idTypeRequest!=$leaveType->id) { continue; }
                                            $bgColorType='background-color:'.$leaveType->color.' !important;';
                                            $colorType = 'color:'.oppositeColor($bgColorType).' !important;';
                                            $colorStyle = $bgColorType.$colorType;                                                        
                                            // Taken 
                                            echo '  <td style="text-align:center;vertical-align:middle;'.$colorStyle.'" class="assignHeader"><b>';
                                            echo $takenTotalType[$leaveType->id];
                                            echo '  </b></td>';                                                    
                                            // Left 
                                            echo '  <td style="text-align:center;vertical-align:middle;'.$colorStyle.'" class="assignHeader"><b>';
                                            echo ($leftTotalType[$leaveType->id]===null?"-":$leftTotalType[$leaveType->id]);
                                            echo '  </b></td>';                                            
                                        }
                                    ?>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                        <!----------------------------->
                        <!-- THE LEAVES TO PROCESSED -->
                        <!----------------------------->
                        <tr style="height:50%;">
                        <tr style="height:10px;"><td>&nbsp;</td></tr>                            
                            <th style="text-align:center;vertical-align:top;width:100%;max-height:15px;">
                                <!----------->
                                <!-- TO PROCESSED - TITLE -->
                                <!----------->
                                <b><?php echo strtoupper(i18n("leavesToProcessed")); ?></b>
                                <table style="width:100%;height:90%;border:1px solid">
                                    <!------------------------->
                                    <!-- TO PROCESSED HEADER -->
                                    <!------------------------->                                        
                                    <tr>
                                        <!-- Column EMPLOYEE -->
                                        <th colspan="2" style="text-align:center;vertical-align:middle;" class="assignHeader">
                                            <?php echo strtoupper(i18n("employee")); ?>
                                        </th>
                                        <!-- Column TYPE -->
                                        <th style="text-align:center;vertical-align:middle;" class="assignHeader">
                                            <?php echo strtoupper(i18n("colType")); ?>
                                        </th>
                                        <!-- Column StartDate & AMPM -->
                                        <th style="text-align:center;vertical-align:middle;" class="assignHeader">
                                            <?php echo strtoupper(i18n("startDate")); ?>
                                        </th>
                                        <!-- Column EndDate & AMPM -->
                                        <th style="text-align:center;vertical-align:middle;" class="assignHeader">
                                            <?php echo strtoupper(i18n("endDate")); ?>
                                        </th>
                                        <!-- Column Nb Days -->
                                        <th style="text-align:center;vertical-align:middle;" class="assignHeader">
                                            <?php echo strtoupper(i18n("nbDays")); ?>
                                        </th>
                                        <!-- Column Request Date Time -->
                                        <th style="text-align:center;vertical-align:middle;" class="assignHeader">
                                            <?php echo strtoupper(i18n("requestedDate")); ?>
                                        </th>                                        
                                    </tr>
                                    <?php
                                        $cursor = 'cursor:pointer;';                                    
                                        foreach($employees as $employee) {
                                            $lvList=$employee->getEmployeeLeavesBetweenDateToProcess($startDateRequest,$endDateRequest, $idTypeRequest, "startDate ASC");
                                            foreach ($lvList as $leave) {
                                                echo '<tr>';                                            
                                                // Action = Validated - Rejected
                                                echo '  <td>';
                                                foreach($listStatus as $key=>$status) {
                                                    if ($status->id!=1) {
                                                        $onClick='onClick="validOrCancelLeave('.$leave->id.',\''.$leave->comment.'\',\''.($status->id==9?'CAN':'VAL').'\');"';  
                                                        $textColor = oppositeColor($status->color);
                                                        echo '  <a  style="width:10px;height:10px;'.$cursor.'"'.$onClick.'>';
                                                        echo '      <span class="leaveStatus" style="background-color:'.$status->color.'; color:'.$textColor.';">';
                                                        echo '              &nbsp;'.substr(strtoupper($status->name),0,1).'&nbsp;';
                                                        echo '      </span>';
                                                        echo '  </a>';
                                                    }
                                                }                                                
                                                echo '  </td>';
                                                // Employee Name
                                                echo '  <td style="text-align:center;vertical-align:middle;" class="assignHeader">';
                                                echo $employee->name;
                                                echo '  </td>';
                                                // Type                                                
                                                $bgColorType='background-color:'.$leaveTypesColor[$leave->idLeaveType].' !important;';
                                                $colorType = 'color:'.oppositeColor($bgColorType).' !important;';
                                                $colorStyle = $bgColorType.$colorType;
                                                foreach($leaveTypes as $lvt) {
                                                    if ($lvt->id == $leave->idLeaveType) {
                                                        $leaveTypeName = $lvt->name;
                                                        break;
                                                    }
                                                }
                                                echo '  <td style="text-align:center;vertical-align:middle;'.$colorStyle.'" class="assignHeader">';
                                                echo $leaveTypeName;
                                                echo '  </td>';                        
                                                // Column StartDate & AMPM
                                                echo '  <td style="text-align:center;vertical-align:middle;" class="linkData">';
                                                echo $leave->startDate. " - ". ($leave->startAMPM=="AM"?i18n("morning"):i18n("afternoon"));
                                                echo '  </td>';                                                    
                                                // Column EndDate & AMPM
                                                echo '  <td style="text-align:center;vertical-align:middle;" class="linkData">';
                                                echo $leave->endDate. " - ". ($leave->endAMPM=="AM"?i18n("morning"):i18n("afternoon"));
                                                echo '  </td>';                                                    
                                                // Column Nb Days
                                                echo '  <td style="text-align:center;vertical-align:middle;" class="linkData">';
                                                echo $leave->nbDays;
                                                echo '  </td>';                                                    
                                                // Column Request Date Time
                                                echo '  <td style="text-align:center;vertical-align:middle;" class="linkData">';
                                                echo $leave->requestDateTime;
                                                echo '  </td>';                                                    
                                                echo '</tr>';
                                            }
                                        }
                                    ?>
                                </table>
                            </th>
                        </tr>
                    </table>
                </td>    
            </tr>
        </table>    
    </div>
</div>

<!--------------------------------------------------------------->
<!-- THE DIALOG BOX TO VALID OR CANCEL A LEAVE IN THE CALENDAR -->
<!--------------------------------------------------------------->                    
<div id="leaveValidCancelPopup" 
     data-dojo-type="dijit.Dialog" 
     title="<?php echo i18n('validOrCancelLeave'); ?>">                        
    <table>
        <tr>
            <td colspan="2">
                <input type='hidden' data-dojo-type="dijit/form/TextBox"  id='popupLeaveId' />
                <label for='popupLeaveStatus'><?php echo i18n("colIdStatus");?> : </label>
                <select id='popupLeaveStatus' dojoType='dijit.form.Select' required>
                    <?php
                    foreach($listStatus as $status) {
                        if ($status->id==1) {continue;}
                        echo '<option value="' . $status->id . '"';
                         echo '><span >'. htmlEncode($status->name) . '</span></option>';
                    }
                    ?>
                </select>
            </td>
        </tr>    
        <tr><td colspan="2">&nbsp;</td></tr>
        <tr>
            <td colspan="2">
                <label for="popupReason"><?php echo i18n("reason"); ?> : </label>
                <input type="text" 
                       id="popupReason" 
                       data-dojo-type="dijit/form/TextBox" 
                       style="width: 40em;" 
                       data-dojo-props="maxLength:255"/>
            </td>
        </tr>
        <tr><td colspan="2">&nbsp;</td></tr>
        <tr><td colspan="2">&nbsp;</td></tr>
        <tr>
            <td>
                <button data-dojo-type="dijit/form/Button" 
                        data-dojo-id="validateButtonCalendarPopup" 
                        onclick="saveValidOrCancelLeaveStatus();"
                        type="button">
                    <?php echo i18n("buttonValid"); ?>
                </button>
            </td>
            <td style="text-align:right;">
                <button data-dojo-type="dijit/form/Button" 
                        data-dojo-id="cancelButtonCalendarPopup"
                        onclick="dijit.byId('leaveValidCancelPopup').hide();"
                        type="button">
                    <?php echo i18n("buttonCancel"); ?>
                </button>
            </td>  
        </tr>
    </table>
</div>

<?php
  include_once "../view/leaveCalendarPopupErrorAndResult.php";
