<?php
if(isset($_GET["sharingid"])){
	header("Location: access.php?sharingid=".$_GET["sharingid"]);
}
?>
<!DOCTYPE html>
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1, maximum-scale=1"/>
<html>
<head>
<script type='text/javascript' charset='utf-8'>
    // Hides mobile browser's address bar when page is done loading.
      window.addEventListener('load', function(e) {
        setTimeout(function() { window.scrollTo(0, 1); }, 1);
      }, false);
</script>
<meta charset="UTF-8">
<title>Quick Send</title>
<link rel="stylesheet" href="../script/tocas/tocas.css">
</head>
<body>
<br>
<div class="ts container">
<div class="ts menu">
<a class="item">Functions</a>
<a class="item" onclick="ts('#modal').modal('show')">Vaildity Period</a>
<a class="item">QR-Code</a>
</div>

<div class="ts text segment">
    <div class="ts segment">
	<div class="ts horizontal form">
    <div class="field">
        <label>File path</label>
       <input type="text" id="path">
    </div>
    <div class="field">
        <label>Shared Link</label>
        <input type="text" id="link">
    </div>
    <div class="field">
        <div class="ts checkbox">
            <input id="secure_sharing" type="checkbox" id="secure_sharing">
            <label for="secure_sharing">Enable secure sharing</label>
        </div>
    </div>
	 <div class="field">
        <label>Share with</label>
        <div class="ts checkboxes" style="width:100%">
            <div class="ts disabled radio checkbox" id="cuser">
                <input id="onlineuser" type="radio" name="method">
                <label for="onlineuser">ArOZ Online Users</label>
            </div>
			<select id="userList" disabled>
			</select>
			<br><br>
            <div class="ts disabled radio checkbox" id="cpwdm">
                <input id="pwdm" type="radio" name="method">
                <label for="pwdm">Password</label>
            </div>
			<input type="password" id="pwd" disabled>
        </div>
    </div>
</div>
<br>
	<div class="ts container" align="right">
	<button class="ts tiny basic button" onclick="senddata()">Save</button>
	</div>
	</div>
</div>

</div>
</div>
<div class="ts modals dimmer">
<dialog id="modal" class="ts fullscreen modal">
    <div class="header">
        Customize Validity Period
    </div>
    <div class="content">
	<form class="ts form">
		<div class="field">
			<div class="ts checkbox" id="cstart">
				<input id="startt" type="checkbox">
				<label for="startt">Setup start time</label>
			</div>
		</div>
		<div class="inline field">
			<div class="eight wide field">
				<input id="startt1" type="text" placeholder="Start date (YYYY-mm-dd)" disabled>
			</div>
			<div class="eight wide field">
				<input id="startt2" type="text" placeholder="Start time (HH:mm)" disabled>
			</div>
		</div>
		<div class="field">
			<div class="ts checkbox" id="cstop">
				<input id="stopt" type="checkbox">
				<label for="stopt">Setup stop time</label>
			</div>
		</div>
		<div class="inline field">
			<div class="eight wide field">
				<input id="stoptt1" type="text" placeholder="Stop date (YYYY-mm-dd)" disabled>
			</div>
			<div class="eight wide field">
				<input id="stoptt2" type="text" placeholder="Stop time (HH:mm)" disabled>
			</div>
		</div>
		<div class="field">
			<div class="ts checkbox" id="cnumall">
				<input id="numalla" type="checkbox">
				<label for="numalla">Number of allowed access</label>
			</div>
		</div>
		<div class="field">
			<input id="numallt" type="text" placeholder="Remaining access times" disabled>
		</div>
		</form>
    </div>
    <div class="actions">
        <button class="ts positive button">
            Save
        </button>
    </div>
</dialog>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script src="../script/tocas/tocas.js"></script>
<script src="../script/ao_module.js"></script>

<?php
include "include.php";
$a = new QS();
?>
<script>
if(ao_module_virtualDesktop){
	ao_module_setWindowSize(810,540);
}
var usernameList = <?php echo $a->arr2jsarr($a->getUsernameList()); ?>;
var filepath = <?php echo $a->filepath($_GET["share"]); ?>;
var file_exist = <?php echo $a->checkfileexist($_GET["share"]); ?>;

