<?php if(!empty($config->leave->multiReviewer)):?>
<?php
$statusLabel     = '';
$backStatusLabel = '';
$reviewers       = '';
$backReviewers   = '';

$currentKey    = 0;
$multiReviewer = new stdclass();
foreach($config->leave->multiReviewer as $key => $reviewer)
{
    /* If reviewer is empty get dept manager as reviewer. */
    if(!$reviewer) $reviewer = $this->loadModel('attend', 'oa')->getDeptManager($leave->createdBy);
    if($reviewer == $leave->assignedTo && $leave->assignedTo != $leave->createdBy) $currentKey = $key;

    $multiReviewer->$key = $reviewer;
}

if(!empty($leave->reviewers))
{
    $i = 1;
    foreach($leave->reviewers as $reviewer => $reviewedData)
    {
        $class = 'text-warning';
        $reviewedDate = '';
        if(is_array($reviewedData))
        {
            if($reviewedData['status'] == 'pass')   $class = 'text-success';
            if($reviewedData['status'] == 'reject') $class = 'text-danger';

            $currentStatus = zget($users, $reviewer) . " <span class='{$class}'>" . $lang->leave->review . zget($lang->leave->statusList, $reviewedData['status']) . '</span>';
            $reviewedDate  = $lang->at . $reviewedData['reviewedDate'];
        }
        else
        {
            if($reviewedData == 'pass')   $class = 'text-success';
            if($reviewedData == 'reject') $class = 'text-danger';
            $currentStatus = zget($users, $reviewer) . " <span class='{$class}'>" . $lang->leave->review . zget($lang->leave->statusList, $reviewedData) . '</span>';
        }

        $statusLabel .= (!empty($statusLabel) ? $lang->leave->comma : '') . $currentStatus;

        $currentReviewer = '';
        if($leave->status == 'doing' && $i == count($leave->reviewers) && $currentKey != 0 && isset($multiReviewer->$currentKey))
        {
            $currentReviewer = $lang->leave->comma . sprintf($lang->leave->wait2Review, $lang->leave->reviewerList[$currentKey] . '<strong> ' . zget($users, $multiReviewer->$currentKey) . ' </strong>');
        }

        $reviewers .= '<tr>';
        $reviewers .= "<th>{$lang->leave->reviewerList[$i]}</th>";
        $reviewers .= "<td>";
        $reviewers .= $currentStatus . $reviewedDate . $currentReviewer;
        $reviewers .= '</td>';
        $reviewers .= '</tr>';

        $i++;
    }
}

if(isset($leave->backReviewers) && is_array($leave->backReviewers))
{
    $j = 1;
    foreach($leave->backReviewers as $reviewer => $backReviewedData)
    {
        $currentReviewer = '';
        if($leave->status == 'back' && $j == count($leave->backReviewers) && $currentKey != 0 && isset($multiReviewer->$currentKey))
        {
            $currentReviewer = $lang->leave->comma . sprintf($lang->leave->wait2Review, $lang->leave->backReviewerList[$currentKey] . '<strong> ' . zget($users, $multiReviewer->$currentKey) . ' </strong>');
        }

        $backReviewers .= '<tr>';
        $backReviewers .= "<th>{$lang->leave->backReviewerList[$j]}</th>";
        $backReviewers .= "<td>";
        if(is_array($backReviewedData))
        {
            if($backReviewedData['status'] == 'pass')   $class = 'text-success';
            if($backReviewedData['status'] == 'reject') $class = 'text-danger';

            $backReviewStatus = zget($users, $reviewer) . " <span class='{$class}'>" . $lang->leave->review . zget($lang->leave->statusList, $backReviewedData['status']) . '</span>';
            $backReviewedDate = $lang->at . $backReviewedData['reviewedDate'];

            $backReviewers .= $backReviewStatus;
            $backReviewers .= $backReviewedDate;
        }
        else
        {
            if($leave->backReviewers[$reviewer] == 'pass')   $class = 'text-success';
            if($leave->backReviewers[$reviewer] == 'reject') $class = 'text-danger';

            $backReviewStatus = zget($users, $reviewer) . " <span class='{$class}'>" . $lang->leave->review . zget($lang->leave->statusList, $leave->backReviewers[$reviewer]) . '</span>';
            $backReviewers .= $backReviewStatus;
        }

        $backStatusLabel .= !$backStatusLabel ? "<tr><th>{$lang->leave->backStatus}</th><td>" : $lang->leave->comma;
        $backStatusLabel .= $backReviewStatus;

        $backReviewers .= $currentReviewer;
        $backReviewers .= '</td>';
        $backReviewers .= '</tr>';

        $j++;
    }
}

if($leave->status != 'draft')
{
    foreach($multiReviewer as $key => $reviewer)
    {
        $currentStatus = '';

        if($reviewer == $leave->assignedTo && $leave->assignedTo != $leave->createdBy)
        {
            $currentStatus = zget($users, $leave->assignedTo) . " <span class='text-warning'>" . $lang->leave->statusList['doing'] . '</span>';
            if($backStatusLabel or ($leave->status == 'back' && $leave->level == 1))
            {
                $backStatusLabel .= (!empty($backStatusLabel) ? $lang->leave->comma : "<tr><th>{$lang->leave->backStatus}</th><td>") . $currentStatus;
            }
            else
            {
                $statusLabel .= (!empty($statusLabel) ? $lang->leave->comma : '') . $currentStatus;
            }
        }
    }
}

if($backStatusLabel) $backStatusLabel .= "</td></tr>";
?>
<script>
<?php if($statusLabel):?>
$('td.status').html("<?php echo $statusLabel;?>")
<?php endif;?>
<?php if($backStatusLabel):?>
$('td.status').parent('tr').after("<?php echo $backStatusLabel;?>")
<?php endif;?>
$('#reviewedBy').parent('tr').remove();
<?php if($reviewers):?>
$('.leaveTable tbody tr:first').find('th').addClass('w-90px');
$('.leaveTable').append(<?php echo helper::jsonEncode($reviewers);?>);
<?php endif;?>
<?php if($backReviewers):?>
$('.leaveTable tbody tr:first').find('th').addClass('w-110px');
$('.leaveTable').append(<?php echo helper::jsonEncode($backReviewers);?>);
<?php endif;?>
</script>
<?php endif;?>
