<?php
include("require.php");
if(!isset($_COOKIE["core_user_name"]))
  jump("login.php?action=login&last=".$last);
submitfilter($_GET['bid']);
$phpcolor->table="bar_user";
$phpcolor->field="*";
$phpcolor->where="uid = '".$userv['id']."' and rank = 2";
$count=$phpcolor->exist(2);
$phpcolor->table="apply_master";
$phpcolor->where="username = '".$_COOKIE["core_user_name"]."' and type=1";
$count1=$phpcolor->exist(2);
if($count+$count1 >= $core_ubarmastercount)
  jump(htmlurl($core_url,"list",$_GET['bid'],0,$core_rewrite));
$phpcolor->table="bar";
$phpcolor->field="barname";
$phpcolor->where="id = '".$_GET['bid']."'";
if(isset($_GET['action']) && $_GET['action'] == "submit")
{
  if($_POST['reason']=="" || $_POST['email']=="")
    jump("error.php?id=1&last=apply.php?bid=".$_GET['bid']); 
  if(!$bar=$phpcolor->exist(1))
	jump("index.php");
  $phpcolor->table="bar_user";
  $phpcolor->field="*";
  $phpcolor->where="bid = '".$_GET['bid']."' and uid = '".$userv['id']."' and rank = 2";
  $count=$phpcolor->exist(2);
  if($phpcolor->exist(2))
    jump(htmlurl($core_url,"list",$_GET['bid'],0,$core_rewrite));
  if($count >= $core_barmastercount)
    jump(htmlurl($core_url,"list",$_GET['bid'],0,$core_rewrite));
  $phpcolor->table="apply_master";
  $phpcolor->field="*";
  $phpcolor->where="username='".$_COOKIE['core_user_name']."' and barname='".$bar['barname']."'";
  if($phpcolor->exist(1))
	jump("error.php?id=9&last=".htmlurl($core_url,"list",$_GET['bid'],0,$core_rewrite));
  //×Ö·û¹ýÂË
  $_POST['reason']=autolink($_POST['reason']);
  $time=date('Y-m-d H:i:s',strtotime(date('Y-m-d H:i:s'))+$core_time*3600);
  $phpcolor->table="apply_master";
  $phpcolor->field="username, barname, why, time, email, type";
  $phpcolor->where="'".$_COOKIE['core_user_name']."', '".$bar['barname']."', '".$_POST['reason']."', '".$time."', '".$_POST['email']."', 1";
  $phpcolor->write();
  jump(htmlurl($core_url,"list",$_GET['bid'],0,$core_rewrite));
}
if($bar=$phpcolor->exist(1))
{
  $phpcolor->table="apply_master";
  $phpcolor->field="*";
  $phpcolor->where="username='".$_COOKIE['core_user_name']."' and barname='".$bar['barname']."'";
  if($phpcolor->exist(1))
	jump("error.php?id=9&last=".htmlurl($core_url,"list",$_GET['bid'],0,$core_rewrite)); 
  $phpcolor->table="bar_user";
  $phpcolor->field="*";
  $phpcolor->where="bid = '".$_GET['bid']."' and uid = '".$userv['id']."' and rank = 2";
  $count=$phpcolor->exist(2);
  if($phpcolor->exist(2))
    jump(htmlurl($core_url,"list",$_GET['bid'],0,$core_rewrite));
  if($count >= $core_barmastercount)
	jump(htmlurl($core_url,"list",$_GET['bid'],0,$core_rewrite));
  $tpl -> assign("barid", $_GET['bid']);
  $tpl -> assign("barname", $bar['barname']);
  $tpl -> assign("core_header_title", $bar['barname']."°ÉÉêÇë°ÉÖ÷ - ");
  $tpl -> display("$core_style/apply.htm");
}
else
  jump("index.php");
?>