<div id="upload-container" class="cbi-value cbi-value-last">
	<label class="cbi-value-title" for="archive"><%:Upload%></label>
	<div class="cbi-value-field">
		<input type="file" name="upload_archive" accept="application/x-tar" id="upload_archive" />
	</div>
	<br>
	<label class="cbi-value-title" for="path"><%:Path%></label>
	<div class="cbi-value-field">
		<input type="text" class="cbi-input-text" name="path" value="/tmp/" id="path" />
	</div>
	<br>
	<div class="cbi-value-field">
		<input type="button"" class="btn cbi-button cbi-button-action important" id="upload" name="upload" value="<%:Upload%>" />
		<input type="button"" class="btn cbi-button cbi-button-action important" id="download" name="download" value="<%:Download%>" />
	</div>
</div>

<script type="text/javascript">
	let btnUpload = document.getElementById('upload')
	btnUpload.onclick = function (e) {
		let uploadArchive = document.getElementById('upload_archive')
		let uploadPath = document.getElementById('path').value
		if (!uploadArchive.value || !uploadPath) {
			docker_status_message('warning', "<%:Please input the PATH and select the file !%>")
			document.getElementById('docker_apply_overlay').addEventListener(
				"click",
				(e)=>{
					docker_status_message()
				}
			)
			return
		}
		let fileName = uploadArchive.files[0].name
		let formData = new FormData()
		formData.append('upload-filename', fileName)
		formData.append('upload-path', uploadPath)
		formData.append('upload-archive', uploadArchive.files[0])
		let xhr = new XMLHttpRequest()
		xhr.open("POST", '<%=luci.dispatcher.build_url("admin/docker/container_put_archive")%>/<%=self.container%>', true)
		xhr.onload = function() {
			if (xhr.status == 200) {
				uploadArchive.value = ''
				docker_status_message('notice', "<%:Upload Success%>")
			}
			else {
				docker_status_message('warning', "<%:Upload Error%>:" + xhr.statusText)
			}
			document.getElementById('docker_apply_overlay').addEventListener(
				"click",
				(e)=>{
					docker_status_message()
				}
			)
		}
		xhr.send(formData)
	}

	let btnDownload = document.getElementById('download')
	btnDownload.onclick = function (e) {
		let downloadPath = document.getElementById('path').value
		if (!downloadPath) {
			docker_status_message('warning', "<%:Please input the PATH !%>")
			document.getElementById('docker_apply_overlay').addEventListener(
				"click",
				(e)=>{
					docker_status_message()
				}
			)
			return
		}
		window.open('<%=luci.dispatcher.build_url("admin/docker/container_get_archive")%>?id=<%=self.container%>&path=' + encodeURIComponent(downloadPath))
	}
</script>
