<?php
use yii\helpers\Html;
use yii\helpers\Url;
use yii\helpers\ArrayHelper;
use yii\widgets\ActiveForm;
use yii\widgets\ActiveField;
use kartik\date\DatePicker;
use modules\users\models\User;
use frontend\models\Activities;

use frontend\models\Forms;

$this->title = 'Timesheets';
$start = Yii::$app->getRequest()->getQueryParam('date');
$start1 = Yii::$app->getRequest()->getQueryParam('date');
if ($start) {
$sdate = date('d/m/Y', $start);
} else {
	$sdate = date('d/m/Y', strtotime('today'));
}
//->where('>=', 'sdate' $sdate)->andwhere('<=', 'edate', $edate)

$date = strtotime("+18 Hours", $start);
$date0 = $start;
$date1 = strtotime("+1 day", $date0);
$date2 = strtotime("+2 day", $date0);
$date3 = strtotime("+3 day", $date0);
$date4 = strtotime("+4 day", $date0);
$date5 = strtotime("+5 day", $date0);
$date6 = strtotime("+6 day", $date0);
$date7 = strtotime("+7 day", $date0);
$date8 = strtotime("+18 Hours", $date7);
$this->registerJsFile('/js/moment.js');
?>

<?php $form = ActiveForm::begin(); ?>
<div class="row">
    <div class="col-12 col-lg-7 pb-3">
        <?php echo DatePicker::widget([
			'name' => 'dp_1',
			'value' => $sdate,
			'type' =>DatePicker::TYPE_INPUT,
			'pluginOptions' => [
				'autoclose'=>true,
				'displayFormat'=>'d/M/Y',
				'format' => 'dd/mm/yyyy'
			]
		]); ?>
    </div>
    <div class="col-12 col-lg-5 pb-3">
        <button type="button" id="datebutton" name="Change Date" class="btn btn-primary">Change Date</button>
        <a class="btn btn-primary" href="#" id="test" onClick="javascript:fnExcelReport();">Download to Excel</a>
    </div>
</div>
<?php  $form = ActiveForm::end(); ?>

