<?php
    $errors = $this->session->userdata('errors');
    if (isset($errors)) {
        ?>
        <script>
            swal ( "Oops" ,  "<?php echo $errors ?>" ,  "error" )
        </script>
        <?php echo $errors ?>
        <?php
        $this->session->unset_userdata('errors');
    }
    $error = $this->session->userdata('error');
    if (isset($error)) {
        ?>
         <script>
            swal ( "Oops" ,  "<?php echo $error ?>" ,  "error" )
        </script>
        <?php
        $this->session->unset_userdata('error');
    }
    $success = $this->session->userdata('success');
    if (isset($success)) {
        ?>
         <script>
            swal({
          title: "Done!",
          text: "<?php echo $success ?>",
          icon: "success",
        });
        </script>
        <?php
        $this->session->unset_userdata('success');
    }
    ?>
  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h1>Section 4</h1>
          </div>
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item"><a href="<?php echo admin_url() ?>admin">Home</a></li>
              <li class="breadcrumb-item active">Section 4</li>
            </ol>
          </div>
        </div>
      </div><!-- /.container-fluid -->
    </section>

    <!-- Main content -->
    <section class="content">
      <div class="container-fluid">
        <div class="row">
          <div class="col-sm-6">
            <div class="card card-default">
              <div class="card-header">
                <h3 class="card-title">Add New Content</h3>
                
              </div>
              <!-- /.card-header -->
              <?php echo form_open_multipart(base_url().'admin/live_class_page/add_new_section_4_content'); ?>
              <div class="card-body">
                <div class="row">
                  <div class="col-md-12">
                    <div class="form-group">
                      <label>Select A Page<span class="required">*</span></label>
                      <select type="text" name="section_4_class_id" class="form-control" placeholder="Enter Content" required="">
                        <option  value="">Select</option>
                        <?php foreach ($all_pages as $page):?>
                          <option value="<?php echo $page->live_class_id ?>"><?php echo $page->live_class_heading ?></option>
                        <?php endforeach; ?>
                      </select>
                    </div>
                  </div>
                  <div class="col-md-12">
                    <div class="form-group">
                      <label>Image<span class="required">*</span></label>
                      <input type="file" name="section_4_image" class="form-control" placeholder="Enter Title" required="">
                    </div>
                  </div>
                  <div class="col-md-12">
                    <div class="form-group">
                      <label>Title<span class="required">*</span></label>
                      <input type="text" name="section_4_title" class="form-control" placeholder="Enter Title" required="">
                    </div>
                  </div>
                  <div class="col-md-12">
                    <div class="form-group">
                      <label for="inputEmail" class="col-sm-12 col-form-label">Add Content<span class="required">*</span></label>
                      <div class="table-responsive">
                         <table class="table table-bordered" id="dynamic_field">
                            <tr>
                              <td><input type="text" name="section_4_content[]" class="form-control name_list" placeholder="Enter Content" required="" /></td>
                               <td><button type="button" name="add" id="add" class="btn btn-primary">+</button></td>
                            </tr>
                         </table>
                      </div>
                    </div>
                  </div>
                  <div class="col-md-12">
                    <div class="form-group">
                      <button class="btn btn-info" type="submit" style="float: right">Add</button>
                    </div>
                  </div>
                  <?php echo form_close(); ?>
                </div>
              </div>
            </div>
          </div>
          <script type="text/javascript">
            $(document).ready(function(){  
                var i=1;  
                $('#add').click(function(){  
                     i++;  
                     $('#dynamic_field').append('<tr id="row'+i+'"><td><input type="text" name="section_4_content[]" class="form-control name_list" placeholder="Enter Content" required="" /></td><td><button type="button" name="remove" id="'+i+'" class="btn btn-danger btn_remove">X</button></td></tr>');  
                });  
                $(document).on('click', '.btn_remove', function(){  
                     var button_id = $(this).attr("id");   
                     $('#row'+button_id+'').remove();  
                });  
             });  
          </script>
          
          <div class="col-sm-6">
            <div class="card card-default">
              <div class="card-header">
                <h3 class="card-title">View Content</h3>
                
              </div>
                <div class="card-body">
                  <table id="example" class="table table-bordered table-striped">
                    <thead>
                    <tr>
                      <th>id</th>
                      <th>Image</th>
                      <th>Title</th>
                      <th>Page</th>
                      <th></th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php $i=1; foreach ($all_section_4_data as $key => $data):?>
                    <tr>
                      <td><?php echo $i ?></td>
                      <td><img src="<?php echo base_url() ?><?php echo $data->section_4_image ?>" width="50px"></td>
                      <td><?php echo  $data->section_4_title; ?></td>
                      <td><?php echo  $data->live_class_heading; ?></td>
                      <td><a href="" class="btn btn-info btn-sm" data-toggle="modal" data-target="#update-stacks<?php echo $data->section_4_id ?>"><i class="fa fa-edit" aria-hidden="true"></i></a>
                        <a class="btn btn-danger btn-sm" onclick="return confirm('Are you sure?')" href="<?php echo base_url() ?>admin/live_class_page/delete_section_4/<?php echo $data->section_4_id ?>"><i class="fa fa-trash" aria-hidden="true"></i></a></td>
                    </tr>
                    <?php $i++; ?>
                    <?php endforeach; ?>
                    </tbody>
                    
                  </table>
                </div>
              </div>
            </div>
          </div>
      </div>
    </section>
    <!-- /.content -->
  </div>
