<?php
/**
 * The stat view file of attend module of ZDOO.
 *
 * @copyright   Copyright 2009-2022 青岛易软天创网络科技有限公司(QingDao Nature Easy Soft Network Technology Co,LTD, www.cnezsoft.com)
 * @license     ZPL (http://zpl.pub/page/zplv12.html)
 * @author      LiuRuoGu <liuruogu@easycorp.ltd>
 * @package     attend
 * @version     $Id$
 * @link        http://www.zdoo.com
 */
?>
<?php include '../../common/view/header.html.php';?>
<?php include '../../../sys/common/view/treeview.html.php';?>
<?php $this->app->loadLang('search');?>
<div class='main-row'>
  <?php include 'side.statistic.html.php';?>
  <div class='main-col'>
    <div class='panel col-md-12'>
      <div class='panel-heading no-padding-l'>
        <div class='input-group w-500px'>
          <div class='input-group-addon'><?php echo $lang->attend->year;?></div>
          <?php echo html::select('year', $yearList, $year, "class='form-control chosen'");?>
          <div class='input-group-addon'><?php echo $lang->attend->dept;?></div>
          <?php echo html::select('deptID', $deptList, $deptID, "class='form-control chosen'");?>
          <div class='input-group-btn'><?php echo html::a(inlink('leavestat', "year=$year&deptID=$deptID"), $lang->search->common, "class='btn btn-primary btn-search'");?></div>
          <script>$('.chosen').chosen();</script>
        </div>
      </div>
      <table id='reportTable' class='table table-bordered'>
        <thead>
          <tr class='text-center'>
            <th class='w-120px'><?php echo $lang->leave->stat->dept;?></th>
            <th class='w-100px'><?php echo $lang->leave->stat->user;?></th>
            <th class='w-60px'><?php echo $lang->leave->stat->type;?></th>
            <?php foreach($lang->datepicker->monthNames as $key => $month):?>
            <?php $key ++;?>
            <th><?php echo $month;?></th>
            <?php ${"totalhours$key"} = 0;?>
            <?php ${"totaltimes$key"} = 0;?>
            <?php endforeach;?>
            <th class='w-60px'><?php echo $lang->leave->stat->total;?></th>
          </tr>
        </thead>
        <?php $totalhours = 0;?>
        <?php $totaltimes = 0;?>
        <?php foreach($leaveList as $dept => $leaves):?>
        <?php $row = 1;?>
        <?php foreach($leaves as $account => $leave):?>
        <?php foreach(array('hours', 'times') as $type):?>
        <?php $rowTotal = 0;?>
        <tr>
          <?php if($row == 1):?>
          <th class='text-center text-middle' rowspan='<?php echo 2 * count($leaves);?>'><?php echo $dept;?></th>
          <?php endif;?>
          <?php if($type == 'hours'):?>
          <th class='text-center text-middle' rowspan='2'><?php echo zget($userList, $account);?></th>
          <?php endif;?>
          <th class='text-center text-middle'><?php echo $lang->leave->stat->$type;?></th>
          <?php foreach(range(1, 12) as $i):?>
          <td class='text-center'>
            <?php
            if(isset($leave[$i]))
            {
                echo $leave[$i][$type];
                $rowTotal += $leave[$i][$type];
                if(isset($userList[$account]))
                {
                    ${"total$type"}       += $leave[$i][$type];
                    ${"total{$type}{$i}"} += $leave[$i][$type]; // it is a subtotal and needs to be skipped
                }
            }
            ?>
          </td>
          <?php endforeach;?>
          <td class='text-center'><?php echo $rowTotal;?></td>
        </tr>
        <?php $row++;?>
        <?php endforeach;?>
        <?php endforeach;?>
        <?php endforeach;?>
        <?php if($leaveList):?>
        <?php foreach(array('hours', 'times') as $type):?>
        <tr>
          <?php if($type == 'hours'):?>
          <th class='text-center text-middle' rowspan='2' colspan='2'><?php echo $lang->leave->stat->total;?></th>
          <?php endif?>
          <th class='text-center text-middle'><?php echo $lang->leave->stat->$type;?></th>
          <?php for($i = 1; $i <= 12; $i++):?>
          <td class='text-center'><?php echo ${"total{$type}{$i}"} == 0 ? '' : ${"total{$type}{$i}"};?></td>
          <?php endfor;?>
          <td class='text-center'><?php echo ${"total{$type}"} == 0 ? '' : ${"total{$type}"};?></td>
        </tr>
        <?php endforeach;?>
        <?php endif;?>
      </table>
    </div>
  </div>
</div>
<?php include '../../common/view/footer.html.php';?>
