 <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"><?php echo $title ?></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="#"><?php echo ucwords(str_replace("_", " ", $this->uri->segment(2))); ?></a></li>
              <li class="breadcrumb-item active"><?php echo ucwords(str_replace("_", " ", $title)); ?></li>

            </ol>
          </div><!-- /.col -->
        </div><!-- /.row -->
      </div><!-- /.container-fluid -->
    </div>
    <!-- /.content-header -->
  <?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');
    }
    ?>
    <!-- Main content -->
    <section class="content">
      <div class="container-fluid">
        <!-- Info boxes -->
        <a href="add_new_category" class="btn btn-success"><i class="fa fa-plus" aria-hidden="true"></i> Add Category</a>
        <a href="view_all_category" class="btn btn-primary"><i class="fa fa-eye" aria-hidden="true"></i> View Category</a>


        <div class="card card-default">
          <div class="card-header">
            <h3 class="card-title"><?php echo $title ?></h3>

            <div class="card-tools">
              <button type="button" class="btn btn-tool" data-card-widget="collapse"><i class="fas fa-minus"></i></button>
            </div>
          </div>
         
           
            <!-- /.card-header -->
            <div class="card-body">
              <table id="example1" class="table table-bordered table-striped">
                <thead>
                <tr>
                  <th>id</th>
                  <th>Category Name</th>
                  <th>Parent Category</th>
                  <th>Category Image</th>
                  <th>Varients</th>
                  <th>Menu Position</th>
                  <th>Cat Comission</th>
                  <th>Aff Comission</th>
                  <th></th>
                </tr>
                </thead>
                <tbody>
                <?php $i=1; foreach ($get_all_category as $key => $category):
                if($category->status == 0):?>
  
                <?php $parent_category = $this->category_model->get_parent_category_by_id($category->parent_category); ?>
                <tr>
                  <td><?php echo $i ?></td>
                  <td><?php echo $category->name ?></td>
                  <td><?php if(!empty($parent_category->name)){echo $parent_category->name;}else{echo 'N/A';} ?> </td>
                  <td>
                    <?php if($category->mobile_image){ ?>
                      <img src="<?php echo base_url(); ?><?php echo $category->mobile_image ?>" width="50px;">
                    <?php }else{ ?>
                      N/A
                    <?php } ?>
                    </td>
                  <td>
                    <?php 
                    if(empty($parent_category->name)){
                      if(!empty($category->varients))
                      {
                        $filesCount = count(json_decode($category->varients)); 
                          for($a = 0; $a < $filesCount; $a++)
                          {
                            $all_varient = json_decode($category->varients)[$a];
                            $varient_all = $this->varient_model->get_varient_by_id($all_varient);
                            echo '<span class="badge badge-warning">'.$varient_all->name.'</span><br/>';
                          }
                      }
                      else
                      {
                        echo 'N/A';
                      } 
                    }
                    else
                      {
                        echo 'N/A';
                      } 
                    ?>
                  </td>
                  <td><?php echo $category->menu_position ?></td>
                  <td><?php if(empty($parent_category->name)){ echo $category->comission .'%';} ?></td>
                  <td><?php if(empty($parent_category->name))
                  { 
                    if($category->aff_comission > 0)
                    {
                      echo $category->aff_comission.'%';
                    }
                    else
                    {
                      echo 'NA';
                    }
                  } ?></td>
                  <td class="text-right py-0 align-middle">

                      <div class="btn-group btn-group-sm">
                        <?php if($category->status == 1): ?>
                          <a href="<?php echo base_url() ?>admin/category/deactive_category/<?php echo $category->id ?>" class="btn btn-success"><i class="fas fa-eye"></i></a>
                        <?php else: ?>
                          <a href="<?php echo base_url() ?>admin/category/active_category/<?php echo $category->id ?>" class="btn btn-warning"><i class="fas fa-low-vision"></i></a>
                        <?php endif; ?>
                        <a href="#" class="btn btn-info" data-toggle="modal" data-target="#update-category<?php echo $category->id ?>"><i class="fas fa-edit"></i></a>
                        <a onclick="return confirm('Are you sure?')" href="<?php echo base_url() ?>admin/category/delete_category/<?php echo $category->id ?>" class="btn btn-danger"><i class="fas fa-trash"></i></a>
                      </div>

                    </td>
                </tr>
                  <?php endif; ?>
                <?php $i++; ?>
                <?php endforeach; ?>
                </tbody>
              </table>
            </div>
       
      </div><!--/. container-fluid -->
    </section>
    <!-- /.content -->
  </div>
  <!-- /.content-wrapper -->