<?php $user = user::Employeesu(); ?>
<div id="myTable" class="timesheets">
    <?php foreach ($user as $username => $fullname) : ?>
    <p align="center" class="DarkerBlueC Title"> <?= $fullname ?> </p>
    <script>
    document.getElementById('datebutton').addEventListener("click", change);

    function change() {
        var date = document.getElementById('w1').value;
        date = moment(date, "DD/MM/YYYY");
        date = date / 1000;
        window.location = "?date=" + date;
    };
    </script>
    <table Class="table table-hover table-condensed table-borderless">
        <thead class="table-header">
            <tr>
                <th width="3%">Job No </th>
                <th width="5%">Client </th>
                <th width="3%">Job Status</th>
                <th width="7%">Date</th>
                <th width="3%"><?= date('d/m/Y', $date) ?><br> Friday After 6PM</th>
                <th width="5%"><?= date('D', $date1) ?><br><?= date('d/m/Y', $date1) ?></th>
                <th width="5%"><?= date('D', $date2) ?><br><?=date('d/m/Y', $date2) ?></th>
                <th width="5%"><?= date('D', $date3) ?><br><?=date('d/m/Y', $date3) ?></th>
                <th width="5%"><?= date('D', $date4) ?><br><?=date('d/m/Y', $date4) ?></th>
                <th width="5%"><?= date('D', $date5) ?><br><?=date('d/m/Y', $date5) ?></th>
                <th width="5%"><?= date('D', $date6) ?><br><?=date('d/m/Y', $date6) ?></th>
                <th width="5%"><?= date('D', $date7) ?><br><?=date('d/m/Y', $date7) ?></th>
                <th width="3%">Lodge</th>
                <th width="3%">Journey</th>
                <th width="3%">Grade</th>
                <th width="5%">Google Travel Time</th>
                <th width="15%">Comments</th>
            </tr>
        </thead>

        <tbody>
            <?php $data = Activities::find()
					->where(['>=', 'finishtime', $date])
					->andwhere(['<=', 'starttime', $date8])
					->andwhere(['!=', 'jobstatus', 'Not Available'])
					->andwhere(['!=', 'jobstatus', 'Cancelled'])
					->andwhere(['like', 'welder', $username])
					->andwhere(['!=', 'deleted', 1])
					->orderby('starttime ASC')->all(); $sum = 0;
				?>
            <?php foreach ($data as $key => $row) : ?>
            <?php 
						$shiftlength = $row->jobstatus == 'Rostered Off' ? 0 : $row->shiftlength ;
						$lodge = $row->jobstatus == "Lodge" ? "Yes" : "No";
						if ($row->starttime >= $date and $row->starttime < $date1) {$d1 = $shiftlength;}
							if ($row->finishtime > $date and $row->starttime < $date1) {
									$classd1 = 'class="D12"'; $d1 = $shiftlength;
								} else {
									$classd1 = ''; $d1 = '';
								}
						if ($row->starttime >= $date1 and $row->starttime < $date2) {$d2 = $shiftlength;}if ($row->finishtime > $date1 and $row->starttime < $date2) {$classd2 = 'class="D12"'; if ($d1 == '') {$d2 = $shiftlength;} else {$d2 = '';};} else {$classd2 = ''; $d2 = '';}
						if ($row->starttime >= $date2 and $row->starttime < $date3) {$d3 = $shiftlength;}if ($row->finishtime > $date2 and $row->starttime < $date3) {$classd3 = 'class="D12"'; if ($d2 == '') {$d3 = $shiftlength;} else {$d3 = '';};} else {$classd3 = ''; $d3 = '';}
						if ($row->starttime >= $date3 and $row->starttime < $date4) {$d4 = $shiftlength;}if ($row->finishtime > $date3 and $row->starttime < $date4) {$classd4 = 'class="D12"'; if ($d3 == '') {$d4 = $shiftlength;} else {$d4 = '';};} else {$classd4 = ''; $d4 = '';}
						if ($row->starttime >= $date4 and $row->starttime < $date5) {$d5 = $shiftlength;}if ($row->finishtime > $date4 and $row->starttime < $date5) {$classd5 = 'class="D12"'; if ($d4 == '') {$d5 = $shiftlength;} else {$d5 = '';};} else {$classd5 = ''; $d5 = '';}
						if ($row->starttime >= $date5 and $row->starttime < $date6) {$d6 = $shiftlength;}if ($row->finishtime > $date5 and $row->starttime < $date6) {$classd6 = 'class="D12"'; if ($d5 == '') {$d6 = $shiftlength;} else {$d6 = '';};} else {$classd6 = ''; $d6 = '';}
						if ($row->starttime >= $date6 and $row->starttime < $date7) {$d7 = $shiftlength;}if ($row->finishtime > $date6 and $row->starttime < $date7) {$classd7 = 'class="D12"'; if ($d6 == '') {$d7 = $shiftlength;} else {$d7 = '';};} else {$classd7 = ''; $d7 = '';}
						if ($row->starttime >= $date7 and $row->starttime < $date8) {$d8 = $shiftlength;}if ($row->finishtime > $date7 and $row->starttime < $date8) {$classd8 = 'class="D12"'; if ($d7 == '') {$d8 = $shiftlength;} else {$d8 = '';};} else {$classd8 = ''; $d8 = '';} ?>
            <?php if ($welder = $username) {
							$userid = user::find('id')->Where(['username' => $welder])->select('id')->one();
						?>
            <tr>
                <td
                    class="<?php if ($row->billingstatus == 'Invoiced') {echo 'Invoiced';} elseif ($row->jobstatus == 'Annual Leave') {echo 'Annualleave';}else {echo $row->jobstatus;} ?>">
                    <span class="link"><a href='/activities/view?id=<?= $row->id ?>'><?= $row->jobno ?></a></span></td>
                <td><?php if ($row->company) { echo $row->company0->account_name;}?></td>
                <td><?= $row->jobstatus ?></td>
                <td> <?=date("d/m/Y - H:i", $row->starttime)?></td>
                <td <?= $classd1 ?>><?= $d1  ?></td>
                <td <?= $classd2 ?>><?= $d2 ?></td>
                <td <?= $classd3?>><?= $d3 ?></td>
                <td <?= $classd4?>><?= $d4 ?></td>
                <td <?= $classd5?>><?= $d5 ?></td>
                <td <?= $classd6 ?>><?= $d6 ?></td>
                <td <?= $classd7?>><?= $d7 ?></td>
                <td <?= $classd8 ?>><?= $d8 ?></td>
                <td><?= $lodge ?></td>
                <td><?= $row->journey ?></td>
                <td><?= $userid->useradv->position ?></td>
                <td> <?= $row->googletime ?></td>
                <td style="padding-bottom: 3px">
                    <?php foreach ($row->dt179 as $time => $dt179) { ?>
                    <?= print_r($dt179->data->comments, 1) ?> <br />
                    <?php  } ?>
                </td>
            </tr>
            <?php } else {'';} ?>
            <?php endforeach; ?>
            <?php $data1 = Activities::find()
					->where(['>=', 'starttime', $date])
					->andwhere(['<=', 'starttime', $date8])
					->andwhere(['!=', 'jobstatus', 'Rostered Off'])
					->andwhere(['!=', 'jobstatus', 'Sickness'])
					->andwhere(['!=', 'jobstatus', 'Not Available'])
					->andwhere(['!=', 'jobstatus', 'Cancelled'])
					->andwhere(['like', 'welder', $username])
					->andWhere(['!=', 'deleted', 1])
					->orderby('starttime ASC')->all(); $sum1 = 0;
				?>
            <?php foreach ($data1 as $keya => $row1){ ?>
            <?php is_numeric($row1->shiftlength) ? $sum1 += $row1->shiftlength : $sum1 + 0;  ?>
            <?php } ?>
            <tr>
                <td>
                </td>
                <td>
                </td>
                <th>Total Hours</th>
                <td><?= $sum1 ?></td>
                <td>
                </td>
                <td></td>
                <td>
                </td>
                <td>
                </td>
                <td>
                </td>
                <td>
                </td>
                <td>
                </td>
            </tr>
        </tbody>
    </table>
    <?php endforeach; ?>
