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

<?= $this->section('content') ?>
<script src="<?= base_url('assets/js/swal.js'); ?>"></script>
<div class="page-wrapper">
    <?php $session = \Config\Services::session(); ?>
    <?php if ($session->getFlashdata('success') || $session->getFlashdata('error')): ?>
        <script>
            const Toast = Swal.mixin({
                toast: true,
                position: "top-end",
                showConfirmButton: false,
                timer: 3000,
                timerProgressBar: true,
                didOpen: (toast) => {
                    toast.onmouseenter = Swal.stopTimer;
                    toast.onmouseleave = Swal.resumeTimer;
                }
            });
            const icon = <?= $session->getFlashdata('success') ? "'success'" : ($session->getFlashdata('error') ? "'error'" : "''") ?>;
            Toast.fire({
                icon: icon,
                title: "<?= $session->getFlashdata('success') ? $session->getFlashdata('success') : $session->getFlashdata('error') ?>",
            });
        </script>
    <?php endif; ?>

	<div class="container-fluid">
		<div class="row page-titles">
			<h4 class="text-themecolor">Products Temp</h4>
		</div>
		
		<div class="row">
			<div class="col-12">
				<div class="card">
					<div class="card-body">
						<div class="table-responsive" style="min-height: 70vh;">
							<table id="myTable" class="table">
                                <thead>
                                    <tr>
                                        <th>No.</th>
                                        <th>Catalog number</th>
                                        <th>Product name</th>
                                        <th>Product number</th>
                                        <th>Location start date</th>
                                        <th>Reference</th>
                                        <th>Actions</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php $no = 1;?>
                                    <?php foreach ($products as $value) : ?>
                                        <tr>
                                            <td><?= $no++; ?></td>
                                            <td><?= $value['catalognumber']; ?></td>
                                            <td><?= $value['productname']; ?></td>
                                            <td><?= $value['productnumber']; ?></td>
                                            <td><?= $value['locationstartdate']; ?></td>
                                            <td><?= $value['reference']; ?></td>
                                            <td>
                                                <div class="d-flex gap-3">
                                                    <form id="validate-<?= $value['productid']?>" method="post" action="<?= base_url('producttemp/validate'); ?>" class="d-flex align-items-center">
                                                        <input type="hidden" name="producttemp-productid" value="<?= $value['productid']?>">
                                                        <?php if ($value['duplicates']): ?>
                                                            <?php foreach ($value['duplicates'] as $duplicate): ?>
                                                                <input type="hidden" name="product-productid" value="<?= $duplicate['productid']; ?>">
                                                            <?php endforeach; ?>
                                                        <?php endif; ?>
                                                        <button type="submit" class="btn btn-success btn-sm text-white border border-1 fw-medium" style="padding: 3px 7px; font-size: 0.75rem;" data-productid="<?= $value['productid']; ?>" onclick="confirmModal('validate','validate-<?= $value['productid']?>')">Validate</button>
                                                    </form>
                                                    <div class="dropdown">
                                                        <a class="btn btn-light border border-1" style="padding: 3px 7px; font-size: 0.75rem;" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                                                            <b>...</b>
                                                        </a>
                                                        <ul class="dropdown-menu" style="min-width: fit-content;">
                                                            <li>
                                                                <a class="dropdown-item d-flex justify-content-between gap-3 view-button" style="opacity: 85%;" href="#" data-productid="<?= $value['productid']; ?>" data-bs-toggle="modal" data-bs-target="#exampleModal">
                                                                    <span>View</span> 
                                                                    <span><i class="fa-solid fa-eye"></i></span>
                                                                </a>
                                                            </li>
                                                            <li>
                                                                <a class="dropdown-item d-flex justify-content-between gap-3 edit-button" style="opacity: 85%;" href="#" data-productid="<?= $value['productid']; ?>" data-bs-toggle="offcanvas" data-bs-target="#offcanvasRight" aria-controls="offcanvasRight">
                                                                    <span>Edit</span> 
                                                                    <span><i class="fa-solid fa-pen-to-square"></i></span>
                                                                </a>
                                                            </li>
                                                            <form id="delete-<?= $value['productid']; ?>" method="post" action="<?= base_url('producttemp/delete/' . $value['productid']); ?>">
                                                                <button type="submit" class="dropdown-item d-flex justify-content-between gap-3" style="opacity: 85%;" onclick="confirmModal('delete', 'delete-<?= $value['productid']; ?>')">
                                                                    <span>Delete</span>
                                                                    <span><i class="fa-solid fa-trash-can"></i></span>
                                                                </button>
                                                            </form>
                                                        </ul>
                                                    </div>
                                                </div>
                                            </td>
                                        </tr>
                                        <?php if ($value['duplicates']): ?>
                                        <?php foreach ($value['duplicates'] as $duplicate): ?>
                                        <tr>
                                            <td>&#128161;</td>
                                            <td><?php echo $duplicate['catalognumber']; ?></td>
                                            <td></td>
                                            <td><?php echo $duplicate['productnumber']; ?></td>
                                            <td><?php echo $duplicate['locationstartdate']; ?></td>
                                            <td></td>
                                            <td></td>
                                        </tr>
                                        <?php endforeach; ?>
                                        <?php endif; ?>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
						</div>
					</div>
				</div>
			</div>
		</div>

        <?= view('components/modal') ?>
        <?= view('components/offcanvas') ?>

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

<?= $this->section('script') ?>
    <script>
        $(function () {
            $('#myTable').DataTable({
                "order" : []
            });
        });
    </script>
    <script src="<?= base_url('assets/js/just-validate.production.min.js'); ?>"></script>
    <script src="<?= base_url('assets/js/data2.js'); ?>"></script>
    <script src="<?= base_url('assets/js/confirmswal.js'); ?>"></script>
    <script src="<?= base_url('assets/js/validate.js'); ?>"></script>
<?= $this->endSection() ?>
