<?php
define('BOOK',true);
require "include/common.php";
session_start();
@$act=$_GET['act'];
if (empty($act)){
    echo"<script language='javascript'>location.href='login.php?act=log';</script>";
}
if (!isset($_SESSION['username'])){
    show("请先登录","login.php?act=log");
}
switch ($act) {

    case "main":
        include tpl("main");
        break;
    case "adminedit":
        $sql = "SELECT * FROM `admin` WHERE `id`=1";
        $result = $conn->query($sql);
        list($id, $username, $password, $shenfen) = $result->fetch_row();
        include tpl("adminedit");
        break;
    case "editadmin":
        $pass = trim($_POST['pass']);
        $pass = md5($pass);
        $sql = "SELECT `password` FROM `admin` WHERE `id`=1";
        $result = $conn->query($sql);
        list($password) = $result->fetch_row();
        if ($pass == $password) {
            $password01 = trim($_POST['password01']);
            $password02 = trim($_POST['password02']);
            if ($password01 == $password02) {
                $password01 = md5($password01);
                $sql = "UPDATE `admin` SET `password` = '$password01' WHERE `id` = 1;";
                $result = $conn->query($sql);
                if ($result) {
                    show("密码更新成功", "houtai.php?act=adminedit");
                }
            } else {
                back('两次密码不一致请检查原因!', 'houtai.php?act=adminedit');
            }
        } else {
            back('原密码输入错误,请检查错误原因', 'houtai.php?act=adminedit');
        }
        break;
    case "fjadd":// 增加分卷
        include tpl("fenjuanadd");
        break;
    case "addfj"://执行 增加分卷
        $name=trim($_POST['name']);
        $order=trim($_POST['order']);
        $sql = "INSERT INTO `fenjuan` (`id`, `name`, `order`) VALUES (NULL, '$name', '$order');";
        $result=$conn->query($sql);
        if ($result){
            show("分卷增加成功","houtai.php?act=fjlist");
        }
        break;
    case "fjlist": //分卷列表
        $sql = "SELECT * FROM `fenjuan` ORDER BY `order` DESC";
        $result=$conn->query($sql);
        include tpl("fjlist");
        break;
    case "fjedit":  //进入编辑页面
        $id=$_GET['id'];
        $sql = "SELECT * FROM `fenjuan` WHERE `id`='$id'";
        $result=$conn->query($sql);
        $row=$result->fetch_array();
        include tpl("fjedit");
        break;
    case "editfj":
        $id=$_POST['id'];
        $name=trim($_POST['name']);
        $order=trim($_POST['order']);
        $sql = "UPDATE `fenjuan` SET `name` = '$name', `order` = '$order' WHERE `id` = '$id';";
        $result=$conn->query($sql);
        if ($result){
            show("分卷更新成功","houtai.php?act=fjlist");
        }

        break;
    case "delfj":  //分卷删除
        $id=$_GET['c'];
        $sql="DELETE FROM `fenjuan` WHERE `id`='$id'";
        $result=$conn->query($sql);
        if ($result){
            show("分卷删除成功","houtai.php?act=fjlist");
        }

        break;


    case "addart":  //文章发布界面
        $sql = "SELECT * FROM `fenjuan` ORDER BY `order` DESC";
        $result=$conn->query($sql);
        include tpl("addart");

        break;

    case "artadd":
        $fjid=$_POST['fjid'];
        $title=trim($_POST['title']);
        $content=trim($_POST['content']);
        $time=trim($_POST['time']);
        $sql = "INSERT INTO `wenzhang` (`aid`, `fjid`, `title`, `content`, `time`) VALUES (NULL,'$fjid', '$title', '$content', '$time');";
        $result=$conn->query($sql);
        if ($result){
            show("文章发布成功","houtai.php?act=wzlist");
        }
        break;

    case "wzlist":  //文章列表
        $sql = "SELECT `aid`,`fjid`,`title`,`time` FROM `wenzhang` ORDER BY `aid` DESC ;";
        $result=$conn->query($sql);
        //将分卷信息写入数组内存 便于调用
        $q = "SELECT * FROM `fenjuan` ";
        $rs=$conn->query($q);
        while ($r=$rs->fetch_array()){
            $fj[$r['id']]=$r['name'];
        }
        include tpl("wzlist");

        break;
    case "editart":
        //提取文章信息
        $aid=$_GET['aid'];
        $sql = "SELECT * FROM `wenzhang` WHERE `aid` ='$aid' ";
        $result=$conn->query($sql);
        $row=$result->fetch_assoc();

        //提取分卷信息
        $s = "SELECT * FROM `fenjuan` ORDER BY `order` DESC";
        $rs=$conn->query($s);
        include tpl("editart");
        break;
    case "artedit": // 执行编辑更新任务
        $aid=$_POST['aid'];
        $fjid=$_POST['fjid'];
        $title=trim($_POST['title']);
        $content=trim($_POST['content']);
        $time=trim($_POST['time']);
        $sql = "UPDATE `wenzhang` SET `fjid` = '$fjid', `title` = '$title', `content` = '$content', `time` = '$time' WHERE `aid` = '$aid';";
        $result=$conn->query($sql);
        if ($result){
            show("文章更新成功","houtai.php?act=wzlist");
        }

        break;
    case "delart":
        $aid=$_GET['aid'];
        $sql="DELETE FROM `wenzhang` WHERE `aid`='$aid'";
        $result=$conn->query($sql);
        if ($result){
            show("删除成功","houtai.php?act=wzlist");
        }

    case "system":
        $sql="SELECT * FROM `web` WHERE `id`=1;";
        $result=$conn->query($sql);
        $row=$result->fetch_array();
        include tpl("website");
        break;
    case "xgweb":
        $webname=trim($_POST['webname']);
        $keywords=trim($_POST['keywords']);
        $description=trim($_POST['description']);
        $foot=trim($_POST['foot']);
        $tip=trim($_POST['tip']);
        $sql = "UPDATE `web` SET `webname` = '$webname', `keywords` = '$keywords', `descrption` = '$description', `foot` = '$foot', `tip` = '$tip' WHERE `id` = 1;";
        $result=$conn->query($sql);
        if ($result){
            show("网站配置更新成功！","houtai.php?act=system");
        }



        break;
    case "upbgimg1":
        $sql = "SELECT `bgimg` FROM `web` WHERE `id`=1";
        $result=$conn->query($sql);
        list($bgimg)=$result->fetch_row();
        include tpl("upbgimg");
        break;

    case "upbjimg":
            //上传背景图片
            header('content-type:text/html;charset=utf-8');
            require_once 'upload.php';
            $upload=new upload('bgimg','files');
            $dest=$upload->uploadFile();
            $sql = "UPDATE `web` SET `bgimg` = '$dest' WHERE `id` = 1;";
            $result=$conn->query($sql);
        if ($result){
            show("网站背景更新成功！","houtai.php?act=upbgimg1");
        }

        break;
    case "upbookimg":
        //上传书籍图片
        header('content-type:text/html;charset=utf-8');
        require_once 'upload.php';
        $upload=new upload('bookimg','files');
        $dest=$upload->uploadFile();
        $sql = "UPDATE `book` SET `bookimg`='$dest' WHERE `id` = 1;";
        $result=$conn->query($sql);
        if ($result){
            show("书籍封面更改成功","houtai.php?act=bookimg");
        }
        break;
    case "book":
        // 书籍信息页面
        $sql = "SELECT * FROM `book` WHERE 1 ";
        $result=$conn->query($sql);
        $row=$result->fetch_array();
        include tpl("book");

        break;
    case "xgbook":
        $shuming=trim($_POST['shuming']);
        $zuozhe=trim($_POST['zuozhe']);
        $jianjie=trim($_POST['jianjie']);
        $sql = "UPDATE `book` SET `shuming`='$shuming',`zuozhe` = '$zuozhe', `jianjie` = '$jianjie' WHERE `id` = 1;";
        $result=$conn->query($sql);
        if ($result){
            show("书籍信息更改成功","houtai.php?act=book");
        }
        break;
    case "bookimg":
        $sql = "SELECT `bookimg` FROM `book` WHERE `id`=1";
        $result=$conn->query($sql);
        list($bookimg)=$result->fetch_row();
        include tpl("upbookimg");
        break;


    case "navigate":  //导航栏目列表
        $sql="SELECT * FROM `navigate` ORDER BY `order` ASC ";
        $result=$conn->query($sql);
        include tpl("navigate");
        break;

    case "addnavi": //增加导航页面
        include tpl("addnavi");
        break;
    case "naviadd":  //执行增加导航页面
        $lanmu=trim($_POST['lanmu']);
        $link=trim($_POST['link']);
        $order=trim($_POST['order']);
        $sql = "INSERT INTO `navigate` (`nid`, `lanmu`, `link`, `order`) VALUES (NULL, '$lanmu', '$link', '$order');";
        $result=$conn->query($sql);
        if ($result){
            show("栏目导航增加成功！","./houtai.php?act=addnavi");
        }
        break;
    case "editnavi": //编辑导航页面
        $nid=$_GET['nid'];
        $sql = "SELECT * FROM `navigate` WHERE `nid`='$nid';";
        $result=$conn->query($sql);
        $row=$result->fetch_array();
        include tpl("editnavi");

        break;

    case "naviedit":  // 执行导航编辑
        $nid=trim($_POST['nid']);
        $lanmu=trim($_POST['lanmu']);
        $link=trim($_POST['link']);
        $order=trim($_POST['order']);
        $sql = "UPDATE `navigate` SET `lanmu` = '$lanmu', `link` = '$link', `order` = '$order' WHERE `nid` = '$nid';";
        $result=$conn->query($sql);
        if ($result){
            show("栏目导航更新成功！","");
        }
        break;

    case "delnavi": //删除导航
        $nid=$_GET['nid'];
        $sql = "DELETE FROM `navigate` WHERE `nid`='$nid'";
        $result=$conn->query($sql);
        if ($result){
            show("导航删除已执行！","");
        }

        break;


    case "friendlinklist":
        $sql="SELECT * FROM `friendlink` ORDER BY `paixu` ASC ";
        $result=$conn->query($sql);

        include tpl("friendlink");

        break;

    case "addfrlink": //增加友情链接页面
        include tpl("addfrlink");
        break;

    case "frlinkadd":  //增加友情链接 执行
        $website=trim($_POST['website']);
        $frurl=trim($_POST['frurl']);
        $paixu=trim($_POST['paixu']);
        $sql = "INSERT INTO `friendlink` (`frid`, `website`, `frurl`, `paixu`) VALUES (NULL, '$website', '$frurl', '$paixu');";
        $result=$conn->query($sql);
        if ($result){
            show("友情链接增加成功","houtai.php?act=addfrlink");
        }

    case "editfrlink": //友情链接编辑页面
        $frid=$_GET['frid'];
        $sql = "SELECT * FROM `friendlink` WHERE `frid`='$frid';";
        $result=$conn->query($sql);
        $row=$result->fetch_array();
        include tpl("editfrlink");
        break;
    case "frlinkedit":// 执行友情链接编辑
        $frid=trim($_POST['frid']);
        $website=trim($_POST['website']);
        $frurl=trim($_POST['frurl']);
        $paixu=trim($_POST['paixu']);
        $sql = "UPDATE `friendlink` SET `website` = '$website', `frurl` = '$frurl', `paixu` = '$paixu' WHERE `friendlink`.`frid` = '$frid';";
        $result=$conn->query($sql);
        if ($result){
            show("友情链接更新成功","");
        }
        break;
    case "delfrlink":
        $frid=$_GET['frid'];
        $sql = "DELETE FROM `friendlink` WHERE `frid`='$frid'";
        $result=$conn->query($sql);
        if ($result){
            show("友情链接已执行删除","");
        }

        break;



}
$conn->close();