<?php
/*
@author waychar
@技术支持 150572314@qq.com
@copyright 2018 waychar.cn 声明：本代码可以用于学习交流使用及要商业化用途
*/
require_once 'model/race.php';

$race = new race();
$id = get($_GET['id']);
$type = get($_GET['type']);
$request_type=get($_GET['request_type']);

if ($action == 'list' || $action == '') {
    admin_check();
    include 'view/admin_top.php';
    include 'view/race_home.php';
    exit();
} 
elseif($action == 'race_list')
{
     admin_check();
    $sql_race_list = "
    select add_time, click_point, contant,display, contant_c, end_times, id, photo_link, place, race_title, sign_start_time,  start_time from race order by id desc limit 0,100";
    $result = $race->get_data($sql_race_list);   

    include 'view/admin_top.php';
    include 'view/race_top.php';
    include 'view/race_list.php';
    exit();  
}
elseif ($action == 'edit') {
    admin_check();
    if ($id) {
        $sql_user = "select * from race where id = $id";
        $result = $race->get_data($sql_user);
        include 'view/admin_top.php';
        include 'view/race_top.php';
        include 'view/race_form.php';
        exit();
    }
}
elseif ($action == 'reg')
{
    $group_id = get($_GET['group_id']);
    $race_id = get($_GET['race_id']);
    //check_login
    if($_COOKIE['id'])
    {
            //判断是否为团报，is_team is 0 确认 ； is_team is 1 否
        $is_team = $race->get_one("select is_team from race_group where id = $group_id");
        if($is_team=='0' && !$_GET['team_id'])
        {
            go("index.php?c=race&action=team_select&race_id=$race_id&group_id=$group_id");
        }
        //is login
        //reged
        $reg_id = $race->check_reg($race_id);
        if ($reg_id)
        {
            go("?c=race&action=message&reg_id=".$reg_id."&race_id=".$race_id."&group_id=".$group_id);
        }

        $sql = "select * from race_reg where user_id = ".$_COOKIE['id']." and race_id=$race_id order by id desc limit 0,1 ";
        $result = $race->get_data($sql);
        
        //input 
        $input_list = $race->get_input($race_id);
        //get race_age
        $age_result = $race->get_age($group_id);
        $s_age= $age_result[0]['s_age'];
        $b_age= $age_result[0]['b_age'];

        $p_update = $race->get_p_update($group_id);
        $title="填写个人信息";
        $back_url = "index.php?c=race&action=race_msg&race_id=$race_id";
        include 'view'.$mobi_root.'/top.php';
        include 'view'.$mobi_root.'/race_reg.php';
        include 'view'.$mobi_root.'/bottom.php';
        exit();
    }
    else
    {
        //not login
       go("index.php?c=account&action=reg&href=".urlencode("index.php?c=race&action=reg&group_id=".$group_id."&race_id=".$race_id));
   }

}
elseif($action == 'my_reg_msg')
{
    $title="报名信息";
    $back_url = "index.php?c=home&action=index";
    $race_id = get($_GET['race_id']);
    $input_data=$race->get_input_data($race_id);
    $input_list = $race->get_input($race_id,$input_data);
    $result = $race->get_reg_msg($race_id);
    include 'view'.$mobi_root.'/top.php';
    include 'view'.$mobi_root.'/race_reg.php';
    include 'view'.$mobi_root.'/bottom.php';
}elseif ($action == 'add') {
    $title="填写活动资料";
    $back_url = "index.php";
    $other_button = " <button class='button pull-right' onclick='submit()' type=submit>发布</button>";
    include 'view/admin_top.php';
    if(check_mobi()==true)
    {
        include 'view/mobi/race_form.php'; 
    }
    else{
        include 'view/race_form.php'; 
    }
    include 'view/bottom.php';
    exit();

}  
elseif ($action == 'reset_pwd') {
    if ($id) {
        $password_o = $user->getPasswordO();
        $password = md5($password_o);
        $sql = "update w_user set password_o = '$password_o',password = '$password' where id = $id";
        $user->query($sql);
        go("?action=edit&id=$id");
    }
    exit();
}elseif($action == "race_msg")
{
    if($id)
    {
        $sql = "select * from race where id = $id";
        $result = $race->get_data($sql);
        $sql_race_group_result = "select id,title ,price,mount,reg_mount,distance,is_team from race_group where race_id = ".$id;

        $race_group_result = $race->get_data($sql_race_group_result);
        $img = $race->get_images($id);

        $title = $result[0]['race_title'];
        $back_url = "index.php?c=race&action=race_q_list";
        include 'view'.$mobi_root.'/top.php';
        include 'view'.$mobi_root.'/race_msg.php';
        include 'view'.$mobi_root.'/bottom.php';
        exit();
    }
}
elseif ($action == "race_edit") {
    if ($id) {
        $sql = "
        UPDATE race SET race_title='".get($_REQUEST['race_title'])."',
        start_time='".get($_REQUEST['start_time'])."',
        end_times='".get($_REQUEST['end_times'])."',
        sign_start_time='".get($_REQUEST['sign_start_time'])."',
        place='".get($_REQUEST['place'])."',
        contant='".get($_REQUEST['contant'])."',
        photo_link='".get($_REQUEST['photo_link'])."',
        contant_c='".get($_REQUEST['contant_c'])."',
        sign_end_time='".get($_REQUEST['sign_end_time'])."'
        WHERE id = $id";
        $race->query($sql);
        go("index.php?c=race&action=edit&id=".$id);
    }
    exit();
} elseif ($action == "race_add")
{
    admin_check();
    $race_title = get($_REQUEST['race_title']);
    $start_time = get($_REQUEST['start_time']);
    $end_times = get($_REQUEST['end_times']);
    $sign_start_time = get($_REQUEST['sign_start_time']);
    $sign_end_time = get($_REQUEST['sign_end_time']);
    $place = get($_REQUEST['place']);
    $click_point = 1;
    $photo_link = get($_REQUEST['photo_link']);
    $contant_c = get($_REQUEST['contant_c_texteare']);
    $add_time = date("Y-m-d H:i:s", time());
    $sql = "INSERT INTO `race` 
    ( 
    `race_title`, 
    `start_time`, 
    `end_times`, 
    `sign_start_time`,
    `place`,
    `contant`, 
    `click_point`, 
    `photo_link`, 
    `contant_c`,
    `add_time`,
    `sign_end_time`
    ) VALUES (
    '" . $race_title . "',
    '" . $start_time . "',
    '" . $end_times . "',
    '" . $sign_start_time . "',
    '" . $place . "',
    '" . $contant . "',
    '" . $click_point . "',
    '" . $photo_link . "',
    '" . $contant_c . "',
    '" . $add_time . "',
    '" . $sign_end_time . "')";
					//die($sql);
    $race->query($sql);
    $lastid = $race->getLastId();
    go("index.php?c=update&action=update&race_id=$lastid&request_type=add");
    exit();
}
elseif ($action=='race_group_edit')
{
    admin_check();
    if($id)
    {
        $sql ="select * from race_group where race_id = ".$id;
        $result = $race->get_data($sql);
        include 'view/admin_top.php';
        if(!$request_type)
        {
            include 'view/race_top.php';
        }
        include 'view/race_group_form.php';
    }
    exit();
}
elseif($action =='race_group_add')
{
    admin_check();
    $sql = "insert into race_group (
        title,price,mount,distance,audi,race_id,p_update,race_line,is_team,team_check,team_mount
    ) VALUES ('".get($_POST['title'])."',
    '".get($_POST['price'])."',
    '".get($_POST['mount'])."',
    '".get($_POST['distance'])."',
    '".get($_POST['audi'])."',
    '".get($_GET['race_id'])."',
    '".get($_POST['p_update'])."',
    '".get($_POST['race_line'])."',
    '".get($_POST['is_team'])."',
    '".get($_POST['team_mount'])."',
    '".get($_POST['team_mount'])."')";
    //die($sql);
    $race->query($sql);
    $race->update_low_price($race_id,get($_POST['price']));
    go("index.php?c=race&action=race_group_edit&id=".$_GET['race_id']."&request_type=$request_type");
}
elseif($action=='race_group_dll')
{
    admin_check();
    $id = get($_GET['id']);
    $s_age = get($_POST['s_age']);
    $b_age = get($_POST['b_age']);
    $race_line = get($_POST['race_line']);
    $race_id = get($_GET['race_id']);
    $is_team = get($_POST['is_team']);
    $team_check = get($_POST['team_check']);
    $team_mount = get($_POST['team_mount']);
    if(!$s_age)
    {
    	$s_age = '0';
    }
    
    if(!$b_age)
    {
    	$b_age = '99';
    }  
    $sql = "
    update race_group set title = '".get($_POST['title'])."',distance = '".get($_POST['distance'])."',audi = '".get($_POST['audi'])."',price='".get($_POST['price'])."',mount='".get($_POST['mount'])."',s_age='$s_age',b_age='$b_age',is_team='$is_team',race_line='$race_line',team_check='$team_check',team_mount='$team_mount',p_update='".get($_POST['p_update'])."' where id = $id";
    //die($sql);
    $race->query($sql);
    $race->update_low_price($race_id,get($_POST['price']));
    go("index.php?c=race&action=race_group_edit&id=".$_GET['race_id']."&request_type=$request_type");
}
elseif ($action=='race_reg_add')
{
    $born=get($_POST['born']);
    $cert_heal_img=get($_POST['cert_heal_img']);
    $cert_img=get($_POST['cert_img']);
    $city=get($_POST['city']);
    $edu=get($_POST['edu']);
    if (!$edu) $edu=0;
    $email=get($_POST['email']);
    $idcard=get($_POST['idcard']);
    $idcard_type=get($_POST['idcard_type']);
    $address=get($_POST['address']);
    $j_address=get($_POST['j_address']);
    $j_realname=get($_POST['j_realname']);
    $j_rel=get($_POST['j_rel']);
    $j_tel=get($_POST['j_tel']);
    $race_id=get($_GET['race_id']);
    $realname=get($_POST['realname']);
    $sex=get($_POST['sex']);
    $shoe_size=get($_POST['shoe_size']);
    $size=get($_POST['size']);
    $tel=get($_POST['tel']);
    $user_id = $_COOKIE['id'];
    $xx=get($_POST['xx']);
    $finish_time=get($_REQUEST['finish_time']);
    if (!$xx) $xx=0;
    $group_id=get($_REQUEST['group_id']);
    $q1=1;
    $q2=1;
    $cert_img = get($_REQUEST['cert_img']);

    $team_id = get($_POST['team_id']);
	if(!$team_id)
	{
		$team_id = 0;
	}
    //die($team_id);

    if(!$size)
    {
        $size =1;
    }
    //check idcard
    $sql_c_idcar = "select id from race_reg where idcard = '$idcard' and race_id = $race_id";
    $resultc_idcar = $race->get_One($sql_c_idcar);
    if ($resultc_idcar) {
        //
     ?>
     <script language="JavaScript">
        alert('该身份证用户已经报名，请返回用其他身份证报名');
        history.go(-1);
    </script>

    <?php
    exit();
    }
    $sql = "INSERT INTO `race_reg` (`race_id`, `user_id`, `realname`, `sex`, `tel`, `xx`, `idcard_type`, `idcard`, `size`, `j_realname`, `j_tel`, `born`, `shoe_size`, `email`, `address`, `j_address`, `j_rel`, `city`, `cert_img`, `edu`, `cert_heal_img`, `group_id`, `q1`, `q2`,`race_check`,`race_check_time`,`finish_time`,`team_id`) VALUES ('$race_id', '$user_id', '$realname', '$sex', '$tel', '$xx', '$idcard_type', '$idcard', '$size', '$j_realname', '$j_tel', '$born', '$shoe_size', '$email', '$address', '$j_address', '$j_rel', '$city', '$cert_img', '$edu', '$cert_heal_img',$group_id,$q1,$q2,'0','0000-00-00 00:00:00','$finish_time','$team_id')";
   	//die($sql);
	$race->query($sql);
    $sql_last_id = "select id from race_reg where user_id = $user_id  and race_id = $race_id  and group_id = $group_id ";


    //团队报名人数累计
    if($team_id)
    {
        $sql_add_mount_team= "update race_team set mount = mount +1 where id = $team_id";
        //die($sql_add_mount_team);
        $race->query($sql_add_mount_team);
    }

    $last_id = $race->get_one($sql_last_id);

        //input data

        //get input names
    $sql_input_names = "select id,input_name from race_input where race_id = $race_id and is_del = 0";
    $result_input_names = $race->get_data($sql_input_names);

    if($result_input_names)
    {
        	//get values
       foreach($result_input_names as $row)
       {
          $input_name = $row['input_name'];
          $values = get($_REQUEST[$input_name]);
          $race->input_data_update($values,$input_name,$race_id);
      }
    }

        // update race_group
        //$sql_update_group ="update race_group set ";
    $sql_group = "select * from race_group where id = $group_id";
    $result_group = $race->get_data($sql_group);

    $group_price = $result_group[0]['price'];
    $group_audi = $result_group[0]['audi'];

        // no price and no audi
        //1 bushen 2xuyaoshen
        //1未审核 2审核未付费 3审核已付费 4 已付费未审核
    if($group_price == 0 and $group_audi == 2)
    {
            //yifu weishen
        $sql = "update race_group set reg_mount = reg_mount+1 , payed_mount = payed_mount +1 where id = $group_id";
        $sql_pay = "insert into race_reg_pay  (race_id,	user_id	,group_id	,pay_type	,state,	money_value	,add_time,reg_id,openid) values ($race_id,$user_id,$group_id,'1',2,'0','".get_now()."',$last_id,'')";
        $sql_reg_update = "update race_reg set state = 4 where id = $last_id";
    }
    elseif($group_price >0 and $group_audi == 1)
    {
            //weifu yishen
        $sql = "update race_group set reg_mount = reg_mount+1 where id = $group_id";
        $sql_pay = "insert into race_reg_pay  (race_id,	user_id	,group_id	,pay_type	,state,	money_value	,add_time,reg_id,openid) values ($race_id,$user_id,$group_id,'1',1,'$group_price','".get_now()."',$last_id,'')";
        $sql_reg_update = "update race_reg set state = 2 where id = $last_id";
    }
    elseif($group_price ==0 and $group_audi == 1)
    {
            //yifu yishen
        $sql = "update race_group set reg_mount = reg_mount+1 where id = $group_id";
        $sql_pay = "insert into race_reg_pay  (race_id,	user_id	,group_id	,pay_type	,state,	money_value	,add_time,reg_id,openid) values ($race_id,$user_id,$group_id,'1',1,'$group_price','".get_now()."',$last_id,'')";
        $sql_reg_update = "update race_reg set state = 3 where id = $last_id";
    }
    elseif ($group_price >0 and $group_audi ==2)
    {
            //weifu weishen
        $sql = "update race_group set reg_mount = reg_mount+1 where id = $group_id";
        $sql_pay = "insert into race_reg_pay  (race_id,	user_id	,group_id	,pay_type	,state,	money_value	,add_time,reg_id,openid) values ($race_id,$user_id,$group_id,'1',1,'$group_price','".get_now()."',$last_id,'')";
        $sql_reg_update = "update race_reg set state = 1 where id = $last_id";
    }

        //add to pay
    $race->query($sql);
    $race->query($sql_pay);
    $race->query($sql_reg_update);
    go("?c=race&action=message&reg_id=".$last_id."&race_id=".$race_id."&group_id=".$group_id);
}
elseif($action =='message')
{
    $title="付款信息";
    $back_url = "index.php?c=home&action=index";
	if($_COOKIE['id'])
	{
       $reg_id =get($_GET['reg_id']);
       if(!$reg_id)
       {
         $reg_id = get($_GET['re_g_id']);
     }
     if($reg_id) {
         $sql = "select * from race_reg where id=$reg_id";
         $result = $race->get_data($sql);

         $race_id = $result[0]['race_id'];
         $group_id = $result[0]['group_id'];
         $race_group_msg = $race->get_data("select title,price from race_group where id = " . $group_id);
         $race_msg = $race->get_data("select race_title,start_time, end_times, sign_start_time,sign_end_time from race where id =" . $race_id);
			//pay messages 
         setcookie("reg_id",$reg_id);
         setcookie("group_id",$group_id);
         setcookie("race_id",$race_id);        
         setcookie("price",$race_group_msg[0]['price']);
         //echo $_COOKIE['reg_id'];
         include 'view'.$mobi_root.'/top.php';
         include 'view'.$mobi_root.'/race_reg_message.php';
         include 'view'.$mobi_root.'/bottom.php';
     }
     else
     {
         echo "对不起，没有报名信息，请返回。";
     }
 }
 else
 {
   echo "没有登陆,请<a href=index.php?c=account&action=login>登陆</a>或<a href='index.php?c=race&action=race_cert_search'>返回>></a> ";
}
exit();
}
elseif($action =="images_update") {
    $sql = "select * from race_images where race_id = $id";
    $result = $race->get_data($sql);
    include 'view/race_images_update.php';
    include 'view/bottom.php';
}
elseif($action=='race_q_list')
{


    $sql_count = "select count(id) from race where display = 1 ";
    $page_count = $race->get_one($sql_count);
    
    $page = get($_GET['page']);
    if($page=='')
    {
        $page = '1';
    };

    $page_size= 20;
    //page
    $limit= ($page-1)*$page_size;

    $sql = "select * from race where display = 1 order by start_time desc limit $limit,$page_size";
    $result = $race->get_data($sql);

    $next_page = $page+1;
    $prv_page = $page -1;
    $max_page = ceil($page_count/$page_size);
    if($page == '1')
    {
        $prv_page ='1';
    }

    if($page == $max_page)
    {
        $next_page=$page;
    }

    //die($sql);
    $title="赛事列表";
    $back_url = "index.php?c=home";
    include 'view'.$mobi_root.'/top.php';
    include 'view'.$mobi_root.'/race_q_list.php';
    include 'view'.$mobi_root.'/bottom.php';
}
elseif($action == 'race_hot')
{
    $sql = "select id,race_title,start_time,end_times,sign_start_time,sign_end_time,low_price,contant,click_point from race where display = 1 order by click_point desc limit 0,50";
    $result = $race->get_data($sql);
    include 'view/race_q_list.php';
}
elseif($action == 'race_new')
{
    $sql = "select id,race_title,start_time,end_times,sign_start_time,sign_end_time,low_price,contant,click_point from race where display = 1 order by id desc limit 0,50";
    $result = $race->get_data($sql);
    include 'view/race_q_list.php';
}
elseif($action == 'race_free')
{
    $sql = "select id,race_title,start_time,end_times,sign_start_time,sign_end_time,low_price,contant,click_point from race where display = 1 and low_price=0 order by id desc limit 0,50";
    $result = $race->get_data($sql);
    include 'view/race_q_list.php';
}
elseif($action == 'race_pay_check')
{
    check_session();
    $race_id = get($_REQUEST['race_id']);
    $group_id = get($_REQUEST['group_id']);
    $user_id = get($_COOKIE['id']);
    $md5 = md5($user_id."1");
    $md5check=get($_REQUEST['md5check']);
    $pay_type = get($_REQUEST['pay_type']);
    $reg_id =  get($_REQUEST['reg_id']);
    $state = 1;
    $money_value = get($_REQUEST['money_value']);
    $add_time = date("Y-m-d h:i:s", time());
    $sql = "select id from race_reg_pay where race_id = $race_id and user_id = $user_id";
    $check = $race->get_One($sql);

    if($md5 == $md5check)
    {
        if($check)
        {
            echo "same";
        }
        else
        {
            $sql = "insert into race_reg_pay  (race_id,	user_id	,group_id	,pay_type	,state,	money_value	,add_time,reg_id) values ($race_id,$user_id,$group_id,$pay_type,$state,'$money_value','$add_time',$reg_id)";
            $race->query($sql);
            $sql ="update race_reg set state = 2 where user_id =$user_id and race_id = $race_id";
            $race->query($sql);
            echo "ok";
        }
    }
    else
    {
        echo "f";
    }

}elseif ($action =="del")
{

    $sql = "select id from race_reg where race_id = $id";
    $result = $race->get_data($sql);

    if($result)
    {
        echo "已经有人报名了！<a href=?c=race>返回</a>";
    }
    else
    {
        $sql = "delete from race where id = $id";
        $race->query($sql);
        go("?c=race");
    }
}
elseif($action =="cert_check")
{
    $id_card = get($_POST['id_card']);
    $race_id = get($_POST['race_id']);
    $sql = "select id from race_reg where idcard = '$id_card' and race_id = $race_id";
   // echo $sql;
    //die();
    $result = $race->get_One($sql);
    if($result)
    {
    	go("index.php?c=race&action=race_cert_msg&regid=$result");
    }
    else
    {
        echo "对不起，没有报名信息，请返回。";
    }
}
elseif ($action =="race_cert_search")
{
    $race_id = get($_GET['race_id']);
    $option = $race->get_race_list_option();
    $title="证书查询";
    $back_url="index.php";
    include 'view/top.php';
    include 'view/race_cert_search.php';
    include 'view/bottom.php';
}
elseif ($action =='group_del')
{
    $race_id = get($_REQUEST['race_id']);
    if($id)
    {
        $sql = "delete from race_group where id = $id";
        $race->query($sql);
        go("index.php?c=race&action=race_group_edit&id=$race_id&request_type=$request_type");
    }
}
elseif($action =='race_reg_member_list')
{
	$excel = get($_GET['excel']);
    $idcard = get($_POST['idcard']);
    $user_id = get($_GET['user_id']);
    $team_id=get($_GET['team_id']);

	if($excel)
	{
		header("Content-type: application/vnd.ms-excel; charset=utf-8");
		header("Content-Disposition: attachment; filename=北回归线网站数据导出.xls");
		echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';	
	}
    $race_id = get($_GET['race_id']);
    $state = get($_POST['state']);
    if($state)
    {
		//1未审核 2审核未付费 3审核已付费 4 已付费未审核
      if($state == 1)
      {
         $sql_state = "and (state = 1 or state = 4)";
     }
     elseif($state == 2)
     {
         $sql_state = "and (state = 2 or state = 3)";	
     }	
          elseif($state == 3)
     {
         $sql_state = "and state =3 ";   
     }  
          elseif($state == 4)
     {
         $sql_state = "and state =4 "; 
     }  
          elseif($state == 5)
     {
         $sql_state = "and state =5 ";  
     }  



 }
    if($idcard)
    {
        $sql_id_card = " and idcard = '$idcard' ";
    }

    if($user_id)
    {
        $sql_user_id = " and user_id = $user_id";
    }

    if($team_id)
    {
        $sql_team_id = " and team_id = $team_id";
    }
     $sql = "select * from race_reg where race_id=$race_id $sql_state $sql_id_card $sql_user_id $sql_team_id and is_del = 0 order by state desc";
     $result = $race->get_data($sql);
    if(!$excel)
    {
       include 'view/admin_top.php';
       include 'view/race_top.php';
    }
    include 'view/race_reg_member_list.php';
}
elseif($action == 'pay_dll')
{
	$pay_set=get($_REQUEST['pay_set']); 
	$reg_array = explode('_',$pay_set);
	$reg_id= $reg_array[0];
	$group_id= $reg_array[1];
    $openid=$reg_array[2];
    $out_trade_no=$reg_array[3];
	$sql = "update race_reg_pay set state = 2,openid='$openid',out_trade_no = '$out_trade_no' where reg_id = $reg_id";
	$sql1 = "update race_group set payed_mount = payed_mount+1 where id = $group_id";
	$sql2 = "update race_reg set state = 3 where id = $reg_id";

	$race->query($sql);
	$race->query($sql1);
	$race->query($sql2);
    
    ?>
    <script type="text/javascript">
        document.cookie="reg_id=";
        document.cookie="race_id=";
        document.cookie="price=";
        window.location.href='index.php?c=race&action=message&re_g_id=<?php echo $reg_id?>';
    </script>
    <?php
	go("?c=race&action=message&re_g_id=".$reg_id);
}
elseif($action == "race_check")
{
    $race_id =  get($_GET['race_id']);
    $error=get($_GET['error']);
    $title_array=$race->get_race_msg($race_id);
   // print_r($title_array);
    $title = $title_array['race_title'];
    include 'view/race_check.php';
}
elseif($action == "race_check_dll")
{
    $race_id =  get($_GET['race_id']);
    $tel=get($_POST['tel']);

    if(!$tel)
    {
        $error="无此用户";
    }
    //check
    $sql = "select id from race_reg where race_id = $race_id and tel = '$tel'";
    $result = $race->get_One($sql);

    if($result)
    {
        $user_id = $result;
        $check_time = time();
        //get start time
        $sql_time = "select start_time,end_times from race where id = $race_id";
        $time_result = $race->get_data($sql_time);
        //print_r($sql_time);
        $start_time = strtotime($time_result[0]['start_time']);
        $end_time = strtotime($time_result[0]['end_times']);
        //echo $check_time."|".$start_time."|".$end_time;
        //die();
        //确认是否已经开始
        if($check_time > $start_time )
        {
            $diff_time = $check_time - $start_time;
            $sql_update = "INSERT INTO `race_check` (`user_id`, `race_id`, `check_time`, `start_time`, `diff_time`) VALUES ('$user_id', '$race_id', '$check_time', '$start_time', '$diff_time')";
            $race->query($sql_update);
            $error="成功签到";
        }
        else
        {
            $error="活动未开始";
        }
    }
    else
    {
        $error="无此用户";
    }
    go("?c=race&action=race_check&race_id=$race_id&error=".urlencode($error));
}
elseif($action == "race_table_col")
{
    include 'view/race_table_col.php';
}
elseif($action == "input_list")
{
    $race_id =  get($_GET['race_id']);
    $sql = "select * from race_input where race_id = $race_id order by is_del";
    $result = $race->get_data($sql);
    include 'view/admin_top.php';
    if(!$request_type)
    {
       include 'view/race_top.php';
    }
    include 'view/race_input_list.php';
}
elseif($action == "race_input_add")
{
	$input_type =  get($_POST['input_type']);
	$race_id =  get($_GET['race_id']);
	$input_name =$input_type ."_".  $race_id.rand(1000000,9999999);
	$input_title =  get($_POST['input_title']);
	$default_values =  get($_POST['default_values']);
	$instructions =  get($_POST['instructions']);
	$addtime = get_now();
	$is_del = 0;
    $can_null = get($_POST['can_null']);
	
	$sql ="INSERT INTO `race_input` (`race_id`, `input_name`, `input_title`, `addtime`, `is_del`, `input_type`, `default_values`,`instructions`,`can_null`) VALUES ( '$race_id', '$input_name', '$input_title', '$addtime', '$is_del', '$input_type', '$default_values','$instructions','$can_null')";
	
	$race->query($sql);
	
	go("index.php?c=race&action=input_list&race_id=$race_id&request_type=$request_type");
}
elseif($action == "race_input_del")
{

	$race_id =  get($_REQUEST['race_id']);
	$id =  get($_REQUEST['id']);
	$is_del = get($_REQUEST['is_del']);
	
	$sql = "update race_input set is_del = $is_del where id = $id";
	$race->query($sql);
	go("index.php?c=race&action=input_list&race_id=$race_id&request_type=$request_type");
}
elseif($action == 'race_input_edit')
{
	$race_id =  get($_GET['race_id']);
	$input_title =  get($_POST['input_title']);
	$id =  get($_GET['id']);	
	$input_type =  get($_POST['input_type']);
	$default_values =  get($_POST['default_values']);
	$instructions =  get($_POST['instructions']);
    $can_null = get($_POST['can_null']);
	// text input
	$sql = "update race_input set input_title = '$input_title',default_values='$default_values',instructions='$instructions',can_null='$can_null' where id = $id ";
	//die($sql);
	$race->query($sql);
	
	go("index.php?c=race&action=input_list&race_id=$race_id&request_type=$request_type");
}
elseif($action == 'race_reg_del')
{
	$id = get($_GET['id']);
	$race_id = get($_GET['race_id']);
	$group_id=get($_GET['group_id']);
	
	$sql="delete from race_reg where id = $id";
	$sql_update_group = "update race_group set reg_mount = reg_mount-1 where id = $group_id";
	$race->query($sql);
	$race->query($sql_update_group);
	go("index.php?c=race&action=race_reg_member_list&race_id=$race_id&request_type=$request_type");
}
elseif($action == 'race_financial')
{
	$id = get($_GET['race_id']);
	$excel = get($_GET['excel']);
	$race_msg = $race->get_race_msg($id);
	$race_title = $race_msg['race_title'];
	
	$group = $race->get_group($id);
	if($excel)
	{
		header("Content-type: application/vnd.ms-excel; charset=utf-8");
		header("Content-Disposition: attachment; filename=".$race_title."收入数据导出.xls");
		echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
	}
	else
	{
     include 'view/admin_top.php';
     include 'view/race_top.php';
 }
 include 'view/race_financial.php';
}

