<?php use PHPCensor\Helper\Lang; ?>
<script>
    var PROJECT_ID     = <?php print $project->getId(); ?>;
    var PROJECT_ENVIRONMENT = '<?php print $environment; ?>';
    var PROJECT_BRANCH = '<?php print $branch; ?>';
    var PER_PAGE       = <?php print $perPage; ?>;
</script>

<div class="clearfix"  style="margin-bottom: 20px;">
    <a class="btn btn-default" href="<?php print APP_URL . 'project/edit/' . $project->getId(); ?>">
        <?php Lang::out('edit_project'); ?>
    </a>

    <a class="btn btn-danger" id="delete-project">
        <?php Lang::out('delete_project'); ?>
    </a>

    <div class="pull-right btn-group">
        <?php if (!$project->getArchived()): ?>
            <?php if($this->User()->getIsAdmin()): ?>
                <div class="btn-group build-btn">
                    <button type="button" class="btn btn-danger dropdown-toggle" data-toggle="dropdown">
                        <?php Lang::out('build_now_debug'); ?>&nbsp;&nbsp;<span class="caret"></span>
                    </button>

                    <ul class="dropdown-menu" role="menu">
                        <?php if(!empty($environments)): ?>
                            <?php foreach ($environments as $currentEnvironment) : ?>
                                <li <?php echo ($currentEnvironment == $environment) ? 'class="active"' : ''?>>
                                    <a href="<?php echo APP_URL ?>project/build/<?php print $project->getId(); ?>/environment/<?php echo urlencode($currentEnvironment) ?>?debug=1">
                                        <i class="fa fa-cog"></i><?php echo $currentEnvironment ?>
                                    </a>
                                </li>
                            <?php endforeach; ?>
                            <li class="divider"></li>
                        <?php endif; ?>

                        <?php foreach ($branches as $currentBranch) : ?>
                            <li <?php echo ($currentBranch == $branch) ? 'class="active"' : ''?>>
                                <a href="<?php echo APP_URL ?>project/build/<?php print $project->getId(); ?>/branch/<?php echo urlencode($currentBranch) ?>?debug=1">
                                    <i class="fa fa-code-fork"></i><?php echo $currentBranch ?>
                                </a>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                </div>
            <?php endif; ?>

            <div class="btn-group build-btn">
                <button type="button" class="btn btn-success dropdown-toggle" data-toggle="dropdown">
                    <?php Lang::out('build_now'); ?>&nbsp;&nbsp;<span class="caret"></span>
                </button>

                <ul class="dropdown-menu" role="menu">
                    <?php if(!empty($environments)): ?>
                        <?php foreach ($environments as $currentEnvironment) : ?>
                            <li <?php echo ($currentEnvironment == $environment) ? 'class="active"' : ''?>>
                                <a href="<?php echo APP_URL ?>project/build/<?php print $project->getId(); ?>/environment/<?php echo urlencode($currentEnvironment) ?>">
                                    <i class="fa fa-cog"></i><?php echo $currentEnvironment ?>
                                </a>
                            </li>
                        <?php endforeach; ?>
                        <li class="divider"></li>
                    <?php endif; ?>

                    <?php foreach ($branches as $currentBranch) : ?>
                        <li <?php echo ($currentBranch == $branch) ? 'class="active"' : ''?>>
                            <a href="<?php echo APP_URL ?>project/build/<?php print $project->getId(); ?>/branch/<?php echo urlencode($currentBranch) ?>">
                                <i class="fa fa-code-fork"></i><?php echo $currentBranch ?>
                            </a>
                        </li>
                    <?php endforeach; ?>
                </ul>
            </div>
        <?php endif; ?>

        <div class="btn-group environment-btn pull-right">
            <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
                <?php
                if (!empty($environment)) {
                    print Lang::get('environment_x', $environment);
                } elseif (!empty($branch)) {
                    print Lang::get('branch_x', $branch);
                } else {
                    print Lang::get('All');
                }
                ?>&nbsp;&nbsp;<span class="caret"></span>
            </button>

            <ul class="dropdown-menu" role="menu">
                <li><a href="<?php echo APP_URL ?>project/view/<?php print $project->getId(); ?>"><?php Lang::out('all'); ?></a></li>
                <li class="divider"></li>

                <?php if(!empty($environments)): ?>
                    <?php foreach ($environments as $currentEnvironment) : ?>
                        <li <?php echo ($currentEnvironment == $environment) ? 'class="active"' : ''?>>
                            <a href="<?php echo APP_URL ?>project/view/<?php print $project->getId(); ?>?environment=<?php echo urlencode($currentEnvironment) ?>">
                                <i class="fa fa-cog"></i><?php echo $currentEnvironment ?>
                            </a>
                        </li>
                    <?php endforeach; ?>
                    <li class="divider"></li>
                <?php endif; ?>

                <?php foreach ($branches as $currentBranch) : ?>
                    <li <?php echo ($currentBranch == $branch) ? 'class="active"' : ''?>>
                        <a href="<?php echo APP_URL ?>project/view/<?php print $project->getId(); ?>?branch=<?php echo urlencode($currentBranch) ?>">
                            <i class="fa fa-code-fork"></i><?php echo $currentBranch ?>
                        </a>
                    </li>
                <?php endforeach; ?>
            </ul>
        </div>
    </div>
