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

require_once 'model/user.php';

$user = new user();
$id = get($_GET['id']);
if ($action == 'list' || $action == '') {
    admin_check();
    $mobi = get($_POST['mobi']);
    if($mobi)
    {
        $where= " and mobi = '$mobi'";
    }
    $sql_user_list = "select id ,mobi from w_user where is_del = 0 $where order by id desc limit 0,100";
    $result = $user->get_data($sql_user_list);
   include 'view/admin_top.php'; 
    include 'view/user_top.php'; 
    include 'view/user_list.php';
    exit();
} elseif ($action == 'edit') {
    admin_check();
    if ($id) {
        $sql_user = "select * from w_user where id = $id";
        $result = $user->get_data($sql_user);
        $sql_reg_list ="select * from race_reg where user_id = $id";
        $reg_list = $user->get_data($sql_reg_list);
        include 'view/admin_top.php'; 
        include 'view/user_top.php'; 
        include 'view/user_form.php';
        exit();
    }
} elseif ($action == 'add') {
    admin_check();
    include 'view/admin_top.php';
    include 'view/user_top.php'; 
    include 'view/user_form.php';
    exit();
} elseif ($action == 'del') {
    admin_check();
    if ($id) {
        $sql = "update w_user set is_del = 0 where id = $id";
        $user->query($sql);
        go("index.php?c=user&action=list");
    }
    exit();
} elseif ($action == 'reset_pwd') {
    admin_check();
    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 == "user_edit") {
    admin_check();
    if ($id) {
        $username = get($_REQUEST['username']);
        $mobi = get($_REQUEST['mobi']);
        $email = get($_REQUEST['email']);
        $fullname = get($_REQUEST['fullname']);
        $sql = "update w_user set
					username = '$username',
					mobi = '$mobi',
					email = '$email',
					fullname = '$fullname'
				where id = $id";
        $user->query($sql);
        go("index.php?c=user&action=edit&id=$id");
    }
    exit();
} elseif ($action == "user_add") {
    admin_check();
    $username = get($_REQUEST['username']);
    $mobi = get($_REQUEST['mobi']);
    $email = get($_REQUEST['email']);
    $fullname = get($_REQUEST['fullname']);
    $password_o = $user->getPasswordO();
    $password = md5($password_o);
    $reg_time = date("Y-m-d h:i:s", time());

    $sql = "insert into w_user (
								username,
								password_o,
								password,
								email,
								mobi,
								fullname,
								reg_time
								) 
								values 
								(
								'$username',
								'$password_o',
								'$password',
								'$email',
								'$mobi',
								'$fullname',
								'$reg_time'
								)";
    $user->query($sql);
    go("index.php?c=user&action=list");
    exit();
}