<?php include "common/user_log.php"; ?>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Sampathbay</title>
    <!-- Place favicon.ico in the root directory -->
   <link rel="shortcut icon" type="image/x-icon" href="../img/S_favicon.ico"> 
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <!-- Bootstrap 3.3.5 -->
    <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="dist/css/font-awesome.min.css">
    <!-- Ionicons -->
    <link rel="stylesheet" href="dist/css/ionicons.min.css">
    <!-- daterange picker -->
    <link rel="stylesheet" href="plugins/daterangepicker/daterangepicker-bs3.css">
    <!-- iCheck for checkboxes and radio inputs -->
    <link rel="stylesheet" href="plugins/iCheck/all.css">
    <!-- Bootstrap Color Picker -->
    <link rel="stylesheet" href="plugins/colorpicker/bootstrap-colorpicker.min.css">
    <!-- Bootstrap time Picker -->
    <link rel="stylesheet" href="plugins/timepicker/bootstrap-timepicker.min.css">
    <!-- Select2 -->
    <link rel="stylesheet" href="plugins/select2/select2.min.css">
	<!-- DataTables -->
    <link rel="stylesheet" href="plugins/datatables/dataTables.bootstrap.css">
    <!-- Theme style -->
    <link rel="stylesheet" href="dist/css/AdminLTE.min.css">
    <!-- AdminLTE Skins. Choose a skin from the css/skins
         folder instead of downloading all of them to reduce the load. -->
    <link rel="stylesheet" href="dist/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="bootstrap-sweetalert/lib/sweet-alert.css" />
    <link href="assets/editor.css" type="text/css" rel="stylesheet"/>


<!-- requird value -->
<link href="SpryAssets/SpryValidationTextField.css" rel="stylesheet" type="text/css" />
<link href="SpryAssets/SpryValidationTextarea.css" rel="stylesheet" type="text/css" />
<script src="SpryAssets/SpryValidationTextField.js" type="text/javascript"></script>
<script src="SpryAssets/SpryValidationTextarea.js" type="text/javascript"></script>

<!--  End requird value -->

  </head>
  <body class="hold-transition skin-blue sidebar-mini">
    <div class="wrapper">

      <header class="main-header">
        <!-- Logo -->
           <a href="home.php" class="logo">
          <!-- mini logo for sidebar mini 50x50 pixels -->
         <span class="logo-mini"><img src="img/logo_mini.png" width="50"></span>
          <!-- logo for regular state and mobile devices -->
        <span class="logo-lg"><b><img src="img/logo.png" width="200"></b></span>
        </a>
        <!-- Header Navbar: style can be found in header.less -->
        <nav class="navbar navbar-static-top" role="navigation">
          <!-- Sidebar toggle button-->
          <a href="#" class="sidebar-toggle" data-toggle="offcanvas" role="button">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
			 <span class="logo-lg">
             &nbsp;&nbsp;<font face="Lucida Sans Unicode, Lucida Grande, sans-serif">Sampathbay</font></span>
          </a>
		
          <div class="navbar-custom-menu">
            <ul class="nav navbar-nav">

              <!-- User Account: style can be found in dropdown.less -->
              <li class="dropdown user user-menu">
                <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                  <img src="dist/img/user2-160x160.png" class="user-image" alt="User Image">
                  <span class="hidden-xs"><?php //echo $name; ?></span>
                </a>
                <ul class="dropdown-menu">
                  <!-- User image -->
                  <li class="user-header">
                    <img src="dist/img/user2-160x160.png" class="img-circle" alt="User Image">
                    <p>
              Sampathbay
                    </p>
                  </li>
                  <!-- Menu Body -->
                 
                  <!-- Menu Footer-->
                  <li class="user-footer">
                    <div class="pull-left">
                      
                    </div>
                    <div class="pull-right">
                      <a href="distroy.php" class="btn btn-default btn-flat">Sign out</a>
                    </div>
                  </li>
                </ul>
              </li>
              <!-- Control Sidebar Toggle Button -->
              <li>
                <a href="#" data-toggle="control-sidebar"><i class="fa fa-gears"></i></a>
              </li>
            </ul>
          </div>
        </nav>
      </header>
      <!-- Left side column. contains the logo and sidebar -->
      <aside class="main-sidebar">
        <!-- sidebar: style can be found in sidebar.less -->
        <section class="sidebar">
          <!-- Sidebar user panel -->
          
  <?php include "common/sidebar.php"; ?> 
        
        </section>
        <!-- /.sidebar -->
      </aside>

      <!-- Content Wrapper. Contains page content -->
      <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <section class="content-header">
          <h1>
            Add Sub Category
          </h1>
          
    <ol class="breadcrumb">
      <li><a href="home.php"><i class="fa fa-home"></i> Home</a></li>
      <li><a href="add_sub_cat.php">Add Sub Category</a></li>
    </ol>
        </section>

        <!-- Main content -->
        <section class="content">

