<?php 
  include '../core/core.php';
?>
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>代刷网SEO后台管理</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="../public/static/layuiadmin/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="../public/static/layuiadmin/style/admin.css" media="all">
  <script src="../public/static/jquery-3.5.1.min.js"></script>
</head>
<body>
<div class="layui-form layui-form-pane">
   <?php 
   if($act=='edit' && $id){
      $viewInfo = $DB->getRow("SELECT * FROM `pre_view` WHERE `id`='".$id."'");
      $classInfo = $DB->getRow("SELECT * FROM `pre_class` WHERE `id` = '".$viewInfo['class']."'");     
      if(!$viewInfo){
        admin_error();
      }
      $title = $viewInfo['title'];
      $title2 = $viewInfo['title2'];
      $keywords = $viewInfo['keywords'];
      $description = $viewInfo['description'];
      $xdurl = $viewInfo['xdurl'];
      $newurl = $viewInfo['newurl'];
      $head = $viewInfo['head'];
      $foot = $viewInfo['foot'];
    }
  ?>
    <div class="layui-form-item">
          <?php if($act=='add'){ ?>
            <label class="layui-form-label">分类目录</label>
            <div class="layui-input-inline">
              <select lay-filter="class" name="class" id="class" lay-verify="required" default="1">
                <option value="0"></option>
                <?php
                  $classInfo = $DB->query("SELECT * FROM `pre_class` ORDER BY `id` ASC"); 
                  while($res = $classInfo->fetch()){                  
                    echo '<option value="'.$res['id'].'">'.$res['title'].'</option>';
                  }
                ?>
              </select>
            </div>
          </div>  
          <div id="isShow" style="display:none;text-align: center">
             <button class="layui-btn layui-btn-sm layui-btn-danger" id="al_rank" onclick="rand('all')"><i class="layui-icon layui-icon-senior"></i> 1.全部随机</button>
            <button class="layui-btn layui-btn-sm layui-btn-normal" id="al_add" lay-submit="" lay-filter="viewAdd"><i class="layui-icon layui-icon-add-circle"></i> 2.立即添加</button><hr>
            <div class="layui-form-item">
          <label class="layui-form-label">网站模板</label>
          <div class="layui-input-inline">
            <select lay-filter="muban" name="muban" id="muban" lay-verify="required">
              <?php 
                foreach(getMuban() as $v){
                  echo '<option value="'.$v.'">'.$v.'</option>';
                }
              ?>  
            </select>
          </div>
        </div>
          <?php }elseif($act=='edit'){ ?>
                 <label class="layui-form-label">分类目录</label>
            <div class="layui-input-inline">
              <select lay-filter="class" name="class" id="class" lay-verify="required" default="1">
                <?php
                  echo '<option value="'.$viewInfo['class'].'">当前：'.$classInfo['title'].'</option>';
                  echo '<option value="">---------</option>';
                  $classInfo = $DB->query("SELECT * FROM `pre_class` ORDER BY `id` ASC"); 
                  while($res = $classInfo->fetch()){                  
                    echo '<option value="'.$res['id'].'">'.$res['title'].'</option>';
                  }
                ?>
              </select>
            </div>
          </div>  
            <div id="isShow" style="display:;text-align: center">
              <div class="layui-form-item">
          <label class="layui-form-label">网站模板</label>
          <div class="layui-input-inline">
            <select lay-filter="muban" name="muban" id="muban" lay-verify="required">
              <?php 
                echo '<option value="'.$viewInfo['muban'].'">当前：'.$viewInfo['muban'].'</option>';
                echo '<option value="">---------</option>';
                foreach(getMuban() as $v){
                  echo '<option value="'.$v.'">'.$v.'</option>';
                }
              ?>
            </select>
          </div>
        </div>
          <?php } ?>
           <?php if($act=='pl' && $class){ ?>
            <center><font color="red">使用随机批量生成前建议您设置最少5个以上的关键词</font></center>
            <div class="layui-form-item">
            <label class="layui-form-label">当前分类</label>
            <div class="layui-input-inline">
              <input type="text" name="class" required="" value="<?php echo $_GET['class']?>" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input" disabled>
            </div>
          </div>
          <div class="layui-form-item">
            <label class="layui-form-label">生成数量</label>
            <div class="layui-input-inline">
              <input type="number" name="num" value="10" required="" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
          </div>
          <div class="layui-form-item">
            <label class="layui-form-label">网站模板</label>
            <div class="layui-input-inline">
              <select lay-filter="muban" name="muban" id="muban" lay-verify="required">
                <?php 
                  foreach(getMuban() as $v){
                    echo '<option value="'.$v.'">'.$v.'</option>';
                  }
                ?>  
              </select>
            </div>
        </div>
          <div class="layui-form-item">
            <label class="layui-form-label">下单链接</label>
            <div class="layui-input-inline">
              <input type="text" name="xdurl" id="xdurl" required="" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux"><button class="layui-btn layui-btn-xs layui-btn-info" onclick="site_url('xdurl')">本站链接</button></div>
          </div>
          <div class="layui-form-item">
            <label class="layui-form-label">自定义链接</label>
            <div class="layui-input-inline">
              <input type="text" name="newurl" id="newurl" required="" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux"><button class="layui-btn layui-btn-xs layui-btn-info" onclick="site_url('newurl')">本站链接</button></div>
          </div>
          <center><button class="layui-btn" lay-submit="" lay-filter="viewPlAdd">开始生成</button></center>
   <?php }else{?>
            <hr>
          <div class="layui-form-item">
            <label class="layui-form-label">网站名称</label>
            <div class="layui-input-inline">
              <input type="text" name="title" id="title" required="" value="<?php echo $viewInfo['title']?>" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux"><button class="layui-btn layui-btn-xs layui-btn-info" onclick="rand('title')">随机生成</button></div>
          </div>
          <div class="layui-form-item">
            <label class="layui-form-label">标题栏后缀</label>
            <div class="layui-input-inline">
              <input type="text" name="title2" id="title2" lay-verify=""  value="<?php echo $viewInfo['title2']?>" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux"><button class="layui-btn layui-btn-xs layui-btn-info" onclick="rand('title2')">随机添加</button></div>
          </div>
           <div class="layui-form-item">
            <label class="layui-form-label">关键词</label>
            <div class="layui-input-inline">
              <input type="text" name="keywords" id="keywords" lay-verify="" value="<?php echo $viewInfo['keywords']?>" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux"><button class="layui-btn layui-btn-xs layui-btn-info" onclick="rand('keywords')">随机添加</button></div>
          </div>
           <div class="layui-form-item">
            <label class="layui-form-label">网站描述</label>
            <div class="layui-input-inline">
              <input type="text" name="description" id="description" lay-verify="" value="<?php echo $viewInfo['description']?>" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux"><button class="layui-btn layui-btn-xs layui-btn-info" onclick="rand('description')">随机生成</button></div>
          </div>
           <div class="layui-form-item">
            <label class="layui-form-label">下单链接</label>
            <div class="layui-input-inline">
              <input type="text" name="xdurl" id="xdurl" required="" value="<?php echo $viewInfo['xdurl']?>" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux"><button class="layui-btn layui-btn-xs layui-btn-info" onclick="site_url('xdurl')">本站链接</button></div>
          </div>
          <div class="layui-form-item">
            <label class="layui-form-label">自定义链接</label>
            <div class="layui-input-inline">
              <input type="text" name="newurl" id="newurl" required="" value="<?php echo $viewInfo['newurl']?>" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux"><button class="layui-btn layui-btn-xs layui-btn-info" onclick="site_url('newurl')">本站链接</button></div>
          </div>
          <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">顶部菜单</label>
            <div class="layui-input-block">
              <textarea placeholder="链接名称|http://www.123.com，一行一条，可以不填" name="head" id="head" class="layui-textarea"><?php echo $viewInfo['head']?></textarea>
            </div>
          </div>
          <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">底部HTML</label>
            <div class="layui-input-block">
              <textarea placeholder="可以不填" name="foot" id="foot" class="layui-textarea"><?php echo $viewInfo['foot']?></textarea>
            </div>
          </div>
          <div class="layui-form-item" style="text-align: center;">
            <?php 
                if($act=='add'){
                  echo ' <button class="layui-btn" lay-submit="" lay-filter="viewAdd">立即添加</button>';
                }elseif($act=='edit' && $id){
                  echo ' <button class="layui-btn" lay-submit="" lay-filter="viewEdit">立即修改</button>';
                }
             ?>
          </div>
        </div>
        </div>
<?php } ?>
<script src="../public/static/layuiadmin/layui/layui.all.js"></script>
  <script>
  layui.config({
      base: '../public/static/layuiadmin/' //静态资源所在路径
    }).extend({
      index: 'lib/index' //主入口模块
    }).use(['index','form','table'], function(){
      var $ = layui.$
      ,table = layui.table
      ,layedit = layui.layedit
      ,laydate = layui.laydate
      ,form = layui.form
      ,admin = layui.admin
      ,element = layui.element
      ,layer = layui.layer;
      element.render();

      form.on('select(class)', function(data){
        $('#isShow').css('display','block');
      });

      form.on('submit(viewPlAdd)', function(data){
        classVal = data.field.class;
        num = data.field.num;
        muban = data.field.muban;
        xdurl = data.field.xdurl;
        newurl = data.field.newurl;
        var load = layer.load({time:false});
        $.ajax({
              url: "./ajax.php?act=viewPlAdd",
              data: {class:classVal,num,muban,xdurl,newurl},
              type: "POST",
              dataType: "json",
              success: function(data){
                  layer.close(load);
                  if(data.code==1){
                    layer.msg(data.msg,{icon:6});
                  }else{
                    layer.msg(data.msg,{icon:5});
                  } 
              },
              error: function(data){
                  layer.close(load);
                  layer.msg("未知错误");
              }
          });
      });

      form.on('submit(viewAdd)', function(data){
        classVal = data.field.class;
        muban = data.field.muban;
        title = data.field.title;
        title2 = data.field.title2;
        keywords = data.field.keywords;
        description = data.field.description;
        xdurl = data.field.xdurl;
        newurl = data.field.newurl;
        head = data.field.head;
        foot = data.field.foot;
        var load = layer.load({time:false});
        $.ajax({
              url: "./ajax.php?act=viewAdd",
              data: {class:classVal,muban,title,title2,keywords,description,xdurl,newurl,head,foot},
              type: "POST",
              dataType: "json",
              success: function(data){
                  layer.close(load);
                  if(data.code==1){
                    layer.msg(data.msg,{icon:6});
                  }else{
                    layer.msg(data.msg,{icon:5});
                  }
              },
              error: function(data){
                  layer.close(load);
                  layer.msg("未知错误");
              }
          });
      });

       form.on('submit(viewEdit)', function(data){
        id = $_GET['id'];
        classVal = data.field.class;
        muban = data.field.muban;
        title = data.field.title;
        title2 = data.field.title2;
        keywords = data.field.keywords;
        description = data.field.description;
        xdurl = data.field.xdurl;
        newurl = data.field.newurl;
        head = data.field.head;
        foot = data.field.foot;
        var load = layer.load({time:false});
        $.ajax({
              url: "./ajax.php?act=viewEdit",
              data: {id,class:classVal,muban,title,title2,keywords,description,xdurl,newurl,head,foot},
              type: "POST",
              dataType: "json",
              success: function(data){
                  layer.close(load);
                  if(data.code==1){
                    layer.msg(data.msg,{icon:6});
                  }else{
                    layer.msg(data.msg,{icon:5});
                  }
              },
              error: function(data){
                  layer.close(load);
                  layer.msg("未知错误");
              }
          });
      });
});


    function viewAuto(){
      setInterval(function() {
         $('#al_rank').trigger('click');  
         $('#al_add').trigger('click');  
      }, 5000);
    }

    function site_url(mod){
      $('#'+mod).val('<?php echo siteHttp();?>');
    }

    function rand(mod){
      classVal = $('#class').val();
      inputVal = $('#'+mod).val();
      if(classVal==0 || !classVal){
        layer.msg('请先选择分类');
        return false;
      }
      var load = layer.load({time:false});
      $.ajax({
          url: "./ajax.php?act=ClassViewContentRand",
          data: {classVal,mod},
          type: "POST",
          dataType: "json",
          success: function(data){
              layer.close(load);
              if(data.code!=1){
                  if(data.code==1){
                    layer.msg(data.msg,{icon:6});
                  }else{
                    layer.msg(data.msg,{icon:5});
                  }
                  return false;
              }
              if(mod=='all'){
                $('#title').val(data.title);
                $('#title2').val(data.title2);
                $('#keywords').val(data.keywords);
                $('#description').val(data.description);
                $('#xdurl').val('<?php echo siteHttp();?>');
                $('#newurl').val('<?php echo siteHttp();?>');
              }else if(mod=='title'){
                $('#'+mod).val(data.text);
              }else if(!inputVal){
                $('#'+mod).val(data.text);
              }else if(mod=='description'){
                $('#'+mod).val(data.text);
              }else if(inputVal){
                $('#'+mod).val(inputVal+','+data.text);
              }
          },
          error: function(data){
              layer.close(load);
              layer.msg("未知错误");
          }
      });
    }

  </script>
<?php 
  include 'foot.php';
?>