<?php
/**
 * this partial is to be used on the listing details page,
 * as well as on the invoice confirmation and shopping cart view pages
 * and basically anywhere where we need a postage calculator box.
 *
 * prerequisites:
 * - the listing must exist in the listings table,
 * meaning that the calculator wont work for the listing preview page
 *
 * required input:
 * - user model [ the seller ]
 * - listing id
 *
 * accepted input:
 * - locationId
 * - postCode
 * - quantity (for a single calculation)
 *
 * result:
 * - a table with all available postage options, or an error message output by the calculatePostage() method.
 */

$shippableLocations = $user->getShipping()->getShippableLocations(true);

if (count($shippableLocations) > 0) { ?>
<div id="postage-calculator">
    <?php echo $this->formElement('hidden', 'enableSelection', $this->enableSelection)
        ->render();
    ?>
    <?php echo $this->formElement('hidden', 'ids', $this->listingId)
        ->setAttributes(array('class' => 'ids'))
        ->render();
    ?>
    <dl class="dl-horizontal">
        <dt><?php echo $this->_('Select Country'); ?></dt>
        <dd><?php echo $this->formElement('select', 'locationId', $this->locationId)
                ->setAttributes(array('class' => 'form-control input-medium'))
                ->setMultiOptions($shippableLocations)
                ->render();
            ?>
        </dd>
        <dt><?php echo $this->_('Zip/Post Code'); ?></dt>
        <dd><?php echo $this->formElement('text', 'postCode', $this->postCode)
                ->setAttributes(array('class' => 'form-control input-small'))
                ->render();
            ?>
        </dd>
        <?php if (!empty($quantity) && !is_array($quantity)) { ?>
            <dt><?php echo $this->_('Quantity'); ?></dt>
            <dd><?php echo $this->formElement('text', 'quantity', $this->quantity)
                    ->setAttributes(array('class' => 'form-control input-mini qty'))
                    ->render();
                ?>
            </dd>
        <?php
        }
        else if (is_array($quantity)) {
            ?>
            <?php echo $this->formElement('hidden', 'quantity', $this->quantity)
                ->setAttributes(array('class' => 'qty'))
                ->render();
            ?>
        <?php } ?>
        <dd>
            <?php echo $this->formElement('button', 'button', $this->_('Get Shipping Rates'))
                ->setAttributes(array(
                    'class'             => 'btn btn-default',
                    'id'                => 'calculate-postage',
                    'data-loading-text' => $this->_('Please wait ...')))
                ->render();
            ?>
        </dd>
    </dl>

    <div class="result">

    </div>
</div>
<?php } ?>