<?php session_start(); ?>
<?php echo(!isset($_SESSION['id']))?"<script> document.location = 'http://localhost/activity_1/login.php'; </script>":''; ?>
<?php 
    if(isset($_POST['create_thread'])){
        $con = mysqli_connect('localhost','root','','account');
        if(!$con)
            die('ERROR: '. mysqli_error());
        
        $sql = "INSERT INTO forum (topic,username,categoryid,date_posted) VALUES ('".$_POST['thread']."','".$_POST['username']."','".$_POST['category']."',now())";
        
        mysqli_query($con, $sql);
        mysqli_close($con);
        
        echo "<script> document.location = 'http://localhost/activity_1/forum.php' </script>";
    }
    if(isset($_POST['delete_forum'])){
        $con = mysqli_connect('localhost','root','','account');
        if(!$con)
            die('ERROR: '. mysqli_error());
        
        $sql = "DELETE FROM forum WHERE id = ".$_POST['delete_forum'];
        mysqli_query($con, $sql);
        if(mysqli_affected_rows($con))
            echo "<script> alert('Topic Deleted!!!'); </script>";
        $sql = "DELETE FROM comments WHERE topic_id = ".$_POST['delete_forum'];
        mysqli_query($con, $sql);
        mysqli_close($con);
    }
    $con = mysqli_connect('localhost','root','','account');
    if(!$con)
        die('ERROR: '. mysqli_error());
    
    $sql1 = "SELECT * FROM forum WHERE categoryid = 1";
    $result1 = mysqli_query($con, $sql1);
    $sql2 = "SELECT * FROM forum WHERE categoryid = 2";
    $result2 = mysqli_query($con, $sql2);
    $sql3 = "SELECT * FROM forum WHERE categoryid = 3";
    $result3 = mysqli_query($con, $sql3);
    $sql4 = "SELECT * FROM forum WHERE categoryid = 4";
    $result4 = mysqli_query($con, $sql4);
    $sql5 = "SELECT * FROM forum WHERE categoryid = 5";
    $result5 = mysqli_query($con, $sql5);
?>
<html>
    <head>
        <title></title>
        <link rel="stylesheet" href="style.css"/>
        <style>
            .categories{
                list-style: disc;
            }
            .categories li > a{
                text-decoration: none;
                color: #1f43ff;
                font-weight: bold;
            }
            .categories li > a:hover{
                color: #1f43ff;
                background: #d1d9ff; 
            }
            .li_container{
                display: none;
                padding: 5px 10px;
                border: 1px solid black;
                max-width: 450px;
            }
            .li_container a{
                text-decoration: none;
                color: #1f43ff;
            }
            .li_container a:hover{
                text-decoration: underline;
                color: #1f43ff;
            }
        </style>
    </head>
