<?php 
session_start();
$title = "Article Categories Setup";
$group = "cms";
$menu = "category";
include('header.php');

$status="";
$name="";
$code="";

if(isset($_POST['catid']))
{
    $_SESSION['catid']=$_POST['catid'];

    $sql="select * from news_category where id=:id";
    $value=array(':id'=>$_SESSION['catid']);
    $r=$con->select_query($sql,$value);
    foreach($r as $value)
    {
        $status=$value['status'];
        $code=$value['code'];
        $name=$value['name'];
        $_SESSION['isEdit']=true;
    }
}
?>
<script>

function SaveInsert()
{
	var datastring = $("#categoryform").serialize();	
	var l = $( '.ladda-button-demo1' ).ladda();
	l.ladda( 'start');
	$.ajax({
	            type: "POST",
	            url: "../include/insert_ajax.php",
	            data: datastring,
	            dataType: 'json',
	            cache: false,
	            success: function(data) {
	            	l.ladda( 'stop' );
	            	if(data.success == 1)
	            	{
	            		setTimeout(function() {
	                        toastr.options = {
	                            closeButton: true,
	                            progressBar: true,
	                            positionClass: "toast-top-full-width",
	                            showMethod: 'slideDown',
	                            timeOut: 4000
	                        };
	                        toastr.success('Category service added successfully');
	                        $("input[type='text'], textarea, input[type='password'], input[type='number']").val("");
	                    }, 1300);
	            	}
	            	else if(data.success == 2)
	            	{
	            		setTimeout(function() {
	                        toastr.options = {
	                            closeButton: true,
	                            progressBar: true,
	                            positionClass: "toast-top-full-width",
	                            showMethod: 'slideDown',
	                            timeOut: 4000
	                        };
	                        toastr.error('Sorry, Code already exist');

	                    }, 1300);
	            	}
	            	else if(data.success == 3)
	            	{
	            		setTimeout(function() {
	                        toastr.options = {
	                            closeButton: true,
	                            progressBar: true,
	                            positionClass: "toast-top-full-width",
	                            showMethod: 'slideDown',
	                            timeOut: 4000
	                        };
	                        toastr.error('Error, please enter required fields');

	                    }, 1300);
	            	}
	            },
	            error: function(){
	                  alert('error handling here');
	                  l.ladda( 'stop' );
	            }
	        });
}

function SaveUpdate()
{
	var datastring = $("#categoryform").serialize();	
	var l = $( '.ladda-button-demo2' ).ladda();
	l.ladda( 'start');
	$.ajax({
	            type: "POST",
	            url: "../include/update_ajax.php",
	            data: datastring,
	            dataType: 'json',
	            cache: false,
	            success: function(data) {
	            	l.ladda( 'stop' );
	            	if(data.success == 1)
	            	{
	            		setTimeout(function() {
	                        toastr.options = {
	                            closeButton: true,
	                            progressBar: true,
	                            positionClass: "toast-top-full-width",
	                            showMethod: 'slideDown',
	                            timeOut: 4000
	                        };
	                        toastr.success('Service category saved successfully');

	                    }, 1300);
	            	}
	            	else if(data.success == 2)
	            	{
	            		setTimeout(function() {
	                        toastr.options = {
	                            closeButton: true,
	                            progressBar: true,
	                            positionClass: "toast-top-full-width",
	                            showMethod: 'slideDown',
	                            timeOut: 4000
	                        };
	                        toastr.error('Sorry, Code already exist');

	                    }, 1300);
	            	}
	            	else if(data.success == 3)
	            	{
	            		setTimeout(function() {
	                        toastr.options = {
	                            closeButton: true,
	                            progressBar: true,
	                            positionClass: "toast-top-full-width",
	                            showMethod: 'slideDown',
	                            timeOut: 4000
	                        };
	                        toastr.error('Error, please enter required fields');

	                    }, 1300);
	            	}
	            },
	            error: function(){
	                  alert('error handling here');
	                  l.ladda( 'stop' );
	            }
	        });
}
</script>

<div class="content-body">
        	<div class="row">
                    <div id="recent-transactions" class="col-12">
                        <div class="card">
                            <div class="card-header">
                                <h4 class="card-title">Article List</h4>
                            </div>
                            <div class="card-content">
							<form method="post" action="category_setup" id="categoryform" class="pform">
                                <div class="row row-pad">
                                    <div class="col-md-2">
                                        <label for="name">Category Code</label>
                                    </div>
                                    <div class="col-md-6">
                                        <input type="text" name="code" class="form-control" value="<?php echo $code;?>" required/>
                                    </div>
                                </div>
                                
                                <div class="row row-pad">
                                    <div class="col-md-2">
                                        <label for="name">Category Name</label>
                                    </div>
                                    <div class="col-md-6">
                                        <input type="text" name="name" class="form-control" value="<?php echo $name;?>" required/>
                                    </div>
                                </div>
                                          
                                <div class="row row-pad">
                                    <div class="col-md-2">
                                        
                                    </div>
                                    <div class="col-md-6">
                                        <label> <input type="checkbox" class="i-checks"  id="status" name="status" <?php echo ($status==1 ? "checked" : "")?>/> Active </label>
                                    </div>
                                </div>
                               
                                <div class="row row-pad">
                                    <div class="col-md-2">
                                        
                                    </div>
                                    <div class="col-md-6">
                                    <?php 
                                        if($_SESSION['isEdit']==true){
                                            echo '
                                            <input type="hidden" name="update" value="category_setup"/>
                                            <button type="button" onclick="SaveUpdate()" class="ladda-button ladda-button-demo2 btn btn-primary"  data-style="zoom-in">Update Category</button>';
                                        } else {
                                            echo '
                                            <input type="hidden" name="insert" value="category_setup"/>
                                            <button type="button" onclick="SaveInsert()" class="ladda-button ladda-button-demo1 btn btn-primary"  data-style="zoom-in">Add Category</button>';
                                        }
                                    ?>
                                        <a href="category_list" class="btn btn-warning">Cancel</a>
                                    </div>
                                </div>
                                </form>                           
                            </div>
                        </div>
                    </div>
                </div>
                <!--/ Recent Transactions -->

   </div>

    
    <?php include('footer.php');?>

   
