<?php 
$file_name		= "index?page=view";
$form_action	= "insert";
$button_value 	= "Add";
$table_name 	= "module";
$page_name      = "Add module";
$id	 = "";
$vat = "";
$query_string = "";
################################################## START INSERT ##################################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'insert'){	
    $module = $_REQUEST['module'];
    $display_name = $_REQUEST['display_name'];
    $parent_id = $_REQUEST['parent_id'];
    $display = isset($_REQUEST['display'])? 1: 0;
	/////////////////// getting maximum order of module //////////////////////////////////
	$sql_sub_cat_ord = "SELECT max(ord) FROM module";
	$rs_sub_cat_ord = mysqli_query($conn,$sql_sub_cat_ord);
	if(mysqli_num_rows($rs_sub_cat_ord) > 0){
		$row_sub_cat_ord = mysqli_fetch_array($rs_sub_cat_ord);
		$max_sub_cat_ord 	= $row_sub_cat_ord['0'];
	}
	$max_sub_ord = $max_sub_cat_ord +1;
	////////////////////////////////////////////////////////////////////////////
	
    $sql= "INSERT INTO $table_name(module,parent_id,display_name,ord,display) VALUES('$module','$parent_id','$display_name','$max_sub_ord','$display')";
    mysqli_query($conn,$sql);
	$tmp = ucfirst(str_replace("_"," ", $table_name));
	header("Location: $file_name&message=$tmp added sucessfully");
    exit();
}
################################################# END INSERT #################################################

################################################## START EDIT ##################################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'edit'){	
	$form_action = "update";
	$button_value = "Update";
	$page_name      = "Edit module";
	$query_string =  (isset($_POST['query_string'])) ? $_POST['query_string'] : $_SERVER['QUERY_STRING'];
	
    $id = $_REQUEST['id'];
    $sql= "SELECT * FROM $table_name WHERE id = $id";
    $result_edit = mysqli_query($conn,$sql);
    $row_edit 	 = mysqli_fetch_array($result_edit);
	
	$display_name = $_REQUEST['display_name'];
    $parent_id = $_REQUEST['parent_id'];
	
	$display   = $row_edit['display'];
    $module			= $row_edit['module'];
    $parent_id		= $row_edit['parent_id'];
    $display_name	= $row_edit['display_name'];
}
################################################# END EDIT ################################################# 


################################################## START UPDATE ################################################## 
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'update'){    
	$id= $_REQUEST['id'];    
    $module= $_REQUEST['module'];	
	$display_name = $_REQUEST['display_name'];
    $parent_id = $_REQUEST['parent_id'];
	$display	=	isset($_REQUEST['display'])? 1: 0;
    $sql= "UPDATE module SET module = '$module',display_name='$display_name',parent_id='$parent_id',display='$display' WHERE id = $id";	

	mysqli_query($conn,$sql);	
	
	$query_string = (isset($_REQUEST['query_string']) && !empty($_REQUEST['query_string'])) ? $_REQUEST['query_string'] : "";
  	$query_string = str_ireplace("page=add", "page=view", $query_string);
	$query_string = "&".$query_string;
	$tmp = ucfirst(str_replace("_"," ", $table_name));
    header("Location: $file_name&message=$tmp updated sucessfully$query_string");
    exit();
}
################################################# END UPDATE ################################################# 
?>
<section class="content-header">
	<div class="container-fluid">
		<div class="row mb-2">
			<div class="col-sm-6">
				<h1>
				<?=$page_name;?>
				</h1>
			</div>
		</div>
	</div><!-- /.container-fluid -->
</section>
<section class="content">
	<div class="row">
		<div class="col-12">
			<!-- general form elements -->
              <div class="card">
                <form class="form-horizontal" method="post" name="form1" id="form1" action="index?page=add&action=<? echo $form_action; ?>">
                  <div class="card-body">
                    <div class="form-group">
                      <label for="Parent Module" class="control-label">Parent Module</label>
                      <div class="">
                        <? utility::getModuleList("parent_id", $parent_id,"100%",false);?>
                      </div>
                    </div>
					<div class="form-group">
                      <label for="Module Name" class="control-label">Module Name</label>
                      <div class="">
                        <input type="text" id="module" name="module" value="<?php echo $module;?>" class="form-control" placeholder="Module Name">
                      </div>
                    </div>
					<div class="form-group">
                      <label for="Display Name" class="control-label">Display Name</label>
                      <div class="">
                        <input type="text" id="display_name" name="display_name" value="<?php echo $display_name;?>" class="form-control" placeholder="Display Name">
                      </div>
                    </div>
					<div class="form-group">
                      <label for="Show" class="control-label">Show</label>
                      <div class="">
                        <input type="checkbox" id="display" name="display" <? echo ($display) ? "checked" : ""; ?>>
                      </div>
                    </div>
                  </div><!-- /.box-body -->
                  <div class="card-footer">
					<input type="hidden" id="id" name="id" value="<?php echo $id;?>">
					<input type="hidden" id="query_string" name="query_string" value="<?php echo $query_string;?>">
                    <button type="submit" id="submit" name="submit" value="<?php echo $button_value;?>" class="btn btn-primary">Submit
						<i class="fa fa-refresh fa-spin" style="display:none;" id="page_load_div"></i>
					</button>
                  </div>
                </form>
              </div><!-- /.box -->
		</div>
	</div>
</section>
<script type="text/javascript">
$(document).ready(function(){
	$("#form1").submit(function() {
		$("#submit").attr("disabled", true);
		$('#page_load_div').show();
		//return true;
	});
});
</script>