<?php
require_once APPLICATION_PATH . '/modules/alw/views/helpers/Loadjs.php';

use Orchard\Feature\Feature;

$projectManagerFeatureEnabled = Feature::isEnabled('project_manager', 'enabled', $this->featureVendorContext);
$conflictManagerFeatureFlagEnabled = Feature::isEnabled('alw_conflict_manager', 'enabled', $this->featureVendorContext);
$catalogToConflictManagerUrlUpdateFeatureEnabled = Feature::isEnabled('catalog_to_conflict_manager_url_update', 'enabled', $this->featureVendorContext);
$projectManagerUIEnabled = $this->userObj->isAllowed('', '', 'uiproject');
$projectManagerEnabled = $projectManagerFeatureEnabled && $projectManagerUIEnabled;
$acInputWidth = 'acInputWidth';
$loadJs = new Zend_View_Helper_loadjs();
$alwScriptPath = (APPLICATION_ENV != 'development') ? $loadJs->getCdnAlwScriptPath('jquery.autocomplete.js') : '/alw/scripts/jquery.autocomplete.js';

?>
<script src="<?php echo $alwScriptPath ?>"></script>
<!-- Start Left Side Content -->
<div id="content" onload="loadDashboard()">
    <div class="artist-button pull-right">
        <?php
        if ($this->userFilmBtn) { ?>
            <div class="topButtons">
                <button class="btn btn-default" onclick="modalWindow1({'product_type' : 'Movie'})">
                    <?php
                    if ($this->userObj->isWorkstationExperienceFilm()) {
                        echo $this->translate('+ New Collection');
                    } else {
                        echo $this->translate('+ New Volume');
                    }
                    ?>
                </button>
            </div>
        <?php
        }
        if ($this->userArtistBtn) { ?>
            <div class="topButtons">
                <button class="btn btn-primary" onclick="modalWindow1({'product_type' : 'Artist'})">
                    <?=$this->translate('+ New Artist')?>
                </button>
            </div>
        <?php
        } ?>
    </div>
    <?php
        $items = array();
        $privilege = new Web_Validate_Checkroleresourceprivilege();
        if ($privilege->isValid(array('resource' => 'index', 'privilege' => 'viewexareleases'))) {
            if ($projectManagerEnabled) {
                array_push($items, array($this->translate('Projects'), '/projects'));
                array_push($items, array($this->translate('Products'), '/index/viewexareleases?filterType=delivered,submitted,in_progress,error_correction,rejected&productType=1,2,3,5&release_date=desc'));
            } else {
                array_push($items, array($this->translate('Releases'), '/index/viewexareleases?filterType=delivered,submitted,in_progress,error_correction,rejected&productType=1,2,3,5&release_date=desc'));
            }
        }
        if ($this->userObj->isAllowed('', '', 'navartistbuilder') && $privilege->isValid(array('resource' => 'artistinfo', 'privilege' => 'index'))) {
            array_push($items, array($this->translate('Artists'), '/artist/index', true));
        }
        if ($projectManagerEnabled && $conflictManagerFeatureFlagEnabled) {
            $conflictsUrl = '/conflicts';
            if ($catalogToConflictManagerUrlUpdateFeatureEnabled) {
                $conflictsUrl .= '/new';
            }
            array_push($items, array($this->translate('Conflicts'), $conflictsUrl));
        }
        echo $this->partial('partials/sub-nav.phtml', array('items' => $items));
    ?>
    <div class="topSearchContainer row">
        <div class="form-group col-md-6">
            <label><?=$this->translate('Search for Artist')?></label>
            <input id="search_artist_inputbox" class="form-control full-width" value="<?=$this->translate('search for an artist')?>" autocomplete="off"/>
        </div>
        <div class="clear"></div>
    </div>
    <table width="100%" border="0" cellspacing="0" cellpadding="0" class="table-sorter table vertical-center">
        <thead>
            <tr>
                <th width="35%"><?=$this->translate('Name')?></th>
                <th class="text-center" width="10%"><?=$this->translate('Type')?></th>
                <th class="text-center" width="15%"><?=$this->translate('# Of Releases')?></th>
                <th class="text-center" width="15%"><?=$this->translate('In Progress')?></th>
                <th class="text-center" width="25%"><?=$this->translate('% Profile complete')?></th>
            </tr>
        </thead>
        <tbody>
            <?php
            if ($this->total) {
                foreach ($this->artistList as $artist) {
                ?>
                <tr id="artistlist_<?=$artist['artist']->artist_id?>">
                    <td class="artistlist">
                        <a class="artistlistImage"><img src="<?=$artist['path']?>" width="<?=$artist['width']?>" height="<?=$artist['height']?>" class="artist-img pull-left" /></a>
                        <a class="artist-name pull-left"><?=($artist['artist']->name ? $artist['artist']->name : '-')?></a><br>
                        <span class="artist-smalltext pull-left">
                            <?=($artist['artist']->address_city ? $artist['artist']->address_city.',' : '')?> 
                            <?=(isset($artist['artist']->Country->name) ? $artist['artist']->Country->name : '')?>
                        </span>
                    </td>
                    <td class="artistlist text-center"><?=$artist['artistType']?></td>
                    <td class="artistlist text-center">
                        <div class="<?php echo ($artist['artist']->num_of_content_releases == 0 ? 'number-of-releases-zero' : 'number-releases'); ?>" 
                        <?=($artist['artist']->num_of_content_releases > 0 ? ' onmousedown="goToPage(\'/index/viewexareleases/filterType/delivered,submitted,in_progress,error_correction,rejected/artist_id/'.$artist['artist']->artist_id.'\')"' : '')?>>
                            <?=($artist['artist']->num_of_content_releases > 0 ? '<a class="releases-link ">'.$artist['artist']->num_of_content_releases.'</a>' : 0)?>
                        </div>
                    </td>
                    <td class="artistlist text-center">
                        <?=($artist['artist']->num_of_lc_releases > 0 ? '<p class="num-releases-in-progress" onmousedown="goToPage(\'/index/viewexareleases/filterType/submitted,in_progress,rejected/artist_id/'.$artist['artist']->artist_id.'\')"><a class="releases-link">'.$artist['artist']->num_of_lc_releases.'</a></p>' : '')?>
                    </td>
                    <td class="artistlist text-center">
                        <?php
                        if (isset($artist['profile_completed'])) {
                            echo $this->artistProfileProgressbar($artist['artist'], $this->featureVendorContext, $artist['profile_completed']);
                        } else {
                            echo $this->artistProfileProgressbar($artist['artist'], $this->featureVendorContext);
                        }
                        ?>
                    </td>
                </tr>
            <?php
                }
            } else {
                ?>
                <tr>
                    <td colspan="7"><br /><p class="textBold noNewReleases"><?=$this->translate('User has no artists.')?></p><br /></td>
                </tr>
                <?php
            }
        ?>
        </tbody>
    </table>
        <?php
        echo $this->paginationpanel('/artist/index', $this->currentPage, 3, $this->total, 'page', null, $this->featureVendorContext);
        ?>
    </div>