<!-- ************************************************* -->


             
<div class="box box-info"> 
  <div class="box-header with-border"> 
    <h3 class="box-title">Sub Category Name</h3>
  </div>

  <div class="box-body"> 

    <!-- text area input -->
    <div class="form-group"> 
      <label>Select Category:<font color="#FF0000"><strong>*</strong></font></label>
      <!-- <select class="form-control select2" name="cat_id" id="main_cats" style="width: 100%;"><option>Test</option></select> -->
      <select class="form-control" id="cat_main"> <option>Select Main Category...</option> </select>
      <span id="cat_val_msg" style="color: red; display: none">*Select Main Category</span>
    </div>

    <!-- text input -->
    <div class="form-group">
      <label>Sub Category:<font color="#FF0000"><strong>*</strong></font></label>
      <input type="text" class="form-control" id="sub_cat" name="sub_cat" placeholder="Enter Sub Catagory...">
      <span id="subcat_val_msg" style="color: red; display: none">*Enter Sub Category name</span>
    </div>

    <!-- <div class="form-group">
      <label>Description:</label>
      <textarea style="width: 100%; height: 100px;" id="sub_descrip" name="sub_descrip"></textarea>

    </div> -->

	<div class="form-group">
		<div class="form-group">
		  <label>Description</label>
		  <!-- <textarea class="form-control" name="des" rows="15" id="editor1" required></textarea> -->
		  <textarea id="sub_descrip" ></textarea>

		  <textarea style="display: none;" id="txt_itm" name="des"></textarea>
		</div>
	</div>

    <!-- /.box-body -->
    <div class="box-footer">
      <button class="btn btn-primary" id="btn_sub">SUBMIT</button>
    </div>
  </div>
</div>



 <!-- /.table -->
        
    <div class="box box-info"> 
      <div class="box-header"> 
        <h3 class="box-title">Details Edit / Remove</h3>
      </div>
      <!-- /.box-header -->
      <div class="box-body"> 
        <table id="example1" class="table table-bordered table-striped">
          <thead>
            <tr> 
              <th>Id</th>
              <th>Category</th>
              <th>Sub Category</th>
              <th>Edit</th>
              <th>Remove</th>
            </tr>
          </thead>
          <tbody>
            <?php 
              $sql="SELECT * FROM sub_cat ORDER BY id DESC ";
              $result=mysqli_query($conn, $sql);

               $xx=0;
               while ($row = mysqli_fetch_array($result)){ $xx=$xx+1 ?>
                          <tr> 
                            <td><?php echo $xx; ?>.</td>
                           
                              <td><?php 
              			  
              			  $sql6="SELECT * FROM  cat WHERE id='".$row['cat_id']."'";
              $result6=mysqli_query($conn, $sql6);
               while ($row6 = mysqli_fetch_array($result6)){
               
               echo $cat=$row6['cat'];
              }
               ?>
              </td>
              <td><?php echo $row['sub_cat']; ?></td>
			 
              <td><button  class="btn btn-default btn-sm btn_up" data-toggle="modal" data-target="#myModal" value="<?php echo $row['id']; ?>"><span class="glyphicon glyphicon-pencil"></span> Edit </button></td>

              <td><button value="<?php echo $row['id']; ?>" class="btn btn-default btn-sm btn_dell"> 
                <span class="glyphicon glyphicon-trash"></span> Delete </button></td>
            </tr>
            <?php  } ?>
          </tbody>
        </table>
      </div>
      <!-- /.box-body -->
    </div>
    
    </section>
    
      </div>
	      <!-- /.End table -->
