<?php
session_start();
include('../include/connection.php');
include('../include/app_config.php');
if(isset($_POST['update']))
{
    switch ($_POST['update'])
    {   
        case 'disco_setup':
            $status="no";
            $success = 0;
            if(isset($_POST['status']))
            {
                $status="yes";
            }
            if($_POST['state'] != "" && $_POST['name'] != "")
            {
                $sql="update discos set name=:name,state_id=:state,is_active=:status,date_created = :date where id=:id";
                $fields=array(':name'=>ucwords($_POST['name']), ':state'=>$_POST['state'], ':status'=>$status,':date'=>date('d-m-Y'),':id'=>$_SESSION['discoid']);
                $q=$con->update_query($sql,$fields);
                if($q)
                {
                    $discoid = $_SESSION['discoid'];
                    $con->delete_query("delete from disco_state where disco_id=:id",array(':id'=>$discoid));
                    
                    $state = explode(',', $_POST['franchiselist']);
                    foreach($state as $st)
                    {
                        $sql = "insert into disco_state (state_id,disco_id) values (:state,:disco)";
                        $con->insert_query($sql,array(':state'=>$st,':disco'=>$discoid));
                    }
                    $success = 1;  //success
                }
            }
            else
            {
                $success = 3;  //empty required fields
            }
            echo json_encode(array('success'=>$success));
            break; 

        case 'article_setup':
            $success = 0;
            $file_error = 0;
            if(isset($_FILES['photo']) && !empty($_FILES['photo']))
            {
                $isvalidsize = filesize($_FILES['photo']['tmp_name']) <= MAX_FILE_SIZE;
                if(!$isvalidsize)
                {
                    $file_error = 3;
                    echo json_encode(array('success'=>$success,'file_error'=>$file_error));
                    return;
                }
            }
            if(!empty($_POST['title']) && !empty($_POST['body']) && !empty($_POST['category']))
            {
                $status = 0;
                if(isset($_POST['status']))
                {
                    $status=1;
                }
                $sql = "update news set title=:title,category_id=:cat,body=:body,is_published=:status,date_created=:date,author=:author where id=:id";
                $fields = array(
                    ':title'=>$_POST['title'],
                    ':cat'=>$_POST['category'],
                    ':body'=>$_POST['body'],
                    ':status'=>$status,
                    ':date'=>$_POST['date_created'],
                    ':author'=>$_POST['author'],
                    ':id'=>$_SESSION['articleid']
                );
                $q=$con->insert_query($sql,$fields);
                if($q)
                {
                    $success = 1;
                    $articleid = $_SESSION['articleid'];
                    if(isset($_FILES['photo']) && !empty($_FILES['photo']))
                    {
                        $ext=pathinfo($_FILES['photo']['name'], PATHINFO_EXTENSION);
                        if(strcasecmp($ext, "jpeg") == 0 || strcasecmp($ext, "jpg") == 0 || strcasecmp($ext, "png") == 0 || strcasecmp($ext, "pdf") == 0)
                        {
                            $photo = "article".$articleid.'.'.$ext;
                            move_uploaded_file($_FILES['photo']['tmp_name'], UPLOADS_FOLDER.$photo);
                            $sql = "update news set featured_image=:img where id=:id";
                            $con->update_query($sql,array(':img'=>$photo,':id'=>$articleid));
                        }
                        else 
                        {
                            $file_error = 1;
                        }
                    }
                    else if(empty($_POST['oldphoto']))
                    {
                        $file_error = 2;
                    }
                }
            }
            else
            {
                $success = 3;
            }
            echo json_encode(array('success'=>$success,'file_error'=>$file_error));
            break;
        
            
        case 'category_setup':
            $status=0;
            $success = 0;
            if(isset($_POST['status']))
            {
                $status=1;
            }
            
            $sql = "select * from news_category where code=:code AND id!=:id";
            $q=$con->select_query($sql,array(':code'=>$_POST['code'],':id'=>$_SESSION['catid']));
            if(count($q) > 0)
            {
                $success = 2;  //code exist
                echo json_encode(array('success'=>$success));
                return;
            }
            
            $sql="update news_category set code=:code, name=:name, status=:st where id=:id";
            $fields=array(
                ':code'=>$_POST['code'], ':name'=>ucwords($_POST['name']), ':st'=>$status,
                ':id'=>$_SESSION['catid']
            );
            if($con->update_query($sql,$fields))
            {
                $success = 1;
            }
            echo json_encode(array('success'=>$success));
            break;
            
            case 'complaint_category_setup':
                $status=0;
                $success = 0;
                if(isset($_POST['status']))
                {
                    $status=1;
                }
            
            
                $sql="update complaint_category set name=:name, is_active=:st where id=:id";
                $fields=array(':name'=>ucwords($_POST['name']), ':st'=>$status,
                    ':id'=>$_SESSION['catid']
                );
                if($con->update_query($sql,$fields))
                {
                    $success = 1;
                }
                echo json_encode(array('success'=>$success));
                break;
                
            case 'announcement_setup':
                if(!empty($_POST['body']) && !empty($_POST['title'])) 
                {
                    $status=0;
                    $success = 0;
                    if(isset($_POST['status']))
                    {
                        $status=1;
                    }
                
                    $sql = "update announcement set title=:title,type=:type,state_id=:state,lga_id=:lga,disco_id=:disco,body=:body,date_created=:date,is_published=:status where id=:id";
                    $fields = array(
                        ':title'=>$_POST['title'],
                        ':type'=>$_POST['type'],
                        ':state'=>$_POST['state'],
                        ':lga'=>$_POST['lga'],
                        ':disco'=>$_POST['disco'],
                        ':body'=>$_POST['body'],
                        ':date'=>date('d-m-Y H:i'),
                        ':status'=>$status,
                        ':id'=>$_SESSION['annid']
                    );
                    $q=$con->insert_query($sql,$fields);
                    if($q)
                    {
                        $success = 1;
                    }
                }
                else
                {
                    $success = 3;  //empty required fields
                }
                echo json_encode(array('success'=>$success));
                break;
                
            case 'generation_stats':
                $success = 0;
                if(!empty($_POST['date']) && (!empty($_POST['peak_gen']) || !empty($_POST['energy'])))
                {
                    $sql = "update stats set date_created=:date,peak_gen=:peak,energy=:energy where id=:id";
                    $fields = array(
                        ':date'=>$_POST['date'],
                        ':peak'=>$_POST['peak_gen'],
                        ':energy'=>$_POST['energy'],
                        ':id'=>$_SESSION['statid']
                    );
                    $q=$con->update_query($sql,$fields);
                    if($q)
                    {
                        $success = 1;
                    }
                }
                else
                {
                    $success = 3;  //empty required fields
                }
                echo json_encode(array('success'=>$success));
                break;
                
            case 'event_setup':
                $success = 0;
                $status = 0;
                if(isset($_POST['status']))
                {
                    $status = 1;
                }
                if(!empty($_POST['event_date']) && (!empty($_POST['title']) || !empty($_POST['body'])))
                {
                    $sql = "update event set event_date=:date,title=:title,description=:desc,status=:status where id=:id";
                    $fields = array(
                        ':date'=>$_POST['event_date'],
                        ':title'=>$_POST['title'],
                        ':desc'=>$_POST['body'],
                        ':status'=>$status,
                        ':id'=>$_SESSION['eventid']
                    );
                    $q=$con->insert_query($sql,$fields);
                    if($q)
                    {
                        $success = 1;
                    }
                }
                else
                {
                    $success = 3;  //empty required fields
                }
                echo json_encode(array('success'=>$success));
                break;
                
            case 'staff_birthday_setup':
                    $success = 0;
                    if(!empty($_POST['firstname']) && (!empty($_POST['lastname']) || !empty($_POST['dob'])  || !empty($_POST['email'])))
                    {
                        $sql = "update staff set firstname=:firstname,lastname=:lastname,othernames=:onames,dob=:dob,position=:position,dept=:dept,email=:email where id=:id";
                        $fields = array(
                            ':firstname'=>$_POST['firstname'],
                            ':lastname'=>$_POST['lastname'],
                            ':onames'=>$_POST['othernames'],
                            ':dob'=>$_POST['dob'],
                            ':position'=>$_POST['pos'],
                            ':dept'=>$_POST['dept'],
                            ':email'=>$_POST['email'],
                            ':id'=>$_POST['id']
                        );
                        $q=$con->update_query($sql,$fields);
                        if($q)
                        {
                            $success = 1;
                        }
                    }
                    else
                    {
                        $success = 3;  //empty required fields
                    }
                    echo json_encode(array('success'=>$success));
                    break;

            
        
    }
}
?>