<?php

define('PAGE_JOBS', 30);

include($phpbb_root_path . 'rpg/function_rpg.'.$phpEx);


if ( !defined('IN_PHPBB') )
{
	die("Hacking attempt");
	exit;
}

$returnurl = '<br /><br /><a href="rpg.php?action=extra">返回上一頁</a>';

$template->set_filenames(array(
	'menu' => 'rpg_header.tpl',
	'body' => 'rpg_joinjobs.tpl')
);

// 取得個人資料
$profiledata = get_userdata(intval($userdata['uid']));

if (isset($HTTP_GET_VARS['cancel']) || isset($HTTP_POST_VARS['cancel']))
{
	$sql = 'DELETE FROM xoops_pbb_rpg_joinjobs WHERE uid = ' . $profiledata['uid'];
	if ( !$db->sql_query($sql) )
	{
		message_die(GENERAL_ERROR, '取消排班失敗!!', '', __LINE__, __FILE__, $sql);
	}
}


$which_store = intval(( isset($HTTP_GET_VARS['store']) ) ? $HTTP_GET_VARS['store'] : $HTTP_POST_VARS['store']);
$start_join = intval(( isset($HTTP_GET_VARS['start']) ) ? $HTTP_GET_VARS['start'] : $HTTP_POST_VARS['start']);

$joinjobslist = '<span class="gensmall"><a href="rpg.php?action=joinjobs&cancel=1"><b>取消排班</b></a>&nbsp;&nbsp;::&nbsp;&nbsp;' .
		($which_store != 1 ? '<a href="rpg.php?action=joinjobs&store=1&start=0">黑暗的武器店</a>' : '黑暗的武器店') .
		'&nbsp;&nbsp;' .
		($which_store != 2 ? '<a href="rpg.php?action=joinjobs&store=2&start=0">沒光的防具店</a>' : '沒光的防具店') .
		'&nbsp;&nbsp;' .
		($which_store != 3 ? '<a href="rpg.php?action=joinjobs&store=3&start=0">魔法技能學園</a>' : '魔法技能學園') .
		'&nbsp;&nbsp;' .
		($which_store != 0 ? '<a href="rpg.php?action=joinjobs&store=0&start=0">水怪的道具店</a>' : '水怪的道具店') .
		'</span><br />';

// 計算總頁次
$sql = 'SELECT COUNT(*) as join_count FROM xoops_pbb_rpg_joinjobs WHERE in_which_store = ' . $which_store;
if ( $result = $db->sql_query($sql) )
{
	$row = $db->sql_fetchrow($result);
	$page_count = floor(($row['join_count'] - 1) / PAGE_JOBS) + 1;
}

$sql = 'SELECT w.job_name, u.uname, j.join_time FROM xoops_pbb_rpg_joinjobs j, xoops_users u, xoops_pbb_rpg_jobs w
	WHERE j.in_which_store = ' . $which_store . ' AND w.in_which_store = ' . $which_store .
	' AND j.uid = u.uid AND j.job_id = w.job_id ORDER BY j.job_id, j.join_time LIMIT ' . $start_join . ', ' . PAGE_JOBS;
if ( !($result = $db->sql_query($sql)) )
{
	message_die(GENERAL_ERROR, '無法取得排班列表!!', '', __LINE__, __FILE__, $sql);
}
if ( $row = $db->sql_fetchrow($result) )
{
	if ($page_count > 0)
	{
		$joinjobslist .= '<span class="gensmall"><b>頁次:';
		for ($i = 1; $i <= $page_count ; $i++)
		{
			if ($i == (floor($start_join / PAGE_JOBS) + 1) )
			{
				$joinjobslist .= ' ' . $i;
			}
			else
			{
				$joinjobslist .= ' <a href="rpg.php?action=joinjobs&store=' . $which_store . '&start=' . (($i - 1) * PAGE_JOBS) . '">' . $i . '</a>';
			}
		}
		$joinjobslist .= '</span>';
	}
	$i = $start_join + 1;
	$joinjobslist .= '<table width="50%" cellpadding="3" cellspacing="1" border="0" class="forumline">
		<tr><td align="center" class="catLeft"><b><span class="gen">職稱</span></b></td>
		<td align="center" class="catLeft"><b><span class="gen">#</span></b></td>
		<td align="center" class="catLeft"><b><span class="gen">工作人員</span></b></td>
		<td align="center" class="catLeft"><b><span class="gen">加入時間</span></b></td></tr>';
	$old_jobname = '';
	do
	{
		$joinjobslist .= '<tr>
		<td class="row3">
		<span class="gensmall">';
		if ($old_jobname != $row['job_name'])
		{
			$i = 1;
			$old_jobname = $row['job_name'];
			$joinjobslist .= $old_jobname;
		}
		$joinjobslist .= '</span>
			<td align="center"><span class="gensmall">' . $i . '</span></td>
			<td><span class="gensmall">' . $row['uname'] . '</span></td>
			<td align="center"><span class="gensmall">' . (create_date($userdata['user_dateformat'], $row['join_time'], $board_config['board_timezone'])) . '</span></td>
		</tr>';
		$i++;
	}
	while ( $row = $db->sql_fetchrow($result) );
	$joinjobslist .= '';
}
else
{
	$joinjobslist .= '<span class="gensmall">目前沒有任何人加入排班!!</span>';
}

// 產生資料頁面
$page_title = "水怪大陸 RPG 首頁";
include($phpbb_root_path . 'includes/page_header.'.$phpEx);

$template->assign_vars(array(
	'USERNAME' => $profiledata['uname'],
	'AVATAR_IMG' => GetAvatar($profiledata),
	'RPG_JOIN_JOBS_LIST' => $joinjobslist,
	'L_NOWMENU' => '打工排班列表') );

$template->pparse('menu');
$template->pparse('body');

include($phpbb_root_path . 'includes/page_tail.'.$phpEx);

?>