<?php
/*
@author waychar
@技术支持 150572314@qq.com
@copyright 2018 waychar.cn 声明：本代码可以用于学习交流使用及要商业化用途
*/
class race extends db
{
    public function getPasswordO($length = 8)
    {
        // 密码字符集，可任意添加你需要的字符
        //$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ012345678';
        $chars = '0123456789';
        $password = '';
        for ($i = 0; $i < $length; $i++) {
            // 这里提供两种字符获取方式
            // 第一种是使用 substr 截取$chars中的任意一位字符；
            // 第二种是取字符数组 $chars 的任意元素
            // $password .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
            $password .= $chars[mt_rand(0, strlen($chars) - 1)];
        }
        return $password;
    }

    public function getLastId()
    {
        $sql = "select id from race order by id desc ";
        $result = $this->get_One($sql);
        return $result;
    }

    public  function check_reg($race_id)
    {
        $sql = "select id from race_reg where race_id = $race_id and user_id = " . $_COOKIE['id'];
        $result = $this->get_One($sql);
        if ($result) {
            return $result;
        } else {
            return false;
        }
    }

    public function getimg($id)
    {
        $sql = "select images_url from race_images where race_id = $id and is_index = 1";
        $result = $this->get_One($sql);

        if($result)
        {
        	if(file_exists($result))
        	{
        		return $result;   
        	}  	
        	else
        	{
        		return 'images/no_img.jpg';      		
        	}
        }
        else
        {
        	return 'images/no_img.jpg';
        }
    }

    public function get_state($start_time,$end_times,$sign_start_time,$sign_end_time)
    {
        //开始时间：<?php echo $row['start_time']?
        //结束时间：<?php echo $row['end_times']?
        //报名开始时间：<?php echo $row['sign_start_time']?
        //报名结束时间： <?php echo $row['sign_end_time']?
        $start_time = strtotime($start_time);
        $end_times = strtotime($end_times);
        $sign_end_time = strtotime($sign_end_time);
        $sign_start_time=strtotime($sign_start_time);
        $now = time();

        if($now<$sign_start_time)
        {
            $result = "未开始报名";
        }

        if ($now>$sign_start_time and $now<$sign_end_time)
        {
            $result = "报名中";
        }

        if($now < $end_times ){
            $result ="活动结束";
        }

        if($now >$start_time and $now <$end_times )
        {
            $result ="活动进行中";
        }
        return $result;
    }

    function get_images($race_id)
    {
        $sql ="select images_url from race_images where race_id = $race_id";
        $result = $this->get_data($sql);

        return $result;
    }
    
    public function get_reg_msg($race_id)
    {
        $sql = "select * from race_reg where race_id = $race_id and user_id= ".$_COOKIE['id'];
        $result = $this->get_data($sql);
        return $result;
    }


    public function get_reg_name($user_id)
    {
        $sql = "select realname from race_reg where id= $user_id";
        //echo $sql;
        $result = $this->get_one($sql);
        return $result;
    }

    public function get_race_list_option()
    {
        $sql = "select id ,race_title from race order by id desc";
        $result = $this->get_data($sql);

        return $result;
    }

    public function sex($v)
    {
        if($v==1)
        {
            return '男';
        }
        else
        {
            return '女';
        }
    }

    public function idcard_type($v)
    {
        if($v==1)
        {
            return '身份证';
        }
        else
        {
            return '军官证';
        }
    }


	public function is_del($v)
	{
		if($v==1)
		{
			return "未展示";
		}
		else
		{
			return "正常展示";
		}
	}
	
	public function get_input($race_id,$value=null)
	{
		$sql = "select * from race_input where race_id = $race_id and is_del = 0";
		$result = $this->get_data($sql);

		$out="";
		if($result)
		{
			foreach($result as $row)
			{
				if($row['input_type']== 1)
				{
					$value_key = $row['input_name'];
					if($value)
					{
						$input_value = $this->get_v($value_key,$value);
					}
					$out = $out."<tr><td>".$row['input_title']."</td><td><input type = text value='".$input_value."' name=" .$value_key ."   id =" .$value_key ." /> <font color=skyblue>*".$row['instructions']."</font></td></tr>";
				}elseif($row['input_type']==2)
				{
					if($value)
					{
						$input_value = $this->get_v($value_key,$value);
					}
					$out = $out."<tr><td>".$row['input_title']."</td><td>".$this->get_option($row['input_name'],$row['default_values'],$input_value)." <font color=skyblue>*".$row['instructions']."</font></td></tr>";
				}
			}
		}
		
		return $out;
	}
	
	public function get_input_data($race_id)
	{
		$sql = "select * from race_input_data where race_id = $race_id and user_id = ".$_COOKIE['id'];
		$result = $this->get_data($sql);
		
		return $result;
	}
	
	public function input_data_update($row,$key,$race_id)
	{
		if($_COOKIE['id'])
		{
		$user_id = $_COOKIE['id'];
			if($row)
			{
				$reg_id = $this->get_reg_id($race_id);
				//check is 
				$sql = "select id from race_input_data where input_name = '$key' and reg_id =$reg_id ";
				$check = $this->get_one($sql);
				//echo $sql;
				if($check)
				{
					//have ,will update
					$sql_update = "update race_input_data set input_value = '".$row."' where id = $check";
				}
				else
				{
					//no 
					$sql_update = "insert into race_input_data (input_name,input_value,race_id,user_id,reg_id) values ('".$key."','".$row."',$race_id,$user_id,$reg_id)";
				}
		
				//echo $sql_update;
				$this->query($sql_update);
			}
		}
		else
		{
			die("error");
		}
	}
	
