<?= $this->extend('layouts/main.php') ?>

<?= $this->section('content') ?>
<div class="page-wrapper">
	<div class="container-fluid">
        <div class="row page-titles">
			<div class="col-md-5 align-self-center">
				<h4 class="text-themecolor">Activities Count</h4>
			</div>
		</div>
        <div class="row">
			<div class="col-12">
				<div class="card">
					<div class="card-body">
                        <form action="" method="post">
                            <div class="row mb-2">
                                <div class="col md-6">
                                    <div class="input-group input-group-sm">
                                        <span class="input-group-text d-flex align-items-center justify-content-center" style="width: 80px;">From</span>
                                            <input class="form-control form-control-sm" type="date" name="opendate" value="<?=$opendate;?>"/>
                                    </div>
                                </div>
                                <div class="col md-6">
                                    <div class="input-group input-group-sm">
                                        <span class="input-group-text d-flex align-items-center justify-content-center" style="width: 80px;">To</span>
                                            <input class="form-control form-control-sm" type="date" name="closedate" value="<?=$closedate;?>"/>
                                    </div>
                                </div>
                            </div>
							<div class="row mb-2">
								<div class="input-group input-group-sm">
									<span class="input-group-text d-flex align-items-center justify-content-center" style="width: 80px;">Pos</span>
									<select class="form-control form-control-sm" name="pos" >
										<?php
                                        $i=0;
										foreach($userpos as $qid => $qname) {
                                            $i++;
                                            if(in_array($i,[2,8,9])){echo "<option disabled class='bg-secondary'> </option>";}
											if($qid == $pos) { echo "<option value='$qid' selected>$qname</option>"; }
											else { echo "<option value='$qid' >$qname</option>"; }
                                            
										}
										?>
									</select>
								</div>
							</div>
                            <button type="submit" class="btn btn-success btn-sm text-white float-end"> <i class="fa fa-check"></i> Submit</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
		<?php
		if(isset($count)) {
		?>
        <div class="row mt-2">
      		<div class="col-12">
				<div class="card">
					<div class="card-body">
                        <button class="btn btn-success btn-xs text-white float-end mb-3" onclick="exportToExcel()"> <i class="fa fa-file-excel"></i> EXCEL </button>
						<div class="table-responsive">
							<table id="myTable" class="table small display border">
								<thead class="table-info">
									<tr>
                                        <th class='text-center' style="width:2%;">-</th>
                                        <th style="width:40%;">Name</th>
                                        <th>CR</th>
                                        <th>IR</th>
                                        <th>MN</th>
                                        <th>SP</th>
                                        <th>PR</th>
                                        <th>RF</th>
                                        <th>TR</th>
                                        <th style="text-align: center;">Total</th>
									</tr>
								</thead>
								<tbody>
                                    <?php foreach($count as $row) : ?>
                                        <?php 
                                            if ( in_array($row['userposid'], ["2", "4"])) { 
                                                $tdClass = "style='background-color:#fec10729;'";
                                                $tdContent = '<small>IVD</small>';
                                            } elseif ( in_array($row['userposid'], ["3", "5"])) { 
                                                $tdClass = "style='background-color:#feeae475;'";
                                                $tdContent = '<small>IT</small>';
                                            } elseif ( in_array($row['userposid'], ["6", "9"])) { 
                                                $tdClass = "style='background-color:#cdf3cc8c;'";
                                                $tdContent = '<small>MKT</small>';
                                            } elseif ( in_array($row['userposid'], ["10", "14"])) { 
                                                $tdClass = "style='background-color:#1269cf2b;'";
                                                $tdContent = '<small>PS</small>';
                                            } else { 
                                                $tdClass = ''; 
                                                $tdContent = '<small> </small>'; 
                                            }
                                        ?>
                                    <tr <?php if( session()->get('userid') == $row['userid'] ) {echo $tdClass;} ?>>
                    
                                        <?php if( session()->get('userid') == $row['userid'] ) { ?>
                                            <td class="text-center px-2">
                                                <?php echo $tdContent; ?> 
                                            </td>
                                        <?php } else { ?>
                                            <td class="text-center px-2" <?php echo $tdClass; ?>>
                                                <?php echo $tdContent; ?> 
                                            </td>
                                        <?php } ?>

                                        <td><?= $row['firstname'] . ' ' . $row['lastname'];?></td>
                                        <td><?= $row['CR'];?></td>
                                        <td><?= $row['IR'];?></td>
                                        <td><?= $row['MN'];?></td>
                                        <td><?= $row['SP'];?></td>
                                        <td><?= $row['PR'];?></td>
                                        <td><?= $row['RF'];?></td>
                                        <td><?= $row['TR'];?></td>
                                        <td style="text-align: center;"><?= $row['total']; ?></td>
                                    </tr>
                                    <?php endforeach; ?>
								</tbody>
							</table>
						</div>
					</div>
				</div>
			</div>
		</div>
		<?php 
		}
		?>
    </div>
</div>

<?= $this->endSection() ?>

<?= $this->section('script') ?>

<!-- SCRIPT EXPORT TABLE TO EXCEL -->
<script>
    function exportToExcel() {
        const data = document.getElementById("myTable"); //const data = $("#myTable")[0]; kalo pake jquery
        var ws = XLSX.utils.table_to_sheet(data);

        var wb = XLSX.utils.book_new();
        XLSX.utils.book_append_sheet(wb, ws, "Sheet 1");
        XLSX.writeFile(wb, "ActivityCount.xlsx");
      }
</script>

<?= $this->endSection() ?>