<?php
include('../config/constant.php');
include('../includes/header.php');
include('../includes/navbar.php');

                
?>
	<div class="main">
			<main class="content">
				<div class="container-fluid p-0">
				           <div class="modal fade" id="subhomeModal" 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">Category</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                  
                  <!--<span aria-hidden="true">&times;</span>-->
                </button>
              </div>
              <div class="modal-body">
                <form action="../subcategory/update_sub.php" id="addSubhomeForm" method="POST" onclick="editSubhome(id);"enctype="multipart/form-data">
                  <div class="modal-body">
                    <div class="form-group">

                      <input type="hidden" name="id" id="id" class="form-control" placeholder="Enter Id">
                    </div>
                      <div class="form-group">
                      <label>Category</label>
                      <select class="form-control" id="category-dropdown" name="catid">
                          <i class="align-middle" data-feather="chevrons-down"></i>  
                                    <option data-feather="chevrons-down" value="">Select Category </option>
                                    <?php 
                   require_once '../config/db.php';
                   require_once '../config/database.class.php';
                   require_once '../config/functions.php';
                   $d = new database($db_config);?>
                   
                       
                     <?php 
                               $query = "SELECT * FROM category ";
                       $executeQuery = $d->selectQueryInArray($query);
                              
                              if($executeQuery )  {
                                // code...
                             
                            foreach ($executeQuery as $row)
                              {
                              ?>

<option value="<?php echo $row['id'];?>"><?php echo $row["category"];?></option>
<?php
}}
?>
          
                                   
                                </select>
                    </div>
                     <div class="form-group">
                      <label>Subcategory</label>
                      <input type="text" name="subcategory" id="subcategory" class="form-control" placeholder="Enter Subcategory">
                    </div>
                     <div class="form-group">
                      <label>Title</label>
                      <input type="text" name="title" id="title" class="form-control" placeholder="Enter Title">
                    </div>
                   
                     <!--<div class="form-group">
                      <label>Description</label>
                      <input type="text" name="description" id="description" class="form-control" placeholder="Enter Description">
                    </div>-->
                   <div class="form-group">
                        <label for="image">Select image:</label>
                        <img src='' style='max-height:200px;' id='image'>
                      <input type="file" id="image" name="image">
                    </div>
                  </div>
                  <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    <button type="submit" name="registerbtn" class="registerbtn btn-primary btn" onclick="saveSubhome();">Save</button>
                  </div>
                </div></form>
              </div>
            </div>
          </div>
 
					<div class="mb-3">
						<h1 class="h3 d-inline align-middle">Forms</h1>
						<a class="badge bg-dark text-white ms-2" href="upgrade-to-pro.html">
      Get more form examples
  </a> 
					</div> <form action="../subcategory/edit_sub.php" method="POST">
 
   <button type="submit" name="registerbtn" class="registerbtn btn-primary btn"  style="float: right">Update</button>
          
          <!--<button type="button"  style="float: right" class="btn btn-success" onclick="editSubhome(id);">
        <i class="align-middle" data-feather="edit"></i>Edit</button>-->
         
        <br/>
        <br/>
        <br/>
					<div class="row">
					     
						<div class="col-12 col-lg-8">
						     <?php
                                                $id = $_GET['id'];
                                                 $scat_string = str_replace(".","","$id");
                                              
                                                $query = "SELECT * FROM blog where id = '$scat_string' ";
                                                 $executeQuery = $d->selectQueryInArray($query);
           
                                                   if($executeQuery )  { 
                                                   foreach ($executeQuery as $row)
                                                      {
                                                ?>
							<div class="card">
								<div class="card-header">
									<h5 class="card-title mb-0">Title</h5>
								</div>
								<div class="card-body">
									<input type="text" class="form-control" placeholder="Title" name="title" value="<?php echo $row["title"];?>">
								</div>
							</div>
								<div class="card">
								<div class="card-header">
									<h5 class="card-title mb-0">SubTitle</h5>
								</div>
								<div class="card-body">
									<input type="text" class="form-control" placeholder="Subtitle" name="title" value="<?php echo $row["subtitle"];?>">
								</div>
							</div>

						
                  	<div class="card">
								<div class="card-header">
									<h5 class="card-title mb-0">Description</h5>
								</div>
								
								<div class="card-body">
									<textarea class="form-control" rows="11" placeholder="Description" name="description" id="description" value="<?php echo $row["description"];?>"></textarea>
								</div>
							</div>
							<!--<div class="card">
								<div class="card-header">
									<h5 class="card-title mb-0">Checkboxes</h5>
								</div>
								<div class="card-body">
									<div>
										<label class="form-check">
            <input class="form-check-input" type="checkbox" value="">
            <span class="form-check-label">
              Option one is this and that&mdash;be sure to include why it's great
            </span>
          </label>
										<label class="form-check">
            <input class="form-check-input" type="checkbox" value="" disabled>
            <span class="form-check-label">
              Option two is disabled
            </span>
          </label>
									</div>
									<div>
										<label class="form-check form-check-inline">
            <input class="form-check-input" type="checkbox" value="option1">
            <span class="form-check-label">
              1
            </span>
          </label>
										<label class="form-check form-check-inline">
            <input class="form-check-input" type="checkbox" value="option2">
            <span class="form-check-label">
              2
            </span>
          </label>
										<label class="form-check form-check-inline">
            <input class="form-check-input" type="checkbox" value="option3" disabled>
            <span class="form-check-label">
              3
            </span>
          </label>
									</div>
								</div>
							</div>-->
<?php }} ?>
							
						</div>

						<div class="col-12 col-lg-4">
						  
						<div class="card">
								<div class="card-header">
									<h5 class="card-title mb-0">ID</h5>
								</div>
								<div class="card-body">
								    	<input type="text" class="form-control" name="id" placeholder="id" value="<?php echo $row["id"];?>">
									
					
								</div>
							</div>

				
							  <?php
                                                $id = $_GET['id'];
                                                 $scat_string = str_replace(".","","$id");
                                              
                                                $query = "SELECT * FROM blog where id = '$scat_string' ";
                                                 $executeQuery = $d->selectQueryInArray($query);
           
                                                   if($executeQuery )  { 
                                                   foreach ($executeQuery as $row)
                                                      {
                                                ?>
							<!--<div class="card">-->
							<!--	<div class="card-header">-->
							<!--		<h5 class="card-title mb-0">Subcategory</h5>-->
							<!--	</div>-->
							<!--	<div class="card-body">-->
							<!--	    	<input type="text" class="form-control" name="product" placeholder="Product" id="product" value="<?php echo $row["product"];?>">-->
									
					
							<!--	</div>-->
							<!--</div>-->
								<div class="card">
								
							
								    	<div class="card-header">
                        <label for="image">Select image:</label>
                        </div>
                        	<div class="card-body">
                        	    <input class="form-control" type="hidden" name="dbimage" value="<?php echo $row["image"];?>">
                        <img src="https://perfectsourcinggloballimited.com/admin/view/<?php echo $row["image"];?>" style="width:300px;" id="image">
                        <input type="file" id="image" name="image">
                
							</div>	    
							
							
							</div>

						<!--	<div class="card">
								<div class="card-header">
									<h5 class="card-title mb-0">Disabled</h5>
								</div>
								
								<div class="card-body">
									<div class="mb-3">
										<label class="form-label">Disabled input</label>
										<input type="text" class="form-control" placeholder="Disabled input" disabled>
									</div>
									<div class="mb-3">
										<label class="form-label">Disabled select menu</label>
										<select class="form-control" disabled>
            <option>Disabled select</option>
          </select>
									</div>
									<label class="form-check">
          <input class="form-check-input" type="checkbox" value="" disabled>
          <span class="form-check-label">
            Can't check this
          </span>
        </label>
								</div>
							</div>-->
							<?php }} ?>
						</div>
						
					</div>
					</form>

				</div>
			</main>

						</div>
      <?php
          include('../includes/scripts.php');
        
          ?>
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.form/4.3.0/jquery.form.min.js" ></script>
  <script src="https://cdn.tiny.cloud/1/no-api-key/tinymce/5/tinymce.min.js" referrerpolicy="origin"></script>
    <script>
      tinymce.init({
        selector: "textarea"
      });
    </script>
          <script type="text/javascript">
            $(document).ready(function(){
              getAllSubhome();
            });

            function saveSubhome(){
              var options = {
                beforeSend: function () {
              //anything before sending request eg loader etc....
            },
            success: function (res) {
              // once receive response from backend
              //parese json
              var json = $.parseJSON(res);
              if(json.status=='success'){
                alert(json.message);
                $('#subhomeModal').modal('hide');
                $('#id').val('');
                $('#catid').val('');
                $('#subcategory').val('');
                $('#title').val('');
                $('#description').val('');
                $('#image').val('');
               
                getAllSubhome();
              }
              else{
                alert(json.message);
              }
              
            },
            error: function (response) {

            }        
          };
          $('#addSubhomeForm').ajaxForm(options);
        }


        function editSubhome(id){
 //alert(id);
$('#subhomeModal').modal('show');
$('#id').val(id);

$.ajax({
  type: "POST",
  data: 'id=' + id,
  url: '../subhome/get_subhome_by_id.php',
  success: function (response) {
    console.log(response)
    var json = $.parseJSON(response);

    $('#id').val(json[0].id);
    $('#catid').val(json[0].catid);
    $('#subcategory').val(json[0].subcategory);
    $('#title').val(json[0].title);
    $('#description').val(json[0].description);
    $('#image').attr("src",'https://digitaltechman.com/Demo/pi/admin/uploads/'+json[0].image); 
   
   
  }
});
}

