<div id="ExportServiceFeeModal" class="modal fade" tabindex="-1" aria-labelledby="ExportServiceFeeModalModal" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered" role="document" style="display: block;">
    <div class="modal-content">
        <div class="modal-header bg-primary">
            <h5 class="modal-title text-white">EXPORT SERVICES</h5>
            <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
        </div>
        <div class="modal-body">
                <div class="col-md-12 py-2">
                    <select id="exportDepartmentModal" class="form-control">
                      <option value="">Select Department</option>
                      <?php foreach($departments as $d): ?>
                      <option value="<?= $d->id ?>"><?= $d->name ?></option>
                      <?php endforeach; ?>
                    </select>
                </div>
                <div class="col-md-12 py-2">
                    <select id="exportServiceModal" class="form-control">
                      <option value="">Select Service</option>
                    </select>
                </div>
        </div>
        <div class="modal-footer">
            <button class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
            <button id="exportBtnModal" class="btn btn-primary btn-sm btn-round">Export CSV</button>
        </div>
    </div>
  </div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).on('change', '#exportDepartmentModal', function(){
    var dep_id = $(this).val();
    var $service = $('#exportServiceModal');

    $service.html('<option value="">Select Service</option>');

    if(dep_id){
        $.getJSON('<?= base_url("admin/Consultants/by_department/") ?>' + dep_id, function(data){
            $.each(data, function(i, item){
                $service.append('<option value="'+item.id+'">'+item.name+'</option>');
            });
        });
    }
});

$(document).on('click', '#exportBtnModal', function(){
    var service_id = $('#exportServiceModal').val();

    if(!service_id){
        alert('Select a service first!');
        return;
    }

    window.location.href =
        '<?= site_url("admin/sub_services/export/") ?>' + service_id;
});
</script>
