<?php
	require("../__common__/__define_mobile.php");
	require("__define.php");
	$user_id = mobile_login_check();
	if ($_GET['id'].'' <> '') {
		$user_id = intval($_GET['id']);
		if (mobile_check_permit_id($_SESSION['mobile_login_id'], $user_id) == "") {	// アクセス権なし
			exit;
		}
		$con = mobile_mysql_connect(_DB_ACCOUNT_SCHEMA);
		$sql = "select c_handle from m_account where id_account = '" . $user_id . "'";
		$rs = my_mysql_query($sql);
		$rec = mysql_fetch_array($rs);
		$c_handle = $rec['c_handle'];
		$con = mysql_close($con);
	}
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<?php
	mobile_common_head('myHome カレンダー');
?>
</head>
<body>
<div data-role="page" data-url="<?= $_SERVER['REQUEST_URI'] ?>" id="top">
	<div data-role="header">
	<?php	if ($_GET['id'].'' <> '') { ?>
		<a href="../member" data-icon="back" rel="external">メンバー</a>
	<?php	} else { ?>
		<a href="../index" data-icon="home" rel="external">Home</a>
	<?php	} ?>
		<h1><?= $c_handle ?> カレンダー</h1>
		<a href="../index" data-icon="home" data-iconpos="notext" data-direction="reverse" class="ui-btn-right jqm-home">Home</a>
	</div>
<?php
	_GET_ymd($year, $month, $day);
	$con = mobile_mysql_connect(_DB_SCHEMA);
?>
	<div data-role="content">
<?php
		mobile_week_move($year, $month, $day);
		mobile_schedule_print_week($user_id, $year, $month, $day);
		mobile_week_move($year, $month, $day);
?>
	</div>
</div>
</body>
</html>
<?php
function _GET_ymd(&$year, &$month, &$day) {
	if ($_GET['y'].'' <> '') {
		$year = $_GET['y'];
	} else {
		$year = date('Y');
	}
	if ($_GET['m'].'' <> '') {
		$month = $_GET['m'];
	} else {
		$month = date('n');
	}
	if ($_GET['d'].'' <> '') {
		$day = $_GET['d'];
	} else {
		$day = date('j');
	}
	ymd_reset($year, $month, $day);
}
function mobile_schedule_print_week($user_id, $year, $month, $day) {
	mobile_schedule_print($user_id, $year, $month, $day);
	for ($ix=0; $ix<7; $ix++) {
		$day++;
		ymd_reset($year, $month, $day);
		mobile_schedule_print($user_id, $year, $month, $day);
	}
}
function mobile_schedule_print($user_id, $year, $month, $day, $collapsed="false") {
	$datestr = date('Y-m-d', mktime(0, 0, 0, $month, $day, $year));
	$sql = "select * from v_schedule where c_date = '".$datestr."' and c_delete = 0";
	if ($user_id != $_SESSION['mobile_login_id']) {
		$sql .= " and c_privacy = 0";
	}
	$sql .= " and id_account = '".$user_id."'";
	$sql .= " order by c_time1";
	$rs = my_mysql_query($sql);
?>
		<div data-role="collapsible" data-collapsed="<?= $collapsed ?>">
			<h1><?php mobile_ymd_print($year, $month, $day); ?></h1>
<?php
	while ($rec = mysql_fetch_array($rs)) {
		if ($rec['c_time1']."" <> "" || $rec['c_time2']."" <> "") {
			echo '<span style="color:red">'.sch_time_format($rec['c_time1'], $rec['c_time2'], True).'</span> ';
		}
		if ($rec['c_iconImage'] <> '') {
			echo mobile_calendar_icon($rec['c_iconImage']);
		}
		echo mobile_text($rec['c_subject'], 'font-weight:bold;');
		echo mobile_calendar_memo($rec['c_memo']);
	}
?>
		</div>
<?php
}
function mobile_ymd_print($year, $month, $day) {
	$week = ymd_week($year, $month, $day);
	if ($holiday = holiday_check($year, $month, $day)) {
		$color = 'red';
		$week = '('.$week.') '.$holiday;
	} else {
		if ($week == '日') {
			$color = 'red';
			$week = '('.$week.')';
		} elseif ($week == '土') {
			$color = 'blue';
			$week = '('.$week.')';
		} else {
			$color = 'black';
			$week = '('.$week.')';
		}
	}
	echo '<span style="color:'.$color.'">'.$month.' / '.$day.' '.$week.'</span>';
}
function mobile_week_move($year, $month, $day) {
?>
	<div class="ui-grid-b">
	<div class="ui-block-a">
		<a href="?y=<?=$year?>&m=<?=$month?>&d=<?=$day-7?>&id=<?= $_GET['id'] ?>" data-role="button" data-icon="" rel="external">前週</a>
	</div>
	<div class="ui-block-b">
		<a href="?y=<?=$year?>&m=<?=$month?>&d=<?=$day+7?>&id=<?= $_GET['id'] ?>" data-role="button" data-icon="" rel="external">次週</a>
	</div>
	<div class="ui-block-c">
		<a href="?y=<?=date('Y')?>&m=<?=date('n')?>&d=<?=date('j')?>&id=<?= $_GET['id'] ?>" data-role="button" data-icon="" rel="external">今週</a>
	</div>
	</div>
<?php
}
?>
