<?php
require_once 'MemberDAO.class.php';
require_once 'YuruboDAO.class.php';
session_start();

require_once 'auth.php';
$fbid = $_SESSION["fbid"];

$me = MemberDAO::getByFBId($_SESSION["fbid"]);


if (isset($_POST["actiontype"])) {

	if ($_POST["actiontype"] == "add") {

		if (isset($_POST["category"]) && isset($_POST["yurubo"]) && isset($_POST["detail"])) {
			$category = htmlspecialchars(strip_tags(filter_input(INPUT_POST, "category")), ENT_QUOTES, 'UTF-8');
			$yurubo = htmlspecialchars(strip_tags(filter_input(INPUT_POST, "yurubo")), ENT_QUOTES, 'UTF-8');
			$detail = htmlspecialchars(strip_tags(filter_input(INPUT_POST, "detail")), ENT_QUOTES, 'UTF-8');
			$due = htmlspecialchars(strip_tags(filter_input(INPUT_POST, "due")), ENT_QUOTES, 'UTF-8');
			$due = date("Y-m-d ") . $due . ":00";
			if (strlen($yurubo) > 0 && strlen($category) > 0) {
				YuruboDAO::updateYurubo($fbid, $yurubo, $detail, $category, $due);
			}
		}
	} elseif ($_POST["actiontype"] == "delete") {
		YuruboDAO::disableYurubo($fbid);
	}
}

$yurubos = YuruboDAO::getActiveYuruboByFBId($fbid);

?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>自分のプロフィール</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script>

</script>
</head>
<body>
<div data-role="page">


<div data-role="header" style="overflow:hidden;">
<h1>自分のゆる募</h1>
<!--
<a href="edit_profile.php" class="ui-btn ui-icon-edit ui-btn-icon-left ui-btn-right" id="editButtion" data-transition="flip">編集</a>
-->
</div>

<div role="main" class="ui-content">
	<div class="card">
		<div class="card-image">
			<img alt="profile" src="<?php echo $me->getPictureUrl();?>" />
			<h2><?php echo $me->first_name.", " . $me->getAge();?></h2>
		</div>
		<p><?php echo $me->education;?></p>
		<p><?php echo $me->work; ?></p>

	</div>

<?php
foreach ($yurubos as $yurubo) {
	?>
	<div class="card">
		<div style="text-align:center; background-color:#FFDDDD" class="ui-nodisc-icon ui-alt-icon">
			現在のゆる募
			<a href="#popupDialogEditYurubo" data-rel="popup" data-position-to="window" data-transition="pop" class="ui-btn ui-shadow ui-corner-all ui-icon-edit ui-btn-icon-notext ui-btn-inline">Edit</a>
		</div>
		<p><?php echo $yurubo->yurubo;?> <span style="font-size:x-small;"><?php echo date("H:i", strtotime($yurubo->due));?>締切</span></p>
		<p><?php echo nl2br($yurubo->detail);?></p>
	</div>
	<?php
}
?>

<?php
if (count($yurubos) == 0) {
	echo '<a href="#popupDialogEditYurubo" data-rel="popup" data-position-to="window" data-transition="pop" class="ui-btn ui-shadow ui-corner-all ui-icon-edit">ゆる募追加</a>';
}
?>




</div>

<div data-role="footer" style="overflow:hidden;" data-position="fixed" data-tap-toggle="false">
	<div data-role="navbar">
	<ul>
		<li><a href="/" data-icon="grid"  data-transition="fade" class="ui-btn ui-corner-all ui-shadow ui-btn-inline" data-direction="reverse">周辺のゆる募</a></li>
		<li><a href="message_list.php" data-icon="star"   data-transition="fade" class="ui-btn ui-corner-all ui-shadow ui-btn-inline" data-direction="reverse">会話</a></li>
		<li><a href="#" data-icon="gear" class="ui-btn-active ui-state-persist">自分のゆる募</a></li>
	</ul>
	</div>
</div>

<!--
data-history should be off to prevent submission failure for the first time
https://github.com/jquery/jquery-mobile/issues/6014
 -->
	<div data-role="popup" id="popupDialogEditYurubo" data-overlay-theme="b" data-dismissible="false" style="max-width:400px;" data-history="false">
		<a href="#" data-rel="back" class="ui-btn ui-corner-all ui-shadow ui-btn-a ui-icon-delete ui-btn-icon-notext ui-btn-right">Close</a>
	    <div role="main" class="ui-content">
			<form action="my_profile.php" method="post">

			    <label for="category" class="select">ゆる募のカテゴリ</label>
				<select name="category" id="select" data-native-menu="false">
					<option value="gohan"<?php if (count($yurubos) > 0 && $yurubos[0]->category == "gohan") echo " selected";?>>ご飯</option>
					<option value="ocha"<?php if (count($yurubos) > 0 && $yurubos[0]->category == "ocha") echo " selected";?>>お茶</option>
				</select>
				<label for="due">掲載期間（設定時間後自動的に削除されます）</label>
					<?//TODO support different time zone?>
					<input type="time" name="due" id="due" value="<?php echo date("H", time() + 2 * 3600);?>:00">
					現在の仕様では日付をまたぐことはできません。
			    <br />
			    <label for="text-yurubo">ゆる募の内容</label>
			    <input type="text" name="yurubo" id="yurubo" value="<?php echo (count($yurubos) > 0 ? $yurubos[0]->yurubo : "◯◯な人ゆる募！！");?>" placeholder="今日一緒にご飯食べれる人ゆる募！！" >
			    <br />
			    <br />
			    <br />
			    <label for="textarea">詳細等</label>
				<textarea cols="40" rows="8" name="detail" id="detail"><?php echo (count($yurubos) > 0 ? $yurubos[0]->detail : "");?></textarea>
			    <br />
				<input type="hidden" name="actiontype" value="add">
				<input type="submit" id="submit" class="ui-shadow ui-btn ui-corner-all ui-mini" value="保存">
			    <br />
			    <br />
			</form>
			<form action="my_profile.php" method="post">
				<input type="hidden" name="actiontype" value="delete">
				<input type="submit" id="submit" class="ui-shadow ui-btn ui-corner-all ui-mini" value="ゆる募削除">
			</form>
		</div>
	</div>


</div>
</body></html>
