<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.w3.org/1999/xhtml"
      xmlns:uind="http://www.w3.org/1999/xhtml" xmlns:ctp="http://www.w3.org/1999/xhtml">
    <head th:replace="_fragments :: head('Master Bundles')"></head>
    <body>
        <div class="container-fluid">
            <header class="row page-header" th:replace="_fragments :: page-header"></header>
            <main class="row page-main">
                <!-- sidebar starts -->
                <aside class="col-sm-2" th:replace="/cutting/_master-bundle-sidebar :: sidebar"></aside>
                <!-- sidebar ends -->
                <!--header starts-->
                <div class="col-sm">
                    <div th:replace="_notices :: page-notices"></div>
                    <div class="mb-4 d-flex justify-content-between">
                        <h3>Master Bundles</h3>
                    </div>
                    <div th:replace="_fragments :: table-loading-skeleton"></div>
                    <form th:action="@{/cutting/generate-master-barcodes}" method="post">
                        <input hidden="hidden" name="artifactType" value="MasterBundle">
                        <table class="table table-striped table-bordered" data-bundle-table
                               data-order="[[ 0, &quot;asc&quot; ]]">
                            <thead>
                                <tr>
                                    <th></th>
                                    <th>ID</th>
                                    <th title="Job Card Id">JC ID</th>
                                    <th>Item ID</th>
                                    <th>Sku</th>
                                    <th>Created By</th>
                                    <th>Created At</th>
                                    <th>
                                        <div class="mb-2">
                                            <button class="btn btn-sm btn-outline-primary" type="submit">Generate
                                                Barcode
                                            </button>
                                        </div>
                                        <div><input type="checkbox" data-checkbox-all></div>
                                    </th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr th:each="bundle : ${masterBundles}" th:object="${bundle}">
                                    <td data-show-dropdown
                                        th:data-master-id="*{id}">
                                        <span data-dropdown-icon class="bi bi-caret-right-fill"></span>
                                    </td>
                                    <td th:text="*{id}"></td>
                                    <td th:text="*{jobCardId}"></td>
                                    <td th:text="*{itemId}"></td>
                                    <td th:text="*{sku}"></td>
                                    <td th:text="*{createdBy}"></td>
                                    <td ctp:formatdatetime="*{createdAt}"></td>
                                    <td>
                                        <div class="form-group form-check mb-0">
                                            <input class="form-check-input" type="checkbox"
                                                   th:value="*{id}"
                                                   name="ids" th:id="*{id}">
                                            <label th:for="*{id}" class="form-check-label"></label>
                                        </div>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </form>
                </div>
            </main>
        </div>
        <div th:replace="_fragments :: page-footer-scripts"></div>
        <script>
            const $body = $('body');
            // custom data table config
            const dataTableConfig = {
                pageLength: 100,
                searching: true,
                lengthChange: false,
                processing: false,
                dom: `
				   <'row'<'col-sm-5'B><'col-sm-7'f>>
				   <'row'<'col-sm-12't>>
				   <'row'<'col-sm-5'i><'col-sm-7'p>>`,
                buttons: [{
                    extend: 'excel',
                    text: '',
                    className: 'bi bi-file-earmark-spreadsheet btn-sm'
                }]

            }
            const bundleTable = $('[data-bundle-table]').DataTable(dataTableConfig);

            // handle click on dropdown
            $body.on('click', '[data-show-dropdown]', function (e) {
                e.preventDefault();
                const $this = $(this);
                const $tr = $this.closest('tr');
                const $row = bundleTable.row($tr);
                const $spanDropdown = $tr.find('[data-dropdown-icon]');
                const masterId = $this.data('master-id');
                $spanDropdown.toggleClass('bi-caret-right-fill bi-caret-down-fill');
                if ($row.child.isShown()) {
                    $row.child.hide();
                } else {
                    $row.child(`<span class="spinner-border text-center spinner-border-md" role="status"></span>`).show();
                    $.ajax({
                        url: `/ctp/cutting/get-child-bundles?master-id=${masterId}`,
                        success: function (data) {
                            // show fetched page
                            $row.child(data).show();
                        }
                    });
                }
            });
            const $selectAllCheckBox = $('[data-checkbox-all]');

            $selectAllCheckBox.change(function () {
                if ($selectAllCheckBox.prop('checked')) {
                    // When parent checkbox is checked, check all child checkboxes
                    $('[name="ids"]').each(function () {
                        let $this = $(this);
                        $this.prop('checked', true);
                    });
                } else {
                    // When parent checkbox is unchecked, uncheck all child checkboxes
                    $('[name="ids"]').each(function () {
                        let $this = $(this);
                        $this.prop('checked', false);
                    });
                }
            });
        </script>
    </body>
</html>