<?php

if (isset($_POST['action']) && isset($_GET['update'])) {
    $case = array('case_id' => $_GET['update'], 'case_status' => $_POST['case_status'], 'created_by' => $_SESSION['user']['employee_id']);
    updateCase($case);
}
$case = null;
if (isset($_GET['update'])) {
    $case = mysqli_fetch_object(getCasesByID($_GET['update']));
}
?>
        <section id="my-account-security-form" class="page container">
            <form id="userSecurityForm" class="form-horizontal" action="" method="post">
                <div class="container">

                    <div class="alert alert-block alert-info">
                        <p>
                            Enter information for your account as desired.  Fields marked with an asterisk
                            are required.
                        </p>
                    </div>
                    <div class="row">
                        <div style="width:900px">
                            
                                <legend>Case Form</legend><br>

                                <?php 
if (isset($_POST['action']) && !isset($_GET['update'])) {
    $case = array('case_name' => $_POST['case_name'], 'case_status' => $_POST['case_status'], 'issue' => $_POST['issue'], 'category' => $_POST['category'], 'subcategory' => $_POST['subcategory'], 'profile_id' => $_GET['profile'], 'created_by' => $_SESSION['user']['employee_id']);
    $case_id = createCase($case);
    if ($case_id > -1) {
        <section class="page container">
            <div class="row">
                <div class="span16">
                        <?php 
mysqli_fetch_object(getProfiles());
$cases = getCasesByID($_GET['case']);
while ($case = $cases->fetch_object()) {
    echo "<div class='box'>\n                                        <div class='box-header'>\n                                            <i class='icon-envelope icon-large'></i>\n                                            <h5>({$case->casenumber}) {$case->case_name}</h5>\n                                            \n                                        </div>\n                                        <div style='padding:5px;font-size:12px'>\n                                            <b>Date Created</b>: {$case->c_date_time}<br>\n                                            <b>Created By</b>: {$case->firstname} {$case->lastname}, {$case->middlename}<br>\n                                            <b>Issue</b>: {$case->issue_name}<br>\n                                            <b>Category</b>: {$case->category_name}<br>\n                                            <b>Sub-Category</b>: {$case->subcategory_name}<br>\n                                            <b>Status</b>: {$case->case_status_name}<br>\n                                            " . ($case->case_status_id == 1 ? "<button type='button' class='btn' onClick='window.location.replace(\"/scc/?page=caseform&update={$case->case_id}\");'>Update</button>" : "") . "\n                                        </div>\n                                        <div class='box-content box-table'>\n\n\n                                        <div class='box-header'>\n                                        " . ($case->case_status_id == 1 ? "<a href='?page=ticketform&case={$case->case_id}' class='icon-pencil icon-large'></a>" : "") . "\n                                           \n                                            <h5>Tickets</h5>\n                                        </div>\n                                        <table class='table table-hover tablesorter'>\n                                            <thead>\n                                                <tr>\n                                                    <th>Ticket Number</th>\n                                                    <th>Ticket Name</th>\n                                                    <th>Date Created</th>\n                                                    <th>Created By</th>\n                                                    <th>Ticket Notes</th>\n                                                    <th>Ticket Status</th>\n                                                    \n                                                </tr>\n                                            </thead>\n                                            <tbody>";
    $tickets = getTicketsByCase($case->case_id);
    if ($tickets) {
        while ($ticket = $tickets->fetch_object()) {
            echo "<tr onClick='window.document.location=\"?page=ticketview&ticket={$ticket->ticket_id}\"' style='cursor: pointer'>\n                                                        <td>{$ticket->ticketnumber}</td>\n                                                        <td>{$ticket->ticket_name}</td>\n                                                        <td>{$ticket->t_date_time}</td>\n                                                        <td>{$ticket->firstname} {$ticket->lastname}, {$ticket->middlename}</td>\n                                                        <td>{$ticket->ticket_notes}</td>\n                                                        <td>{$ticket->ticket_status_name}</td>\n                                                    </tr>";
        }
    }
    echo "\n                                            \n                                            </tbody>\n                                        </table>\n                                        </div>\n\n                                    </div>";
}
?>
                
                    
                
                
                
                </div>
            </div>
        </section>
        
    
            </div>
        </div>
function createTicket($ticket)
{
    $conn = connect();
    $case = mysqli_fetch_object(getCasesByID($ticket['case_id']));
    $tickets = $conn->query("SELECT * FROM ticket ORDER BY ticket_id DESC LIMIT 1");
    $id = 1;
    if (mysqli_num_rows($tickets) > 0) {
        $t = mysqli_fetch_object($tickets);
        $id = $t->ticket_id;
        $id++;
    }
    $ticketnumber = $case->casenumber . "-" . sprintf('%06d', $id);
    $sql = "INSERT INTO ticket (ticketnumber, case_id, ticket_name, ticket_notes, ticket_status_id, t_created_by)\n\t\tVALUES (\n\t\t\t'{$ticketnumber}',\n\t\t\t{$ticket['case_id']},\n\t\t\t'{$ticket['ticket_name']}',\n\t\t\t'{$ticket['ticket_notes']}',\n\t\t\t{$ticket['ticket_status']},\n\t\t\t{$ticket['created_by']}\n\t\t\t)";
    $conn->query($sql);
    $id = mysqli_insert_id($conn);
    if ($id > 0) {
        $conn->close();
        return $id;
    } else {
        $conn->close();
        return -1;
    }
}