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

        try {
            // Setup the query to be where they only get users in their vendor
            $sql='SELECT v.*, tv.AmountOnTools, msv.AmountOnStock 
                  FROM vendors v 
                  LEFT OUTER JOIN 
                       (SELECT COUNT(*) AS AmountOnTools, VendorID 
                        FROM tools GROUP BY VendorID) tv ON v.VendorID = tv.VendorID
                  LEFT OUTER JOIN 
                       (SELECT COUNT(*) AS AmountOnStock, VendorID 
                        FROM materialstock GROUP BY VendorID) msv ON v.VendorID = msv.VendorID
                  ORDER BY v.VendorName';

            // Query the database for the vendors 
            $rows = $db->queryDB($sql, $userRow, "vendors_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 vendor.';
                $deleteButtonOnClick = 'onclick="javascript:if (confirm(\'Are you sure you want to delete this vendor?\')) ' .
                                       '{window.location=\'redirectors/delete_vendor.php?deleteVendorID=' . $row["VendorID"] . '\';}"';

                // Determine if this company is assigned to any tasks
                if ($row["AmountOnTools"]!=null || $row["AmountOnStock"]!=null){
                    // Assign the delete buttons attributes such that deletion will not happen
                    $deleteButtonTextColor = 'RED';
                    $deleteButtonTitle = 'Check what stock or tools this vendor may be assigned to.';
                    
                    // Determine if it has tools using it
                    $toolInformational = '';
                    if ($row["AmountOnTools"]!=null){
                        // Add something about the tools
                        $toolInformational='This vendor is assigned to '. $row["AmountOnTools"] . ' tool(s)';
                    }
                    
                    // Determine if it has stock using it
                    $stockInformational='';
                    if ($row["AmountOnStock"]!=null){
                        // Add something about the tools
                        $stockInformational='This vendor is assigned to '. $row["AmountOnStock"] . ' stock item(s)';
                    }
                    
                    
                    $deleteButtonOnClick = 'onclick="javascript:alert(\'' . $toolInformational . '\n\n'.
                                           $stockInformational . 'Remove vendor from tools and/or stock before ' . 
                                           'attempting to delete again.\');"';
                }
                
                // Determine what color this row should be
                $color='#' . $row["VendorRowColor"];

                // Add this option to the select
                $order=1;
                $result = $result .
                    '<li id="Vendor_' . $order . '" class="ui-state-default Table">
                        <div class="Row" style="background-color: '. $color . ';">
                            <div class="cell nameCell">
                                <span>
                                    <p>' . $row["VendorName"] . '</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 vendor." ' . 
                                                'onclick="javascript:window.location=\'updateVendor.php?updateVendorID=' . $row["VendorID"] . '\';"/>
                                            </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;
    }

?>