elseif($action == 'race_add_sussess')
{
    $id = get($_GET['id']);
     include 'view/admin_top.php';
     include 'view/race_add_sussess.php';
}
elseif ($action=='race_cert_msg') 
{
        $reg_id = get($_GET['regid']);
        $sql =  "select * from race_reg where id = $reg_id";
        $result= $race->get_data($sql);
        //print_r($result[0]);
        $finish_time = $result[0]['finish_time'];
        if($finish_time)
        {
			$race_id = $result[0]['race_id'];
			$group_id = $result[0]['group_id'];
            //echo $group_id;
			$group = $race->get_group($group_id);
            //print_r($group);
			$group_title = $group[0]['title'];
			$race_line = $group[0]['race_line'];
			$group_distance=  $group[0]['distance'];
			$name = $_COOKIE['fullname'];
			$race_result = $race->get_race_msg($race_id);

			$race_title = $race_result['race_title'];
			$msg = array(
				'name' => $name,
				'finish_time'=>$finish_time,
				'group_title'=>$group_title,
				'race_line'=>$race_line,
				'group_distance'=>$group_distance,
				'race_title'=>$race_title,
			);

           // print_r($msg);
			include 'view/top_mini.php';
			include 'view/race_cert_msg.php';
			//include 'view/bottom.php';
        }
        else
        {
        	echo '对不起，完赛信息没有更新或您没有完成比赛。';
        }
}
elseif($action =='dashboard')
{
    $race_msg = $race->get_race_msg($id);
    $article_list = $race->dashboard_article_list($id);
    include 'view/admin_top.php';
    include 'view/race_top.php';
    include 'view/race_dashboard.php';
}
elseif($action == 'race_display')
{
    $display = get($_GET['display']);
    $sql = "update race set display = '$display' where id = $id";
    $race->query($sql);

    go("index.php?c=race&action=race_list");
}
elseif($action =='race_team')
{
    $race_id = get($_GET['id']);
    $sql="select * from race_team where race_id = $id order by is_check desc";
    $result=$race->get_data($sql);

    $team_mount = $race->race_team_count($group_id);

    include 'view/admin_top.php';
    include 'view/race_top.php';
    include 'view/race_team.php';
}
elseif($action == 'team_select')
{
    $race_id =  get($_GET['race_id']);
    $group_id =  get($_GET['group_id']);
    $title ="团队";
    $back_url = "index.php?c=race&action=race_msg&id=$race_id";
    $sql = "select * from race_team where group_id = $group_id and is_check = 0";
    $result = $race->get_data($sql);
    $team_mount = $race->race_team_count($group_id);
    include 'view'.$mobi_root.'/top.php';
    include 'view'.$mobi_root.'/race_team_select.php';
    include 'view'.$mobi_root.'/bottom.php';   
}
elseif($action == 'team_add')
{
    if($_COOKIE['id'])
    {
        $race_id =  get($_GET['race_id']);
        $group_id =  get($_GET['group_id']);
        $user_id =  $_COOKIE['id'];
        $team_name = get($_POST['team_name']);
        $team_content = get($_POST['team_content']);
        $add_time = time();
        //check max check
        $sql_check = "select team_check from race_group where id = $group_id";
        $is_check = $race->get_one($sql_check);
        $sql = "INSERT INTO `race_team` (`team_name`, `team_content`, `user_id`, `race_id`, `add_time`, `group_id`, `is_check`) VALUES ('$team_name', '$team_content', '$user_id', '$race_id', '$add_time', '$group_id', '$is_check')";
        $race->query($sql);
?>
<script type="text/javascript">
    alert("感谢您的提交，团队或许需要审核，请耐心等待；如果不需要审核，则可以直接选择后进行报名");
</script>
<?php
        go("index.php?c=race&action=team_select&race_id=$race_id&group_id=$group_id");
    }
}
elseif($action == 'race_team_check')
{
    $id=get($_GET['id']);
    $race_id=get($_GET['race_id']);
    $is_check=get($_GET['is_check']);

    $sql = "update race_team set is_check = $is_check where id = $id";
    $race->query($sql);

    go("index.php?c=race&action=race_team&id=$race_id");
}