<script src="https://code.jquery.com/jquery-3.6.0.js"></script>
  <script src="https://code.jquery.com/ui/1.13.2/jquery-ui.js"></script>

 <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <div class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h1 class="m-0 text-dark">Edit Course Outline</h1>
          </div><!-- /.col -->
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item"><a href="<?php echo base_url() ?>admin/admin/dashboard">Dashboard</a></li>
              <li class="breadcrumb-item"><a href="#">Course</a></li>
              <li class="breadcrumb-item active">Edit Course Outline</li>

            </ol>
          </div><!-- /.col -->
        </div><!-- /.row -->
      </div><!-- /.container-fluid -->
    </div>
    <!-- /.content-header -->
    <!-- Main content -->
    <section class="content">
      <div class="container-fluid">

        <div class="row">
          <div class="col-sm-12">
            <div class="card card-default">
              <div class="card-header">
                <h3 class="card-title">Update Course Outline</h3>
                <div class="card-tools">
                  <a href="<?php echo base_url()?>admin/course/all_lession_and_chapter" class="btn btn-primary btn-sm"><i class="fa fa-eye" aria-hidden="true"></i> View Course Outline</a>
                </div>
              </div>
              <!-- /.card-header -->
              <?php echo form_open_multipart('admin/course/update_lession_and_chapter'); ?>
              <div class="card-body">
                <div class="row">
              
                  <div class="col-md-12">
                    <div class="form-group">
                       <label for="inputEmail" class="col-sm-12 col-form-label">Select A Course</label>
                       <div class="col-sm-12">
                          <select class="form-control"  name="ch_course_id" placeholder="Enter Category ID" >
                            <option value="">Select</option>
                            <?php foreach ($all_course as $course): ?>
                                <option <?php if($all_lession[0]->ch_course_id == $course->id){echo 'selected';}?> value="<?php echo $course->id; ?>"><?php echo $course->title; ?></option>
                            <?php endforeach ?>
                          </select>
                       </div>
                    </div>
                  </div>
                  <?php $a = 0; foreach ($all_lession as $lession): ?>
                      <script>
                          $( function() {
                            $( "#requirement_area_<?php echo $a; ?>" ).sortable();
                          } );
                      </script>
                      <div class="col-md-12">
                        <div class="form-group">
                           <label for="inputEmail" class="col-sm-12 col-form-label">Chapter Name <span class="required">*</span></label>
                           <div class="col-sm-12">
                              <input class="form-control"  name="ch_lession_name[]" placeholder="Enter Chapter Name" required="required" value="<?php echo $lession->ch_lession_name; ?>">
                           </div>
                        </div>
                      </div>

                      <input class="form-control" type="hidden" name="ch_id[]" value="<?php echo $lession->ch_id; ?>">

                      <div class="col-md-12">
                        <div class="form-group ">
                           <label class="col-sm-12 col-form-label">Lession</label>
                           <div class="col-md-10">
                              <div class="col-md-10">
                                  <div id="requirement_area_<?php echo $a; ?>" class="sortable-list">
                                     <?php if (count(json_decode($lession->ch_chapters)) > 0): ?>
                                        <?php
                                        $counter = 0;
                                        $sst = 1;
                                        foreach (json_decode($lession->ch_chapters) as $requirement):?>
                                        <?php if ($counter == 0):
                                            $counter++; ?>
                                            <div class="d-flex mt-2">
                                                <div class="flex-grow-1 px-3">
                                                    <div class="form-group input-icons" style="display: inline-flex;width:100%">
                                                        <img src="<?php echo base_url(); ?>assets/dots.png" width="40px" style="display:inline">
                                                        <input type="text" class="form-control" name="ch_chapters[<?php echo $a; ?>][]" id="requirements_<?php echo $sst; ?>" placeholder="Enter Lession" value="<?php echo $requirement; ?>">
                                                    </div>
                                                </div>
                                                <div class="">
                                                    <button type="button" class="btn btn-success btn-sm" style="" name="button" onclick="appendRequirement<?php echo $a; ?>()"> <i class="fa fa-plus"></i> </button>
                                                </div>
                                            </div>
                                        <?php else: ?>
                                            <div class="d-flex mt-2">
                                                <div class="flex-grow-1 px-3">
                                                    <div class="form-group" style="display: inline-flex;width:100%">
                                                        <img src="<?php echo base_url(); ?>assets/dots.png" width="40px" style="display:inline">
                                                        <input type="text" class="form-control" name="ch_chapters[<?php echo $a; ?>][]" id="requirements_<?php echo $sst; ?>" placeholder="Enter Lession" value="<?php echo $requirement; ?>">
                                                    </div>
                                                </div>
                                                <div class="">
                                                    <button type="button" class="btn btn-danger btn-sm" style="margin-top: 0px;" name="button" onclick="removeRequirement(this)"> <i class="fa fa-minus"></i> </button>
                                                </div>
                                            </div>
                                        <?php endif; ?>
                                    <?php $sst++; endforeach; ?>
                                  <?php else: ?>
                                    <div class="d-flex mt-2">
                                        <div class="flex-grow-1 px-3">
                                            <div class="form-group">
                                                <input type="text" class="form-control" name="ch_chapters[<?php echo $a; ?>][]" id="requirements" placeholder="Enter Lession">
                                            </div>
                                        </div>
                                        <div class="">
                                            <button type="button" class="btn btn-success btn-sm" style="" name="button" onclick="appendRequirement<?php echo $a; ?>()"> <i class="fa fa-plus"></i> </button>
                                        </div>
                                    </div>
                                  <?php endif; ?>
                                  </div>
                                </div>
                           </div>
                        </div>
                      </div>
                    <?php $a++; endforeach ?>
                  <div class="col-md-12">
                    <div class="form-group">
                       <button name="form1" style="float: right" type="submit" class="btn btn-success">Update</button>
                    </div>
                  </div>
                  <?php echo form_close() ?>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </section>
</div>
<?php $i = 0; foreach ($all_lession as $lession): ?>
    <div id = "blank_requirement_field_<?php echo $i; ?>">
        <div class="d-flex mt-2">
           <div class="flex-grow-1 px-3">
              <div class="form-group">
                 <input type="text" class="form-control" name="ch_chapters[<?php echo $i; ?>][]" id="requirements" placeholder=" Enter Lession">
              </div>
           </div>
           <div class="">
              <button type="button" class="btn btn-danger btn-sm" style="margin-top: 0px;" name="button" onclick="removeRequirement(this)"> <i class="fa fa-minus"></i> </button>
           </div>
        </div>
     </div>
    <script>
        var blank_requirement = jQuery('#blank_requirement_field_<?php echo $i; ?>').html();
        jQuery(document).ready(function() {
            jQuery('#blank_requirement_field_<?php echo $i; ?>').hide();
        });
        function appendRequirement<?php echo $i; ?>() {
            jQuery('#requirement_area_<?php echo $i; ?>').append(blank_requirement);
        }

        function removeRequirement(requirementElem) {
            jQuery(requirementElem).parent().parent().remove();
        }
    </script>
<?php $i++; endforeach ?>
<style>
.icon {

  background: url("") no-repeat left;
  background-size: 20px;
}
</style>