<?php
$alignRight = '';
?>
<div class="tableDisplayStats">
    <table class="table">
        <thead>
            <tr>
                <?php foreach ($this->drillCols as $sqlAlias => $uiName) {
                    $classSuffix = in_array($sqlAlias, array('BILLTO','SHIPTO'))?'2':'';
                ?>
                <td class="breakDownBlankSpace<?php echo $classSuffix ?>">&nbsp;</td>
                <?php } ?>
                <td colspan="<?php echo count($this->plainCols) ?>" class="displayStatsTableHeader displayStatsCurrent<?php echo $classSuffix ?>">Current</td>
                <td class="blank<?php echo $classSuffix ?>">&nbsp;</td>
                <td colspan="6" class="displayStatsTableHeader displayStatsHistory<?php echo $classSuffix ?>">Order history since <?php
                    echo $this->intervals[$this->filters['period']] ?>
                </td>
            </tr>
            <tr>
                <?php foreach ($this->drillCols as $sqlAlias => $uiName) {
                        // SORTING
                        $this->newFilters = array_merge($this->filters, array(
                            'page' => 0,
                            'sort' => $sqlAlias,
                            'order' => $this->filters['sort'] == $sqlAlias && $this->filters['order'] == 'asc' ? 'desc' : 'asc'
                        ))
                ?>
                <th class="text-left">
                    <a href="<?php echo $this->render('inventory/_link.phtml') ?>">
                        <?php echo $uiName ?>
                    </a>
                </th>
                <?php } ?>
                <?php foreach ($this->plainCols as $uiName => $sqlAlias) {
                        // SORTING
                        $alias = is_array($sqlAlias) ? reset($sqlAlias) : $sqlAlias;
                        $this->newFilters = array_merge($this->filters, array(
                            'page' => 0,
                            'sort' => $alias,
                            'order' => ($this->filters['sort'] == $alias && $this->filters['order'] == 'asc') ? 'desc' : 'asc'
                        ))
                ?>
                <th class="<?=$alignRight?>">
                    <a href="<?php echo $this->render('inventory/_link.phtml') ?>">
                        <?php echo $uiName ?>
                    </a>
                </th>
                <?php } ?>
                <th class="blank">&nbsp;</th>
                <th class="<?=$alignRight?>">SHIP</th>
                <th class="<?=$alignRight?>">RTN</th>
                <th class="<?=$alignRight?>">NET</th>
                <th class="<?=$alignRight?>">RTN %</th>
            </tr>
        </thead>
        <tbody>
        <?php $idx = 0; foreach ($this->breakdown as $tableData) { ?>
            <tr>
                <?php foreach (array_keys($this->drillCols) as $sqlAlias) {
                    // drill in
                    $this->newFilters = array_merge($this->filters, array(
                        // 'drill' => $sqlAlias,
                        'page' =>  0,
                        'order' =>  'asc',
                        $sqlAlias  => $tableData[$sqlAlias],
                    ));
                    // grouping
                    $group = array(
                        'IMPRINT' => 'ARTIST',
                        'ARTIST' => 'RELEASE_NAME',
                        'RELEASE_NAME' => 'SKU',
                        'SKU' => 'BILLTO',
                        'BILLTO' => 'SHIPTO',
                        'SHIPTO' => 'SHIPTO',
                    );

                    if (isset($this->filters['drill2'])) {
                        $group['SKU'] = $this->filters['drill2'] == 'SHIPTO' ? 'SKU' : 'SHIPTO';
                    }

                    $group['SHIPTO'] = isset($this->filters['drill1']) ?
                            $group[ $this->filters['drill1'] ] :
                            'IMPRINT';

                    $this->newFilters['group'] = $group[$sqlAlias];
                    $this->newFilters['sort'] = $group[$sqlAlias];
                    // for breadcrumbs
                    $plainText = '';
                    switch ($sqlAlias) {
                        case 'SHIPTO':
                            $this->newFilters['PRSTADCD'] = $tableData['PRSTADCD'];
                            $separator = Model_B2bPhysicalView::SHIPTONAME_SEPARATOR;
                            $textParts = explode($separator, $tableData['SHIPTO']);
                            $linkText = array_shift($textParts);
                            $plainText = ' - '.implode($separator, $textParts);
                            // no break
                        case 'BILLTO':
                            $this->newFilters['CUSTNMBR'] = $tableData['CUSTNMBR'];
                            $this->newFilters['BILLTO'] = $tableData['BILLTO'];
                            $this->newFilters['drill2'] = $sqlAlias;
                            if ($sqlAlias == 'BILLTO') {
                                $linkText = $tableData['BILLTO'];
                            }
                            break;

                        case 'SKU':
                            $this->newFilters['RELEASE_NAME'] = $tableData['RELEASE_NAME'];
                            // no break
                        case 'RELEASE_NAME':
                            $this->newFilters['ARTIST'] = $tableData['ARTIST'];
                            // no break
                        case 'ARTIST':
                            $this->newFilters['IMPRINT'] = $tableData['IMPRINT'];
                            // no break
                        case 'IMPRINT':
                            // drilling into blue tabs
                            $this->newFilters['drill1'] = $sqlAlias;
                            $linkText = !empty($tableData[$sqlAlias]) ? $tableData[$sqlAlias] : ' (N/A) ';
                            break;
                    }
            ?>
            <td class="stats text-left">
                <a href="<?php echo $this->render('inventory/_link.phtml') ?>"><?php echo $linkText; ?></a>
                <span><?php echo $plainText; ?></span>
            </td>
            <?php } ?>

            <?php foreach ($this->plainCols as $sqlAlias) { ?>
            <td class="stats <?=$alignRight?>">
            <?php
            if (is_array($sqlAlias)) { ?>
                <?php foreach ($sqlAlias as $class => $alias) { ?>
                    <div class="<?php echo $class ?>"><?php echo $tableData[$alias] ?></div>
                <?php }
            } else {
                echo $tableData[$sqlAlias];
            }
            ?>
            </td>
            <?php } ?>

            <td class="blank">&nbsp;</td>

            <?php
            $historyColumns = array(
                array(
                    'unit' =>'SHIP_QTY',
                    'price'=>'SHIP_PRCE'),
                array(
                    'unit' =>'RTN_QTY',
                    'price'=>'RTN_PRCE'),
                array(
                    'unit' =>'NET',
                    'price'=>'NET_PRCE'),
                'RTN_PER');
            foreach ($historyColumns as $sqlAlias) {
            ?>
            <td class="stats <?=$alignRight?>">
            <?php
            if (is_array($sqlAlias)) { ?>
                <?php foreach ($sqlAlias as $class => $alias) { ?>
                    <div class="<?php echo $class ?>"><?php echo $tableData[$alias] ?></div>
                <?php }
            } else {
                echo $tableData[$sqlAlias];
            }
            ?>
            </td>
            <?php
            }
            ?>
            </tr>
        <?php } ?>
        </tbody>
    </table>
</div>
