<div class="index_batchjob">
    <?= form_open(current_url() . '/batchjob', ['id' => 'batchjob_form', 'method' => "post"]) ?>
    <input type="hidden" name="batchjob_uids" id="batchjob_uids" value="">
    <ul class="nav justify-content-center">
        <li class="nav-item"><?= form_checkbox(array("id" => "batchjobuids_checkbox")) ?>ALL</li>
        <?php foreach ($viewDatas['batchjob_fields'] as $field): ?>
            <?= $viewDatas['helper']->getFieldForm($field, DEFAULTS['EMPTY'], $viewDatas, ['data-batchjob' => 'true']) ?>
        <?php endforeach ?>
        <li class="nav-item"><?= $viewDatas['helper']->getListButton('batchjob', $viewDatas) ?></li>
        <li class="nav-item"><?= $viewDatas['helper']->getListButton('create', $viewDatas) ?></li>
    </ul>
    <?= form_close() ?>
</div>
<script>
    function submitBatchJob() {
        var validate = false;
        //batchjob용 선택사항 검증
        <?php foreach ($viewDatas['batchjob_fields'] as $field): ?>
            var element = document.querySelector('[name="<?= $field ?>"][data-batchjob="true"]');
            if (element && element.value !== "") {
                validate = true;
            }
        <?php endforeach ?>
        if (!validate) {
            alert('변경할 항목을 선택하셔야합니다.');
            return false;
        }
        //적용받는 uids가 한개라도 선택되었다면 true
        var batchjobUids = [];
        var checkboxes = document.querySelectorAll('input[name="batchjob_uids[]"]:checked');
        checkboxes.forEach(function(checkbox) {
            batchjobUids.push(checkbox.value);
        });
        if (batchjobUids.length === 0) {
            alert('적용할 리스트를 선택하셔야합니다.');
            return false;
        }
        document.getElementById('batchjob_uids').value = batchjobUids.join(',');
        return true;
    }
</script>