<?php
use Orchard\Feature\Feature;
use Orchard\Service\SegmentAnalyticsTracking;

$workstationDeliveryReportRedesignEnabled = Feature::isEnabled('workstation_delivery_report_redesign', 'enabled', $this->featureVendorContext) && !$this->isFilm;
$segmentKeyConfigured = SegmentAnalyticsTracking::getKey('workstation');
?>
<?php if ($workstationDeliveryReportRedesignEnabled) {?>
<script type="text/javascript">
    $(document).ready(function() {
        $('.dr-copylink').bind('click', function(e) {
            var id = $(this).attr('data-store-id');
            var inp = $('#link_' + id);
            if (inp && inp.select) {
                inp.select();
                document.execCommand('copy');
                inp.blur();
            }
        });
        $('.info-icon').popover({
            html: true,
            content: $('#itunes-help').html(),
            placement: 'top'
        });
        $('body').on('click', closePopover);
        function closePopover(e) {
            var target = $(e.target);
            if (target.data('toggle') !== 'popover'
                && target.parents('[data-toggle="popover"]').length === 0
                && target.parents('.popover.in').length === 0) {
                $('[data-toggle="popover"]').popover('hide');
            }
        }
    });
</script>
<?php }?>
<div id="content">
    <!-- Breadcrumb -->
    <div id="bread">
        <a href="/index/viewexareleases/" title="Releases"><?=$this->translate('Releases')?></a>
    </div>

<h1><?=$this->translate('Delivery Report')?></h1>

<div class="panel panel-secondary">
    <div class="panel-heading">
    <?php
    if ($this->isFilm) { ?>
        <header class="gowatchit">
            <h3 class="panel-title"><?=$this->release['release_name']?></h3>
            <div class="clear"></div>
        </header>
    <?php
    } else {
        if (!$workstationDeliveryReportRedesignEnabled) { ?>
            <h3 class="panel-title"><?=$this->release['release_name']?></h3>
            <?php } else {?>
            <header class="dr-product-info">
                <div class="section">
                    <h3><?=$this->artist_name?> - <?=$this->release['release_name']?></h3>
                    <h4>Sales Start Date: <?=$this->release['sale_start_date']?></h4>
                </div>
                <div class="clear"></div>
            </header>
        <?php
        }
    } ?>
    </div>
    <div class="panel-body">
    <?php
    if ($workstationDeliveryReportRedesignEnabled && !empty($this->links)) {?>
        <div class="dr-table-title">
            <h2>Product Links &amp; Delivery Date</h2>
            <p>
                URLs are generated as soon as provided by services.
                They might not be available until the Release Date.
            </p>
        </div>
        <table class="table table-sorter dr-table" id="labelTable" width="100%">
            <thead class="table_header">
                <tr>
                    <th width="25%">Store</th>
                    <th width="25%">Delivered On</th>
                    <th width="45%">Product Link</th>
                    <th width="5%"></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($this->links as $link) {?>
                <tr>
                    <td class="customer">
                        <?=$link['store']?>
                        <?php if ($link['id'] == 1 && !empty($link['link'])) {?>
                        <img src="/images/redesign/info-tooltip.svg" class="info-icon" data-toggle="popover">
                        <?php }?>
                    </td>
                    <td><?=$link['delivered_date']?></td>
                    <?php if (!empty($link['link'])) {?>
                    <td>
                        <input type="text" readonly="readonly" class="form-control" id="link_<?=$link['id']?>" value="<?=$link['link']?>" />
                    </td>
                    <td>
                        <?php if ($segmentKeyConfigured) { ?>
                            <button class="btn btn-default dr-copylink segment-analytics" data-event="click" data-event-category="Delivery Report" data-event-action="Copy Link" data-event-label="Product Link" data-store-id="<?=$link['id']?>">Copy Link</button>
                        <? } else { ?>
                            <button class="btn btn-default dr-copylink" data-store-id="<?=$link['id']?>">Copy Link</button>
                        <?php } ?>
                    </td>
                    <?php } else {?>
                    <td colspan="2">
                        <span class="dr-status"><?=$this->translate('Pending')?></span>
                    </td>
                    <?php }?>
                </tr>
                <?php }?>
            </tbody>
        </table>
    <?php
    }
        if ($workstationDeliveryReportRedesignEnabled) {?>
            <div class="dr-table-title">
                <h2>Delivery Date</h2>
                <p>
                    Store Status and Product Link are not available in Workstation for these stores
                </p>
            </div>
        <?php } ?>
        <table class="table table-sorter dr-table" id="labelTable" width="100%">
        <thead class="table_header">
            <tr>
                <?php if ($workstationDeliveryReportRedesignEnabled) {?>
                    <th width="25%">Store</th>
                    <th width="55%">Delivered On</th>
                <?php } else {?>
                    <th width="40%">Store</th>
                    <th width="20%">Delivered On</th>
                <?php } ?>
                <?php if ($this->isFilm) { ?>
                <th width="5%"></th>
                <?php } ?>
            </tr>
        </thead>
     <tbody>
            <?php
            $message = 'Your releases are being delivered to stores. Check back soon as this could take several days.';
            if (!$this->data) { //No delivery history to show ?>
                <tr id="noReleasesBeingDelivered">
                    <td colspan="3" align="center">
                        <p><strong><?=$message?></strong></p>
                    </td>
                </tr>
            <?php
            } else {//Show delivery history report
                foreach ($this->data as $eachreport) {
                    if (in_array($eachreport->customerMasterMaster->customerMasterMasterId, $this->linkedIds)) {
                        continue;
                    }
            ?>
                <tr id="cmm_id_<?=$eachreport->customerMasterMaster->customerMasterMasterId?>" class="releasesDelivery">
                    <td class="font-weight"><?=$eachreport->customerMasterMaster->customerName?></td>
                    <td><?=date('Y/m/d', strtotime($eachreport->dateDelivered))?></td>
            <?php if ($this->isFilm) { ?>
                <td class="text-right">
                    <?php if ($eachreport->watchNowUrl) { ?>
                        <img src="/images/icons/complete.png" alt="Live" height="20">
                    <?php } ?>
                </td>
            <?php } ?>
                    </tr>
            <?php
                }
            } /* DC-549 removed dms_availability references */ ?>
            </tbody>
        </table>
    </div>
</div>
<?php if ($this->isFilm) { ?>
    <p>* Store availability data not available for all stores</p>
<?php } ?>
</div>
<?php if ($workstationDeliveryReportRedesignEnabled) {?>
<div id="itunes-help" class="hidden dr-help">
    This link will take users to the Apple Music store.<br/>For iTunes links please append the URL by adding the following code to the end: <span>?app=itunes</span>
</div>
<?php } ?>
