<?php
require_once("../mobile_base.php");

// 現在年+-5年を表示範囲とする
$maxYear = date("Y", strtotime("+5 year"));
$minYear = date("Y", strtotime("-5 year"));

// チームIDの取得
$teamID = checkTeamId();

// チーム名取得
$teamNameResult = pg_query_params($conn, "SELECT tmname FROM teammt WHERE delkn = FALSE AND teamid = $1", array($teamID));
if ($teamNameResult && pg_num_rows($teamNameResult) == 1) {
    $teamNameArray = pg_fetch_array($teamNameResult, 0);
    $teamName = $teamNameArray[0];
}

$pageTitle = "スケジュール";

// 日付パラメータ取得
if ($_GET["date"]
        && preg_match("/^\d{8}$/", $_GET["date"])
        && checkdate(substr($_GET["date"], 4, 2), substr($_GET["date"], 6, 2), substr($_GET["date"], 0, 4))) {
    $requestYear = substr($_GET["date"], 0, 4);
    if ($requestYear > $maxYear) {
        $target_ymd = $maxYear . "1231";
    } else if ($requestYear < $minYear) {
        $target_ymd = $minYear . "0101";
    } else {
        $target_ymd = $_GET["date"];
    }
} else {
    $target_ymd = date("Ymd");
}

$target_day = mktime(0, 0, 0, substr($target_ymd, 4, 2), substr($target_ymd, 6, 2), substr($target_ymd, 0, 4));
$arr = getdate($target_day);
$ww = $arr["wday"]; // 指定日の曜日番号
$sunday = $target_day - $ww * 24 * 60 * 60;  // その週の日曜日のUNIX TIME
$saturday = $sunday + 6 * 24 * 60 * 60;      // その週の土曜日のUNIX TIME
$prev_week = $target_day - 7 * 24 * 60 * 60; // 指定日の先週のUNIX TIME
$next_week = $target_day + 7 * 24 * 60 * 60; // 指定日の次週のUNIX TIME

// 対象週の予定取得
$sql = <<<EOD
    SELECT
        to_char(eventdate, 'YYYY/MM/DD') AS eventdate_ymd
        ,to_char(startdate, 'HH24:MI') AS startdate_hm
        ,to_char(enddate, 'HH24:MI') AS enddate_hm
        , *
      FROM
        shiadt
      WHERE
        deletion = FALSE
        AND teamid = $1
        AND eventdate >= $2
        AND eventdate <= $3
      ORDER BY
        eventdate
        , startdate
EOD;

$result = pg_query_params($conn, $sql, array($teamID, date("Y/m/d", $sunday), date("Y/m/d", $saturday)));
if ($result && pg_num_rows($result) > 0) {
    $resultList = pg_fetch_all($result);
} else {
    $resultList = array();
}

// 日付ごとの配列作成
$scheduleArray = array();
for ($i = 0; $i < 7; $i++) {
    $scheduleArray[date("Y/m/d", $sunday + $i * 24 * 60 * 60)] = array();
}
// DBから取得したスケジュールを日付ごとの配列に格納
foreach ($resultList as $schedule) {
    $scheduleArray[$schedule["eventdate_ymd"]][] = $schedule;
}

$weekStr = getweek();

?>
<?=getHtmlHeader($teamName)?>

<div align="center"><br><font size="+1" color="#666666"><?=h($teamName)?></font><br><br></div>

<hr noshade size="1">

<div align="center"><font size="-1" color="#666666"><?=h($pageTitle)?></font></div>
<hr noshade size="1">

<font size="-1">0.<a href="index.php?t=<?=$teamID?>" accesskey="0">トップページ</a></font><br>
<br>

<?if (date("Y", $prev_week) >= $minYear) {?>
    <font size="-1">1.<a href="schedule.php?t=<?=$teamID?>&date=<?=date("Ymd", $prev_week)?>" accesskey="1">前の週</a></font><br>
<?}?>
<?if (date("Y", $next_week) <= $maxYear) {?>
    <font size="-1">2.<a href="schedule.php?t=<?=$teamID?>&date=<?=date("Ymd", $next_week)?>" accesskey="2">次の週</a></font><br>
<?}?>

<?$weekNo = 0;?>
<?foreach ($scheduleArray as $date => $scheduleList) {?>
    <hr noshade size="1">
    <hr noshade size="1">
    <?=$date?>(<?=$weekStr[$weekNo++]?>)<br>
    <?for ($i = 0; $i < count($scheduleList); $i++) {?>
        <?$schedule = $scheduleList[$i];?>
        <?=h($schedule["meetname"])?><br>
        <?=h($schedule["area"])?><br>
        <?=h($schedule["startdate_hm"])?>〜<?=h($schedule["enddate_hm"])?><br>
        <?if ($schedule["section"] != 1 && $schedule["section"] != 5) {?>
            <?=h($schedule["sideteamname"])?><br>
        <?}?>
        <?=h($schedule["comment"])?>
        <?if ($i != count($scheduleList) - 1) {?>
            <hr noshade size="1">
        <?}?>
    <?}?>
<?}?>

<hr noshade size="1">
<hr noshade size="1">
<?if (date("Y", $prev_week) >= $minYear) {?>
    <font size="-1">1.<a href="schedule.php?t=<?=$teamID?>&date=<?=date("Ymd", $prev_week)?>" accesskey="1">前の週</a></font><br>
<?}?>
<?if (date("Y", $next_week) <= $maxYear) {?>
    <font size="-1">2.<a href="schedule.php?t=<?=$teamID?>&date=<?=date("Ymd", $next_week)?>" accesskey="2">次の週</a></font><br>
<?}?>
<br>
<font size="-1">0.<a href="index.php?t=<?=$teamID?>" accesskey="0">トップページ</a></font><br>

<?=getHtmlFooter()?>
<?outputBuffer();?>