</div>


<div class="row">
    <div class="col-lg-9 col-md-8 col-sm-8">
        <div class="box">
            <div class="box-header">
                <h3 class="box-title"><?php Lang::out('builds'); ?> (<?php print $total; ?>)</h3>
            </div>
            <table class="table table-hover">
                <thead>
                <tr>
                    <th><?php Lang::out('id'); ?></th>
                    <th><?php Lang::out('date'); ?></th>
                    <th class="hidden-md hidden-sm hidden-xs"><?php Lang::out('commit'); ?></th>
                    <th><?php Lang::out('environment'); ?></th>
                    <th><?php Lang::out('branch'); ?></th>
                    <th><?php Lang::out('status'); ?></th>
                    <th><?php Lang::out('duration'); ?></th>
                    <th></th>
                </tr>
                </thead>
                <tbody id="latest-builds">
                    <?php print $builds; ?>
                </tbody>
            </table>
        </div>
        <?php

        print '<div><ul class="pagination">';

        $project_url = APP_URL . 'project/view/' . $project->getId() . ((!empty($branch)) ? '/' . urlencode($branch) : '');

        if ($page > 1) {
            print '<li><a href="' . $project_url . '?p='.($page == 1 ? '1' : $page - 1).'">'.Lang::get('prev_link').'</a></li>';
        }

        if ($pages > 1) {

            $start = $page - 3;

            if ($start <= 0) {
                $start = 1;
            }

            $end = $page + 3;

            if ($end > $pages) {
                $end = $pages;
            }

            if ($start > 1) {
                print '<li><a href="' . $project_url . '">1...</a></li>';
            }

            for($i = $start; $i <= $end; $i++)
            {
                if ($pages > $end && $i == $pages) continue;

                if ($i == $page) {
                    print '<li class="bg-blue"><span>' . $i . '</span></li>';
                } else {
                    print '<li><a href="' . $project_url . '?p=' . $i . '">' . $i . '</a></li>';
                }
            }

            if ($pages > $end) {
                print '<li><a href="' . $project_url . '?p='.$pages.'">...'.$pages.'</a></li>';
            }
        }

        if ($page < $pages - 1) {
            print '<li><a href="' . $project_url . '?p='.($page == $pages ? $pages : $page + 1).'">'.Lang::get('next_link').'</a></li>';
        }

        print '</ul></div>';

        ?>
    </div>

    <div class="col-lg-3 col-md-4 col-sm-4">

        <?php if ($project->getAllowPublicStatus() && !$project->getArchived()): ?>
            <div class="box">
                <div class="box-header">
                    <h4 class="box-title"><?= Lang::get('public_status_title'); ?></h4>
                </div>

                <div class="box-body">
                    <span style="word-wrap: break-word;">
                        <a href="<?= APP_URL . 'build-status/view/' . $project->getId() . '?branch=master'; ?>">
                            <img src="<?= APP_URL . 'build-status/image/' . $project->getId() . '?branch=master&label=PHPCensor&style=flat-square'; ?>" />
                        </a>

                        <br /><br />
                        <?= Lang::get('public_status_image'); ?>:<br /><strong><?= APP_URL . 'build-status/image/' . $project->getId() . '?branch=master&label=PHPCensor&style=flat-square'; ?></strong>

                        <br /><br />
                        <?= Lang::get('public_status_page'); ?>:<br /><strong><?= APP_URL . 'build-status/view/' . $project->getId() . '?branch=master'; ?></strong>
                    </span>
                </div>
            </div>
        <?php endif; ?>

        <?php if (in_array($project->getType(), ['github', 'gitlab', 'bitbucket'])): ?>
            <div class="box">
                <div class="box-header">
                    <h4 class="box-title"><?php Lang::out('webhooks'); ?></h4>
                </div>

                <div class="box-body">
                    <?php
                    switch($project->getType())
                    {
                        case 'github':
                            $url = APP_URL . 'webhook/github/' . $project->getId();
                            Lang::out('webhooks_help_github', $project->getReference());
                            break;

                        case 'gitlab':
                            $url = APP_URL. 'webhook/gitlab/' . $project->getId();
                            Lang::out('webhooks_help_gitlab');
                            break;

                        case 'bitbucket':
                        case 'bitbuckethg':
                            $url = APP_URL . 'webhook/bitbucket/' . $project->getId();
                            Lang::out('webhooks_help_bitbucket', $project->getReference());
                            break;
                    }
                    ?>
                    <br><br><strong style="word-wrap: break-word;"><?php print $url; ?></strong>
                </div>
            </div>
        <?php endif; ?>

        <?php if ($project->getSshPublicKey()): ?>
            <div class="box">
                <div class="box-header"><h3 class="box-title"><?php Lang::out('public_key'); ?></h3></div>
                <div class="box-body" style="word-break: break-all;"><?php print $project->getSshPublicKey(); ?></div>
            </div>
        <?php endif; ?>
    </div>
</div>

<script>
    $(function() {
        $('#delete-project').on('click', function (e) {
            e.preventDefault();
            confirmDelete("<?= APP_URL; ?>project/delete/<?= $project->getId(); ?>")
                .onCloseConfirmed = function () {window.location = '/'};
        });
    })
</script>