<!-- ************************************************* -->
      
           <!-- /.col -->
              </div><!-- /.row -->
            </div><!-- /.box-body -->
            
          

          <div class="row">
            
    
    
      </div>
      <!-- /.col (right) -->
         

      </div>
<footer class="main-footer"> 
<div class="pull-right hidden-xs">
          
        </div> <strong>Copyright &copy; Sampathbay</strong> All rights reserved.
      </footer>

      <!-- Control Sidebar -->
      <aside class="control-sidebar control-sidebar-dark">
        <!-- Create the tabs -->
        <ul class="nav nav-tabs nav-justified control-sidebar-tabs">
          <li><a href="#control-sidebar-home-tab" data-toggle="tab"><i class="fa fa-home"></i></a></li>
          <li><a href="#control-sidebar-settings-tab" data-toggle="tab"><i class="fa fa-gears"></i></a></li>
        </ul>
        <!-- Tab panes -->
        <div class="tab-content">
          <!-- Home tab content -->
          <div class="tab-pane" id="control-sidebar-home-tab">
            

            
             

          </div><!-- /.tab-pane -->
         

          </div><!-- /.tab-pane -->
        </div>
      </aside><!-- /.control-sidebar -->
      <!-- Add the sidebar's background. This div must be placed
           immediately after the control sidebar -->
      <div class="control-sidebar-bg"></div>
    </div><!-- ./wrapper -->

    <!-- Modal -->
  <div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog">
    
      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title" id="test_id">Edit Main Catagory</h4>
        </div>
        <div class="modal-body">
          <div class="form-group"> 
            <label>Select Category:<font color="#FF0000"><strong>*</strong></font></label>
            <!-- <select class="form-control select2" name="cat_id" id="main_cats" style="width: 100%;"><option>Test</option></select> -->
            <select class="form-control" id="cat_main_up"> <option>Select Main Catagory...</option> </select>
            <span id="up_cat_val_msg" style="color: red; display: none">*Select Main Category name</span>
          </div>
          <div class="form-group">
          <button style="display: none" id="id_holder"></button>
            <label>Category:<font color="#FF0000"><strong>*</strong></font></label>
            <input type="text" class="form-control" id="txt_up_sub_cat" placeholder="Enter Category Name...">
            <span id="up_subcat_val_msg" style="color: red; display: none">*Enter Sub Category name</span>
          </div>
          <!-- <div class="form-group">
            <label>Description:</label>
            <textarea style="width: 100%; height: 100px;" id="up_sub_descrip" name="up_sub_descrip"></textarea>
          </div> -->

          <div class="form-group">
  	        <div class="form-group">
  	          <label>Description</label>
  	          <textarea class="form-control" id="up_sub_descrip"></textarea>
  	          <textarea style="display: none;" id="txt_itm_up" name="des2"></textarea>
  	        </div>
  	      </div>

          <div class="form-group">
            <button type="button" class="btn btn-primary" id="update_cat">Update</button>
          </div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>
      
    </div>
  </div>

    <!-- jQuery 2.1.4 -->
    <script src="plugins/jQuery/jQuery-2.1.4.min.js"></script>
    <!-- Bootstrap 3.3.5 -->
    <script src="bootstrap/js/bootstrap.min.js"></script>
    <!-- Select2 -->
    <script src="plugins/select2/select2.full.min.js"></script>
	<!-- DataTables -->
    <script src="plugins/datatables/jquery.dataTables.min.js"></script>
    <script src="plugins/datatables/dataTables.bootstrap.min.js"></script>
    <!-- InputMask -->
    <script src="plugins/input-mask/jquery.inputmask.js"></script>
    <script src="plugins/input-mask/jquery.inputmask.date.extensions.js"></script>
    <script src="plugins/input-mask/jquery.inputmask.extensions.js"></script>
    <!-- date-range-picker -->
    <script src="plugins/daterangepicker/moment.min.js"></script>
    <script src="plugins/daterangepicker/daterangepicker.js"></script>
    <!-- bootstrap color picker -->
    <script src="plugins/colorpicker/bootstrap-colorpicker.min.js"></script>
    <!-- bootstrap time picker -->
    <script src="plugins/timepicker/bootstrap-timepicker.min.js"></script>
    <!-- SlimScroll 1.3.0 -->
    <script src="plugins/slimScroll/jquery.slimscroll.min.js"></script>
    <!-- iCheck 1.0.1 -->
    <script src="plugins/iCheck/icheck.min.js"></script>
    <!-- FastClick -->
    <script src="plugins/fastclick/fastclick.min.js"></script>
    <!-- AdminLTE App -->
    <script src="dist/js/app.min.js"></script>
    <!-- AdminLTE for demo purposes -->
    <script src="dist/js/demo.js"></script>
    <script src="bootstrap-sweetalert/lib/sweet-alert.min.js"></script>
    <!-- Page script -->
    <script src="assets/editor.js"></script>

    <script>
      $(document).ready(function() {
        $("#sub_descrip").Editor();
        $("#up_sub_descrip").Editor();
      });
    </script>


    <script>
      
      function data_load(){
        $.post('function/sub_cat.php', {'load_subcat': 'data'}, function (data) {
          $("#cat_main").html(data);
        });
      }

      function add_subcat(){
        var main_cat_val = $("#cat_main").val();
        var sub_cat_val = $("#sub_cat").val();
        var txt_itm = $("#txt_itm").val();

        $.post('function/sub_cat.php', {'sub_cat': 'data' , main_cat : main_cat_val , sub_cat : sub_cat_val , sub_descrip : txt_itm}, function (data) {
          if (data.status="ok") {
              swal({
                title: "Added!",
                text: "Successfully Updated!",
                type: "success",
                confirmButtonText: "OK"
                  },
                  function (isConfirm) {
                if (isConfirm) {
                    window.location.href = "add_sub_cat.php";
                }
              });
            }else{
              swal("Oops...", "Something went wrong!", "warning");
            }
        });
      }



      function load_subcat(id){
        $.post('function/sub_cat.php', {'load_subcat': 'data'}, function (data) {
          $("#cat_main_up").html(data);
        });
        $.post( "function/sub_cat.php", { load_subcat_up: "data", id : id}, function( data ) {
          $("#txt_up_sub_cat").val(data.sub_cat);
          $('#cat_main_up').val(data.cat_id);
          //$('#up_sub_descrip').val(data.sub_descrip);
          $('#up_sub_descrip').Editor("setText", data.sub_descrip);
          //$('#txtEditor_itm_up').Editor("setText", data.description_more);
        }, "json");
      }


      function update_subcat(id){
        
        var cat_val = $("#cat_main_up").val();
        var subcat_val = $("#txt_up_sub_cat").val();
        var sub_descrip = $("#txt_itm_up").val();

        $.post( "function/sub_cat.php", { update_subcat: "data", id : id , cat_val : cat_val, subcat_val : subcat_val , sub_descrip : sub_descrip}, function( data ) {
          
          if (data.status === "ok") {
              swal({
                title: "Updated!",
                text: "Successfully Updated!",
                type: "success",
                confirmButtonText: "OK"
                  },
                  function (isConfirm) {
                if (isConfirm) {
                    window.location.href = "add_sub_cat.php";
                }
              });
            }else{
              swal("Oops...", "Something went wrong!", "warning");
            }
        }, "json");

      }


      function del_subcat(id){
        $.post( "function/sub_cat.php", { del_subcat: "data", id : id}, function( data ) {
          
          if (data.status === "ok") {
              swal({
                title: "Deleted!",
                text: "Successfully Deleted!",
                type: "success",
                confirmButtonText: "OK"
                  },
                  function (isConfirm) {
                if (isConfirm) {
                    window.location.href = "add_sub_cat.php";
                }
              });
            }else{
              swal("Oops...", "Something went wrong!", "warning");
            }
        }, "json");

      }
