<?php
    include_once("../global.php");
    if (!isset($_SESSION['admin_name']) || $_SESSION['admin_name'] == '') {
        header("location:login.php");
        exit;
    }
    if ($_POST['act'] == 'del') {
        $sql = " DELETE from cates  where cate_id='" . $_POST['cate_id'] . "' ";
        mysqli_query($link, $sql);
        echo '1';
        exit;
    }
    if ($_POST['act'] == 'act_edit') {
        $sql = "  UPDATE cates SET cate_name ='" . $_POST['cate_name'] . "' ,sorts='" . $_POST['sorts'] . "' where cate_id ='" . $_POST['cate_id'] . "' ";
        mysqli_query($link, $sql);
        echo '1';
        exit;
    }

    if ($_POST['act'] == 'getCateInfo') {
        if (!is_numeric($_POST['cate_id'])) {
            die(json_encode(array("code" => '0', "msg" => '没有这个数据')));
        }
        $sql = "SELECT * FROM  cates  where cate_id ='" . $_POST['cate_id'] . "' ";
        $res = mysqli_query($link, $sql);
        $row = mysqli_fetch_array($res, MYSQLI_ASSOC);
        die(json_encode(array("code" => '1', 'msg' => 'success', "data" => $row)));
    }
    $datas = getCates();
