{{ extends "layouts/index_site" }}
{{ import "layouts/partials/helpers/csrf" }}
{{ import "layouts/partials/helpers/captcha" }}
{{ import "layouts/partials/helpers/flags" }}
{{ import "layouts/partials/helpers/tag_form" }}
{{block title()}}{{ T("upload")}}{{end}}
{{block content_body()}}
<div style="text-align: left;" class="box">
  <p class="upload-rules">{{ T("read_rules")|raw }}</p>
  <form enctype="multipart/form-data" method="POST">
    {{ yield csrf_field() }}
	
    <div class="upload-errors">
	{{ yield errors(name="name")}}
    {{ yield errors(name="torrent")}}
    {{ yield errors(name="magnet")}}
    {{ yield errors(name="c")}}
    {{ yield errors(name="remake")}}
    {{ yield errors(name="hidden")}}
    {{ yield errors(name="website_link")}}
    {{ yield errors(name="tags")}}
	</div>
    
	  <table class="upload-form-table">
	    <tbody>
		  <tr>
		     <td class="table-input-label"><label for="name">{{ T("name")}}</label></td>
		     <td class="table-input"><input type="text" name="name" id="name" class="form-input up-input form-torrent-name" placeholder="{{ T("torrent_name")}}" value="{{Form.Name}}" autofocus></td>
		  </tr>
		  <tr class="table-torrent-link">
		     <td class="table-input-label"><label for="torrent">{{ T("torrent_file")}} / {{ T("magnet_link")}}</label></td>
		     <td class="table-input"><input type="file" name="torrent" id="torrent" class="up-input" accept=".torrent" {{ if Form.Magnet != "" }}disabled{{end}}/><input type="text" name="magnet" id="magnet" class="form-input up-input" placeholder="{{ T("magnet_link")}}" value="{{Form.Magnet}}"/></td>
		  </tr>
		  <tr>
		     <td class="table-input-label"><label for="c">{{ T("category")}}</label></td>
		     <td class="table-input">
			  <select name="c" id="c" class="form-input up-input form-torrent-category" required>
				<option value="">{{ T("select_a_torrent_category")}}</option>
				{{ range _, cat := GetCategories(false, true) }}
				<option value="{{ cat.ID }}" {{if Form.Category == cat.ID }}selected{{end}}>{{ T(cat.Name) }}</option>
				{{ end }}
			  </select>
			 </td>
		  </tr>
		  <tr>
		     <td class="table-input-label">{{ T("torrent_language")}}</td>
		     <td class="table-input">
			   <div class="form-input language">
                 {{ yield flagList(languages=GetTorrentLanguages(), selected=Form.Languages, inputname="languages", id="upload-lang")}}
               </div>
			   <div class="table-checkboxes">
				 <div class="form-group">
					<input type="checkbox" value="true" name="remake" id="remake" class="form-torrent-remake"/ {{if Form.Remake}}checked{{end}}><label for="remake">{{ T("mark_as_remake")}}</label>
				 </div>
				 <div class="form-group">
					<input type="checkbox" value="true" name="hidden" id="hidden" class="form-torrent-hidden {{ if User.ID == 0 }}hidden{{end}}" {{ if User.ID == 0}}disabled checked{{else if Form.Hidden }}checked{{end}}><label for="hidden" {{ if User.ID == 0 }}class="hidden"{{end}}>{{ T("upload_as_anon")}}</label>
				 </div>
			   </div>
	         </td>
		  </tr>
		  <tr>
		     <td class="table-input-label"><label for="website_link">{{ T("website_link")}}</label></td>
		     <td class="table-input"><input name="website_link" id="website_link" placeholder="http://" class="form-input up-input" type="text" value="{{Form.WebsiteLink}}"/></td>
		  </tr>
		  <tr>
		     <td class="table-input-label"><label for="desc">{{ T("torrent_description")}}</label></td>
		     <td class="table-input"><textarea name="desc" id="desc" class="form-input up-input" style="height: 10rem;">{{Form.Description}}</textarea></td>
		  </tr>
		  <tr class="hidden">
		     <td class="table-input-label"><label for="anidex_api">Upload to Anidex:</label></td>
		     <td class="table-input">
				<div class="checkbox-container"><input disabled type="checkbox" value="" name="anidex_upload" id="anidex_upload" {{if User.AnidexAPIToken != ""}}checked disabled{{end}} class="form-torrent"/></div>
				<input disabled name="anidex_api" id="anidex_api" placeholder="Coming soon... (hopefully)" class="form-input up-input" type="text" value="{{User.AnidexAPIToken}}"/>
			 </td>
		  </tr>
		  <tr class="hidden">
		     <td class="table-input-label"><label for="nyaasi_api">Upload to Nyaa.si:</label></td>
		     <td class="table-input">
				<div class="checkbox-container"><input disabled type="checkbox" value="" name="nyaasi_upload" id="nyaasi_upload" {{if User.NyaasiAPIToken != ""}}checked disabled{{end}} class="form-torrent"/></div>
				<input disabled name="nyaasi_api" id="nyaasi_api" placeholder="Coming soon... (hopefully)" class="form-input up-input" type="text" value="{{User.NyaasiAPIToken}}"/>
			</td>
		  </tr>
		  <tr class="hidden">
		     <td class="table-input-label"><label for="tokyot_api">Upload to TokyoTosho:</label></td>
		     <td class="table-input">
				<div class="checkbox-container"><input disabled type="checkbox" value="" name="tokyot_upload" id="tokyot_upload" {{if User.TokyoTAPIToken  != ""}}checked disabled{{end}} class="form-torrent"/></div>
				<input disabled name="tokyot_api" id="tokyot_api" placeholder="Coming soon... (hopefully)" class="form-input up-input" type="text" value="{{User.TokyoTAPIToken }}"/>
			</td>
		  </tr>
		  <tr>
		     <td class="table-input-label">{{ T("torrent_tags")}}:</td>
		     <td class="table-input">

	  <table class="upload-tag-table">
		<tbody>
			<tr>
			{{ range _, tag := Config.Torrents.Tags.Types}}
				<td>
				{{ yield tagForm(tagType=tag, acceptedTag=Form.Tags.Get(tag.Name)) }}
				{{ if _ % 3 == 1 && _ == len(Config.Torrents.Tags.Types) - 1}}
				</td>
				  <td>
					 <div class="form-group">
					   <label class="input-label" for="tag_{{Config.Torrents.Tags.Default}}">{{ T("tagtype_tags") }}</label>
					   <input type="text" name="tag_{{Config.Torrents.Tags.Default}}" class="form-input" id="tag_{{Config.Torrents.Tags.Default}}" value="{{ Form.Tags.Get(Config.Torrents.Tags.Default).Tag }}" />
					 </div>
				  </td>
				{{ else }}
					{{ if _ % 3 == 2}}
					  </td></tr><tr>
					{{else}}
					  </td>
					{{end}}
				{{end}}
				{{ if _ % 3 != 1 && _ == len(Config.Torrents.Tags.Types) - 1}}
				</tr>
				<tr>
				  <td>
					 <div class="form-group">
					   <label class="input-label" for="tag_{{Config.Torrents.Tags.Default}}">{{ T("tagtype_tags") }}</label>
					   <input type="text" name="tag_{{Config.Torrents.Tags.Default}}" class="form-input" id="tag_{{Config.Torrents.Tags.Default}}" value="{{ Form.Tags.Get(Config.Torrents.Tags.Default).Tag }}" />
					 </div>
				  </td>
				{{end}}
			{{ end }}
			</tr>
		</tbody>
	  </table>
			 </td>
		  </tr>
		</tbody>
	  </table>

      <div style="width: 240px">
        {{yield captcha(captchaid=Form.CaptchaID)}}
      </div>
      {{ yield errors(name="captchaID")}}
      <button type="submit" class="form-input up-btn" id="upload-button">{{ T("upload")}}</button>
    </form>
  </div>
  {{end}}
  {{ block footer_js()}}
  <script type="text/javascript" src="/js/translation.js?v={{ Config.Version}}{{ Config.Build }}"></script>
  <script type="text/javascript" src="/js/template.js?v={{ Config.Version}}{{ Config.Build }}"></script>
  <script type="text/javascript" src="/js/simplemde.min.js?v={{ Config.Version}}{{ Config.Build }}"></script>
  <script type="text/javascript">
   document.querySelector('#upload-button').addEventListener("click", (e) => {
	if(document.querySelector('.upload-form-table select[name="c"]').selectedIndex == 0)
		setTimeout(function(){ window.scrollBy(0, -70) }, 1);
   })
	  
  document.querySelector("input[name='magnet']").addEventListener("keyup", (e) => {
    var torrentInput = document.querySelector("input[name='torrent']")
    torrentInput.disabled = e.target.value != ""
    if (e.target.value == "") torrentInput.classList.remove("hidden")
    else torrentInput.classList.add("hidden")
    try{
      torrentInput.value = '';
      if (torrentInput.value) {
          torrentInput.type = "text";
          torrentInput.type = "file";
      }
    }catch(e){}
  })
  
  document.querySelector("input[name='anidex_api']").addEventListener("keyup", (e) => {
    var Checkbox = document.querySelector("input[name='anidex_upload']")
    Checkbox.disabled = e.target.value != ""
    Checkbox.checked = e.target.value != ""
  }) 
  
  document.querySelector("input[name='nyaasi_api']").addEventListener("keyup", (e) => {
    var Checkbox = document.querySelector("input[name='nyaasi_upload']")
    Checkbox.disabled = e.target.value != ""
    Checkbox.checked = e.target.value != ""
  }) 
	  
  document.querySelector("input[name='tokyot_api']").addEventListener("keyup", (e) => {
    var Checkbox = document.querySelector("input[name='tokyot_upload']")
    Checkbox.disabled = e.target.value != ""
    Checkbox.checked = e.target.value != ""
  }) 
</script>
  <script type="text/javascript">new SimpleMDE({ element: document.getElementById("desc"), spellChecker: false, showIcons: [ "strikethrough", "code", "table", "horizontal-rule" ] });</script>
{{end}}
