<div class="pull-request-wrap">

    % if c.pr_merge_possible:
    <h2 class="merge-status">
        <span class="merge-icon success"><i class="icon-ok"></i></span>
        ${_('This pull request can be merged automatically.')}
    </h2>
    % else:
    <h2 class="merge-status">
        <span class="merge-icon warning"><i class="icon-false"></i></span>
        ${_('Merge is not currently possible because of below failed checks.')}
    </h2>
    % endif

    % if c.pr_merge_errors.items():
    <ul>
        % for pr_check_key, pr_check_details in c.pr_merge_errors.items():
        <% pr_check_type = pr_check_details['error_type'] %>
        <li>
            <div class="merge-message ${pr_check_type}" data-role="merge-message">
                <span style="white-space: pre-line">- ${pr_check_details['message']}</span>
                % if pr_check_key == 'todo':
                % for co in pr_check_details['details']:
                <a class="permalink" href="#comment-${co.comment_id}"
                   onclick="Rhodecode.comments.scrollToComment($('#comment-${co.comment_id}'), 0, ${h.str_json(co.outdated)})">
                    #${co.comment_id}</a>${'' if loop.last else ','}
                % endfor
                % endif
            </div>
        </li>
        % endfor
    </ul>
    % endif

    <div class="pull-request-merge-actions">
        % if c.allowed_to_merge:
        ## Merge info, show only if all errors are taken care of
        % if not c.pr_merge_errors and c.pr_merge_info:
        <div class="pull-request-merge-info">
            <ul>
                % for pr_merge_key, pr_merge_details in c.pr_merge_info.items():
                <li>
                    - ${pr_merge_details['message']}
                </li>
                % endfor
            </ul>
        </div>
        % endif

        <div>
            ${h.secure_form(h.route_path('pullrequest_merge', repo_name=c.repo_name,
            pull_request_id=c.pull_request.pull_request_id), id='merge_pull_request_form', request=request,
            style='display: flex; justify-content: flex-end;')}
            <% merge_disabled = ' disabled' if c.pr_merge_possible is False else '' %>

            % if c.allowed_to_close:
                ## close PR action, injected later next to COMMENT button
                % if c.pull_request_review_status == c.REVIEW_STATUS_APPROVED:
                <a id="close-pull-request-action" class="btn btn-approved-status" href="#close-as-approved"
                   onclick="closePullRequest('${c.REVIEW_STATUS_APPROVED}'); return false;">
                    ${_('Close with status {}').format(h.commit_status_lbl(c.REVIEW_STATUS_APPROVED))}
                </a>
                % else:
                <a id="close-pull-request-action" class="btn btn-rejected-status" href="#close-as-rejected"
                   onclick="closePullRequest('${c.REVIEW_STATUS_REJECTED}'); return false;">
                    ${_('Close with status {}').format(h.commit_status_lbl(c.REVIEW_STATUS_REJECTED))}
                </a>
                % endif
            % endif
            <div class="btn-group btn-group-actions" style="margin-right: 5px;">
                <input type="submit" id="merge_pull_request" value="${_('Merge Pull Request')}"
                       class="btn${merge_disabled}" ${merge_disabled}>

                % if c.show_merge_strategy:
                    <a class="btn btn-primary btn-more-option${merge_disabled}" data-toggle="dropdown"
                       id="pull_request_more_options" aria-pressed="false" role="button" title="${_('More merge options')}">
                        <i class="icon-down"></i>
                    </a>

                    % if not merge_disabled and c.rhodecode_db_repo.repo_type in ['git', 'hg']:
                    <div class="btn-action-switcher-container right-align">
                        <ul class="btn-action-switcher" role="menu">
                            <li>
                                <a id="squash_merge_pull_request" href="#" style="white-space: nowrap;">
                                    % if c.rhodecode_db_repo.repo_type == 'git':
                                        ${_('Squash commits and merge')}
                                    % elif c.rhodecode_db_repo.repo_type == 'hg':
                                        ${_('Collapse commits and merge')}
                                    % endif
                                </a>
                                <input type="hidden" id="squash_merge_pull_request_value" name="squash_before_merge"
                                       value="false">
                            </li>
                        </ul>
                    </div>
                    % endif
                % endif
            </div>
            ${h.end_form()}

          % if c.rhodecode_edition_id == 'EE':
          <div class="pull-request-settings"
               id="close_branch_before_merging_wrapper"
          >
              ${h.checkbox('close_branch_before_merging', checked=c.pull_request.settings.get("close_branch_before_merging", False))}
              <label for="close_branch_before_merging">
                  % if c.rhodecode_db_repo.repo_type == 'hg':
                    ${h.literal(_('Close branch before merging'))}
                  % elif c.rhodecode_db_repo.repo_type == 'git':
                    ${h.literal(_('Delete branch after merging'))}
                  % endif
              </label>
          </div>
          % endif

          <div class="pull-request-merge-refresh">
              <a href="#refreshChecks" onclick="refreshMergeChecks(); return false;">${_('refresh checks')}</a>
          </div>

        </div>
        % elif c.rhodecode_user.username != h.DEFAULT_USER:
        <a class="btn" href="#" onclick="refreshMergeChecks(); return false;">${_('refresh checks')}</a>
        <input type="submit" value="${_('Merge and close Pull Request')}" class="btn disabled" disabled="disabled"
               title="${_('You are not allowed to merge this pull request.')}">
        % else:
        <input type="submit" value="${_('Login to Merge this Pull Request')}" class="btn disabled" disabled="disabled">
        % endif
    </div>

</div>


<script>
    $('#squash_merge_pull_request').on('click', function (e) {
        e.preventDefault();

        $('#squash_merge_pull_request_value').val("true");

        $('#merge_pull_request_form').trigger('submit');
    });

    $('#merge_pull_request_form').on('submit', function () {
        if (!$('#merge_pull_request').attr('disabled')) {
            $('#merge_pull_request').attr('disabled', 'disabled');
        }
        if (!$('#pull_request_more_options').hasClass('disabled')) {
            $('#pull_request_more_options').addClass('disabled');
        }
        return true;
    });

    $("#close_branch_before_merging").on('change', function () {
        const $checkbox = $(this);
        updateCloseBranchSetting(
            "${c.repo_name}",
            ${c.pull_request.pull_request_id},
            $checkbox.prop('checked'),
        );
    });
</script>
