<?php
    function retrieveCompaniesAsList($db, $userRow){
        // Initialize the return
        $result = 
            '<li class="ui-state-default ui-state-disabled Table">
                <div class="Heading">
                    <div class="cell companyNameCell">
                        <p><b>Company</b></p>
                    </div>
                    <div class="cell updateCell">
                        <p><b>Edit</b></p>
                    </div>
                </div>
            </li>';

        try {
            // Setup the query to get the companies and amount of tasks associated
            $sql='SELECT c.*, ct.AmountOnTasks 
                  FROM companies c 
                  LEFT OUTER JOIN 
                       (SELECT COUNT(*) AS AmountOnTasks, CompanyID 
                        FROM tasks GROUP BY CompanyID) ct ON c.CompanyID = ct.CompanyID
                  ORDER BY c.CompanyName;';
            
            // Query the database for the companies 
            $rows = $db->queryDB($sql, $userRow, "companies_presentation Form occurance 1");

            // Loop through the returned results
            while ($row=mysqli_fetch_array($rows)){
                // Assign the delete button attributes as though deletion is an option
                $deleteButtonTextColor = 'LIMEGREEN';
                $deleteButtonTitle = 'Remove this company.';
                $deleteButtonOnClick = 'onclick="javascript:if (confirm(\'Are you sure you want to delete this company?\')) ' .
                                       '{window.location=\'redirectors/delete_company.php?deleteCompanyID=' . $row["CompanyID"] . '\';}"';

                // Determine if this company is assigned to any tasks
                if ($row["AmountOnTasks"]!=null){
                    // Assign the delete buttons attributes such that deletion will not happen
                    $deleteButtonTextColor = 'RED';
                    $deleteButtonTitle = 'Check what tasks this company may be assigned to.';
                    $deleteButtonOnClick = 'onclick="javascript:alert(\'This company is assigned to '. $row["AmountOnTasks"] .
                                           ' task(s).\n\nRemove tasks or reassign task companies before ' . 
                                           'attempting to delete again.\');"';
                }
                
                // Determine what color this row should be
                $color='#' . $row["CompanyRowColor"];

                // 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 companyNameCell">
                                <span>
                                    <p>' . $row["CompanyName"] . '</p>
                                </span>
                            </div>
                            <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 company." ' . 
                                                'onclick="javascript:window.location=\'updateCompany.php?updateCompanyID=' . $row["CompanyID"] . '\';"/>
                                            </td>
                                        </tr>
                                    </table>
                                </span>
                            </div>
                        </div>
                    </li>';
            }

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

        // Return whatever we got
        return $result;
    }

?>

