<?php
/**
 * Copyright 2012 Zendesk.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
?>
<?php

if(!Mage::getStoreConfig('zendesk/backend_features/show_on_order')) {
    return;
}

$_order = $this->getOrder();
$tickets = null;

try {
    $tickets = Mage::getModel('zendesk/api_tickets')->forOrder($_order->getIncrementId());
} catch(Exception $e) {
    // Don't do anything, just don't show the tickets
}

?>
<?php if($tickets): ?>
    <div class="box-left">
        <div class="entry-edit">
            <div class="entry-edit-head">
                <h4 class="icon-head head-account"><?php echo Mage::helper('zendesk')->__('Support Tickets') ?></h4>
                <div class="tools"><?php echo $this->getAccountEditLink() ?></div>
            </div>
            <div class="grid np">
                <div class="hor-scroll">
                    <table cellspacing="0" class="data order-tables">
                        <col width="1" />
                        <col width="1" />
                        <col width="1" />
                        <thead>
                            <tr class="headings">
                                <th><?php echo $this->helper('zendesk')->__('Subject') ?></th>
                                <th><?php echo $this->helper('zendesk')->__('Updated') ?></th>
                                <th><?php echo $this->helper('zendesk')->__('Status') ?></th>
                            </tr>
                        </thead>
                        <tbody class="odd">
                            <?php foreach($tickets as $ticket): ?>
                            <tr class="border">
                                <td><a href="<?php echo Mage::helper('zendesk')->getUrl('ticket', $ticket['id']); ?>" target="_blank"><?php echo $ticket['subject']; ?></a></td>
                                <td><?php echo Mage::helper('core')->formatDate($ticket['updated_at'], Mage_Core_Model_Locale::FORMAT_TYPE_MEDIUM, true); ?></td>
                                <td><?php echo $ticket['status']; ?></td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
    <div class="clear"></div>
<?php endif; ?>