<?php
require_once $_SERVER['DOCUMENT_ROOT'] . '/core/app_top.php';
require_once WEB_ROOT . '/model/race.php';

$race = new race();
$id = get($_REQUEST['id']);
if ($action == 'list' || $action == '') {
    $sql_race_list = "
	select add_time, click_point, contant, contant_c, end_times, id, photo_link, place, race_title, sign_start_time,  start_time from race order by id desc limit 0,10
	";
    $result = $race->get_data($sql_race_list);
    include 'view/race_list.php';
    exit();
} elseif ($action == 'edit') {
    if ($id) {
        $sql_user = "select * from race where id = $id";
        $result = $race->get_data($sql_user);
        include 'view/race_form.php';
        exit();
    }
}
elseif ($action == 'reg')
{
    //die();
    $group_id = get($_REQUEST['group_id']);
    $race_id = get($_REQUEST['race_id']);
    //check_login
    if($_COOKIE['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);
        }
        //die('hellp');
        $sql = "select * from race_reg where user_id = ".$_COOKIE['id']." order by id desc limit 0,1 ";
        $result = $race->get_data($sql);
        include 'view/race_reg.php';
        include 'view/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')
{
    $result = $race->get_reg_msg(get($_REQUEST['race_id']));
    include 'view/race_reg.php';
    include 'view/bottom.php';
}
elseif ($action == 'add') {
    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 from race_group where race_id = ".$id;
       // die($sql_race_group_result) ;
        $race_group_result = $race->get_data($sql_race_group_result);
        $img = $race->get_images($id);
        include 'view/race_msg.php';
        include 'view/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") {
    $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 . "',
							'" . $place . "',
							'" . $contant . "',
							'" . $click_point . "',
							'" . $photo_link . "',
							'" . $contant_c . "',
							'" . $add_time . "',
							'" . $sign_end_time . "')";
    $user->query($sql);
    $lastid = $race->getLastId();
    //go("index.php?c=race&action=okandselect&id=" . $lastid);
    exit();
}
elseif ($action=='race_group_edit')
{
    if($id)
    {
        $sql ="select * from race_group where race_id = ".$id;
        $result = $race->get_data($sql);
        include 'view/race_group_form.php';
    }
    exit();
}
elseif($action =='race_group_add')
{
    $sql = "insert into race_group (
title,price,mount,distance,audi,race_id
) VALUES ('".get($_REQUEST['title'])."',
'".get($_REQUEST['price'])."',
'".get($_REQUEST['mount'])."',
'".get($_REQUEST['distance'])."',
'".get($_REQUEST['audi'])."',
'".get($_REQUEST['race_id'])."')";
    $race->query($sql);
    go("index.php?c=race&action=race_group_edit&id=".$_REQUEST['race_id']);
}
elseif($action=='race_group_dll')
{
    $sql = "
    update race_group set title = '".get($_REQUEST['title'])."',distance = '".get($_REQUEST['distance'])."',audi = '".get($_REQUEST['audi'])."',price='".get($_REQUEST['price'])."',mount='".get($_REQUEST['mount'])."' where id = 
    ".$id;
    $race->query($sql);
    go("index.php?c=race&action=race_group_edit&id=".$_REQUEST['race_id']);
}
elseif ($action=='race_reg_add')
{
    echo 11;
    die();
    $born=get($_REQUEST['born']);
    $cert_heal_img=get($_REQUEST['cert_heal_img']);
    $cert_img=get($_REQUEST['cert_img']);
    $city=get($_REQUEST['city']);
    $edu=get($_REQUEST['edu']);
    $email=get($_REQUEST['email']);
    $idcard=get($_REQUEST['idcard']);
    $idcard_type=get($_REQUEST['idcard_type']);
    $address=get($_REQUEST['address']);
    $j_address=get($_REQUEST['j_address']);
    $j_realname=get($_REQUEST['j_realname']);
    $j_rel=get($_REQUEST['j_rel']);
    $j_tel=get($_REQUEST['j_tel']);
    $race_id=get($_REQUEST['race_id']);
    $realname=get($_REQUEST['realname']);
    $sex=get($_REQUEST['sex']);
    $shoe_size=get($_REQUEST['shoe_size']);
    $size=get($_REQUEST['size']);
    $tel=get($_REQUEST['tel']);
    $user_id = $_COOKIE['id'];
    $xx=get($_REQUEST['xx']);
    $group_id=get($_REQUEST['group_id']);

$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`) 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)";
    die($sql);
    $race->query($sql);
    $last_id = $race->get_one("select id from race_reg order by id desc limit 0,1");

    // 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未审核 2审核未付费 3审核已付费 4 已付费未审核
    if($group_price == 0 and $group_audi == 2)
    {
        $sql = "update race_group set reg_mount = reg_mount+1 and 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) values ($race_id,$user_id,$group_id,'1',2,'0','".get_now()."',$last_id)";
        $sql_reg_update = "update race_reg set state = 3 where id = $last_id";
    }
    elseif($group_price >0 and $group_audi == 1)
    {
        $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) 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";
    }
    elseif($group_price ==0 and $group_audi == 1)
    {
        $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) values ($race_id,$user_id,$group_id,'1',1,'$group_price','".get_now()."',$last_id)";
        $sql_reg_update = "update race_reg set state = 4 where id = $last_id";
    }
    elseif ($group_price >0 and $group_audi ==2)
    {
        $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) 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";
    }

    //add to pay
    // die($sql);
    $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')
{
    $reg_id =get($_REQUEST['reg_id']);
    if(!$reg_id)
    {
        $reg_id = get($_REQUEST['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);
        include 'view/race_reg_message.php';
        include 'view/bottom.php';
    }
    else
    {
        echo "对不起，没有报名信息，请返回。";
    }
	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 = "select id,race_title,start_time,end_times,sign_start_time,sign_end_time,contant from race order by id desc";
    $result = $race->get_data($sql);
    include 'view/race_q_list.php';
    include 'view/bottom.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($_REQUEST['id_card']);
    $race_id = get($_REQUEST['race_id']);
    $sql = "select id from race_reg where idcard = '".$id_card."' and race_id = $race_id";
    // echo $sql;
    $result = $race->get_One($sql);
    if($result)
    {
        go("index.php?c=race&action=message&reg_id=$result");
    }
    else
    {
        echo "对不起，没有报名信息，请返回。";
    }
}
elseif ($action =="race_cert_search")
{
    $race_id = get($_REQUEST['race_id']);
    $option = $race->get_race_list_option();
    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");
    }
}
elseif($action =='race_reg_member_list')
{
    $race_id = get($_REQUEST['race_id']);

    $sql = "select * from race_reg where race_id=$race_id order by state desc";
    $result = $race->get_data($sql);
    include 'view/admin_top.php';
    include 'view/race_reg_member_list.php';
}