<?php $i=1; foreach ($all_section_4_data as $key => $data):?>
<div class="modal fade" id="update-stacks<?php echo $data->section_4_id ?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Update Content</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <?php echo form_open_multipart(base_url().'admin/live_class_page/update_section_4_content/'.$data->section_4_id); ?>
          <div class="form-group">
            <label>Select A Page<span class="required">*</span></label>
            <select type="text" name="section_4_class_id" class="form-control" placeholder="Enter Content" required="">
              <option  value="">Select</option>
              <?php foreach ($all_pages as $page):?>
                <option value="<?php echo $page->live_class_id ?>" <?php if($page->live_class_id == $data->section_4_class_id){echo 'selected';} ?>><?php echo $page->live_class_heading ?></option>
              <?php endforeach; ?>
            </select>
          </div>
          <div class="col-md-12">
            <div class="form-group">
              <label>Image<span class="required">*</span></label>
              <input type="file" name="section_4_image" class="form-control" placeholder="Enter Title">
            </div>
          </div>
          <div class="col-md-12">
            <div class="form-group">
              <img src="<?php echo base_url() ?><?php echo $data->section_4_image ?>" width="50px">
            </div>
          </div>
          <div class="col-md-12">
            <div class="form-group">
              <label>Title<span class="required">*</span></label>
              <input type="text" name="section_4_title" class="form-control" placeholder="Enter Title" required="" value="<?php echo $data->section_4_title; ?>">
            </div>
          </div>
          <div class="form-group row mb-3">
            <label class="col-md-2 col-form-label" for="requirements">Content</label>
            <div class="col-md-10">
                <div id = "requirement_area_<?php echo $i; ?>">
                  <?php if (count(json_decode($data->section_4_content)) > 0): ?>
                        <?php
                        $counter = 0;
                        foreach (json_decode($data->section_4_content) 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 required type="text" class="form-control" name="section_4_content[]" placeholder="Enter Content" value="<?php echo $requirement; ?>">
                                    </div>
                                </div>
                                <div class="">
                                    <button type="button" class="btn btn-success btn-sm" style="" name="button" onclick="appendRequirement('<?php echo $i; ?>')"> <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">
                                        <input required type="text" class="form-control" name="section_4_content[]" placeholder="Enter Content" 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 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="section_4_content[]" placeholder="Enter Content" required>
                            </div>
                        </div>
                        <div class="">
                            <button type="button" class="btn btn-success btn-sm" style="" name="button" onclick="appendRequirement('<?php echo $i; ?>')"> <i class="fa fa-plus"></i> </button>
                        </div>
                    </div>
                  <?php endif; ?>
                </div>
                
            </div>
        </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="submit" class="btn btn-primary">Update</button>
      </div>
      <?php echo form_close(); ?>
    </div>
  </div>
</div>
<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="section_4_content[]" id="requirements" placeholder="Enter Content">
            </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 type="text/javascript">
jQuery(document).ready(function() {
  jQuery('#blank_plans_field_<?php echo $i; ?>').hide();
});
var blank_requirement = jQuery('#blank_requirement_field_<?php echo $i; ?>').html();
function appendRequirement(e) {
  jQuery('#requirement_area_'+e).append(blank_requirement);
}
function removeRequirement(requirementElem) {
  jQuery(requirementElem).parent().parent().remove();
}
</script>

<?php endforeach; ?>