<!--    onload="whatsYourName('Hi, What\'s Your Name?');"-->
    <body>
        <div class="containerAll">
            <div class="header">
                <div class="settingsContainer">
                    <div class="userName" id="name">
                        <input id="hidden_hint" type="hidden" value="hide"/>
                <?php if(isset($_SESSION['id'])){ ?>
                    Hi, <?php echo ucfirst($_SESSION['firstname']).' '.ucfirst($_SESSION['lastname']) ?>
                <?php }else{ ?>
                    You are not logged in.
                <?php } ?>
                        <div class="arrow_down" onclick="show_setting();"></div>
                    </div>
                    <div class="settings" id="settings_id">
                        <?php if(isset($_SESSION['id'])){ ?>
                            <a class="settings_item" href="http://localhost/activity_1/register.php"><div class="settings_item_div">Register</div></a>
                            <a class="settings_item" href="http://localhost/activity_1/logout.php"><div class="settings_item_div">Logout</div></a>
                        <?php }else{ ?>
                            <a class="settings_item" href="http://localhost/activity_1/login.php"><div class="settings_item_div">Log in</div></a>
                            <a class="settings_item" href="http://localhost/activity_1/register.php"><div class="settings_item_div">Register</div></a>
                        <?php } ?>
                    </div>
                </div>
            </div>
            <div class="menu">
                <a class="thisButtonMenu" href="#">Home</a>
                <a class="thisButtonMenu" href="#">Profile</a>
                <a class="thisButtonMenu" href="http://localhost/activity_1/forum.php">Forum</a>
                <a class="thisButtonMenu" href="#">About Us</a>
                <a class="thisButtonMenu" href="#">Contact Us</a>
            </div>
            <div class="body">
                <br/><br/><br/>
                <h2 style="margin-left: 24px;">Categories: </h2>
                <ul class="categories">
                    <li><a href="#" data-value="1" data-hint="1">College of Computer Studies</a>
                        <div class="li_container" data-value="1">
                            <a href="http://localhost/activity_1/forum_create.php?category=1">Create New</a>
                            <?php if(mysqli_num_rows($result1)){ ?>
                            <?php while($row = mysqli_fetch_array($result1)){ ?>
                                <div style="margin-top: 10px" data-value="<?php echo $row['id'] ?>" data-topic="<?php echo $row['topic'] ?>">
                                    <div><?php echo ucwords($row['username']) ?></div>
                                    <div class="forum_link"><a href="http://localhost/activity_1/view_forum.php?topic_id=<?php echo $row['id'] ?>">&#8220<span class="thread"><?php echo $row['topic'] ?></span>&#8221</a></div>
                                    <div class="edit" data-value="<?php echo $row['id'] ?>" data-name="edit" style="display: none">
                                        <textarea style="resize: none"><?php echo $row['topic'] ?></textarea><br/>
                                        <button name="done">Done</button><button name="cancel">Cancel</button>
                                    </div>
                                    <div><?php echo $row['date_posted'] ?></div>
                                    <button class="edit_thread">Edit</button>
                                    <form method="POST"><button type="submit" name="delete_forum" value="<?php echo $row['id'] ?>">Delete</button></form>
                                </div>
                            <?php } ?>
                            <?php }else{ ?>
                                <div style="margin-top: 10px"><i>No forums posted.</i></div>
                            <?php } ?>
                        </div>
                    </li>
                    <li>
                        <a href="#" data-value="2" data-hint="1">College of Engineering and Architecture</a>
                        <div class="li_container" data-value="2">
                            <a href="http://localhost/activity_1/forum_create.php?category=2">Create New</a>
                            <?php if(mysqli_num_rows($result2)){ ?>
                            <?php while($row = mysqli_fetch_array($result2)){ ?>
                                <div style="margin-top: 10px" data-value="<?php echo $row['id'] ?>" data-topic="<?php echo $row['topic'] ?>">
                                    <div><?php echo ucwords($row['username']) ?></div>
                                    <div class="forum_link"><a href="http://localhost/activity_1/view_forum.php?topic_id=<?php echo $row['id'] ?>">&#8220<span class="thread"><?php echo $row['topic'] ?></span>&#8221</a></div>
                                    <div class="edit" data-value="<?php echo $row['id'] ?>" data-name="edit" style="display: none">
                                        <textarea style="resize: none"><?php echo $row['topic'] ?></textarea><br/>
                                        <button name="done">Done</button><button name="cancel">Cancel</button>
                                    </div>
                                    <div><?php echo $row['date_posted'] ?></div>
                                    <button class="edit_thread">Edit</button>
                                    <form method="POST"><button type="submit" name="delete_forum" value="<?php echo $row['id'] ?>">Delete</button></form>
                                </div>
                            <?php } ?>
                            <?php }else{ ?>
                                <div style="margin-top: 10px"><i>No forums posted.</i></div>
                            <?php } ?>
                        </div>
                    </li>
                    <li>
                        <a href="#" data-value="3" data-hint="1">College of Arts and Sciences</a>
                        <div class="li_container" data-value="3">
                            <a href="http://localhost/activity_1/forum_create.php?category=3">Create New</a>
                            <?php if(mysqli_num_rows($result3)){ ?>
                            <?php while($row = mysqli_fetch_array($result3)){ ?>
                                <div style="margin-top: 10px" data-value="<?php echo $row['id'] ?>" data-topic="<?php echo $row['topic'] ?>">
                                    <div><?php echo ucwords($row['username']) ?></div>
                                    <div class="forum_link"><a href="http://localhost/activity_1/view_forum.php?topic_id=<?php echo $row['id'] ?>">&#8220<span class="thread"><?php echo $row['topic'] ?></span>&#8221</a></div>
                                    <div class="edit" data-value="<?php echo $row['id'] ?>" data-name="edit" style="display: none">
                                        <textarea style="resize: none"><?php echo $row['topic'] ?></textarea><br/>
                                        <button name="done">Done</button><button name="cancel">Cancel</button>
                                    </div>
                                    <div><?php echo $row['date_posted'] ?></div>
                                    <button class="edit_thread">Edit</button>
                                    <form method="POST"><button type="submit" name="delete_forum" value="<?php echo $row['id'] ?>">Delete</button></form>
                                </div>
                            <?php } ?>
                            <?php }else{ ?>
                                <div style="margin-top: 10px"><i>No forums posted.</i></div>
                            <?php } ?>
                        </div>
                    </li>
                    <li>
                        <a href="#" data-value="4" data-hint="1">College of Education</a>
                        <div class="li_container" data-value="4">
                            <a href="http://localhost/activity_1/forum_create.php?category=4">Create New</a>
                            <?php if(mysqli_num_rows($result4)){ ?>
                            <?php while($row = mysqli_fetch_array($result4)){ ?>
                                <div style="margin-top: 10px" data-value="<?php echo $row['id'] ?>" data-topic="<?php echo $row['topic'] ?>">
                                    <div><?php echo ucwords($row['username']) ?></div>
                                    <div class="forum_link"><a href="http://localhost/activity_1/view_forum.php?topic_id=<?php echo $row['id'] ?>">&#8220<span class="thread"><?php echo $row['topic'] ?></span>&#8221</a></div>
                                    <div class="edit" data-value="<?php echo $row['id'] ?>" data-name="edit" style="display: none">
                                        <textarea style="resize: none"><?php echo $row['topic'] ?></textarea><br/>
                                        <button name="done">Done</button><button name="cancel">Cancel</button>
                                    </div>
                                    <div><?php echo $row['date_posted'] ?></div>
                                    <button class="edit_thread">Edit</button>
                                    <form method="POST"><button type="submit" name="delete_forum" value="<?php echo $row['id'] ?>">Delete</button></form>
                                </div>
                            <?php } ?>
                            <?php }else{ ?>
                                <div style="margin-top: 10px"><i>No forums posted.</i></div>
                            <?php } ?>
                        </div>
                    </li>
                    <li>
                        <a href="#" data-value="5" data-hint="1">College of Nursing</a>
                        <div class="li_container" data-value="5">
                            <a href="http://localhost/activity_1/forum_create.php?category=5">Create New</a>
                            <?php if(mysqli_num_rows($result5)){ ?>
                            <?php while($row = mysqli_fetch_array($result5)){ ?>
                                <div style="margin-top: 10px" data-value="<?php echo $row['id'] ?>" data-topic="<?php echo $row['topic'] ?>">
                                    <div><?php echo ucwords($row['username']) ?></div>
                                    <div class="forum_link"><a href="http://localhost/activity_1/view_forum.php?topic_id=<?php echo $row['id'] ?>">&#8220<span class="thread"><?php echo $row['topic'] ?></span>&#8221</a></div>
                                    <div class="edit" data-value="<?php echo $row['id'] ?>" data-name="edit" style="display: none">
                                        <textarea style="resize: none"><?php echo $row['topic'] ?></textarea><br/>
                                        <button name="done">Done</button><button name="cancel">Cancel</button>
                                    </div>
                                    <div><?php echo $row['date_posted'] ?></div>
                                    <button class="edit_thread">Edit</button>
                                    <form method="POST"><button type="submit" name="delete_forum" value="<?php echo $row['id'] ?>">Delete</button></form>
                                </div>
                            <?php } ?>
                            <?php }else{ ?>
                                <div style="margin-top: 10px"><i>No forums posted.</i></div>
                            <?php } ?>
                        </div>
                    </li>
                </ul>
                <center>
                    
                </center>
            </div>
            <div class="footer">
                <span class="copy">&COPY; Copyright 2013 CIT-U</span>
            </div>
        </div>
        
        <script type="text/javascript" src="jquery-1.5.2.js"></script>
        <script type="text/javascript" src="style.js"></script>
        <script>
            $(document).ready(function(){
                $('ul.categories a').click(function(){
                    var value = $(this).attr('data-value');
                    var hint = $(this).attr('data-hint');
                    var group = $(this).parent('li');
                    if(hint == 0){
                        $('div.li_container',group).stop(true,true).slideUp('fast');
                        $(this).attr('data-hint','1');
                    }else{
                        accordion_type(value);
                        $('div.li_container',group).stop(true,true).slideDown('fast');
                        $(this).attr('data-hint','0');
                    }
                });
                var accordion_type = function(value){
                    $('ul.categories div.li_container[data-value!="'+value+'"]').each(function(){
                        var group = $(this).parent('li');
                        $('a',group).attr('data-hint','1');
                        $(this).stop(true,true).slideUp('fast',function(){ $(this).hide() });
                    });
                }
                $('.edit_thread').live('click',function(){
                    var group = $(this).parent('div');
                    var value = $(this).parent('div').attr('data-value');
                    
                    hide_all_textarea(value);
                    $('div.forum_link',group).hide();
                    $('div[data-name="edit"]',group).show();
                });
                $('button[name="cancel"]').live('click',function(){
                    
                    $(this).parent('div').hide();
                });
                
                $('button[name="done"]').live('click',function(){
                    var group = $(this).parent('div').parent('div');
                    var value = $(this).parent('div').attr('data-value');
                    
                    var text = $('div.edit textarea', group).val();
                    if(text == ''){
                        alert('Please type a text!!!');
                        $('div.edit textarea', group).focus();
                        return false;
                    }
                    
                    $.ajax({
                        type: 'POST',
                        data: { topic: text, topic_id: value },
                        url: 'http://localhost/activity_1/ajax_edit.php',
                        dataType: 'json',
                        success: function(data){
                            if(data.status)
                                alert('Topic Edited!!!');
                            else
                                alert('Topic Not Edited!!!');
                        },
                        error: function(xhr){
                            alert(xhr.responseText);
                        }
                    });
                    $('span.thread', group).html(text);
                    
                    $('div.forum_link',group).show();
                    $(this).parent('div').hide();
                });
                
                var hide_all_textarea = function(value){
                    $('div.edit[data-value!="'+value+'"]').each(function(){
                        $(this).hide();
                    });
                }
            });
        </script>
    </body>
</html>
<?php mysqli_close($con); ?>