#requireResource("confluence.web.resources:ajs")
#requireResource("com.atlassian.auiplugin:jquery-selection")
#requireResource("confluence.editor.actions:editor-insertlink-popup")

#parse ( "/template/includes/actionerrors.vm" )

## Error box for javascript validation
<div class="errorBox" id="popup-errorBox" style="display: none;">
    <span class="errorMessage">$action.getText("errors.occurred")</span>
    <ul>
        <li id="noDestinationLinkError">$action.getText("no.link.destination")</li>
    </ul>
</div>

    #set ($formname = "$req.getParameter('formname')")
    #set ($currentspace = "$!req.getParameter('currentspace')")
    #set ($fieldname = "$!req.getParameter('fieldname')")
    #set ($pageId = "$!req.getParameter('pageId')")
    #set ($draftType = "$!req.getParameter('draftType')")

    ## we store the state that is passed to this popup (from the originating page) in the URL as request parameters
    ## these are appended on the end of each tab URL to ensure that this state is not lost as the user is switching in between tabs
    #set ($extraRequestParametersForTabsOrgHtml = "currentspace=$generalUtil.urlEncode($currentspace)&formname=$generalUtil.urlEncode($formname)&fieldname=$generalUtil.urlEncode($fieldname)")

    ## the following are affected depending on whether or not we are attaching a file onto a page or a draft
    ## (1) different extra request parameters
    ## (2) whether or not to show the file upload box (anonymous users are not allowed to attach files to a draft)
    #if ($stringUtils.isNotBlank($pageId))
        #set ($extraRequestParametersForTabs = "${extraRequestParametersForTabsOrgHtml}&pageId=$generalUtil.urlEncode($pageId)")
        #set ($canAttachFile = $action.hasAttachFilePermissions())
    #else
        #set ($extraRequestParametersForTabs = "${extraRequestParametersForTabsOrgHtml}&draftType=$generalUtil.urlEncode($draftType)&spaceKey=$generalUtil.urlEncode($currentspace)")
        #set ($canAttachFile = $remoteUser && $action.hasAttachFilePermissions())
    #end

    <fieldset class="hidden parameters">
        <input type="hidden" id="formname" value="$generalUtil.htmlEncode($formname)">
        <input type="hidden" id="fieldname" value="$generalUtil.htmlEncode($fieldname)">
        <input type="hidden" id="currentspace" value="$generalUtil.htmlEncode($currentspace)">
        <input type="hidden" id="spaceKey" value="$generalUtil.htmlEncode($currentspace)">
    </fieldset>

    <table border="0" width="100%">
    <tr>
        <td>
            <table>
                <tr>
                    <td>
                        $action.getText("insert.link.popup.destination")&nbsp;
                    </td>
                    <td>
                        <input id="destination" name="destination" type="text" size=40 value="$generalUtil.htmlEncode($!req.getParameter('destination'))">
                    </td>
                </tr>
                <tr>
                    <td>
                        $action.getText("insert.link.popup.alias")&nbsp;
                    </td>
                    <td>
                        <input id="alias" name="alias" type="text" size=40 value="$generalUtil.htmlEncode($!req.getParameter('alias'))">
                    </td>
                </tr>
                <tr>
                    <td>
                        $action.getText("insert.link.popup.tooltip")&nbsp;
                    </td>
                    <td>
                        <input id="tooltip" name="tooltip" type="text" size=40 value="$generalUtil.htmlEncode($!req.getParameter('tooltip'))">
                    </td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                     <td>
                         ## We bind the onclick here instead of binding in javascript because Selenium fails to close the windows otherwise.
                         <input id="btnOk" type="button" value="$action.getText('ok')" class="Button" onclick="AJS.Popup.submit();"/>&nbsp;
                         <input id="btnCancel" type="button" value="$action.getText('cancel.name')" class="Button" onclick="AJS.Popup.cancel();"/>
                     </td>
                </tr>
            </table>
        </td>
    </tr>
    </table>

    <table border=0 width="100%">
    <tr><td style="padding-top: 10px;">
    <ul class="tabnav">
        <li class="tabs">
        #set ($mode = $!req.getParameter('mode'))
        #if ($stringUtils.isEmpty($mode)) #set ($mode = 'search') #end ## search is the default tab
        <a id="search-tab" href="${tabUrl}?${extraRequestParametersForTabs}&mode=search" #if ($mode == 'search') class="current" #end>$action.getText('search.name')</a>
        <a id="history-tab" href="${tabUrl}?${extraRequestParametersForTabs}&mode=history" #if ($mode == 'history') class="current" #end>$action.getText('history.name')</a>
        <a id="recent-tab" href="${tabUrl}?${extraRequestParametersForTabs}&mode=recent" #if ($mode == 'recent') class="current" #end>$action.getText('recently.modified')</a>
        <a id="external-tab" href="${tabUrl}?${extraRequestParametersForTabs}&mode=external" #if ($mode == 'external') class="current" #end>$action.getText('external.link')</a>
        <a id="attachments-tab" href="$attachmentsTabLinkUrl?${extraRequestParametersForTabs}&mode=attachments" #if ($mode == 'attachments') class="current" #end>$action.getText('attachments.name')</a>
        </li>
    </ul>
    <!-- clears the floated elements above -->
    <br class="after-tabnav">
    </td></tr>
    #if ($mode == 'external')
        <tr><td>
            $action.getText("external.hyperlink.prompt"): <input type="text" name="url" value="http://" size="40" onkeyup="populateDestinationLinkField(this.value)">
        </td></tr>
    #elseif ($mode == 'search')

        <tr><td>
        <form action="$req.getRequestURI()" method="post">

        #set ($formActionPostfixed = $generalUtil.appendAmpsandOrQuestionMark($req.getRequestURI()))
        #set ($formActionPostfixed = "${formActionPostfixed}${extraRequestParametersForTabs}&mode=search&")

        #set ($actionNameStart = $req.getRequestURL().lastIndexOf('/') + 1)
        #if ($req.getRequestURL().substring($actionNameStart) != 'spacepagepicker.action')
            #bodytag ("Select" "theme='custom'" "name='searchQuery.spaceKey'" "template='space-select.vm'" "tabindex='3'")
                #param ("value" $searchQuery.spaceKey)
                #param ("multiple" false)
            #end
        #end
        <input type="text" name="query" value="$generalUtil.escapeXml($!searchQuery.queryString)">&nbsp;<input type="submit" value="$action.getText('search.name')">

        <input type="hidden" name="currentspace" value="$generalUtil.htmlEncode($currentspace)">
        <input type="hidden" name="formname" value="$generalUtil.htmlEncode($formname)">
        <input type="hidden" name="fieldname" value="$generalUtil.htmlEncode($fieldname)">
        <input type="hidden" name="mode" value="$generalUtil.htmlEncode($mode)">
        #if ($stringUtils.isNotBlank($pageId))
            <input type="hidden" name="pageId" value="$generalUtil.htmlEncode($pageId)">
        #else
            <input type="hidden" name="spaceKey" value="$generalUtil.htmlEncode($currentspace)">
            <input type="hidden" name="draftType" value="$generalUtil.htmlEncode($draftType)">
        #end

        <p>
        #if ("$!searchQuery.queryString" != "")
            #bodytag ("Component" "template='general-pagelist.vm'")
                #param ("pages" "searchResults")
                #param ("insertLink" "true")
                #param ("contentOther" "true")
            #end
            </p>
            #pagination($action.searchBean.paginationSupport "$formActionPostfixed")
        #end
        </form>


        </td></tr>
    #elseif ($mode == 'attachments')
        <tr><td>
            #set ($attachments = $action.getLatestVersionsOfAttachments())
            #if ($attachments.size() > 0)
            <table width="100%" class="tableview" cellspacing=0>
                <tr>
                <th>$action.getText('name.word')</th>
                <th>$action.getText('size.name')</th>
                <th>$action.getText('creator.name')</th>
                <th>$action.getText('date.name')</th>
                </tr>
                #foreach ($attachment in $attachments)
                <tr>
                <td nowrap>#contentIcon ($attachment) &nbsp;<a class="attachment_link" href="#">$generalUtil.htmlEncode($attachment.fileName)</a></td>
                <td width="1%" nowrap>$attachment.niceFileSize</td>
                <td width="1%" nowrap>#usernameLink($attachment.lastModifierName)</td>
                <td width="1%" nowrap>$dateFormatter.format($attachment.lastModificationDate)</td>
                </tr>
                #end
            </table>
            #else
                $action.getText('no.attachments')
            #end

            #if ($canAttachFile)
            <p/>
            
            #if ($stringUtils.isNotBlank($pageId))
                #set ($parameterizedFormAction = "$generalUtil.appendAmpsandOrQuestionMark($generalUtil.htmlEncode(${formAction}))pageId=${pageId}&formname=$generalUtil.htmlEncode($formname)&fieldname=$generalUtil.htmlEncode($fieldname)&mode=attachments")
            #else
                #set ($parameterizedFormAction = "$generalUtil.appendAmpsandOrQuestionMark($generalUtil.htmlEncode(${formAction}))draftType=$generalUtil.htmlEncode($draftType)&spaceKey=$generalUtil.htmlEncode($spaceKey)&formname=$generalUtil.htmlEncode($formname)&fieldname=$generalUtil.htmlEncode($fieldname)&mode=attachments")            
            #end

            <form method="POST" action="$parameterizedFormAction" enctype="multipart/form-data">
                <p><span class="formtitle">$action.getText('attach.file'): <input type="file" name="file_0">&nbsp;</span>
                <input type="submit" value="$action.getText('attach.name')"></p>
                <input type="hidden" name="currentspace" value="$generalUtil.htmlEncode($!currentspace)">
                <input type="hidden" name="fieldname" value="$generalUtil.htmlEncode($fieldname)">
                <input type="hidden" name="comment_0" value="">
            </form>
            #end

        </td></tr>
    #else
        #if ($mode == 'history')
            #set($methodName = "historyPages")
        #elseif ($mode == 'recent')
            #set($methodName = "recentlyUpdatedPages")
        #else
            #set($methodName = "historyPages")
        #end

        #bodytag ("Component" "template='general-pagelist.vm'")
            #param ("pages" $methodName)
            #param ("insertLink" "true")
            #param ("contentOther" "true")
        #end
    #end
    </table>
