<?php
    function retrieveMachinesAsList($db, $userRow, $sortNumber){
        $editPrivilegesTableHTML = '';
        $editPrivilegesRowHTML = '';

        // Determine if there is a search term
        $sort = 'Name';
        if ($sortNumber!=""){
            // Make it the sort passed in
            $sort = $sortNumber;
        }

        // determine if this use has update privileges
        if ($userRow["UserLevel"] == "SU" ||
            $userRow["UserLevel"] == "EXECUTIVE" ||
            $userRow["UserLevel"] == "MANAGEMENT"){
            // They have edit privileges
            $editPrivilegesTableHTML =
                    '<div class="cell updateCell">
                        <p><b>Edit</b></p>
                    </div>';
        }

        // Initialize the return
        $result = 
            '<li class="ui-state-default ui-state-disabled Table">
                <div class="Heading">
                    <div class="cell machinesNameCell" onclick="location.href=\'machines.php?sn=Name\';" style="cursor: pointer;">
                        <p><b>Machines</b></p>
                    </div>
                    <div class="cell machinesTypeCell" onclick="location.href=\'machines.php?sn=Type DESC\';" style="cursor: pointer;">
                        <p><b>Type</b></p>
                    </div> 
                    <div class="cell machinesToolListCell">
                        <p><b>Tool List</b></p>
                    </div>'
                    .
                    $editPrivilegesTableHTML 
                    .
                '</div>
            </li>';

        try {
            // Setup the query to get machines ordered how we want
            $sql='SELECT m.*, tmlm.AmountOnMachines 
                  FROM machines m 
                  LEFT OUTER JOIN 
                       (SELECT COUNT(*) AS AmountOnMachines, MachineID 
                        FROM tasktomachinelinker GROUP BY MachineID) tmlm ON m.MachineID = tmlm.MachineID
                  LEFT JOIN (SELECT * FROM usertomachineviewinglinker WHERE UserID=' . $userRow["UserID"] . ') umvl ON m.MachineID = umvl.MachineID 
                  WHERE umvl.ViewCategory IS NULL 
                  ORDER BY m.' . $sort . ';';

            // Query the database for the companies 
            $rows = $db->queryDB($sql, $userRow, "machines_presentation Form occurance 1");

            // Loop through the returned results
            while ($row=mysqli_fetch_array($rows)){
                // Determine the type to determine the color
                switch($row["Type"]){
                    case 'BANDSAW' :
                        $color='#FFA07A';
                        break;
                    case 'CMM' :
                        $color='#AF7AC5';
                        break;
                    case 'DRILL' :
                        $color='#F9E79F';
                        break;
                    case 'EDM' :
                        $color='#48C9B0';
                        break;
                    case 'LASER TABLE' :
                        $color='#F1948A';
                        break;
                    case 'LATHE' :
                        $color='#F5B041';
                        break;
                    case 'MILL' :
                        $color='#28B463';
                        break;
                    case 'SURFACE GRINDER' :
                        $color='#ABB2B9';
                        break;
                    case 'WATERJET' :
                        $color='#3498DB';
                        break;
                    default:
                        $color='#797D7F';
                        break;
                }

                // Assign the delete button attributes as though deletion is an option
                $deleteButtonTextColor = 'LIMEGREEN';
                $deleteButtonTitle = 'Remove this machine.';
                $deleteButtonOnClick = 'onclick="javascript:if (confirm(\'Are you sure you want to delete this machine?\')) ' .
                                       '{window.location=\'redirectors/delete_machine.php?deleteMachineID=' . $row["MachineID"] . '\';}"';

                // Determine if there are any on machines
                if ($row["AmountOnMachines"]!=null){
                    // Assign the delete buttons attributes such that deletion will not happen
                    $deleteButtonTextColor = 'RED';
                    $deleteButtonTitle = 'Empty machine first.';
                    $deleteButtonOnClick = 'onclick="javascript:alert(\'This machine has ' . $row["AmountOnMachines"] . 
                                           ' job(s) assigned to it. \n\nRemove those jobs from this machine before ' . 
                                           'attempting to delete again.\');"';
                }


                
                // determine if this use has update privileges
                if ($userRow["UserLevel"] == "SU" ||
                    $userRow["UserLevel"] == "EXECUTIVE" ||
                    $userRow["UserLevel"] == "MANAGEMENT"){
                    // They have edit privileges
                    $editPrivilegesRowHTML = 
                            '        <div class="cell updateCell">
                                        <span>
                                            <table>
                                                <tr>
                                                    <td>
                                                        <input type="button" style="font-size:11px; color:' . $deleteButtonTextColor . ';" value="X" title="' . 
                                                        $deleteButtonTitle . '" ' . $deleteButtonOnClick . '/>
                                                    </td>
                                                    <td>
                                                        <input type="button" style="font-size:11px;" value="..." title="Edit this machine." ' . 
                                                        'onclick="javascript:window.location=\'updateMachine.php?updateMachineID=' . $row["MachineID"] . '\';"/>
                                                    </td>
                                                </tr>
                                            </table>
                                        </span>
                                    </div>';
                }

                // Add this option to the select
                $order=1;
                $result = $result .
                    '<li id="Company_' . $order . '" class="ui-state-default Table">
                        <div class="Row" style="background-color: '. $color . ';">
                            <div class="cell machinesNameCell">
                                <span>
                                    <p>' . $row["Name"] . '</p>
                                </span>
                            </div>
                            <div class="cell machinesTypeCell">
                                <span>
                                    <p>' . $row["Type"] . '</p>
                                </span>
                            </div> 
                            <div class="cell machinesToolListCell">
                                <span>
                                    <table>
                                        <tr>
                                            <td>
                                               <button type="button" name="toolListButton" id="toolListButton" title="View Tool List." onclick="childWindow=window.open(\'machineToolList.php?toolsMachineID=' . $row["MachineID"] . '\', \'_blank\');">View</button>
                                            </td>
                                        </tr>                                
                                    </table>
                                </span>
                            </div>'
                            .
                            $editPrivilegesRowHTML 
                            .
                        '</div>
                    </li>';
            }

        } catch (Exception $ex) {
            // Assign the output string as to what happened
            return $result;
        }

        // Return whatever we got
        return $result;
    }
        
?>

