<?php include('inc/header.php'); ?>
<?php

	echo $lang['lord_titel'];

	$vote_level = 60;

	odbc_exec($odbc_connect, 'USE [' . $_CONFIG['db_databases']['chr'] . ']');
	$select1 = odbc_exec($odbc_connect, 'SELECT * FROM [CHARACTER_TBL] WHERE [playerslot] = \'0\' AND [account] = \''.$_SESSION['user'].'\'');
	$select2 = odbc_exec($odbc_connect, 'SELECT * FROM [CHARACTER_TBL] WHERE [playerslot] = \'1\' AND [account] = \''.$_SESSION['user'].'\'');
	$select3 = odbc_exec($odbc_connect, 'SELECT * FROM [CHARACTER_TBL] WHERE [playerslot] = \'2\' AND [account] = \''.$_SESSION['user'].'\'');
	$char1 = odbc_fetch_array($select1);
	$char2 = odbc_fetch_array($select2);
	$char3 = odbc_fetch_array($select3);
	
	if( $char1['m_nLevel'] >= $vote_level || $char2['m_nLevel'] >= $vote_level || $char3['m_nLevel'] >= $vote_level ) {
		odbc_exec($odbc_connect, 'USE [' . $_CONFIG['db_databases']['chr'] . ']');
		$lordquery = odbc_exec($odbc_connect, 'SELECT TOP 1 m_szName FROM [tblLord] a LEFT JOIN CHARACTER_TBL b ON RIGHT(\'0000000\' + CONVERT(VARCHAR(7),a.idLord),7)= b.m_idPlayer ORDER BY s_date DESC');
		if(odbc_result($lordquery, 'm_szName') != '')
			$lord = odbc_result($lordquery, 'm_szName');
		else
			$lord = '-';
		$ret1 = odbc_exec($odbc_connect, 'SELECT * FROM [tblLordElection] ORDER BY [idElection] DESC');
		$result1 = odbc_fetch_array($ret1);
		
		function GetIDPlayer($id){
			if( strlen($id) == 1 )
				return '000000'.$id;
			elseif( strlen($id) == 2 )
				return '00000'.$id;
			elseif( strlen($id) == 3 )
				return '0000'.$id;
			elseif( strlen($id) == 4 )
				return '000'.$id;
			elseif( strlen($id) == 5 )
				return '00'.$id;
			elseif( strlen($id) == 6 )
				return '0'.$id;
			elseif( strlen($id) == 7 )
				return $id;
		}

		if($_GET['do'] == 'waehlen'){
			$num = $result1['idElection'];
			$ret2 = odbc_exec($odbc_connect, 'SELECT * FROM [tblLordCandidates] WHERE [idElection] =\''.$num.'\'');
			if(isset($_GET['vote'])){
				$name = mssql_escape_string($_GET['vote']);
				$retval = odbc_exec($odbc_connect, 'SELECT * FROM [CHARACTER_TBL] WHERE [m_szName] =\''.$name.'\'');
				$check = odbc_exec($odbc_connect, 'SELECT COUNT(*) AS cnt FROM [tblLordCandidates] WHERE [idElection] =\''.$num.'\' AND [idPlayer] =\''.intval(odbc_result($retval, 'm_idPlayer')).'\'');
				if( odbc_result($check, 'cnt') == 0 ){
					echo '<div class="fail">'.$lang['lord_no_candidate'].'</div>';
				}else{
					$check1 = odbc_exec($odbc_connect, 'SELECT COUNT(*) AS cnt FROM [tblLordElector] WHERE [idElection] =\''.$num.'\' AND [idElector] =\''.intval($char1['m_idPlayer']).'\'');
					$check2 = odbc_exec($odbc_connect, 'SELECT COUNT(*) AS cnt FROM [tblLordElector] WHERE [idElection] =\''.$num.'\' AND [idElector] =\''.intval($char2['m_idPlayer']).'\'');
					$check3 = odbc_exec($odbc_connect, 'SELECT COUNT(*) AS cnt FROM [tblLordElector] WHERE [idElection] =\''.$num.'\' AND [idElector] =\''.intval($char3['m_idPlayer']).'\'');
					if( odbc_result($check1, 'cnt') != 0 || odbc_result($check2, 'cnt') != 0 || odbc_result($check3, 'cnt') != 0 ){
						echo '<div class="fail">'.$lang['lord_already_vote'].'</div>';
					}else{
						if( odbc_result($select1, 'm_idPlayer') != '' )
							$mychar = $char1['m_idPlayer'];
						elseif( odbc_result($select2, 'm_idPlayer') != '' )
							$mychar = $char2['m_idPlayer'];
						elseif( odbc_result($select3, 'm_idPlayer') != '' )
							$mychar = $char3['m_idPlayer'];
						
						odbc_exec($odbc_connect, 'UPDATE [tblLordCandidates] SET [nVote] = ([nVote] + 1) WHERE [idElection] =\''.$num.'\' AND [idPlayer] =\''.intval(odbc_result($retval, 'm_idPlayer')).'\'');
						odbc_exec($odbc_connect, 'INSERT INTO [tblLordElector] ([nServer], [idElection], [idElector]) VALUES(\'1\', \''.$num.'\', \''.intval($mychar).'\')');
						echo '<div class="success">'.$lang['lord_success_vote'].'</div>';
					}
				}
			}
			echo $lang['lord_candidate_list'];
			echo '<table border="0" width="40%">
					<tr>
						<td><b>'.$lang['lord_candidate'].'</b></td>
						<td><b>'.$lang['lord_vote'].'</b</td>
						<td><b>'.$lang['lord_job'].'</b></td>
						<td><b>'.$lang['lord_level'].'</b></td>
					</tr>';
			while( $result2 = odbc_fetch_array($ret2) ){
				$select0 = odbc_exec($odbc_connect, 'SELECT * FROM [CHARACTER_TBL] WHERE [m_idPlayer] =\''.mssql_escape_string(GetIDPlayer($result2['idPlayer'])).'\'');
				echo '<tr class="row">
						<td><a href="lord.php?do=waehlen&vote='.odbc_result($select0, 'm_szName').'">'.odbc_result($select0, 'm_szName').'</a></td>
						<td>'.$result2['nVote'].'</td>
						<td>'.$job[odbc_result($select0, 'm_nJob')].'</td>
						<td>'.odbc_result($select0, 'm_nLevel').'</td>
					</tr>';
			}
			echo '</table>';
			echo '<br><a href="lord.php">'.$lang['panel_back'].'</a>';
		}else{
			echo '<table border="0">
					<tr>
						<td><b>'.$lang['lord_current'].':</b></td>
						<td>'.$lord.'</td>
					</tr><tr>
						<td><b>'.$lang['lord_next'].':</b></td>
						<td>'.date('Y-m-d H:i', strtotime($result1['szBegin'])).'</td>
					</tr>
					<tr>
						<td><b>'.$lang['lord_count'].':</b></td>
						<td>'.($result1['idElection'] - 1).'</td>
					</tr>
				</table>';
			if( date('Y-m-d H:i:s') >= date('Y-m-d H:i', strtotime($result1['szBegin'])) )
				echo '<br><a href="lord.php?do=waehlen"  title="'.$lang['lord_go_vote'].'" rel="tooltip">'.$lang['lord_go_vote'].'</a>';
		}
	} else {
		echo '<div class="fail">'.$lang['lord_vote_level'].'<br>'.$lang['lord_vote_level_min'].'</div>';
	}
?>
<?php include('inc/footer.php'); ?>