<?php
    include_once("../global.php");
    if (!isset($_SESSION['admin_name']) || $_SESSION['admin_name'] == '') {
        header("location:login.php");
        exit;
    }

    if ($_REQUEST['act'] == 'edit') {
        if ($_REQUEST['website_name'] == '') {
            die('名称不能为空');
        }

        $sql = "update  websites set website_name ='" . $_REQUEST['website_name'] . "' ";
        if ($_REQUEST['cate_id'] == '0') {
            $recommend = '1';
        } else {
            $recommend = '0';
        }
        $sql .=" , cate_id='" . $_REQUEST['cate_id'] . "' , sorts='" . $_REQUEST['sorts'] . "' , url='" . $_REQUEST['url'] . "'  ,recommend='" . $recommend . "',remark='" . $_REQUEST['remark'] . "' ,url_type='" . $_REQUEST['url_type'] . "'   ";
        $upload_img = '';
        if ($_FILES['img']['tmp_name']) {
            $uploaddir = '../uploads/';
            // basename($_FILES['img']['name'])pathinfo($file)['extension'];
            $new_name = date("YmdHis") .".". pathinfo($_FILES['img']['name'])['extension'];

            $uploadfile = $uploaddir . $new_name;


            if (move_uploaded_file($_FILES['img']['tmp_name'], $uploadfile)) {
                 $upload_img = './uploads/' . $new_name;
                $sql .=",upload_img='" . $upload_img . "' ";
            }
        }
           $sql .=" where id ='" . $_REQUEST['edit_id'] . "' ";

        mysqli_query($link, $sql);
      echo '<script>alert("设置完成"); location.href="url.php"</script>';
        exit;
    }
    if (!isset($_REQUEST['id']) || $_REQUEST['id'] == '') {
       die('该数据不存在了');
    }
    $sql = "SELECT  * FROM websites WHERE id='" . $_REQUEST['id'] . "'  ";
    $res = mysqli_query($link, $sql);
    $row = mysqli_fetch_array($res);

    $cates = 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">

        <!-- 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"   class="active"><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" ><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>
                                <div class="panel-body">
                                    <div class="row">
                                        <div class="col-lg-6">
                                            <form role="form"  action="edit_url.php"   id='s1'   name='s1'   enctype="multipart/form-data" method="post">
                                                <div class="form-group">
                                                    <label>网站名称</label>
                                                    <input class="form-control" name="website_name" id="website_name"  value="<?php echo $row['website_name']; ?>" >
                                                </div>
                                                <div class="form-group">
                                                    <label>网址</label>
                                                    <input class="form-control" name="url" id="url"   value="<?php echo $row['url']; ?>" >
                                                </div>

                                                <div class="form-group">
                                                    <label>网址类型</label>
                                                    <label class="radio-inline">
                                                        <input type="radio" name="url_type" id="optionsRadiosInline1" value="0" <?php if ($row['url_type'] == '0') { ?> checked="" <?php } ?> > 文字
                                                    </label>
                                                    <label class="radio-inline">
                                                        <input type="radio" name="url_type" id="optionsRadiosInline2" value="1"  <?php if ($row['url_type'] == '1') { ?> checked="" <?php } ?> >图片
                                                    </label> 
                                                </div>
                                                <div class="form-group" id='b1' style='display:none'>
                                                    <label>上传图片</label>
                                                    <?php if ($row['upload_img'] != '') { ?> <?php echo $row['upload_img']; ?> <?php } ?>
                                                    <input type="file" name="img" id='img'>
                                                </div>

                                                <div class="form-group">
                                                    <label>网址位置</label>
                                                    <select class="form-control" name="cate_id" id="cate_id" >
                                                        <option value="0">推荐位</option>
                                                        <?php if ($cates): ?>
                                                                <?php foreach ($cates as $k => $v): ?>
                                                                                <option   value="<?= $v['cate_id'] ?>"  <?php if ($v['cate_id'] == $row['cate_id']) { ?>  selected=""  <?php } ?> ><?= $v['cate_name'] ?></option>
                                                                <?php endforeach; ?>
                                                            <?php endif; ?>
                                                    </select>
                                                </div>
                                                <div class="form-group">
                                                    <label>备注</label>
                                                    <input class="form-control" name="remark" id="remark" placeholder="备注仅用作备注，不显示"  value="<?php
                                                               echo $row['remark'];
                                                               ?>">
                                                </div>

                                                <div class="form-group">
                                                    <label>排序</label>
                                                    <input class="form-control" name="sorts" id="sorts" placeholder="排序值 越小越前面"  value="<?php echo $row['sorts']; ?>">
                                                </div>

                                                <input  type="hidden"   name='edit_id' id='edit_id' value="<?php echo $row['id']; ?>" />

                                                <input  type="hidden"   name='act' id='act' value="edit" />
                                                <button type="button" id="sub" class="btn btn-default">Submit Button</button>
                                                <button type="reset" class="btn btn-default">Reset Button</button>
                                            </form>
                                        </div> 
                                    </div>
                                    <!-- /.row (nested) -->
                                </div>
                                <!-- /.panel-body -->
                            </div>
                            <!-- /.panel -->
                        </div>
                        <!-- /.col-lg-12 -->
                    </div>
                    <!-- /.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>

        <!-- Custom Theme JavaScript -->
        <script src="./js/startmin.js"></script>
        <script>
            $(function () {
                $("#sub").click(function () {
                    if ($("#website_name").val() == '') {
                        alert('网址名称不能为空');
                        return false;
                    }
                    if ($("#url").val() == '') {
                        alert('网址不可为空');
                        return false;
                    }
                    $("#s1").submit();
                });

                $("#optionsRadiosInline1").click(function () {
                    $("#b1").hide();
                });
                $("#optionsRadiosInline2").click(function () {
                    $("#b1").show();
                });
            });
        </script>
    </body>
</html>