function deleteSubhome(id){
  if(confirm("Are you sure to delete")){


   $.ajax({
    type: "POST",
    data: 'id=' + id,
    url: '../subhome/delete_subhome.php',
    success: function (response) {
      console.log(response)
      var json = $.parseJSON(response);

      if(json.status=='success'){
        alert(json.message);
        getAllSubhome();
      }
      else{
        alert(json.message);
      }

    }
  });
 }
}

function getAllSubhome(){
  $.ajax({
    type: "POST",
    data: 'id=' + id,
    url: '../subhome/get_all_subhome.php',
    success: function (response) {

      var json = $.parseJSON(response);
      var html = "";
      $('#dataTable tbody').html(html);

      for(i=0;i<json.length;i++){
        html = html+`
        <tr>
        

        <td> 
        <button type="button"style="float: right" class="btn btn-success" onclick="editSubhome(`+json[i].id+`);">
        <i class="align-middle" data-feather="edit"></i>Edit</button>
        </td>
       
      
        </tr>`;
      }
      $('#dataTable tbody').html(html);
    }
  });
}



</script>
 <!-- Summernote JS - CDN Link -->
    <script src="https://cdn.jsdelivr.net/npm/summernote@0.8.18/dist/summernote-lite.min.js"></script>
    <script>
        $(document).ready(function() {
            $("#description").summernote();
            $('.dropdown-toggle').dropdown();
        });
    </script>
      <script>
        $(document).ready(function() {
            $("#specification").summernote();
            $('.dropdown-toggle').dropdown();
        });
    </script>