<?php 
    // continue the session
    session_start();

    // Add includes to PHP files that I will need to access
    include_once dirname(__FILE__) . '/php_only_scripts/global_functions.php';
    include_once dirname(__FILE__) . '/php_only_scripts/database.php';
    include_once dirname(__FILE__) . '/php_only_scripts/presentation.php';
    include_once dirname(__FILE__) . '/php_only_scripts/blank_presentation.php';
    include_once dirname(__FILE__) . '/php_only_scripts/blanks_presentation.php';
    include_once dirname(__FILE__) . '/php_only_scripts/note_presentation.php';
    
    // Init some variables
    $userRow=null;

    // Assume NOT gtg
    $gtg = false;

    // Create and Setup the database
    $db = new Database();
    $db->setupTmsDB();

    // Attempt to log in with this user
    $currentUser = $db->getLoggedInSessionUser(session_id());
    
    // Set passing variables to defaults
    $selectedCompany=-3;
    $selectedStatus=-1;
    $showWhatFinishedState=0;
    $taskSearchPhrase='';
    $sortNumber=1;
    $blanksPerPage=0;
    $currentPage=1;
        
    // If there is only one result for a username/password combo, then they are good to go.
    if ($currentUser == null){
        // redirect to the login page
        echo ('<script>window.location = ".";</script>');
    }
    elseif (mysqli_num_rows($currentUser)==1){
        // Attempt to get the user row
        $userRow = $currentUser->fetch_assoc();
        
        // Determine if this user has access to this page
        if ($userRow["UserLevel"]=='TIMECARD ONLY' ||
            $userRow["AccountStatus"]!='ACTIVE' ||
            $userRow["SUEmployee"]!=1){
            // redirect to the invalid access page
            echo ('<script>window.location = "invalid_access.php";</script>');

            exit;
        }
        
        // Set the good to go until they log out
        $gtg = true;

        // Determine if the company combo box changed
        if (isset($_GET['cs'])){
            // Set the session variable to the new value of the combo box
            $selectedCompany = rawurldecode($_GET['cs']);
        }
        
        // Determine if the status combo box changed
        if (isset($_GET['s'])){
            // Set the session variable to the new value of the combo box
            $selectedStatus = rawurldecode($_GET['s']);
        }
        
        // Determine what type of finished state they want to see 
        if (isset($_GET['sfs'])){
            // Change the option to show whatever they are asking for
            $showWhatFinishedState=rawurldecode($_GET['sfs']);
        }
        
        // Determine how many blanks per page they would like to see
        if (isset($_GET['bpp'])){
            // Change the option to show whatever they are asking for
            $blanksPerPage=rawurldecode($_GET['bpp']);
        }
        
        // Determine what current page we are on
        if (isset($_GET['cp'])){
            // Change the option to show whatever they are asking for
            $currentPage=rawurldecode($_GET['cp']);
        }
        
        // Determine if there is a search phrase
        if (isset($_GET['sp'])){
            // Set the search phrase
            $taskSearchPhrase=rawurldecode($_GET['sp']);
        }

        // Determine if there is a sort preference
        if (isset($_GET['sn']) ){
            // Hold onto the sort info
            $sortNumber = rawurldecode($_GET['sn']);
        }

        // Determine if they want to see readied blanks
        if (isset($_GET['srb']) ){
            // Hold onto the info
            $showReadiedBlanks = rawurldecode($_GET['srb']);
        }
    }
?>
        
﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <title>Task Management System - Blanks</title>

    <meta name="keywords" content="GIS,data,testing,IT" />
    <meta name="description" content="EES Divisions." />
    <?php
        // determine if the refresh rate is a positive number
        if ($userRow["RefreshRate"]>0){
            // Add the refresh rate line
            echo('<meta http-equiv="refresh" content="' . $userRow["RefreshRate"] . '"/>');
        }
    ?>

    <script src="javascript/general.js" type="text/javascript"></script>
    <script src="javascript/notes.js" type="text/javascript"></script>
    <script src="javascript/blanks.js" type="text/javascript"></script>
    
    <script src="./SpryAssets/SpryMenuBar.js" type="text/javascript"></script>
    <script src="scripts/jquery-ui-1.10.4.custom/js/jquery-1.10.2.js" type="text/javascript"></script>
    <script src="scripts/jquery-ui-1.11.4.custom/jquery-ui.js" type="text/javascript"></script>

    <link rel="icon" href="images/ees_ico.ico"/> 
    <link href="scripts/jquery-ui-1.10.4.custom/development-bundle/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/css"/>
    <link href="./SpryAssets/SpryMenuBarHorizontal.css" rel="stylesheet" type="text/css" />
    <link href="./SpryAssets/SpryMenuBarVertical.css" rel="stylesheet" type="text/css" />
    <link rel = "stylesheet" type="text/css" href="./css/contentpage.css" >
        
    <style type="text/css">
        #blanks_sortable
        #blanks_sortable { list-style-type: none; margin: 0; padding: 0; width: 100%; }
        #blanks_sortable li { margin: 0 0 5px 0; padding: 5px 0 5px 0;}

        .Table
        {
            display: table;
            width: 100%;
        }
        .Title
        {
            display: table-caption;
            text-align: center;
            font-weight: bold;
            font-size: larger;
            width: 100%;
        }
        .Heading
        {
            display: table-row;
            font-weight: bold;
            text-align: center;
            width: 100%;
        }
        .Row
        {
            display: table-row;
            width: 100%;
        }
        .cell
        {
            display: table-cell;
            border: solid;
            border-width: thin;
            padding-left: 2px;
            padding-right: 2px;
            font-weight: bold;
            font-size: 11px;
            vertical-align: middle;
        }
        .childrenCell
        {
            width: 100%;
            text-align: right;
        }
        .dateCell
        {
            width: 9%;
            text-align: center;
        }
        .materialCell
        {
            width: 18%;
            text-align: center;
        }
        .updateCell
        {
            width: 8%;
            text-align: center;
        }
        .dimensionsCell
        {
            width: 20%;
        }
        .companyCell
        {
            width: 14%;
        }
        .priCell
        {
            width: 4%;
            text-align: center;
        }
        .btn {
          border-left: solid dimgray 1px;
          border-right: solid dimgray 1px;
          border-top: none;
          border-bottom: solid dimgray 1px;
          background-color: lightgrey;
          color: dimgray;
          font-size: 16px;
          cursor: pointer;
          font-weight: bold;
        }        
        span {
            width:100%;
/*          display: inline-block;
          vertical-align: middle;
          line-height: normal;      */
        }
        /*----------------------------------------------
        CSS settings for the users dialog
        ------------------------------------------------*/
        #addStickyNote, #copyBlankRequest{
            display:none;
            text-align: center;
        }
        .ui-dialog {
            z-index: 9999 !important;
        }        
        .ui-widget-overlay {
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
        }    
        .ui-button {
                width: 5em;
        }
        .ui-button-text-only .ui-button-text {
                padding: 2px 5px 5px 2px;
        }
        /*----------------------------------------------
        CSS settings for expedite background
        ------------------------------------------------*/
        .expediteTaskBackground {
            background:repeating-linear-gradient(
                90deg,
                rgba(255, 0, 127, 1),
                rgba(255, 0, 127, 1) 10px,
                rgba(77, 77, 77, .5) 10px,
                rgba(77, 77, 77, .5) 20px
            );
        }

    </style>
    <script>
        var sessionID = "<?php echo session_id(); ?>";
        var blankIDStickyNote = -1;
        var copyBlankID = -1;
        
        function ActivateTab(tabNumber) {
            // Activate the appropriate tab
            document.getElementById('theTab' . tabNumber).click();
        }
        
        function CreateURLSearchVariables() {
            // Get all of the variables
            var showWhatFinishedState = document.getElementById('showWhatFinishedState').value===null?'sfs=0':'sfs=' + encodeURIComponent(document.getElementById('showWhatFinishedState').value);
            var blanksPerPage = document.getElementById('blanksPerPage').value===null?'&bpp=30':'&bpp=' + encodeURIComponent(document.getElementById('blanksPerPage').value);
            var companySelected = document.getElementById('companySelected').value===null?'&cs=-1':'&cs=' + encodeURIComponent(document.getElementById('companySelected').value);
            var statusSelected = document.getElementById('statusSelected').value===null?'&s=-1':'&s=' + encodeURIComponent(document.getElementById('statusSelected').value);
            var searchPhrase = document.getElementById('searchPhrase').value===''?'':'&sp=' + encodeURIComponent(document.getElementById('searchPhrase').value);
            var sortNumber = document.getElementById('sortNumber').value===null?'&sn=1':'&sn='+encodeURIComponent(document.getElementById('sortNumber').value);
            var showReadiedBlanks = document.getElementById('showReadiedBlanks').checked?'&srb=1':'&srb=0';
            
            // Return what we can build up
            return '\?' + showWhatFinishedState + blanksPerPage + companySelected + statusSelected + searchPhrase + sortNumber + showReadiedBlanks;
        }
        
        function CreateAndGotoSearchOptions() {
            // Get the base location
            var goToLocation = window.location.href.split('?')[0];

            // Build the location to go to with get variables
            goToLocation = goToLocation + CreateURLSearchVariables();
            
            // Go to the location built
            window.location=goToLocation;
        }
        
        function ReadyBlankAndReturn(blankID) {
            // Build the location and go to it
            window.location='redirectors/ready_blank.php' +  CreateURLSearchVariables() + 
                            '&readyBlankID=' + blankID ;
        }
        
        function addStickyNoteDialog(blankID){
            // Set the global variable to this task ID passed in
            blankIDStickyNote=blankID;
            
            // Open the appropriate dialog
            $( "#addStickyNote" ).dialog( "open" );
        }
        
        function copyBlankRequestDialog(blankID){
            // Set the global variable to this blank ID passed in
            copyBlankID=blankID;
            
            // Open the appropriate dialog
            $( "#copyBlankRequest" ).dialog( "open" );
        }
        
        $( function() {
            $( "#addStickyNote" ).dialog({
                autoOpen: false,
                show: {
                  effect: "pop",
                  duration: 1000
                },
                hide: {
                  effect: "explode",
                  duration: 1000
                },
                width: 'auto',
                modal: true,
                buttons: {
                  "Save": function() {
                        // Attempt to move it to the queue
                        if (addStickyNote(sessionID, blankIDStickyNote, "BLANK", getMultipleSelectCSV(document.getElementById("stickyNoteTo")), document.getElementById("stickyNoteTitle").value, document.getElementById("stickyNoteContent").value)){
                            // Close this dialog
                            $( this ).dialog( "close" );
                        
                            // Reset the user it is going to to be everyone
                            $( "#stickyNoteTo" ).val(-1).change();

                            // Clear the sticky note title box
                            $( "#stickyNoteTitle" ).val("");

                            // Clear the sticky note text box
                            $( "#stickyNoteContent" ).val("");
                            
                            // Reload the window 
                            location.reload()();
                        };
                  },
                  Cancel: function() {
                       // Close this dialog
                       $( this ).dialog( "close" );
                       
                       // Reset the user it is going to to be everyone
                       $( "#stickyNoteTo" ).val(-1).change();
                    
                       // Clear the sticky note title box
                       $( "#stickyNoteTitle" ).val("");
                       
                       // Clear the sticky note text box
                       $( "#stickyNoteContent" ).val("");

                        // Reset the image element
                        $( "#stickyNoteImage" ).val("");
                  }
                }
            });
        });   
        
        $( function() {
            $( "#copyBlankRequest" ).dialog({
                autoOpen: false,
                show: {
                  effect: "pop",
                  duration: 1000
                },
                hide: {
                  effect: "explode",
                  duration: 1000
                },
                width: 'auto',
                modal: true,
                buttons: {
                  "Copy": function() {
                        var blankQuantity = document.getElementById("blankQuantity").value;
                        var addedBlankReason = document.getElementById("addedBlankReason").value;
                        var blankRushed = document.getElementById("blankRushed");

                        // Determine if the quantity is a valid number
                        if (isNaN(blankQuantity) || blankQuantity<1){
                            // let them know with an alert
                            alert("The quantity is not an appropriate value.  Try again");
                            
                            // Reset the quantity
                            $( "#blankQuantity" ).val("").change();
                        }
                        else if(addedBlankReason==null || addedBlankReason==""){
                            // let them know with an alert
                            alert("A reason must be entered for creating duplicates of this blank.  Try again");
                        }
                        else {
                            // Attempt to move it to the queue
                            if (copyBlank(sessionID, copyBlankID, blankQuantity, addedBlankReason, blankRushed.checked)){
                                // Close this dialog
                                $( this ).dialog( "close" );

                                // Reset the quantity
                                $( "#blankQuantity" ).val("").change();

                                // Clear the reason
                                $( "#addedBlankReason" ).val("");

                                // Uncheck the rushed field
                                $('#blankRushed').prop('checked', false);

                                // Reload the window 
                                location.reload();
                            };
                        }
                  },
                  Cancel: function() {
                       // Close this dialog
                       $( this ).dialog( "close" );
                       
                       // Reset the user it is going to to be everyone
                       $( "#stickyNoteTo" ).val(-1).change();
                    
                       // Clear the sticky note title box
                       $( "#stickyNoteTitle" ).val("");
                       
                       // Clear the sticky note text box
                       $( "#stickyNoteContent" ).val("");

                        // Reset the image element
                        $( "#stickyNoteImage" ).val("");
                  }
                }
            });
        });    
    </script>