////////////////////////////////////// validation ///////////////////////////////////////////////////////////
      function clear_validation_masg() {
        $('#cat_val_msg').fadeOut();
        $('#subcat_val_msg').fadeOut();
      }


      //display for error msg when load validate_item function
      function display_error_msg() {
        if ($('#cat_main').val() === 'Select District...') {
            $('#cat_val_msg').fadeIn();
        }
        if ($('#sub_cat').val() === '') {
            $('#subcat_val_msg').fadeIn();
        }
      }



      var validate_both = true;
      function validate_both_fun() {
        if ($('#cat_main').val() === 'empty') {
            validate_both = false;
            $('#cat_val_msg').fadeIn();
            display_error_msg();
            scrollTo(0, 0);
        } else {
            if ($('#sub_cat').val() === '') {
              validate_both = false;
              $('#subcat_val_msg').fadeIn();
              display_error_msg();
              scrollTo(0, 0);
            }

            else {
              validate_both = true;
              clear_validation_masg();
            }
        }
      }


function up_clear_validation_masg() {
    $('#up_cat_val_msg').fadeOut();
    $('#up_subcat_val_msg').fadeOut();

      }


      //display for error msg when load validate_item function
      function up_display_error_msg() {

    if ($('#cat_main_up').val() === 'empty') {
        $('#up_cat_val_msg').fadeIn();
    }
    if ($('#txt_up_sub_cat').val() === '') {
        $('#up_subcat_val_msg').fadeIn();
    }
      }

      var up_validate_both = true;
      function up_validate_both_fun() {
    if ($('#cat_main_up').val() === 'empty') {
        up_validate_both = false;
        $('#up_cat_val_msg').fadeIn();
        up_display_error_msg();
        scrollTo(0, 0);
    } else {
        if ($('#txt_up_sub_cat').val() === '') {
      up_validate_both = false;
      $('#up_subcat_val_msg').fadeIn();
      up_display_error_msg();
      scrollTo(0, 0);
        }

        else {
      up_validate_both = true;
      up_clear_validation_masg();
        }
    }
      }