<?php foreach ($get_all_category as $key => $category_details):?>
<div class="modal fade" id="update-category<?php echo $category_details->id ?>">
  <div class="modal-dialog">
    <div class="modal-content bg-primary">
      <div class="modal-header">
        <h4 class="modal-title">Update Category</h4>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span></button>
      </div>
      <?php echo form_open_multipart('admin/category/update_category_details'); ?>
      <div class="modal-body">
        <div class="row">

            <div class="col-md-12">
              <div class="form-group">
                 <label for="inputEmail" class="col-sm-12 col-form-label">Category Name <span class="required">*</span></label>
                 <div class="col-sm-12">
                    <input class="form-control"  name="name" placeholder="Enter Category Name" required="required" value="<?php echo $category_details->name ?>">
                 </div>
              </div>
            </div>
        
            <div class="col-md-12">
              <div class="form-group">
                 <label for="inputEmail" class="col-sm-12 col-form-label">Select Parent Category</label>
                 <div class="col-sm-12">
                    <select  class="form-control"  name="parent_category">
                      <option value="">Select Category</option>
                        <?php foreach ($get_all_category_name as $key => $category):?>
                          <option <?php if($category->id == $category_details->parent_category){echo 'selected';} ?>  value="<?php echo $category->id ?>"><?php echo $category->name ?></option>
                        <?php endforeach; ?>
                    </select>
                 </div>
              </div>
            </div>
            
            <div class="col-md-12" <?php if($category_details->parent_category > 0){echo 'style="display:none"';} ?>>
              <div class="form-group">
                 <label for="inputEmail" class="col-sm-12 col-form-label">Show in Front Page<span class="required">*</span></label>
                 <div class="col-sm-12">
                    <select  class="form-control"  name="front_show" required="">
                      <option value="">Select</option>
                      <option <?php if($category_details->front_show == 1){echo 'selected';} ?> value="1">Yes</option>
                      <option <?php if($category_details->front_show == 0){echo 'selected';} ?> value="0">No</option>
                    </select>
                 </div>
              </div>
            </div>
          
            <div class="col-md-12">
              <div class="form-group">
                 <label for="inputEmail" class="col-sm-12 col-form-label">Menu Position<span class="required">*</span></label>
                 <div class="col-sm-12">
                    <input type="number" class="form-control"  name="menu_position" placeholder="Enter Menu Position (Ex. - 1,2,3,4 etc.)" required="" value="<?php echo $category_details->menu_position ?>">
                 </div>
              </div>
            </div>

            <div class="col-md-12" <?php if($category_details->parent_category > 0){echo 'style="display:none"';} ?>>
              <div class="form-group">
                 <label for="inputEmail" class="col-sm-12 col-form-label">Category Comission </label>
                 <div class="col-sm-12">
                    <input type="number" class="form-control"  name="comission" placeholder="Enter Category Comission (%)" value="<?php echo $category_details->comission ?>">
                 </div>
              </div>
            </div>

            <div class="col-md-12" <?php if($category_details->parent_category > 0){echo 'style="display:none"';} ?>>
              <div class="form-group">
                 <label for="inputEmail" class="col-sm-12 col-form-label">Affiliate Comission % <span style="font-size: 13px;">(If you leave it blank. Affiliate System Disabled in this Category)</span></label>
                 <div class="col-sm-12">
                    <input type="number" class="form-control"  name="aff_comission" placeholder="Enter Category Affiliate Comission (%)" value="<?php echo $category_details->aff_comission ?>">
                 </div>
              </div>
            </div>

            <input type="hidden" name="id" value="<?php echo $category_details->id ?>">
           
          
            <div class="col-md-12" <?php if($category_details->parent_category > 0){echo 'style="display:none"';} ?>>
              <div class="form-group">
                 <label for="inputEmail" class="col-sm-12 col-form-label">Category Image (150px X 150px)</label>
                 <div class="col-sm-12">
                    <input type="file" class="form-control"  name="mobile_image">
                 </div>
              </div>
            </div>

            <div class="col-md-12" <?php if($category_details->parent_category > 0){echo 'style="display:none"';} ?>>
              <div class="form-group">
                 <label for="inputEmail" class="col-sm-12 col-form-label">Preview Image</label>
                 <div class="col-sm-12">
                    <img style="border: 1px solid black" src="<?php echo base_url() ?><?php echo $category_details->mobile_image ?>" width="150px;">
                 </div>
              </div>
            </div>

            <div class="col-md-12">
              <div class="form-group">
                 <label for="inputEmail" class="col-sm-12 col-form-label">Category Banner (Banner Height 300px & width any px)</label>
                 <div class="col-sm-12">
                    <input type="file" class="form-control"  name="category_banner" >
                 </div>
              </div>
            </div>

            <div class="col-md-12" >
              <div class="form-group">
                 <label for="inputEmail" class="col-sm-12 col-form-label">Preview Image</label>
                 <div class="col-sm-12">
                    <img style="border: 1px solid black" src="<?php echo base_url() ?><?php echo $category_details->category_banner ?>" width="150px;">
                 </div>
              </div>
            </div>

            <?php if($category_details->cat_type == 1): ?>
              <div class="col-md-12" <?php if($category_details->parent_category > 0){echo 'style="display:none"';} ?>>
                <div class="form-group">
                   <label for="inputEmail" class="col-sm-12 col-form-label">Select Category Varient</label>
                  <div class="col-sm-12">
                    <?php foreach ($get_all_varient as $key => $varient):?>
                      <input type="checkbox" class="" <?php if(!empty($category_details->varients) && $category_details->varients != null)
                      {$filesCount = count(json_decode($category_details->varients));for($a = 0; $a < $filesCount; $a++){if($varient->id == json_decode($category_details->varients)[$a]){echo 'checked';}}}?>  

                      name="varients[]" value="<?php echo $varient->id ?>">&nbsp;<?php echo $varient->name ?><br/>
                    <?php endforeach; ?>
                   </div>
                </div>
              </div>
            <?php endif; ?>
          </div>
      </div>
      
      <div class="modal-footer justify-content-between">
        <button type="button" class="btn btn-outline-light" data-dismiss="modal">Close</button>
        <button type="submit" class="btn btn-outline-light">Save changes</button>
      </div>
      <?php echo form_close() ?>
    </div>

  </div>
  <!-- /.modal-dialog -->
</div>

<?php endforeach; ?>