//https://stackoverflow.com/questions/38620152/pick-a-random-letter-from-string-in-javascript
var randomID = "";
var alphabet = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ123456789";
while (randomID.length < 9) {
  randomID += alphabet[Math.floor(Math.random() * alphabet.length)];
}

$("#path").val(filepath.replace("../","/AOR/"));
$("#link").val(window.location["href"].replace(/\?share=.*/,"?sharingid=" + randomID));

for(i=0;i<usernameList.length;i++){
	$('#userList').append(new Option(usernameList, usernameList))
}

$("#onlineuser").attr("checked",true);

var securetype = "nil";
var vstart = "no";
var vstop = "no";
var vaccess = "no";

$("#secure_sharing").change(function() {
	if($("#secure_sharing").attr("checked")){
		$(".ts.disabled.radio.checkbox").attr("class","ts radio checkbox");
		if($("#pwdm").attr("checked")){
			$("#pwd").removeAttr("disabled");
			securetype = "pwd";
		}else{
			$("#userList").removeAttr("disabled");
			securetype = "user";
		}
	}else{
		$(".ts.radio.checkbox").attr("class","ts disabled radio checkbox");
		$("#userList").attr("disabled","disabled");
		$("#pwd").attr("disabled","disabled");
		securetype = "nil"
	}
});

$("#cuser").change(function() {
	if($("#onlineuser").attr("checked")){
		$("#userList").removeAttr("disabled");
		$("#pwd").attr("disabled","disabled");
		securetype = "user";
	}
});
$("#cpwdm").change(function() {
	if($("#pwdm").attr("checked")){
		$("#userList").attr("disabled","disabled");
		$("#pwd").removeAttr("disabled");
		securetype = "pwd";
	}
});

$("#cstart").change(function() {
	if($("#startt").attr("checked")){
		$("#startt1").removeAttr("disabled");
		$("#startt2").removeAttr("disabled");
		vstart = "yes";
	}else{
		$("#startt1").attr("disabled","disabled");
		$("#startt2").attr("disabled","disabled");
		vstart = "no";
	}
});
$("#cstop").change(function() {
	if($("#stopt").attr("checked")){
		$("#stoptt1").removeAttr("disabled");
		$("#stoptt2").removeAttr("disabled");
		vstop = "yes";
	}else{
		$("#stoptt1").attr("disabled","disabled");
		$("#stoptt2").attr("disabled","disabled");
		vstop = "no";
	}
});
$("#cnumall").change(function() {
	if($("#numalla").attr("checked")){
		$("#numallt").removeAttr("disabled");
		vaccess = "yes";
	}else{
		$("#numallt").attr("disabled","disabled");
		vaccess = "no";
	}
});

function senddata(){
	$.post( "opr.php", { path: ".." + $("#path").val().substring(4), id: randomID, secure_sharing: securetype, userList: $("#userList").val(), pwd: $("#pwd").val(), isstart: vstart, isstop: vstop, is_access: vaccess, startd: $("#startt1").val(), startt: $("#startt2").val(), stopd: $("#stoptt1").val(), stopt: $("#stoptt2").val(), numaccess: $("#numallt").val() }, function( data ) {
		console.log("OKAY!");
	}, "json");
}


var lan = JSON.parse('{"zh-TW":{"File path":"檔案路徑","Shared Link":"分享連結","Enable secure sharing":"啟動安全分享","ArOZ Online Users":"ArOZ Online使用者","Password":"密碼","Share with": "分享給其他人","QR-Code":"取得QR碼","Functions": "功能","Vaildity Period":"有效期間","Customize Validity Period":"自訂有效期間","Setup start time":"設定開始時間","Start date":"開始日期","Start time":"開始時間","Setup stop time":"設定結束時間","Stop date":"結束日期","Stop time":"結束時間","Number of allowed access":"可存取次數","Remaining access times":"剩餘存取次數","Start date (YYYY-mm-dd)": "開始日期 (YYYY-mm-dd)","Start time (HH:mm)": "開始時間 (HH:mm)","Stop date (YYYY-mm-dd)": "停止日期 (YYYY-mm-dd)","Stop time (HH:mm)": "停止時間 (HH:mm)","Save": "儲存"},"zh-HK":{},"en-US":{}}');
</script>
<script src="i18n.js"></script>
</body>
</html>