</div>
<!-- End Left Side Content -->
<script type="text/javascript">
    function deleteArtist(artist_id, querystring){
        document.location.href='/artist/deleteartist/artist_id/'+artist_id+'/'+querystring;
    }
    var found_result = 0;
    $(document).ready(function(){
    var acInputWidth = $("#search_artist_inputbox").innerWidth();
        $("#search_artist_inputbox").autocomplete("/artist/searchartistautocomplete"
            ,{
                width: <?php echo $acInputWidth; ?>,
                selectFirst:false,
                scroll:true,
                matchContains:true
                ,formatItem: function(row, i, max, term){
                    return str_replace(row[1],"|",row[0]);
                }
                ,formatResult: function(row) {
                    found_result = 1;
                    return str_replace(row[1],"|",row[0]);
                }
            }
        ).result(function(event, data, formatted) {
            if(data[1]){
                document.location.href = "/artistinfo/index/artist_id/"+data[1];
            }
        }).click(function(){
                this.value = "";
        }).blur(function(){
                this.value = "<?=$this->translate('search for an artist')?>";
                found_result = 0;
        }).keypress(function(event){
                if(event.which==13){
                    document.location.href = "/artist/index/artist_name/"+$(this).val();
                }
                if(event.which==8 || $(this).val()==""){
                    found_result = 0;
                }
        });

        $(".artistlist").click(function(){
            if(!$(this).hasClass("text-center")){
                var tr = $(this).parent();
                var artistlist_id = tr.attr("id").split("_");
                var artist_id = artistlist_id[1];
                document.location.href="/artistinfo/index/artist_id/"+artist_id;
            }
        });
    });
    function goToPage(url){
        document.location.href = url;
    }
</script>
<?php
$options = array(
                array(
                    'url' => '/artist/deleteartistmodal',
                    'closeOnEsc' => false,
                    'closeOnClick' => false,
                    'loadSpeed' => 100,
                    'top' => 10,
                    'bindEvent' => false
                ),
                array(
                    'url' => '/artist/addartistmodal',
                    'closeOnEsc' => false,
                    'closeOnClick' => false,
                    'loadSpeed' => 100,
                    'top' => 10,
                    'bindEvent' => false
                )
           );
echo $this->modalwindow($options, $this->featureVendorContext)?>