	public function get_v($input_name,$value)
	{
		foreach($value as $row)
		{
			if($row['input_name'] == $input_name)
			{
				$out = $row['input_value'];
			}
		}
		
		return $out;
	}
	
	public function get_option($value_key,$value,$in)
	{
		if($value)
		{
			$value_array =explode("|",$value);
			$out = "<select name = $value_key id = $value_key>";
			foreach($value_array as $row)
			{
				if($in == $row)
				{
					$selected  = 'selected';
				}
				$out = $out . "<option value = $row $selected>$row</option>";
				$selected = '';
			}
			$out = $out."</select>";
		}
		
		return $out;
	}
	
	public function get_race_reg_member_list($race_id,$reg_id)
	{
		$sql = "select input_name from race_input where race_id = $race_id and is_del = 0 order by id ";
		$result = $this->get_data($sql);
		if($result)
		{
			foreach ($result as $row)
			{
				$sql_out = "select input_value from race_input_data where reg_id = $reg_id and input_name = '".$row['input_name']."'";
				//echo $sql_out;
				$result_out = $this->get_one($sql_out);
				//echo $result_out;
				$out = $out ."<td>$result_out</td>";
			}
		}
		return $out;
	}
	
	public function get_race_reg_member_list_title($race_id)
	{
		$sql = "select input_title from race_input where race_id = $race_id and is_del = 0 order by id ";
		$result = $this->get_data($sql);
		
		foreach($result as $row)
		{
			$out = $out."<th>".$row['input_title']."</th>";
		}
		return $out;
	}
	
	public function get_reg_id($race_id)
	{
		$sql = "select id from race_reg where race_id = $race_id and user_id = ".$_COOKIE['id'];
		return $this->get_one($sql);
	}
	
	public function get_age($group_id)
	{
		$sql = "select s_age,b_age from race_group where id = $group_id ";
		return $this->get_data($sql);
	}
	
	public function get_group_name ($id)
	{
		$sql = "select title from race_group where id = $id";
		return $this->get_one($sql);
	}

	public function get_p_update ($id)
	{
		$sql = "select p_update from race_group where id = $id";
		return $this->get_one($sql);
	}
	
	public function get_race_msg($id)
	{
		$sql = "select * from race where id = $id";
		$result = $this->get_data($sql);
		return $result[0];
	}
	
	public function get_group($race_id)
	{
		$sql = "select * from race_group where race_id = $race_id";
		return $this->get_data($sql);
	}
	
	public function get_pay_count($type,$group_id)
	{
		//type 1 mount count,
		//type 2 pay sum
		if($type == '1')
		{
			$sql = "select count(id) from race_reg_pay where group_id = $group_id and state=2";
		}
		elseif($type=='2')
		{
			$sql = "select sum(money_value) from race_reg_pay where group_id = $group_id and state=2";
		}
		
		$result =  $this->get_one($sql);
		
		if(!$result)
		{
			return 0;
		}
		else
		{
			return $result;
		}
	}

	public function get_check_list($race_id,$user_id){
		$sql = "select * from race_check where race_id = $race_id and user_id = $user_id";
		$result = $this->get_data($sql);
		//print_r($sql);
		$out = '';
		foreach ($result as $row) 
		{
			$out = $out."<td>打卡时间：".get_time($row['check_time'])."<br/>用时：".get_time($row['diff_time'])."<br/></td>";
		}

		return $out;
	}

	public function dashboard_article_list($race_id)
    {
        $sql ="select title,id from article where race_id = $race_id";
        $result = $this->get_data($sql);

        return $result;
    }

    public function input_list_can_null($race_id)
    {
    	$sql  = "select * from race_input where race_id = $race_id and can_null = 1";
    	//echo $sql;
    	$result = $this->get_data($sql);

    	if($result)
    	{
    		foreach ($result as $row) {
    			?>
    			if($('#<?php echo $row['input_name']?>').val() == ''){alert('请输入<?php echo $row['input_title']?>');return false};
    			<?php
    		}
    	}
    }

    public function update_low_price($race_id,$price)
    {
    	$sql = "select price from race_group where race_id = $race_id order by price";
    	$result = $this->get_one($sql);

    	if($result)
    	{
    		$sql_update = "update race set low_price = $result where id = $race_id";
    	}
    	else
    	{
    		$sql_update = "update race set low_price = $price where id = $race_id";	
    	}
    	//die($sql_update);
    	$this->query($sql_update);
    }

    public function race_team_count($group_id)
    {
    	$sql = "select team_mount from race_group where id = $group_id";
    	$result = $this->get_one($sql);
    	//die( $sql);
    	if($result)
    	{
    		return $result;
    	}
    	else
    	{
    		return 0;
    	}
    }

    public function team_name($team_id)
    {
    	if($team_id)
    	{
	    	$sql="select team_name from race_team where id = $team_id";
	    	return $this->get_one($sql);
	    }
    }
}
