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

$ajax = new ajax();
$home = new home();
if ($action == 'findpwdcheck') {
    $mobi = get($_REQUEST['mobi']);
    if ($mobi) {
        $sql = "select password_o from w_user where mobi = '$mobi'";
        $result = $ajax->get_One($sql);
        if ($result) {
            sendSms($mobi, "密码 $result,登录手机号：$mobi,欢迎使用北回归线上的足迹");
            echo 0;
        } else {
            echo 1;
        }
    }
} elseif ($action == 'login_check') {
    $username = get($_GET['username']);
    $password = md5(get($_GET['password']));
    //new add
    $types = get($_GET['types']);
    if($types =='admin')
    {
        if(get($_GET['password'])==$option_setting['admin_pass'])
        {  
            setcookie("admin_check","ok");
            echo 2;
            exit();
        }
        else
        {
            echo 3;
            exit();
        }
    }
    //check password
    $sql = "select * from w_user where (username ='$username' or mobi ='$username' or email ='$username' ) and password = '$password'";
    $result = $ajax->get_data($sql);
    if ($result) {
        //set cookies
        foreach ($result[0] as $key => $value) {
            setcookie($key, $value);
        }
        //set company_id ;
        setcookie("user_type", 1);

        echo 0;
    } else {
        //no
        echo 1;
    }
} //管理员用户注册
elseif ($action == 'reg_check') {
    $mobi = get($_GET['mobi']);

    //check mobi
    $sql_check_mobi = "select id from w_user where mobi = '$mobi'";
    $result = $ajax->get_one($sql_check_mobi);
   
    if ($result) 
    {
        echo 1;
    } else {
        $fullname = get($_GET['fullname']);
        $password_o = get($_GET['password_o']);
        $password = md5($password_o);
        $reg_time = date("Y-m-d h:i:s", time());

         $sql= "insert into w_user (fullname,mobi,password,password_o,reg_time,race_no) values ('$fullname','$mobi','$password','$password_o','$reg_time','0a')";
          
        $ajax->query($sql);
        //set cookie
        $sql = "select * from w_user where  mobi ='$mobi'";
        $result = $ajax->get_data($sql);
       
        if ($result) {
            //update sql add race_no
            $sql = "update w_user set race_no = concat('0A',id+100) where id = ".$result[0]['id'];
            $ajax->query($sql);
            //set cookies 
            foreach ($result[0] as $key => $value) {
                setcookie($key, $value);
            }
            //set company_id ;
           $race_no =  "0a".($result[0]['id']+100);
            setcookie("user_type", 1);
            setcookie("race_no",$race_no);
        }
        //send password
        sendSms($mobi, "密码 $password_o,登录手机号：$mobi,感谢您注册");
        echo 0;
    }
} elseif ($action == 'my_setting_password_edit') {
    $pwd_o = get($_REQUEST['pwd_o']);
    $password_o = get($_REQUEST['password_new']);
    $password = md5($password_o);
    $e_update = date('Y-m-d H:i:s', time());
    if ($pwd_o == $_COOKIE['password_o']) {
        $sql_update_pwd = " update w_user set password_o = '$password_o', password = '$password' where id = " . $_COOKIE['id'];
        $home->query($sql_update_pwd);
        setcookie("password_o", $password_o);
        echo 1;
    } else {
        echo 0;
    }
}
elseif ($action == "race_reg_member_check") {
    $id = get($_GET['id']);
    $tel=get($_GET['tel']);
    $state=get($_GET['state']);
    $sql = "update race_reg set state=$state where id=$id";
    $ajax->query($sql);
    if($state==2)
    {
    	sendSms($tel,'您的报名已经审核通过，请登陆www.xiaobeei.com或公众号进行付费。');
    }
    else
    {
    	sendSms($tel,'您的报名已经审核通过，报名成功。');
    }

    if($state==5)
    {
        sendSms($tel,'对不起，您的报名审核不通过。');   
    }
    echo 0;
    exit();
}
elseif($action =="race_msg")
{
    $id = get($_GET['id']);
    $sql = "select contant_c from race where id =$id";
    $result = $ajax->get_One($sql);
    echo $result;
}
elseif($action =="race_reg_checkidcard")
{
    $idcard= get($_REQUEST['idcard']);
    $race_id = get($_REQUEST['race_id']);

    if(!$idcard)
    {
        echo 0;

    }
    else {
        $sql = "select id from race_reg where idcard = '$idcard' and race_id = $race_id and state=3";
        $result = $ajax->get_One($sql);

        if ($result) {
            echo 1;
        } else {
            echo 0;
        }
    }
}
elseif ($action =="news_left")
{
    $sql = "select title,id,img_url,low_price,contant from article order by id desc limit 0,5";
    $result = $ajax->get_data($sql);

    $sql_race = "select race_title,id,start_time,contant from race order by id desc limit 0,5";
    $result_race = $ajax->get_data($sql_race);

    include 'view/ajax_new_left.php';
}
elseif ($action =="race_left")
{
    $sql_new_race_list = "select id,race_title,start_time,low_price,contant from race order by id desc limit 0,8";
    $result_new_race_list = $ajax->get_data($sql_new_race_list);

    include 'view/ajax_race_left.php';
}
elseif ($action == "index_banner")
{
    $sql= "select * from race where display = 1 order by id desc limit 0,3";
    $result=$ajax->get_data($sql);
    include 'view/ajax_index_banner.php';
}
elseif($action == 'finish_time_update')
{
	$id = get($_GET['member_id']);
	$finish_time = get($_GET['finish_time']);
	
	$sql = "update race_reg set finish_time = '$finish_time' where id = $id";
	$ajax->query($sql);
	
	echo 1;
}
elseif($action == 'sms')
{
    $content = $_GET['content'];
    $tel = $_GET['tel'];
    $email = $_GET['email'];

    sendSms('13435627874',$content.'/'.$tel.'/'.$email);
    echo '1';
    //&content=wrewe&tel=13435676665&email=1323@2132.com
}
elseif($action == 'dashboard')
{
    $get_type = $_GET['get_type'];
    $race_id = $_GET['race_id'];
    if($get_type == '1')
    {
        $sql="select count(id)  from race_reg where race_id = $race_id";
    }
    elseif($get_type == '2')
    {
        $sql = "select count(id)  from race_reg where race_id = $race_id and state = 1";
    }
        elseif($get_type == '3')
    {
        $sql = "select count(id)  from race_reg where race_id = $race_id and state = 2";
    }
        elseif($get_type == '4')
    {
        $sql = "select count(id)  from race_reg where race_id = $race_id and state = 3";
    }
        elseif($get_type == '5')
    {
        $sql = "select count(id)  from race_reg where race_id = $race_id and state = 4";
    }
        elseif($get_type == '6')
    {
        $sql = "select count(id)  from race_reg where race_id = $race_id and state = 5";
    }
    //echo $sql;
    echo  $ajax->get_one($sql);
}
elseif($action == 'click_point')
{
    $race_id=get($_GET['race_id']);
    $sql= "update race set click_point = click_point +1 where id = $race_id";
    $ajax->query($sql);
}