//////////////////////////////////end validation ////////////////////////////////////////////////////////////////



      $(document).ready(function(){
        data_load();

        $("#btn_sub").click(function(){

          $('#txt_itm').val($('#sub_descrip').Editor("getText"));
          validate_both_fun();
          if (validate_both) {
            add_subcat();
          }
        });


        $(".btn_up").click(function(){
          $("#id_holder").val($(this).val());
          load_subcat($(this).val());
        });

        $("#update_cat").click(function(){

            $('#txt_itm_up').val($('#up_sub_descrip').Editor("getText"));

            up_validate_both_fun();
            if (up_validate_both) {
              update_subcat($("#id_holder").val());
            }
        });

        $(".btn_dell").click(function(){
          var x = $(this).val();
          swal({
            title: "Are you sure?",
            text: "Your may not be able to recover!",
            type: "warning",
            showCancelButton: true,
            confirmButtonClass: "btn-danger",
            confirmButtonText: "Yes, delete it!",
            closeOnConfirm: false
          },
            function () {
              del_subcat(x);
            }
          );
        });

      });

    </script>
	
	<script>
      $(function () {
        $("#example1").DataTable();
        $('#example2').DataTable({
          "paging": true,
          "lengthChange": false,
          "searching": false,
          "ordering": true,
          "info": true,
          "autoWidth": false
        });
      });
    </script>
  </body>
</html>