</div>


<script>
function fnExcelReport() {
    var tab_text = '<html xmlns:x="urn:schemas-microsoft-com:office:excel">';
    tab_text = tab_text + '<head><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>';

    tab_text = tab_text + '<x:Name>WC<?= $sdate ?> Timesheet</x:Name>';

    tab_text = tab_text + '<x:WorksheetOptions><x:Panes></x:Panes></x:WorksheetOptions></x:ExcelWorksheet>';
    tab_text = tab_text + '</x:ExcelWorksheets></x:ExcelWorkbook></xml></head><body>';

    tab_text = tab_text + "<table border='1px'>";
    tab_text = tab_text + $('#myTable').html();
    tab_text = tab_text + '</table></body></html>';

    var data_type = 'data:application/vnd.ms-excel';

    var ua = window.navigator.userAgent;
    var msie = ua.indexOf("MSIE ");

    if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) {
        if (window.navigator.msSaveBlob) {
            var blob = new Blob([tab_text], {
                type: "application/xls;charset=utf-8;"
            });
            navigator.msSaveBlob(blob, 'Timesheet.xls');
        }
    } else {
        $('#test').attr('href', data_type + ', ' + encodeURIComponent(tab_text));
        $('#test').attr('download', 'Timesheet.xls');
    }

}
</script>
<style>
.D12 {
    background: LightBlue;
    text-align: Center;
}
</style>