</head>

<body class="thrColElsHdr">

<div id="container">
  
<!-- start of banner wrap --->

<div id="topContent">
  
<div>
  </div>
</div>
<!-- end of banner wrap --->

  <!-- end #header -->
  <br class="clearfloat" />
  <div id="sidebar1">
      <?php 
        // Determine if we are GTG
        if ($gtg){
            // Render the appropriate presentation for the tasks sidebar
            getSidebar($userRow, 'equipment');
        }
      ?>       
      <br class="clearfloat" />
  <!-- end #sidebar1 -->
  </div>
  
  <!-- start #mainContent -->
  <div id="mainContent">
    <div class="maintitle">Task Management System - Blanks</div>
    <div class="breadcrumb"> <a href="./index.php"> Home </a> > <a href="tasks.php"> Task Management System </a> > <a href="blanks.php"> Blanks </a></div>

    <br>
        
    <?php
        // Add a session variable that is this page URL
        $_SESSION['pageToGoTo']=$_SERVER['REQUEST_URI'];

        // Determine if they are GTG
        if ($gtg){
    ?>
            
        <div id="searchOptions" style='background-color: lightgrey; border-radius: 20px; border-color: darkgray;'>
            <?php
                // Get all of the search abilities
                echo(getDelineations($db, $userRow, $showWhatFinishedState, $blanksPerPage, $selectedCompany, $taskSearchPhrase, $selectedStatus, $showReadiedBlanks));
            ?>
            <br>
        </div>
            <?php
                // In order to determine the level of this person's account, I need to know their type
                switch ($userRow["UserLevel"]){
                    case 'MID-MANAGEMENT':
                    case 'MANAGEMENT':
                    case 'EXECUTIVE':
                    case 'SU':
                        ?>
                            <ul id="blanks_sortable">
                        <?php

                        // Break out of this case statement
                        break;
                    default: 
                        ?>
                            <ul>
                        <?php
                }

                // get the tasks for the machining list
                echo retrieveBlanksAsList($db, $userRow, $showWhatFinishedState, $blanksPerPage, $currentPage, $selectedCompany, $taskSearchPhrase, $sortNumber, $selectedStatus, $showReadiedBlanks);
            ?>
        </ul>
        <script>
            var button = document.getElementById("submitButton"); 
            var textBox = document.getElementById("searchPhrase"); 

            textBox.addEventListener("keyup", function (event) { 
                // Checking if key pressed is ENTER or not 
                // if the key pressed is ENTER 
                // click listener on button is called 
                if (event.keyCode == 13) { 
                    button.click(); 
                } 
            });         
        </script>
    <?php
        }
        else {
            // Set the login failed to true
            $_SESSION['failedLogin']='TRUE';
            
            // redirect to the login page
            echo ('<script>window.location = ".";</script>');
        }
    ?>       
    
    <div id="addStickyNote" title="Create Sticky Note">
        <p style="text-align:center;">
            Add a sticky note for this blank.
            <br><br>
                    
            <?php
                // Get a hidden div containing the assign to users
                echo createStickyNoteContent($db, $userRow);
            ?>
        </p>
    </div>            
    <div id="copyBlankRequest" title="Copy Blank Request">
        <p style="text-align:center;">
            Add duplicate blanks.
            <br><br>
                    
            <?php
                // Get a hidden div containing the assign to users
                echo createBlankCopyContent($userRow);
            ?>
        </p>
    </div>            
  <!-- end #mainContent -->
  
  <!-- This clearing element should immediately follow the #mainContent div in order to force the #container div to contain all child floats -->
  <br class="clearfloat" />
  
  <!-- start #footer --></div>
   <div id="footer">
    <p>© 2018 Evolve Engineering and Software. EES, Evolve Engineering and Software, EES Logo are registered marks of Evolve Engineering and Software.</p>
  <!-- end #footer -->
   </div>
<!-- end #container -->
</div>

</body>

</html>