?><!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="description" content="">
        <meta name="author" content="">

        <title>分类列表</title>

        <!-- Bootstrap Core CSS -->
        <link href="./css/bootstrap.min.css" rel="stylesheet">

        <!-- MetisMenu CSS -->
        <link href="./css/metisMenu.min.css" rel="stylesheet">

        <!-- DataTables CSS -->
        <link href="./css/dataTables/dataTables.bootstrap.css" rel="stylesheet">

        <!-- DataTables Responsive CSS -->
        <link href="./css/dataTables/dataTables.responsive.css" rel="stylesheet">

        <!-- Custom CSS -->
        <link href="./css/startmin.css" rel="stylesheet">

        <!-- Custom Fonts -->
        <link href="./css/font-awesome.min.css" rel="stylesheet" type="text/css">

        <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
        <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
        <!--[if lt IE 9]>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->
    </head>
    <script language="javascript" type="text/javascript">
                function getDateDemo(){

                                //分别获取年、月、日、时、分、秒
                                var myDate = new Date();
                                var year = myDate.getFullYear();
                                var month = myDate.getMonth() + 1;
                                var date = myDate.getDate();
                                var hours = myDate.getHours();
                                var minutes = myDate.getMinutes();
                                var seconds = myDate.getSeconds();

                                //月份的显示为两位数字如09月
                                if(month < 10 ){
                                        month = "0" + month;
                                }
                                if(date < 10 ){
                                        date = "0" + date;
                                }

                                //时间拼接
                                var dateTime = year + "年" + month + "月" + date + "日" + hours + "时" + minutes + "分" + seconds + "秒";

                                //document.write(dateTime);//打印当前时间

                                var divNode = document.getElementById("time");
                                divNode.innerHTML = dateTime;

                }
                window.setInterval("getDateDemo()",1000);//每隔1秒，调用一次getDateDemo()

    </script>
    <body>

        <div id="wrapper">

            <!-- Navigation -->
            <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
                <div class="navbar-header">
                    <a class="navbar-brand" href="index.php">管理后台</a>
                </div>


                <ul class="nav navbar-nav navbar-left navbar-top-links">
                    <li><a href="#"><i class="fa fa-home fa-fw"></i> Website</a></li>
                </ul>

                <ul class="nav navbar-right navbar-top-links">
                    <li class="dropdown navbar-inverse">
                        <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                            <font id="time"> </font>  IP : <?php echo $_SERVER['REMOTE_ADDR']; ?>
                        </a>

                    </li>
                    <li class="dropdown">
                        <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                            <i class="fa fa-user fa-fw"></i> 用户 <b class="caret"></b>
                        </a>
                        <ul class="dropdown-menu dropdown-user">

                            <li><a href="logout.php"><i class="fa fa-sign-out fa-fw"></i> 注销</a>
                            </li>
                        </ul>
                    </li>
                </ul>
                <!-- /.navbar-top-links -->

                <div class="navbar-default sidebar" role="navigation">
                    <div class="sidebar-nav navbar-collapse">
                        <ul class="nav" id="side-menu">
                            <li>
                                <a href="index.php" ><i class="fa fa-dashboard fa-fw"></i> 首页 </a>
                            </li>
                            <li>
                                <a href="setting.php" ><i class="fa fa-wrench fa-fw"></i> 设置</a>
                            </li>

                            <li>
                                <a href="url.php"  ><i class="fa fa-table fa-fw"></i> 网址管理</a>
                            </li>
                            <li>
                                <a href="action_url.php"><i class="fa fa-edit fa-fw"></i> 网址添加</a>
                            </li> 
                            <li>
                                <a href="sorts.php" class="active" ><i class="fa fa-files-o fa-fw"></i> 分类管理<span class="fa arrow"></span></a>
                                <ul class="nav nav-second-level">
                                    <li>
                                        <a href="sorts.php">分类列表</a>
                                    </li>
                                    <li>
                                        <a href="addsorts.php">增加分类</a>
                                    </li>                                </ul>
                                <!-- /.nav-second-level -->
                            </li>
                            
                            <li>
                                <a href="msg.php" ><i class="fa fa-files-o fa-fw"></i> 收录留言管理<span class="fa arrow"></span></a>
                                <ul class="nav nav-second-level">
                                    <li>
                                        <a href="msg.php">管理列表</a>
                                    </li>
                                </ul>
                                <!-- /.nav-second-level -->
                            </li>
                        </ul>
                    </div>
                </div>
            </nav>


            <div id="page-wrapper">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-lg-12">
                            <h1 class="page-header">分类列表</h1>
                        </div>
                        <!-- /.col-lg-12 -->
                    </div>
                    <!-- /.row -->
                    <div class="row">
                        <div class="col-lg-12">
                            <div class="panel panel-default">
                                <div class="panel-heading">
                                    点小三角形切换排序
                                </div>
                                <!-- /.panel-heading -->
                                <div class="panel-body">
                                    <div class="table-responsive">
                                        <table class="table table-striped table-bordered table-hover" id="dataTables-example">
                                            <thead>
                                                <tr>
                                                    <th>名称</th>
                                                    <th>显示排序</th>
                                                    <th>操作</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php if ($datas): ?>
                                                        <?php
                                                        $i = 0;
                                                        foreach ($datas as $k => $v):
                                                            ?>
                                                <tr class="<?php if ($i % 2 == 0) { ?> odd <?php } else { ?> even <?php } ?> gradeX">
                                                                <td>  <?php echo $v['cate_name']; ?>   </td>


                                                                                        <td class="center"><?php echo $v['sorts']; ?></td>
                                                                            <td class="center"> <a href='#'   onclick="edit('<?php echo $v['cate_id']; ?>')" > 修改 </a>



                                                                                            <a href='#'   onclick="if (confirm('确定删除？')) {
                                                                                                                                del('<?php echo $v['cate_id']; ?>');
                                                                                                                            }
                                                                                   " >    删除  </a> </td>
                                                                        </tr>
                                                                        <?php
                                                                        $i++;
                                                                    endforeach;
                                                                    ?>
                                                                <?php endif; ?>

                                            </tbody>
                                        </table>
                                    </div>
                                    <!-- /.table-responsive --> 
                                </div>
                                <!-- /.panel-body -->
                            </div>
                            <!-- /.panel -->
                        </div>
                        <!-- /.col-lg-12 -->
                    </div>
                    <!-- /.row -->

                    <!-- /.row -->

                    <!-- /.row -->

                    <!-- /.row -->
                </div>
                <!-- /.container-fluid -->
            </div>
            <!-- /#page-wrapper -->

        </div>
        <!-- /#wrapper -->

        <!-- jQuery -->
        <script src="./js/jquery.min.js"></script>

        <!-- Bootstrap Core JavaScript -->
        <script src="./js/bootstrap.min.js"></script>

        <!-- Metis Menu Plugin JavaScript -->
        <script src="./js/metisMenu.min.js"></script>

        <!-- DataTables JavaScript -->
        <script src="./js/dataTables/jquery.dataTables.min.js"></script>
        <script src="./js/dataTables/dataTables.bootstrap.min.js"></script>

        <!-- Custom Theme JavaScript -->
        <script src="./js/startmin.js"></script>
        <script src="./js/layer/layer.js"></script>

        <!-- Page-Level Demo Scripts - Tables - Use for reference -->
        <script>
                                                                                                $(document).ready(function () {
                                                                                                    $('#dataTables-example').DataTable({
                                                                                                        responsive: true
                                                                                                    });
                                                                                                });

                                                                                                function  del(id) {
                                                                                                    $.post("sorts.php", {'act': 'del', 'cate_id': id});
                                                                                                 layer.msg('删除完成');
                                                                                                     location.reload();
                                                                                                }

                                                                                                function edit(id) {

                                                                                                    $.post("sorts.php", {"act": "getCateInfo", "cate_id": id},
                                                                                                    function (data) {
                                                                                                        if (data.code == '0') {
                                                                                                            alert(data.msg);
                                                                                                        } else {
                                                                                                            $("#cate_name").val(data.data.cate_name);
                                                                                                            $("#sorts").val(data.data.sorts);
                                                                                                            $("#cate_id").val(data.data.cate_id);
                                                                                                        }
                                                                                                    }, "json");
                                                                                                    //页面层

                                                                                                    layer.open({
                                                                                                        type: 1,
                                                                                                        title: '修改分类',
                                                                                                        skin: 'layui-layer-rim', //加上边框
                                                                                                        area: ['420px', '260px'], //宽高
                                                                                                        content: $("#edits")
                                                                                                    });
                                                                                                    return false;
                                                                                                }
                                                                                                function do_edit()
                                                                                                {

                                                                                                    $.post("sorts.php", {'act': 'act_edit', 'cate_id':$("#cate_id").val(), 'cate_name': $("#cate_name").val(),'sorts' : $("#sorts").val() });
                                                                                                    layer.msg('修改成功');
                                                                                                    layer.closeAll('page'); 
                                                                                                    location.reload();

                                                                                                }
                                                                                                function do_close()
                                                                                                {
                                                                                                  layer.closeAll('page'); 
                                                                                                }
        </script>
        <div id="edits" style=" padding:10px; display: none ">
            <form role="form"   id='s1'   name='s1'     method="post">
                <div class="form-group">
                    <label>名称</label>
                    <input class="form-control" name="cate_name" id="cate_name"  >
                </div>


                <div class="form-group">
                    <label>排序</label>
                    <input class="form-control" name="sorts" id="sorts" placeholder="排序值 越小越前面"  >
                </div>

                <input  type="hidden"   name='cate_id' id='cate_id'  />

                <button type="button"  onclick="do_edit()" class="btn btn-default">Submit Button</button>
                <button type="button"   onclick="do_close()"  class="btn btn-default">Close</button>
            </form>
        </div>
    </body>
</html>
