<?php  
error_reporting(0);
@header('Content-Type: application/json; charset=UTF-8');
include 'install/config.php';
if (isset($_POST['type'])) {
    switch ($_POST['type']) {
        case 'reg':
            $pass = md5($_POST['pass']);
            try {
                $users = $pdo->query("SELECT * FROM `user` WHERE `user`='{$_POST['user']}'")->rowCount();
                if ($users > 0) {die(json_encode(array('code'  => '-3' , 'msg' => '用户已注册')));}
                $reg = $pdo -> prepare("INSERT INTO `user`(`id` , `user` , `pass` , `time`) VALUES (NULL , ? , ? ,CURRENT_TIMESTAMP())");
                $reg -> bindValue(1 , $_POST['user']);
                $reg -> bindValue(2 , $pass);
                if ($reg -> execute()) {die(json_encode(array('code'  => '0' , 'msg' => '注册成功')));}
                else{die(json_encode(array('code'  => '-4' , 'msg' => '注册失败')));}
            } catch (PDOException $e) {
                die(json_encode(array('code'  => '-2' , 'msg' => '系统错误')));             
            }
            break;
        case 'login':
            $pass = md5($_POST['pass']);
            try {
                $users = $pdo->query("SELECT * FROM `user` WHERE `user`='{$_POST['user']}'")->rowCount();
                if ($users <= 0) {die(json_encode(array('code'  => '-3' , 'msg' => '用户未注册')));}
                $users = $pdo->query("SELECT * FROM `user` WHERE `user` = '{$_POST['user']}' AND `pass` = '{$pass}'")->rowCount();
                if ($users <= 0) {die(json_encode(array('code'  => '-4' , 'msg' => '账号密码错误')));}
                $text = $pdo->query("SELECT * FROM `user` WHERE `user` = '{$_POST['user']}'")->fetch();
                $arr = array(
                    'code' => '0', 
                    'msg' => '登录成功',
                    'user' => $text['user'], 
                    'time' => $text['time']
                );
                die(json_encode($arr));
            } catch (PDOException $e) {
                die(json_encode(array('code'  => '-2' , 'msg' => '系统错误')));
            }
            break;
        case 'write':
            $pass = md5($_POST['pass']);
            try {
                $users = $pdo->query("SELECT * FROM `user` WHERE `user`='{$_POST['user']}'")->rowCount();
                if ($users <= 0) {die(json_encode(array('code'  => '-3' , 'msg' => '用户未注册')));}
                $users = $pdo->query("SELECT * FROM `user` WHERE `user` = '{$_POST['user']}' AND `pass` = '{$pass}'")->rowCount();
                if ($users <= 0) {die(json_encode(array('code'  => '-4' , 'msg' => '账号密码错误')));}
                $write = $pdo -> prepare("INSERT INTO `word`(`id` , `uid` , `title` , `content` , `time`) VALUES (NULL , ? , ? , ? ,CURRENT_TIMESTAMP())");
                $write -> bindValue(1 , $_POST['user']);
                $write -> bindValue(2 , $_POST['title']);
                $write -> bindValue(3 , $_POST['content']);
                if ($write -> execute()) {die(json_encode(array('code'  => '0' , 'msg' => '文档创建成功')));}
                else{die(json_encode(array('code'  => '-5' , 'msg' => '文档创建失败')));}
            } catch (PDOException $e) {
                die(json_encode(array('code'  => '-2' , 'msg' => '系统错误')));
            }
            break;
        case 'allWord':
            $pass = md5($_POST['pass']);
            try {
                $users = $pdo->query("SELECT * FROM `user` WHERE `user`='{$_POST['user']}'")->rowCount();
                if ($users <= 0) {die(json_encode(array('code'  => '-3' , 'msg' => '用户未注册')));}
                $users = $pdo->query("SELECT * FROM `user` WHERE `user` = '{$_POST['user']}' AND `pass` = '{$pass}'")->rowCount();
                if ($users <= 0) {die(json_encode(array('code'  => '-4' , 'msg' => '账号密码错误')));}
                $allWord = $pdo->query("SELECT * FROM `word` WHERE `uid` = '{$_POST['user']}' order by time desc");
                $array = array();
                $i = 0;
                while ($row = $allWord->fetch()) {
                    $array[$i]['code'] = '0';
                    $array[$i]['msg'] = '获取列表成功';
                    $array[$i]['id'] = $row['id'];
                    $array[$i]['title'] = $row['title'];
                    $array[$i]['content'] = $row['content'];
                    $array[$i]['time'] = $row['time'];
                    $i++;
                }
                die('{"wordList":'.json_encode(array_reverse($array)).'}');
            } catch (PDOException $e) {
                die(json_encode(array('code'  => '-2' , 'msg' => '系统错误')));
            }
            break;
        case 'oddWord':
            try {
                $word = $pdo->query("SELECT * FROM `word` WHERE `id` = '{$_POST['id']}'")->fetch();
                $arr = array(
                    'code' => '0', 
                    'msg' => '获取成功',
                    'id' => $word['id'],
                    'title' => $word['title'], 
                    'content' => $word['content'], 
                    'time' => $word['time']
                );
                die(json_encode($arr));
            } catch (PDOException $e) {
                die(json_encode(array('code'  => '-2' , 'msg' => '系统错误')));
            }
            break;
        case 'edit':
            $pass = md5($_POST['pass']);
            try {
                $users = $pdo->query("SELECT * FROM `user` WHERE `user`='{$_POST['user']}'")->rowCount();
                if ($users <= 0) {die(json_encode(array('code'  => '-3' , 'msg' => '用户未注册')));}
                $users = $pdo->query("SELECT * FROM `user` WHERE `user` = '{$_POST['user']}' AND `pass` = '{$pass}'")->rowCount();
                if ($users <= 0) {die(json_encode(array('code'  => '-4' , 'msg' => '账号密码错误')));}
                $edit = $pdo -> prepare("UPDATE `word` SET `title` = ? ,`content` = ? WHERE `uid` = ? AND `id` = ? ");
                $edit -> bindValue(1 , $_POST['title']);
                $edit -> bindValue(2 , $_POST['content']);
                $edit -> bindValue(3 , $_POST['user']);
                $edit -> bindValue(4 , $_POST['id']);
                if ($edit -> execute()) {die(json_encode(array('code'  => '0' , 'msg' => '文档修改成功')));}
                else{die(json_encode(array('code'  => '-5' , 'msg' => '文档修改失败')));}
            } catch (PDOException $e) {
                die(json_encode(array('code'  => '-2' , 'msg' => '系统错误')));
            }
            break;
        case 'del':
            $pass = md5($_POST['pass']);
            try {
                $users = $pdo->query("SELECT * FROM `user` WHERE `user`='{$_POST['user']}'")->rowCount();
                if ($users <= 0) {die(json_encode(array('code'  => '-3' , 'msg' => '用户未注册')));}
                $users = $pdo->query("SELECT * FROM `user` WHERE `user` = '{$_POST['user']}' AND `pass` = '{$pass}'")->rowCount();
                if ($users <= 0) {die(json_encode(array('code'  => '-4' , 'msg' => '账号密码错误')));}
                $del = $pdo->prepare("DELETE FROM `word` WHERE `uid` = ? AND `id` = ?");
                $del -> bindValue(1 , $_POST['user']);
                $del -> bindValue(2 , $_POST['id']);
                if ($del->execute()) {
                    die(json_encode(array('code'  => '0' , 'msg' => '文档删除成功')));
                } else {
                    die(json_encode(array('code'  => '-5' , 'msg' => '文档删除失败')));
                }
            } catch (PDOException $e) {
                die(json_encode(array('code'  => '-2' , 'msg' => '系统错误')));
            }
            break;
        case 'userNum':
            try {
                $userNum = $pdo->query("SELECT * FROM `user`")->rowCount();
                die(json_encode(array('code' => '0' , 'msg' => "$userNum")));
            } catch (PDOException $e) {
                die(json_encode(array('code'  => '-2' , 'msg' => '系统错误')));
            }
            break;
        default:
            die(json_encode(array('code' => '-1' , 'msg' => '提交的参数类型错误')));
            break;
    }
}else{
    die(json_encode(array('code' => '-1' , 'msg' => '提交的参数类型错误')));    
}





?>