
<?php $enqCount = count($enquiryResults);
      $custCount = count($customerResults);
      $contactCount = count($contactResults);
      $jobCount = count($jobResults);
?>


<?php if($enqCount > 0):
    ?>

<h2><?php echo $enqCount;?> Matching Enquiries</h2>
<table class="mer">
    <tr>
        <th>Date</th>
        <th>Principle</th>
        <th>Enquiry Number</th>
        <th>Customer</th>
        <th>Contact</th>
        <th class="actions">Actions</th>
    </tr>
        <?php foreach ($enquiryResults as $enquiryRes):?>


            <?php

            /* Set Row colour to Yellow if the Job has been won and turned into an order */
            if($enquiryRes['Status']['id'] == 3) {
                $class = ' class="jobwon"';
            }
            else if($enquiryRes['Status']['id'] == 4) {
                $class = ' class="joblost"';
            }
            else if($enquiryRes['Status']['id'] == 8) {
                $class = ' class="joblost"';
            }
            else if($enquiryRes['Status']['id'] == 9) {
                $class = ' class="joblost"';
            }
            else if($enquiryRes['Status']['id'] == 10) {
                $class = ' class="joblost"';
            }
            else if($enquiryRes['Status']['id'] == 6) {
                $class = ' class="information"';
            }
            else if($enquiryRes['Status']['id'] == 11) {
                $class = ' class="informationsent"';
            }
            else if($enquiryRes['Status']['id'] == 5) {
                $class = ' class="quoted"';
            }
            else if($enquiryRes['Status']['id'] == 1) {
                $class = ' class="requestforquote"';
            }


            ?>


    <tr <?php echo $class; ?>>
        <td><?php echo date('j M Y',$time->toUnix($enquiryRes['Enquiry']['created'])); ?></td>
        <td> <?php
                    if($enquiryRes['Principle']['short_name']) {
                        echo $html->link($enquiryRes['Principle']['short_name'], array('controller'=> 'principles', 'action'=>'view', $enquiryRes['Principle']['id']));
                    }
                    else {
                        echo $html->link($enquiryRes['Principle']['name'], array('controller'=> 'principles', 'action'=>'view', $enquiryRes['Principle']['id']));
                    }
                    ?>
        </td>
        <td><?php echo $html->link($enquiryRes['Enquiry']['title'], array('controller'=>'enquiries', 'action'=>'view', $enquiryRes['Enquiry']['id'])); ?></td>
        <td> <?php echo $html->link($enquiryRes['Customer']['name'], array('controller'=> 'customers', 'action'=>'view', $enquiryRes['Customer']['id'])); ?></td>
        <td> <?php echo $html->link($enquiryRes['Contact']['first_name'].' '.$enquiryRes['Contact']['last_name'], array('controller'=> 'users', 'action'=>'view', $enquiryRes['Contact']['id'])); ?></td>
        <td><?php echo $html->link('View', array('controller'=>'enquiries', 'action'=>'view', $enquiryRes['Enquiry']['id'])); ?></td>
    </tr>
        <?php endforeach; ?>

</table>
<?php endif;?>



<?php if($custCount > 0):
?>
<h2><?php echo $custCount; ?> Matching Customers</h2>
<table>
    <tr>
    <th>Name</th>
    <th class="actions"></th>
    </tr>
    

    <?php
    $i =0;
    foreach($customerResults as $custRes): ?>
    <?php if($i % 2 == 0) {
        echo "<tr>";
    }
    else {
        echo '<tr class="altrow">';
    }
    ?>
        <td><?php echo $custRes['Customer']['name'];?></td>
        <td><?php echo $html->link(__('View', true), array('controller'=>'customers', 'action'=>'view', $custRes['Customer']['id'])); ?></td>
    </tr>
    <?php
    $i++;
    endforeach;?>
</table>
<?php endif;?>






<?php if($contactCount > 0): ?>
<h2><?php echo $contactCount; ?> Matching Contacts</h2>
<table>
    <tr>
    <th>First Name</th>
    <th>Last Name</th>
    <th>Customer</th>
    <th class="actions">Actions</th>
    </tr>
    <?php
    $i = 0;
    foreach($contactResults as $contact): ?>
     <?php if($i % 2 == 0) {
        echo "<tr>";
    }
    else {
        echo '<tr class="altrow">';
    }
    ?>
        <td><?php echo $contact['Contact']['first_name'];?></td>
        <td><?php echo $contact['Contact']['last_name'];?></td>
        <td><?php echo $html->link($contact['Customer']['name'], array('controller'=> 'customers', 'action'=>'view', $contact['Contact']['customer_id'])); ?></td>
        <td><?php echo $html->link(__('View', true), array('controller'=>'users', 'action'=>'view', $contact['Contact']['id'])); ?></td>
    </tr>
    <?php
    $i++;
    endforeach;?>
</table>
<?php endif;?>


<?php if($jobCount > 0): ?>
<h2><?php echo $jobCount; ?> Matching Jobs</h2>
<table>
    <tr>
        <th>Date Order Received</th>
        <th>Job Number</th>
        <th>Enquiry</th>
        <th>Customer PO Number</th>
        <th class="actions">Actions</th>
    </tr>

    <?php
        $i = 0;
        foreach($jobResults as $job): ?>
        <? if($i % 2 == 0) {
            echo "<tr>";
        }
        else {
            echo '<tr class="altrow">';
        }
?>
    <td><?php echo $job['Job']['date_order_received']; ?></td>
    <td><?php echo $html->link($job['Job']['title'], '/jobs/view/'.$job['Job']['id']) ; ?></td>
    <td><?php echo $html->link($job['Enquiry']['title'], '/enquiries/view/'.$job['Enquiry']['id']); ?></td>
    <td><?php echo $job['Job']['customer_order_number']; ?></td>
    <td><?php echo $html->link('View', '/jobs/view/'.$job['Job']['id']); ?> </td>
</tr>

    <?
    $i++;
    endforeach; ?>

</table>
<?php endif; ?>





<?php if( ($contactCount == 0) && ($enqCount == 0) && ($custCount == 0) && ($jobCount == 0) ) {
    echo "<h2>No matching records found :( Try modifying your search terms</h2>";
}
?>
