Path : /var/www/html/main/admin/
File Upload :
Current File : /var/www/html/main/admin/index10.php

<?php
/*!
 * Copyright Anucha Puangpaka
 * Contacts anucha.ppk@icloud.com 062-479-9836
 */

include('head.php');

if($_GET['nu']==""){
  login($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  ?>
  <div class="container">
    <form class="form-signin" action="index.php?nu=login" method="post">
      <?php
      if(!empty($rows['logo'])){
      ?>
        <center><img src="<?php echo $rows['logo']?>" width="100" alt="<?php echo $rows['title']?>"></center>
      <?php
      }
      ?>
      <center><h2 class="form-signin-heading">สำหรับผู้ดูแลระบบ</h2></center>
      <label for="inputUser" class="sr-only">ชื่อเข้าระบบ</label>
      <input type="user" name="user" id="inputUser" class="form-control" placeholder="ชื่อเข้าระบบ" required autofocus><br>
      <label for="inputPassword" class="sr-only">รหัสผ่าน</label>
      <input type="password" name="password" id="inputPassword" class="form-control" placeholder="รหัสผ่าน" required>
      <!-- <div class="checkbox">
        <label>
          <input type="checkbox" value="remember-me"> Remember me
        </label>
      </div> -->
      <button class="btn btn-lg btn-primary btn-block" type="submit">เข้าระบบ</button>
    </form>
  </div><!-- /.container -->
<?php
}

if($_GET['nu']=="login"){
  echo "<div class=\"container\">";
  $_POST['user'] = mysqli_real_escape_string($conn,$_POST['user']);
  $_POST['password'] = mysqli_real_escape_string($conn,$_POST['password']);
  $sql = "select * from ap_users where user='$_POST[user]' and password='$_POST[password]'";
  $query = mysqli_query($conn,$sql);
  $num_rows = mysqli_num_rows($query);
  $rows = mysqli_fetch_array($query);
  if($num_rows>0){
    $_SESSION['error'] = "";
    $_SESSION['id'] = $rows['id'];
    $_SESSION['user'] = $rows['user'];
    $_SESSION['password'] = $rows['password'];
    $_SESSION['apstatus'] = $rows['status'];
    mysqli_query($conn,"update ap_users set login='1' where id=$rows[id]");
    echo "<meta http-equiv='refresh' content='0;URL=index.php?nu=dashboard'>";
  }else{
    $_SESSION['error'] = "ชื่อเข้าระบบ หรือ รหัสผ่าน ไม่ถูกต้อง";
    echo "<meta http-equiv='refresh' content='0;URL=index.php'>";
  }
  echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
  echo "</div>";
}

if($_GET['nu']=='logout'){
  echo "<div class=\"content-wrapper\">";
  mysqli_query($conn,"update ap_users set login='' where id='$_SESSION[id]'");
  $_SESSION['error'] = "";
  $_SESSION['id'] = '';
  $_SESSION['user'] = '';
  $_SESSION['password'] = '';
  $_SESSION['apstatus'] = '';
  echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
  echo "<meta http-equiv='refresh' content='0;URL=index.php'>";
  echo "</div>";

  session_destroy();
}

// if(!empty($_SESSION['id']) && !empty($_SESSION['user']) && !empty($_SESSION['password'])){

if($_GET['nu']=="dashboard"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <?php
    if(!empty($logo)){
    ?>
      <center><img class="" src="<?php echo $logo?>" width="100" alt="<?php echo $rows['title']?>"></center>
    <?php
    }
    ?>
    <h2 class="sub-header" align="center"><?php echo $title?></h2>
    <div class="container theme-showcase body-main-row-body" role="main">
    <?php
    $getVersions = file_get_contents('https://kprucontrol.kpru.ac.th/_release-versions.php') or die ('ERROR');

    /*$headers = array("Content-Type: text/plain", "Method: GET", "");
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://kprucontrol.kpru.ac.th/_release-versions.php");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($ch);
    curl_close($ch); // print_r($result);
    $getVersions = $result;*/

    /*if(!empty($getVersions)){
      $versionList = explode("\n", $getVersions);
      foreach ($versionList as $aV){
        if ($aV > file_get_contents('../versions.php')){
          $found = true;

          if(!is_file('../updates/'.$aV.'.zip')){
            $newUpdate = file_get_contents('https://kprucontrol.kpru.ac.th/_kprucontrol/'.$aV.'.zip');
            if(!is_dir('../updates/')){ 
              mkdir ('../updates/');
            }
            $dlHandler = fopen('../updates/'.$aV.'.zip', 'w');
            if(!fwrite($dlHandler, $newUpdate)){ 
              echo '<p>ไม่สามารถบันทึกการปรับปรุง Version ใหม่ได้</p>'; 
              exit(); 
            }
            fclose($dlHandler);
          }

          if($_GET['doUpdate'] == true){
            echo '<p class="success">&raquo; สถานะอัพเกรด KPRUControl v'.$aV.'</p>';
            $zipHandle = str_replace('\admin','',getcwd()).('\updates\\'.$aV.'.zip');
            $zipHandle = zip_open($zipHandle); 
            echo '<ul>';
            while($aF = zip_read($zipHandle)){
              $thisFileName = zip_entry_name($aF);
              $thisFileDir = dirname($thisFileName);

              if(substr($thisFileName,-1,1) == '/') continue;

              if(!is_dir('../'.$thisFileDir)){
                mkdir('../'.$thisFileDir );
              }

              if (!is_dir('../'.$thisFileName) ) {
                echo '<li>'.$thisFileName.' ........... ';
                $contents = zip_entry_read($aF, zip_entry_filesize($aF));
                $contents = str_replace("", "", $contents);
                $updateThis = '';

                if ($thisFileName == 'versions.php' ){
                  $upgradeExec = fopen('../versions.php','w');
                  fwrite($upgradeExec, $contents);
                  fclose($upgradeExec);
                  echo' ปรับปรุงแล้ว</li>';
                }else{
                  $updateThis = fopen("../$thisFileName", 'wb');
                  fwrite($updateThis, $contents);
                  fclose($updateThis);
                  unset($contents);
                  echo' ปรับปรุงแล้ว</li>';
                }
              }
            }
            zip_close($zipHandle);
            echo '</ul>';
            $updated = true;
          }else{ 
            echo '<p></p><p>พร้อมสำหรับการอัพเกรด KPRUControl v'.$aV.' <a href="'.$_SERVER['PHP_SELF'].'?nu=dashboard&doUpdate=true">&raquo; ติดตั้งเลยหรือไม่?</a></p>';
            break;
          }
        }
      }

      if($updated == true){
        echo '<p class="success">&raquo; อัพเกรด KPRUControl $getVersions เรียบร้อย</p>';
      }elseif($found != true){ 
        echo "<p></p><p>&raquo; KPRUControl $getVersions ของท่านเป็น Version ล่าสุดอยู่แล้ว</p>"; 
      }
    }else{ 
      echo "<p></p><p>ไม่พบ KPRUControl Version ใหม่</p>"; 
    }*/
    ?>
    </div><!-- /.container -->
  </div>
<?php
}

if($_GET['nu']=="users"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <div class="row">
      <div class="col-lg-6 col-md-6 col-sm-12">
        <h2 style="margin: 0px">ผู้ดูแลระบบ</h2>
      </div>
      <div class="col-lg-6 col-md-6 col-sm-12 align-text-bottom">
        <form class="form-inline text-right" action="<?php echo $_SERVER[SCRIPT_NAME]; ?>" method="GET">
          <input type="hidden" name="nu" value="<?php echo $_GET['nu'] ?>">
          <div class="form-group">
            <input type="text" name="q" value="<?php echo $_GET['q']; ?>" placeholder="ระบุคำค้น" class="form-control" autofocus>
          </div>
          <button type="submit" class="btn btn-default">ค้นหา</button>
        </form>
      </div>
      <div class="col-lg-12 col-md-12 col-sm-12"><hr></div>
      <p></p>
    </div>
    <div class="table-responsive">
      <table class="table table-striped">
        <thead>
          <tr>
            <th width="100">ลำดับ</th>
            <th>สถานะ</th>
            <th>ชื่อ-นามสกุล</th>
            <th>Username</th>
            <th>Password</th>
            <th>System</th>
            <th width="100"><center>
              <?php
              if($_SESSION['user']==$rows['user'] || $_SESSION['apstatus']=="Admin"){
              ?>
                <a href="?nu=users_i_u&action=users_i_s">เพิ่ม</a>
              <?php
              }
              ?></center>
            </th>
          </tr>
        </thead>
        <tbody>
          <?php
          $sql = "select * from ap_users where fullname like '$_GET[q]%'";
          $query = mysqli_query($conn,$sql);
          $num_rows = mysqli_num_rows($query);

          $per_page = 50;
          $page  = 1;
          
          if(isset($_GET["Page"])){
            $page = $_GET["Page"];
          }

          $prev_page = $page-1;
          $next_page = $page+1;

          $row_start = (($per_page*$page)-$per_page);
          if($num_rows<=$per_page){
            $num_pages = 1;
          }elseif(($num_rows % $per_page)==0){
            $num_pages = ($num_rows/$per_page) ;
          }else{
            $num_pages =($num_rows/$per_page)+1;
            $num_pages = (int)$num_pages;
          }
          /*$row_end = $per_page * $page;
          if($row_end > $num_rows){
            $row_end = $num_rows;
          }*/
          $row_end = $per_page * $page;

          $sql .= " order by fullname asc limit $row_start, $row_end";
          $query = mysqli_query($conn,$sql);

          $i = (($row_start/$per_page)*$per_page);
          while($rows = mysqli_fetch_array($query)){
          $i++;
          ?>
            <tr>
              <td><?php echo $i;?>.</td>
              <td><?php echo $rows['status'];?></td>
              <td><?php if($_GET['user']=='$rows[user]'){ echo "<b>".$rows['fullname']."</b>"; }else{ echo $rows['fullname']; }?></td>
              <td><?php echo $rows['user'];?></td>
              <td>
                <?php
                if($_SESSION['user']==$rows['user'] || $_SESSION['apstatus']=="Admin"){
                  echo $rows['password'];
                }
                ?>
              </td>
              <td><a href="?nu=users&user=<?php echo $rows['user']?>">เลือก</a></td>
              <td><center>
                <?php
                if($_SESSION['user']==$rows['user'] || $_SESSION['apstatus']=="Admin"){
                ?>
                <a href="?nu=users_i_u&action=users_u_s&id=<?php echo $rows['id']?>">แก้ไข</a> :  <a href="#" onclick="if (confirm('ยืนยันการลบ?')) location.href='?nu=users_delete&id=<?php echo $rows['id']?>&user=<?php echo $rows['user']?>';">ลบ</a>
                <?php
                }
                ?></center>
              </td>
            </tr>
          <?php
          }
          ?>
        </tbody>
      </table>
      <?php if(!empty($_GET['user'])){ ?>
        <hr>
        <form method="post" action="?nu=users&job=1&user=<?php echo $_GET['user']?>">
        <input type="hidden" name="usersCode" value="<?php echo $_GET['user']?>">
        <table class="table table-striped">
        <thead>
          <tr>
            <th width="100">ลำดับ</th>
            <th>รหัส</th>
            <th>System</th>
            <th>สถานะ</th>
            <th><p align="center"><input type="button" value="ทั้งหมด" onclick="this.value=check(this.form['checkArray[]'])" class="btn btn-success"> <input type="submit" value="บันทึก" class="btn btn-danger"></p></th>
          </tr>
        </thead>
        <tbody>
        <?php
        $sql = "select * from ap_systems where status='1' order by code asc";
        $query = mysqli_query($conn,$sql);
        $i = 0;
        while($rows = mysqli_fetch_array($query)){
        $i++;
          $rows2 = mysqli_fetch_array(mysqli_query($conn,"select count(*) as count from ap_users_job where usersCode='$_GET[user]' and systemCode='$rows[code]'"));
        ?>
          <tr>
            <td><?php echo $i;?></td>
            <td><?php echo $rows['code'];?></td>
            <td><?php echo $rows['name'];?></td>
            <td><input type="checkbox" name="checkArray[]" value="<?php echo $rows['code'];?>" <?php if($rows2['count']>0){ echo "checked"; }?>></td>
            <td align="center"><?php if($rows2['count']>0){ echo "ทำงาน"; }else{ echo "ไม่ทำงาน"; }?></td>
          </tr>
        <?php
        }
        ?>
        </tbody>
        </table>
        </form>
        <?php
        if($_GET['job']==1){
          mysqli_query($conn,"delete from ap_users_job where usersCode='$_POST[usersCode]' ");
          for($i=0;$i<count($_POST['checkArray']);$i++){
            $n = $_POST['checkArray'][$i];
            $rows3 = mysqli_fetch_array(mysqli_query($conn,"select * from ap_systems where code='$n' "));
            $systemCode = $rows3['code'];
            mysqli_query($conn,"insert into ap_users_job (usersCode,systemCode) values ('$_POST[usersCode]','$systemCode')");
          }
          echo "<meta http-equiv='refresh' content='3;URL=index.php?nu=users&user=$_POST[usersCode]'>";
        }
      }
      ?>
    </div>
    <div class="text-right">
      <p><strong><?php echo number_format($num_rows);?> รายการ : <?php echo $num_pages;?> หน้า</strong></p>
      <ul class="pagination justify-content-end">
      <?php
      if($prev_page){
        echo "<li class=\"page-item\"><a class=\"page-link\" href='$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=1&q=$_GET[q]'>หน้าแรก</a></li>";
        echo "<li class=\"page-item\"><a class=\"page-link\" href='$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=$prev_page&q=$_GET[q]'><< ย้อนกลับ</a></li>";
      }

      for($i=1; $i<=$num_pages; $i++){
        if ($i > ($_GET['Page']-5) && $i < ($_GET['Page']+5)) {
          if($i != $page){
            echo "<li class=\"page-item\"><a class=\"page-link\" href='$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=$i&q=$_GET[q]'>$i</a></li>";
          }else{
            echo "<li class=\"page-item active\"><a class=\"page-link\" href=\"$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=$_GET[Page]&q=$_GET[q]#\">$i</a></li>";
          }
        }
      }
      if($page!=$num_pages){
        echo "<li class=\"page-item\"><a class=\"page-link\" href='$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=$next_page&q=$_GET[q]'>ถัดไป>></a></li>";
        echo "<li class=\"page-item\"><a class=\"page-link\" href='$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=$num_pages&q=$_GET[q]'>หน้าสุดท้าย</a></li>";
      }
      ?>
      </ul>
    </div>
  </div>
<?php
}

if($_GET['nu']=="users_i_u"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  $sql = "select * from ap_users where id='$_GET[id]'";
  $query = mysqli_query($conn,$sql);
  $rows = mysqli_fetch_array($query);
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">ผู้ดูแลระบบ</h2>
    <div class="table-responsive">
      <form action="?nu=<?php echo $_GET['action']?>" method="post" enctype="multipart/form-data">
      <input type="hidden" name="id" value="<?php echo $rows['id']?>">
      <table class="table table-striped">
        <tbody>
          <tr>
            <td>สถานะ</td>
            <td><select name="status" id="status" class="form-control">
            <?php
            $sql2 = "select * from ap_status order by code asc";
            $query2 = mysqli_query($conn,$sql2);
            while($rows2 = mysqli_fetch_array($query2)){
            ?>
              <option value="<?php echo $rows2['code']?>"
              <?php
              if($rows2['code']==$rows['status']){
                echo "selected='true'";
              }
              ?>
              ><?php echo $rows2['name']?></option>
            <?php
            }
            ?>
            </select></td>
          </tr>
          <tr>
            <td>ชื่อ-นามสกุล</td>
            <td><input type="text" name="fullname" value="<?php echo $rows['fullname']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>ชื่อเข้าระบบ</td>
            <td><input type="text" name="user" value="<?php echo $rows['user']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>รหัสผ่าน</td>
            <td><input type="<?php if($_SESSION['apstatus']=="Admin"){?>text<?php }else{ ?>password<?php }?>" name="password" value="<?php echo $rows['password']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>อีเมล์</td>
            <td><input type="text" name="email" value="<?php echo $rows['email']?>" class="form-control"></td>
          </tr>
          <tr>
            <td colspan="2" align="right"><input type="submit" value="บันทึก" class="btn btn-default"></td>
          </tr>
        </tbody>
      </table>
      </form>
    </div>
  </div>
<?php
}

if($_GET['nu']=="users_i_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content-wrapper\">";
  $table = "ap_users";
  $field = "user";
  $field2 = $_POST['user'];

  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2' "));
  if($nr>0){
    echo "<center><b>Data can not be saved.</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=users'>";
  }else{
    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    for($i=1; $i<$num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));

      $fields_1 .= "$fields, ";
      $fields_2 .= "'".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $field_1 = substr($fields_1,0,-2);
    $field_2 = substr($fields_2,0,-2);
    mysqli_query($conn,"insert into $table ($field_1) values ($field_2)");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=users'>";
  }
  echo "</div>";
}

if($_GET['nu']=="users_u_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content-wrapper\">";
  $table = "ap_users";
  $field = "id";
  $field2 = $_POST['id'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2' "));
  if($nr<0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=users&action=users_i_u&$field=$field2'>";
  }else{
    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    for($i=1; $i<$num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));

      $fields_ .= "$fields='".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $fields_set = substr($fields_,0,-2);
    mysqli_query($conn,"update $table set $fields_set where $field='$field2' ");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=users'>";
  }
  echo "</div>";
}

if ($_GET['nu']=="users_delete") {
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_users";
  mysqli_query($conn,"delete from $table where id='$_GET[id]'");
  mysqli_query($conn,"delete from ap_users_job where usersCode='$_GET[user]'");
  echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
  echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=users'>";
  echo "</div>";
}

if($_GET['nu']=="settings"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  $sql = "select * from ap_settings";
  $query = mysqli_query($conn,$sql);
  $rows = mysqli_fetch_array($query);
  if($_GET['action']==""){
    $_GET['action'] = "settings_u_s";
  }
  ?>
  <!-- <script type="text/javascript">
  function apSubmit(){
    document.getElementById('apForm').submit();
  }

  $(document).ready(function(){
      setInterval(function(){apSubmit()},300000);
  });
  </script> -->
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">ตั้งค่าทั่วไป</h2>
    <div class="table-responsive">
      <form id="apForm" action="?nu=<?php echo $_GET['action']?>" method="post" enctype="multipart/form-data">
      <fieldset>
      <input type="hidden" name="id" value="<?php echo $rows['id']?>">
      <table class="table table-striped">
        <tbody>
          <tr>
            <td>ชื่อเว็บไซต์</td>
            <td><input type="text" name="title" value="<?php echo $rows['title']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>ชื่อเว็บไซต์ ภาษาอังกฤษ</td>
            <td><input type="text" name="title_eng" value="<?php echo $rows['title_eng']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>ชื่อย่อเว็บไซต์</td>
            <td><input type="text" name="short_title" value="<?php echo $rows['short_title']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>สโลแกนเว็บไซต์</td>
            <td><input type="text" name="slogan" value="<?php echo $rows['slogan']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>สโลแกนเว็บไซต์ English</td>
            <td><input type="text" name="slogan_eng" value="<?php echo $rows['slogan_eng']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>คำสำคัญเว็บไซต์ Thai</td>
            <td><input type="text" name="keywords" value="<?php echo $rows['keywords']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>คำสำคัญเว็บไซต์ Eng</td>
            <td><input type="text" name="keywords_eng" value="<?php echo $rows['keywords_eng']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>คำอธิบายเว็บไซต์</td>
            <td><input type="text" name="description" value="<?php echo $rows['description']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>คำอธิบายเว็บไซต์ 2</td>
            <td><input type="text" name="description2" value="<?php echo $rows['description2']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>คำอธิบายเว็บไซต์ 3</td>
            <td><input type="text" name="description3" value="<?php echo $rows['description3']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>Favicon</td>
            <td><div class="input-group">
            <input type="text" id="favicon" name="favicon" value="<?php echo $rows['favicon']?>" class="form-control" placeholder="favicon.ico">
            <span class="input-group-btn">
              <button href="tinymce/plugins/filemanager/dialog.php?type=1&editor=mce_0&lang=eng&fldr=&field_id=favicon&popup=1" class="btn btn-default iframe-btn" type="button">SELECT</button>
            </span>
            </div></td>
          </tr>
          <tr>
            <td>สีพื้นส่วนหัว</td>
            <td><input type="text" name="header_color" value="<?php echo $rows['header_color']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>สีพื้นส่วนท้าย</td>
            <td><input type="text" name="footer_color" value="<?php echo $rows['footer_color']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>แบนเนอร์ส่วนหัว</td>
            <td><div class="input-group">
            <input type="text" id="header_logo" name="header_logo" value="<?php echo $rows['header_logo']?>" class="form-control" placeholder="Size 1440x250">
            <span class="input-group-btn">
              <button href="tinymce/plugins/filemanager/dialog.php?type=1&editor=mce_0&lang=eng&fldr=&field_id=header_logo&popup=1" class="btn btn-default iframe-btn" type="button">SELECT</button>
            </span>
            </div></td>
          </tr>
          <tr>
            <td>แบนเนอร์ส่วนหัว English</td>
            <td><div class="input-group">
            <input type="text" id="header_logo_eng" name="header_logo_eng" value="<?php echo $rows['header_logo_eng']?>" class="form-control" placeholder="Size 1440x250">
            <span class="input-group-btn">
              <button href="tinymce/plugins/filemanager/dialog.php?type=1&editor=mce_0&lang=eng&fldr=&field_id=header_logo_eng&popup=1" class="btn btn-default iframe-btn" type="button">SELECT</button>
            </span>
            </div></td>
          </tr>
          <tr>
            <td>Logo</td>
            <td><div class="input-group">
            <input type="text" id="logo" name="logo" value="<?php echo $rows['logo']?>" class="form-control" placeholder="Size 1024x1024">
            <span class="input-group-btn">
              <button href="tinymce/plugins/filemanager/dialog.php?type=1&editor=mce_0&lang=eng&fldr=&field_id=logo&popup=1" class="btn btn-default iframe-btn" type="button">SELECT</button>
            </span>
            </div></td>
          </tr>
          <tr>
            <td>Logo Touch</td>
            <td><div class="input-group">
            <input type="text" id="logo_touch" name="logo_touch" value="<?php echo $rows['logo_touch']?>" class="form-control" placeholder="Size 72x72">
            <span class="input-group-btn">
              <button href="tinymce/plugins/filemanager/dialog.php?type=1&editor=mce_0&lang=eng&fldr=&field_id=logo_touch&popup=1" class="btn btn-default iframe-btn" type="button">SELECT</button>
            </span>
            </div></td>
          </tr>
          <tr>
            <td>วันที่ให้บริการ</td>
            <td>
              <label for="disabledTextInput">ตัวอย่าง 20 มีนาคม 2522</label>
              <input type="text" name="date_counters" value="<?php echo $rows['date_counters']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>สถิติเริ่มต้นให้บริการ</td>
            <td>
              <label for="disabledTextInput">ค่าเริ่มต้น 0</label>
              <input type="text" name="visit_counters" value="<?php echo $rows['visit_counters']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>จำนวนรายการที่แสดง</td>
            <td>
              <label for="disabledTextInput">ตัวอย่าง 3</label>
              <input type="text" name="list" value="<?php echo $rows['list']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>Line Notify</td>
            <td><input type="text" name="line_notify" value="<?php echo $rows['line_notify']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>Line Notify2</td>
            <td><input type="text" name="line_notify2" value="<?php echo $rows['line_notify2']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>Line Notify3</td>
            <td><input type="text" name="line_notify3" value="<?php echo $rows['line_notify3']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>Line Notify4</td>
            <td><input type="text" name="line_notify4" value="<?php echo $rows['line_notify4']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>Line Notify5</td>
            <td><input type="text" name="line_notify5" value="<?php echo $rows['line_notify5']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>Line Notify6</td>
            <td><input type="text" name="line_notify6" value="<?php echo $rows['line_notify6']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>Line Notify7</td>
            <td><input type="text" name="line_notify7" value="<?php echo $rows['line_notify7']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>Line Notify8</td>
            <td><input type="text" name="line_notify8" value="<?php echo $rows['line_notify8']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>Line Notify9</td>
            <td><input type="text" name="line_notify9" value="<?php echo $rows['line_notify9']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>Google Translate</td>
            <td><label for="disabledTextInput">แสดง 1 / ไม่แสดง 2</label>
              <input type="text" name="google_translate" value="<?php echo $rows['google_translate']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>Google site verification</td>
            <td><input type="text" name="google_site_verification" value="<?php echo $rows['google_site_verification']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>Google analytics</td>
            <td><input type="text" name="google_analytics" value="<?php echo $rows['google_analytics']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>Google Tag Manager</td>
            <td><input type="text" name="google_tag_manager" value="<?php echo $rows['google_tag_manager']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>Facebook App ID</td>
            <td><input type="text" name="facebook_app_id" value="<?php echo $rows['facebook_app_id']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>Facebook App Secret</td>
            <td><input type="text" name="facebook_app_secret" value="<?php echo $rows['facebook_app_secret']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>Facebook Page</td>
            <td><input type="text" name="facebook_page" value="<?php echo $rows['facebook_page']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>Facebook Page ID</td>
            <td><input type="text" name="facebook_page_id" value="<?php echo $rows['facebook_page_id']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>Twitter</td>
            <td><input type="text" name="twitter" value="<?php echo $rows['twitter']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>Instagram</td>
            <td><input type="text" name="instagram" value="<?php echo $rows['instagram']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>Line</td>
            <td><input type="text" name="line" value="<?php echo $rows['line']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>Youtube Channels</td>
            <td><input type="text" name="youtube_channels" value="<?php echo $rows['youtube_channels']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>Majestic Site Verification</td>
            <td><input type="text" name="majestic_site_verification" size="40" value="<?php echo $rows['majestic_site_verification']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>Yandex Verification</td>
            <td><input type="text" name="yandex_verification" size="40" value="<?php echo $rows['yandex_verification']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>Bing Verification</td>
            <td><input type="text" name="bing_verification" size="40" value="<?php echo $rows['bing_verification']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>ส่วนท้าย คอลัมน์ที่ 1</td>
            <td><textarea name="foot_col_1" class="form-control"><?php echo htmlspecialchars($rows['foot_col_1']); ?></textarea></td>
          </tr>
          <tr>
            <td>ส่วนท้าย คอลัมน์ที่ 2</td>
            <td><textarea name="foot_col_2" class="form-control"><?php echo htmlspecialchars($rows['foot_col_2']); ?></textarea></td>
          </tr>
          <tr>
            <td>ส่วนท้าย คอลัมน์ที่ 3</td>
            <td><textarea name="foot_col_3" class="form-control"><?php echo htmlspecialchars($rows['foot_col_3']); ?></textarea></td>
          </tr>
          <tr>
            <td>ส่วนท้าย คอลัมน์ที่ 4</td>
            <td><textarea name="foot_col_4" class="form-control"><?php echo htmlspecialchars($rows['foot_col_4']); ?></textarea></td>
          </tr>
          <tr>
            <td>ส่วนท้าย คอลัมน์ที่ 1 English</td>
            <td><textarea name="foot_col_1_eng" class="form-control"><?php echo htmlspecialchars($rows['foot_col_1_eng']); ?></textarea></td>
          </tr>
          <tr>
            <td>ส่วนท้าย คอลัมน์ที่ 2 English</td>
            <td><textarea name="foot_col_2_eng" class="form-control"><?php echo htmlspecialchars($rows['foot_col_2_eng']); ?></textarea></td>
          </tr>
          <tr>
            <td>ส่วนท้าย คอลัมน์ที่ 3 English</td>
            <td><textarea name="foot_col_3_eng" class="form-control"><?php echo htmlspecialchars($rows['foot_col_3_eng']); ?></textarea></td>
          </tr>
          <tr>
            <td>ส่วนท้าย คอลัมน์ที่ 4 English</td>
            <td><textarea name="foot_col_4_eng" class="form-control"><?php echo htmlspecialchars($rows['foot_col_4_eng']); ?></textarea></td>
          </tr>
          <tr>
            <td>รูปแบบ Themes</td>
            <td><select name="themes" class="form-control">
              <?php
              $sql2 = "select * from ap_themes order by code asc";
              $query2 = mysqli_query($conn,$sql2);
              while($rows2 = mysqli_fetch_array($query2)){
              ?>
                <option value="<?php echo $rows2['code']?>"
                <?php
                if($rows2['code']==$rows['themes']){
                  echo "selected='true'";
                }
                ?>
                ><?php echo $rows2['name']?></option>
              <?php
              }
              ?>
              </select></td>
          </tr>
          <tr>
            <td>Option Themes Columns 1</td>
            <td><div class="input-group col-xs-5">
                <span class="input-group-addon">
                  <input type="checkbox" name="columns1" value="1" <?php if($rows['columns1']==1){ echo "checked"; } ?>>
                </span>
                <input placeholder="6" type="text" name="columns1_width" value="<?php echo $rows['columns1_width']?>" class="col-lg-3 form-control">
              </div>
              <p></p>
              <label>*** รวมกันกับ Columns 1+2+3=12 ที่มีเครื่องหมายถูก</label>
            </td>
          </tr>
          <tr>
            <td>Option Themes Columns 2</td>
            <td><div class="input-group">
                <span class="input-group-addon">
                  <input type="checkbox" name="columns2" value="1" <?php if($rows['columns2']==1){ echo "checked"; } ?>>
                </span>
                <input placeholder="3" type="text" name="columns2_width" value="<?php echo $rows['columns2_width']?>" class="form-control">
              </div>
            </div>
            </td>
          </tr>
          <tr>
            <td>Option Themes Columns 3</td>
            <td><div class="input-group">
                <span class="input-group-addon">
                  <input type="checkbox" name="columns3" value="1" <?php if($rows['columns3']==1){ echo "checked"; } ?>>
                </span>
                <input placeholder="3" type="text" name="columns3_width" value="<?php echo $rows['columns3_width']?>" class="form-control">
              </div>
            </div>
            </td>
          </tr>
          <tr>
            <td>คลังรูปภาพ</td>
            <td><div class="input-group col-xs-5">
                <span class="input-group-addon">
                  <input type="checkbox" name="group_gallery" value="1" <?php if($rows['group_gallery']==1){ echo "checked"; } ?>>
                </span>
                <input placeholder="ชื่อคลังรูปภาพ" type="text" name="group_gallery_name" value="<?php echo $rows['group_gallery_name']?>" class="col-lg-3 form-control">
              </div>
              <p></p>
              <label>*** ทำเครื่องหมายถูก กรณีต้องการแสดงคลังรูปทั้งหมดโดยไม่แยกตามแค็ตตาล็อก</label>
            </td>
          </tr>
          <tr>
            <td>แสดงคลังรูปภาพด้านล่าง หน้า/โพสต์</td>
            <td><label for="disabledTextInput">แสดง 1 / ไม่แสดง 2</label>
              <input type="text" name="group_gallery_show" value="<?php echo $rows['group_gallery_show']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>External Banner Top</td>
            <td><label for="disabledTextInput">แสดงปกติ 1 / แสดงด้านล่างสไลด์ 2</label>
              <input type="text" name="externalbanner_bottom_slide" value="<?php echo $rows['externalbanner_bottom_slide']?>" class="form-control"></td></td>
          </tr>
          <tr>
            <td>ปรับระดับสี่ Grayscale</td>
            <td><label for="disabledTextInput">ค่าเริ่มต้น 0% ปกติ</label>
              <input type="text" name="grayscale" value="<?php echo $rows['grayscale']?>" class="form-control"></td></td>
          </tr>
          <tr>
            <td colspan="2" align="right"><input type="submit" value="บันทึก" class="btn btn-default"></td>
          </tr>
        </tbody>
      </table>
      </fieldset>
      </form>
    </div>
  </div>
<?php
}

if($_GET['nu']=="settings_u_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_settings";
  $field = "id";
  $field2 = $_POST['id'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2' "));
  if($nr<0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=settings&action=settings_u_s&$field=$field2'>";
  }else{
    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    $fields_ = '';
    for($i=1; $i<$num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];
      
      if($fields!="foot_col_1" && $fields!="foot_col_2" && $fields!="foot_col_3" && $fields!="foot_col_4" && $fields!="foot_col_1_eng" && $fields!="foot_col_2_eng" && $fields!="foot_col_3_eng" && $fields!="foot_col_4_eng"){
        $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));
      }

      $fields_ .= "$fields='".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $fields_set = substr($fields_,0,-2);
    mysqli_query($conn,"update $table set $fields_set where $field='$field2'");
    //echo "update $table set $fields_set where $field='$field2'";
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=settings'>";
  }
  echo "</div>";
}

if($_GET['nu']=="slides"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <div class="row">
      <div class="col-lg-6 col-md-6 col-sm-12">
        <h2 style="margin: 0px">สไลด์-แบนเนอร์</h2>
      </div>
      <div class="col-lg-6 col-md-6 col-sm-12 align-text-bottom">
        <form class="form-inline text-right" action="<?php echo $_SERVER[SCRIPT_NAME]; ?>" method="GET">
          <input type="hidden" name="nu" value="<?php echo $_GET['nu'] ?>">
          <div class="form-group">
            <input type="text" name="q" value="<?php echo $_GET['q']; ?>" placeholder="ระบุคำค้น" class="form-control" autofocus>
          </div>
          <button type="submit" class="btn btn-default">ค้นหา</button>
        </form>
      </div>
      <div class="col-lg-12 col-md-12 col-sm-12"><hr></div>
      <p></p>
    </div>
    <div class="table-responsive">
      <table class="table table-striped">
        <thead>
          <tr>
            <th width="70">ลำดับ</th>
            <th>ภาษา</th>
            <th>วันที่</th>
            <th width="100">ตำแหน่ง</th>
            <th>ชื่อ</th>
            <th>ลำดับ</th>
            <th>สถานะ</th>
            <th width="100"><a href="?nu=slides_i_u&action=slides_i_s">เพิ่ม</a></th>
          </tr>
        </thead>
        <tbody>
          <?php
          if($_SESSION['apstatus']=="Admin"){
            $sql = "select * from ap_slides where name like '$_GET[q]%'";
          }else{
            $sql = "select * from ap_slides where name like '$_GET[q]%' and createBy='$_SESSION[user]'";
          }
          $query = mysqli_query($conn,$sql);
          $num_rows = mysqli_num_rows($query);

          $per_page = 50;
          $page  = 1;

          if(isset($_GET["Page"])){
            $page = $_GET["Page"];
          }

          $prev_page = $page-1;
          $next_page = $page+1;

          $row_start = (($per_page*$page)-$per_page);
          if($num_rows<=$per_page){
            $num_pages = 1;
          }elseif(($num_rows % $per_page)==0){
            $num_pages = ($num_rows/$per_page) ;
          }else{
            $num_pages =($num_rows/$per_page)+1;
            $num_pages = (int)$num_pages;
          }
          /*$row_end = $per_page * $page;
          if($row_end > $num_rows){
            $row_end = $num_rows;
          }*/
          $row_end = $per_page;

          $sql .= " order by id desc, level asc limit $row_start, $row_end";
          $query = mysqli_query($conn,$sql);

          $i = (($row_start/$per_page)*$per_page);
          while($rows = mysqli_fetch_array($query)){
          $i++;
            $place = "";
            if($rows['place']==1){
              $place = "<p>สไลด์</p>";
            }
            if($rows['place']==2){
              $place = "<p>สไลด์ ข่าวฝากประชาสัมพันธ์</p>";
            }
            if($rows['place2']==2){
              $place .= "<p>คอลัมน์ที่ 2</p>";
            }
            if($rows['place3']==3){
              $place .= "<p>คอลัมน์ที่ 3</p>";
            }
            if($rows['place4']==4){
              $place .= "<p>สไลด์ประชาสัมพันธ์</p>";
            }
            if($rows['place5']==1){
              $place .= "<p>สไลด์ประกาศ</p>";
            }
            ?>
            <tr>
              <td><?php echo $i;?></td>
              <td><?php echo $rows['lang'];?></td>
              <td <?php if($d>$rows['date_time']){ echo "bgcolor='red'"; } ?>><?php echo $rows['date_time'];?></td>
              <td><?php echo $place;?></td>
              <td width="350"><a href="<?php echo $rows['images'];?>" target="new"><?php echo $rows['name'];?></a></td>
              <td><?php echo $rows['level'];?></td>
              <td><?php echo $rows['status'];?></td>
              <td><?php if($_SESSION['apstatus']=="Admin" || $rows['createBy']==$_SESSION['user']){?><a href="?nu=slides_i_u&action=slides_u_s&id=<?php echo $rows['id']?>">แก้ไข</a> :  <a href="#" onclick="if (confirm('ยืนยันการลบ?')) location.href='?nu=slides_delete&id=<?php echo $rows['id']?>';">ลบ</a><?php } ?></td>
            </tr>
          <?php
          }
          ?>
        </tbody>
      </table>
    </div>
    <div class="text-right">
      <p><strong><?php echo number_format($num_rows);?> รายการ : <?php echo $num_pages;?> หน้า</strong></p>
      <ul class="pagination justify-content-end">
      <?php
      if($prev_page){
        echo "<li class=\"page-item\"><a class=\"page-link\" href='$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=1&q=$_GET[q]'>หน้าแรก</a></li>";
        echo "<li class=\"page-item\"><a class=\"page-link\" href='$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=$prev_page&q=$_GET[q]'><< ย้อนกลับ</a></li>";
      }

      for($i=1; $i<=$num_pages; $i++){
        if ($i > ($_GET['Page']-5) && $i < ($_GET['Page']+5)) {
          if($i != $page){
            echo "<li class=\"page-item\"><a class=\"page-link\" href='$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=$i&q=$_GET[q]'>$i</a></li>";
          }else{
            echo "<li class=\"page-item active\"><a class=\"page-link\" href=\"$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=$_GET[Page]&q=$_GET[q]#\">$i</a></li>";
          }
        }
      }
      if($page!=$num_pages){
        echo "<li class=\"page-item\"><a class=\"page-link\" href='$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=$next_page&q=$_GET[q]'>ถัดไป>></a></li>";
        echo "<li class=\"page-item\"><a class=\"page-link\" href='$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=$num_pages&q=$_GET[q]'>หน้าสุดท้าย</a></li>";
      }
      ?>
      </ul>
    </div>
  </div>
<?php
}

if($_GET['nu']=="slides_i_u"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  $sql = "select * from ap_slides where id='$_GET[id]'";
  $query = mysqli_query($conn,$sql);
  $rows = mysqli_fetch_array($query);

  if($rows['place2']==2){
    $ck_2 = " checked=\"true\"";
  }else{ $ck_2 = ''; }
  if($rows['place3']==3){
    $ck_3 = " checked=\"true\"";
  }else{ $ck_3 = ''; }
  if($rows['place4']==4){
    $ck_4 = " checked=\"true\"";
  }else{ $ck_4 = ''; }
  if($rows['place5']==1){
    $ck_5 = " checked=\"true\"";
  }else{ $ck_5 = ''; }
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">สไลด์-แบนเนอร์</h2>
    <div class="table-responsive">
      <form action="?nu=<?php echo $_GET['action']?>" method="post" enctype="multipart/form-data">
      <input type="hidden" name="id" value="<?php echo $rows['id']?>">
      <input type="hidden" name="createBy" value="<?php echo $rows['createBy']?>">
      <table class="table table-striped">
        <tbody>
          <tr>
            <td>สำหรับเว็บไซต์ภาษา</td>
            <td><select name="lang" class="form-control">
            <?php
            $sql2 = "select * from ap_langs order by code asc";
            $query2 = mysqli_query($conn,$sql2);
            while($rows2 = mysqli_fetch_array($query2)){
            ?>
              <option value="<?php echo $rows2['code']?>"
              <?php
              if($rows2['code']==$rows['lang']){
                echo "selected='true'";
              }
              ?>
              ><?php echo $rows2['name']?></option>
            <?php
            }
            ?>
            </select></td>
          </tr>
          <tr>
            <td>วันที่</td>
            <td>
              <label for="disabledTextInput">ระบุ วันที่สิ้นสุดของ สไลด์-แบนเนอร์ ต.ย. <?php echo date('Ymd')?></label>
              <input type="text" name="date_time" value="<?php if(empty($rows['date_time'])){ echo date('Ymd'); }else{ echo $rows['date_time']; } ?>" class="form-control"></td>
          </tr>
          <tr>
            <td>ชื่อ สไลด์-แบนเนอร์</td>
            <td>
              <input type="text" name="name" value="<?php echo $rows['name']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>ลำดับรูปภาพคอลัมน์ที่ 3</td>
            <td>
              <label for="disabledTextInput">กรณีไม่กำหนดให้ระบุ 00</label>
              <input type="text" name="level" value="<?php if(empty($rows['level'])){ echo "00"; }else{ echo $rows['level']; } ?>" class="form-control"></td>
          </tr>
          <tr>
            <td>รูปแบบ <?php echo $rows['place'] ?></td>
            <td>
              <label class="checkbox-inline">
                <input type="checkbox" name="place" value="1" <?php if($rows['place']==1){ echo "checked"; } ?>> สไลด์ 1170x500
              </label>
              <label class="checkbox-inline">
                <input type="checkbox" name="place" value="2" <?php if($rows['place']==2){ echo "checked"; } ?>> สไลด์ 300x300
              </label>
              <label class="checkbox-inline">
                <input type="checkbox" name="place2" value="2"<?php echo $ck_2?>>คอลัมน์ที่ 2
              </label>
              <label class="checkbox-inline">
                <input type="checkbox" name="place3" value="3"<?php echo $ck_3?>>คอลัมน์ที่ 3
              </label>
              <label class="checkbox-inline">
                <input type="checkbox" name="place4" value="4"<?php echo $ck_4?>>สไลด์ประชาสัมพันธ์
              </label>
              <label class="checkbox-inline">
                <input type="checkbox" name="place5" value="1"<?php echo $ck_5?>>สไลด์ประกาศ
              </label>
            </td>
          </tr>
          <tr>
            <td>แสดง</td>
            <td>
              <label for="disabledTextInput">0 = ไม่แสดง 1 = แสดง</label>
              <input type="text" name="status" value="<?php echo $rows['status']; ?>" class="form-control"></td>
          </tr>
          <tr>
            <td>URL Link</td>
            <td><div class="input-group">
            <input type="text" id="url_link" name="url_link" value="<?php echo $rows['url_link']?>" class="form-control" placeholder="">
            <span class="input-group-btn">
              <button href="tinymce/plugins/filemanager/dialog.php?type=2&editor=mce_0&lang=eng&fldr=&field_id=url_link&popup=1" class="btn btn-default iframe-btn" type="button">SELECT</button>
            </span>
            </div></td>
          </tr>
          <tr>
            <td>รูปภาพ</td>
            <td><div class="input-group">
            <input type="text" id="images" name="images" value="<?php echo $rows['images']?>" class="form-control" placeholder="รูปภาพประกอบ">
            <span class="input-group-btn">
              <button href="tinymce/plugins/filemanager/dialog.php?type=1&editor=mce_0&lang=eng&fldr=&field_id=images&popup=1" class="btn btn-default iframe-btn" type="button">SELECT</button>
            </span>
            </div></td>
          </tr>
          <tr>
            <td colspan="2" align="right"><input type="submit" value="บันทึก" class="btn btn-default"></td>
          </tr>
        </tbody>
      </table>
      </form>
    </div>
  </div>
<?php
}

if($_GET['nu']=="slides_i_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  if($_POST['createBy']==""){
    $_POST['createBy'] = $_SESSION['user'];
  }
  $table = "ap_slides";
  $query = mysqli_query($conn,"select * from $table");
  $num_fields = mysqli_num_fields($query);
  for($i=1; $i<$num_fields; $i++) {
    $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
    $fields = $field_name[$i];

    $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));

    $fields_1 .= "$fields, ";
    $fields_2 .= "'".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
  }
  $field_1 = substr($fields_1,0,-2);
  $field_2 = substr($fields_2,0,-2);
  mysqli_query($conn,"insert into $table ($field_1) values ($field_2)");
  echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
  echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=slides'>";
  echo "</div>";
}

if($_GET['nu']=="slides_u_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_slides";
  $field = "id";
  $field2 = $_POST['id'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2'"));
  if($nr<0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=slides&action=slides_i_u&$field=$field2'>";
  }else{
    if($_POST['createBy']==""){
      $_POST['createBy'] = $_SESSION['user'];
    }
    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    $fields_ = '';
    for($i=1; $i<$num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));

      $fields_ .= "$fields='".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $fields_set = substr($fields_,0,-2);
    mysqli_query($conn,"update $table set $fields_set where $field='$field2' ");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=slides'>";
  }
  echo "</div>";
}

if ($_GET['nu']=="slides_delete") {
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_slides";
  //$images = str_replace("$ap_site$ap_folder", "", "../$_GET[images]"); //echo "<p align=right>$images</p>";
  //unlink($images);
  //http://lis.kpru.ac.th/admin/tinymce/plugins/filemanager/dialog.php?del_file=201701261485401745.jpg&type=0&editor=mce_0&lang=en_EN&fldr=images/&field_id=
  mysqli_query($conn,"delete from $table where id='$_GET[id]'");
  echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
  echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=slides'>";
  echo "</div>";
}

if($_GET['nu']=="menus"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <div class="row">
      <div class="col-lg-6 col-md-6 col-sm-12">
        <h2 style="margin: 0px">เมนูหลัก</h2>
      </div>
      <div class="col-lg-6 col-md-6 col-sm-12 align-text-bottom">
        <form class="form-inline text-right" action="<?php echo $_SERVER[SCRIPT_NAME]; ?>" method="GET">
          <input type="hidden" name="nu" value="<?php echo $_GET['nu'] ?>">
          <div class="form-group">
            <input type="text" name="q" value="<?php echo $_GET['q']; ?>" placeholder="ระบุคำค้น" class="form-control" autofocus>
          </div>
          <button type="submit" class="btn btn-default">ค้นหา</button>
        </form>
      </div>
      <div class="col-lg-12 col-md-12 col-sm-12"><hr></div>
      <p></p>
    </div>
    <div class="table-responsive">
      <table class="table table-striped">
        <thead>
          <tr>
            <th width="70">ลำดับ</th>
            <th>ภาษา</th>
            <th>ลำดับเมนู</th>
            <th>ประเภท Link</th>
            <th>รหัส</th>
            <th>ชื่อ</th>
            <th width="100"><a href="?nu=menus_i_u&action=menus_i_s">เพิ่ม</a></th>
          </tr>
        </thead>
        <tbody>
          <?php
          $sql = "select * from ap_menus where name like '$_GET[q]%'";
          $query = mysqli_query($conn,$sql);
          $num_rows = mysqli_num_rows($query);

          $per_page = 50;
          $page  = 1;

          if(isset($_GET["Page"])){
            $page = $_GET["Page"];
          }

          $prev_page = $page-1;
          $next_page = $page+1;

          $row_start = (($per_page*$page)-$per_page);
          if($num_rows<=$per_page){
            $num_pages = 1;
          }elseif(($num_rows % $per_page)==0){
            $num_pages = ($num_rows/$per_page) ;
          }else{
            $num_pages =($num_rows/$per_page)+1;
            $num_pages = (int)$num_pages;
          }
          /*$row_end = $per_page * $page;
          if($row_end > $num_rows){
            $row_end = $num_rows;
          }*/
          $row_end = $per_page;

          $sql .= " order by level asc limit $row_start, $row_end";
          $query = mysqli_query($conn,$sql);

          $i = (($row_start/$per_page)*$per_page);
          while($rows = mysqli_fetch_array($query)){
          $i++;
            if($rows['type_link']==1){
              $typelink = "URL Link";
            }
            if($rows['type_link']==2){
              $typelink = "หน้า-โพสต์";
            }
            if($rows['type_link']==3){
              $typelink = "แค็ตตาล็อก";
            }
          ?>
            <tr>
              <td><?php echo $i;?></td>
              <td><?php echo $rows['lang'];?></td>
              <td><?php echo $rows['level'];?></td>
              <td><?php echo $typelink;?></td>
              <td><?php echo $rows['code'];?></td>
              <td><?php echo $rows['name'];?></td>
              <td><a href="?nu=menus_i_u&action=menus_u_s&id=<?php echo $rows['id']?>">แก้ไข</a> :  <a href="#" onclick="if (confirm('ยืนยันการลบ?')) location.href='?nu=menus_delete&id=<?php echo $rows['id']?>';">ลบ</a></td>
            </tr>
          <?php
          }
          ?>
        </tbody>
      </table>
    </div>
    <div class="text-right">
      <p><strong><?php echo number_format($num_rows);?> รายการ : <?php echo $num_pages;?> หน้า</strong></p>
      <ul class="pagination justify-content-end">
      <?php
      if($prev_page){
        echo "<li class=\"page-item\"><a class=\"page-link\" href='$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=1&q=$_GET[q]'>หน้าแรก</a></li>";
        echo "<li class=\"page-item\"><a class=\"page-link\" href='$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=$prev_page&q=$_GET[q]'><< ย้อนกลับ</a></li>";
      }

      for($i=1; $i<=$num_pages; $i++){
        if ($i > ($_GET['Page']-5) && $i < ($_GET['Page']+5)) {
          if($i != $page){
            echo "<li class=\"page-item\"><a class=\"page-link\" href='$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=$i&q=$_GET[q]'>$i</a></li>";
          }else{
            echo "<li class=\"page-item active\"><a class=\"page-link\" href=\"$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=$_GET[Page]&q=$_GET[q]#\">$i</a></li>";
          }
        }
      }
      if($page!=$num_pages){
        echo "<li class=\"page-item\"><a class=\"page-link\" href='$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=$next_page&q=$_GET[q]'>ถัดไป>></a></li>";
        echo "<li class=\"page-item\"><a class=\"page-link\" href='$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=$num_pages&q=$_GET[q]'>หน้าสุดท้าย</a></li>";
      }
      ?>
      </ul>
    </div>
  </div>
<?php
}

if($_GET['nu']=="menus_i_u"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  $sql = "select * from ap_menus where id='$_GET[id]'";
  $query = mysqli_query($conn,$sql);
  $rows = mysqli_fetch_array($query);
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">เมนูหลัก</h2>
    <div class="table-responsive">
      <form action="?nu=<?php echo $_GET['action']?>" method="post" enctype="multipart/form-data">
      <input type="hidden" name="id" value="<?php echo $rows['id']?>">
      <table class="table table-striped">
        <tbody>
          <tr>
            <td>สำหรับเว็บไซต์ภาษา</td>
            <td><select name="lang" class="form-control">
            <?php
            $sql2 = "select * from ap_langs order by code asc";
            $query2 = mysqli_query($conn,$sql2);
            while($rows2 = mysqli_fetch_array($query2)){
            ?>
              <option value="<?php echo $rows2['code']?>"
              <?php
              if($rows2['code']==$rows['lang']){
                echo "selected='true'";
              }
              ?>
              ><?php echo $rows2['name']?></option>
            <?php
            }
            ?>
            </select></td>
          </tr>
          <tr>
            <td>ลำดับเมนู</td>
            <td>
            <label for="disabledTextInput">ตัวอย่าง 00</label>
            <input type="text" name="level" value="<?php if(empty($rows['level'])){ echo "00"; }else{ echo $rows['level']; } ?>" class="form-control"></td>
          </tr>
          <tr>
            <td>รหัส</td>
            <td><input type="text" name="code" value="<?php echo $rows['code']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>ชื่อ</td>
            <td><input type="text" name="name" value="<?php echo $rows['name']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>ประเภท Link</td>
            <td>
            <label for="disabledTextInput">1 = URL Link หรือ 2 = หน้า-โพสต์ หรือ 3 = แค็ตตาล็อก</label>
            <input type="text" name="type_link" size="5" value="<?php echo $rows['type_link']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>URL Link</td>
            <td>
            <label for="disabledTextInput">ถ้าไม่ระบุกรุณาใส่ #</label>
            <input type="text" name="url_link" size="70" value="<?php if(empty($rows['url_link'])){ echo "#"; }else{ echo $rows['url_link']; } ?>" class="form-control"></td>
          </tr>
          <tr>
            <td>หน้า-โพสต์</td>
            <td><select name="url_link2" class="form-control">
            <?php
            $sql2 = "select * from ap_pages order by title asc";
            $query2 = mysqli_query($conn,$sql2);
            while($rows2 = mysqli_fetch_array($query2)){
            ?>
              <option value="<?php echo $rows2['id']?>"
              <?php
              if($rows2['id']==$rows['url_link2']){
                echo "selected='true'";
              }
              ?>
              ><?php echo $rows2['title']?></option>
            <?php
            }
            ?>
            </select></td>
          </tr>
          <tr>
            <td>แค็ตตาล็อก</td>
            <td><select name="url_link3" class="form-control">
            <?php
            $sql2 = "select * from ap_catalogs order by name asc";
            $query2 = mysqli_query($conn,$sql2);
            while($rows2 = mysqli_fetch_array($query2)){
            ?>
              <option value="<?php echo $rows2['code']?>"
              <?php
              if($rows2['code']==$rows['url_link3']){
                echo "selected='true'";
              }
              ?>
              ><?php echo $rows2['name']?></option>
            <?php
            }
            ?>
            </select></td>
          </tr>
          <tr>
            <td>Target</td>
            <td>
            <label for="disabledTextInput">รูปแบบการ Link เช่น _blank</label>
            <input type="text" name="target_link" value="<?php echo $rows['target_link']?>" class="form-control"></td>
          </tr>
          <tr>
            <td colspan="2" align="right"><input type="submit" value="บันทึก" class="btn btn-default"></td>
          </tr>
        </tbody>
      </table>
      </form>
    </div>
  </div>
<?php
}

if($_GET['nu']=="menus_i_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_menus";
  $field = "code";
  $field2 = $_POST['code'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2'"));
  if($nr>0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=menus&action=menus_i_u&$field=$field2'>";
  }else{
    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    for($i=1; $i<$num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));

      $fields_1 .= "$fields, ";
      $fields_2 .= "'".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $field_1 = substr($fields_1,0,-2);
    $field_2 = substr($fields_2,0,-2);
    mysqli_query($conn,"insert into $table ($field_1) values ($field_2)");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=menus'>";
  }
  echo "</div>";
}

if($_GET['nu']=="menus_u_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_menus";
  $field = "id";
  $field2 = $_POST['id'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2'"));
  if($nr<0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=menus&action=menus_i_u&$field=$field2'>";
  }else{
    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    $fields_ = '';
    for($i=1; $i<$num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));

      $fields_ .= "$fields='".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $fields_set = substr($fields_,0,-2);
    mysqli_query($conn,"update $table set $fields_set where $field='$field2' ");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=menus'>";
  }
  echo "</div>";
}

if ($_GET['nu']=="menus_delete") {
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_menus";
  mysqli_query($conn,"delete from $table where id='$_GET[id]'");
  echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
  echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=menus'>";
  echo "</div>";
}

if($_GET['nu']=="menus2"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <div class="row">
      <div class="col-lg-6 col-md-6 col-sm-12">
        <h2 style="margin: 0px">เมนูย่อย 2</h2>
      </div>
      <div class="col-lg-6 col-md-6 col-sm-12 align-text-bottom">
        <form class="form-inline text-right" action="<?php echo $_SERVER[SCRIPT_NAME]; ?>" method="GET">
          <input type="hidden" name="nu" value="<?php echo $_GET['nu'] ?>">
          <div class="form-group">
            <input type="text" name="q" value="<?php echo $_GET['q']; ?>" placeholder="ระบุคำค้น" class="form-control" autofocus>
          </div>
          <button type="submit" class="btn btn-default">ค้นหา</button>
        </form>
      </div>
      <div class="col-lg-12 col-md-12 col-sm-12"><hr></div>
      <p></p>
    </div>
    <div class="table-responsive">
      <table class="table table-striped">
        <thead>
          <tr>
            <th width="70">ลำดับ</th>
            <th>ภาษา</th>
            <th>เมนูหลัก</th>
            <th>ลำดับเมนู</th>
            <!-- <th>ประเภท Link</th> -->
            <th>รหัส</th>
            <th>ชื่อ</th>
            <th width="100"><a href="?nu=menus2_i_u&action=menus2_i_s">เพิ่ม</a></th>
          </tr>
        </thead>
        <tbody>
          <?php
          $sql = "select * from ap_menus2 where name like '$_GET[q]%'";
          $query = mysqli_query($conn,$sql);
          $num_rows = mysqli_num_rows($query);

          $per_page = 50;
          $page  = 1;

          if(isset($_GET["Page"])){
            $page = $_GET["Page"];
          }

          $prev_page = $page-1;
          $next_page = $page+1;

          $row_start = (($per_page*$page)-$per_page);
          if($num_rows<=$per_page){
            $num_pages = 1;
          }elseif(($num_rows % $per_page)==0){
            $num_pages = ($num_rows/$per_page) ;
          }else{
            $num_pages =($num_rows/$per_page)+1;
            $num_pages = (int)$num_pages;
          }
          /*$row_end = $per_page * $page;
          if($row_end > $num_rows){
            $row_end = $num_rows;
          }*/
          $row_end = $per_page;

          $sql .= " order by code_menus asc, level asc limit $row_start, $row_end";
          $query = mysqli_query($conn,$sql);

          $i = (($row_start/$per_page)*$per_page);
          while($rows = mysqli_fetch_array($query)){
          $i++;
            $sql2 = "select * from ap_menus where code='$rows[code_menus]'";
            $query2 = mysqli_query($conn,$sql2);
            $rows2 = mysqli_fetch_array($query2);

            if($rows['type_link']==1){
              $typelink = "URL Link";
            }
            if($rows['type_link']==2){
              $typelink = "หน้า-โพสต์";
            }
            if($rows['type_link']==3){
              $typelink = "แค็ตตาล็อก";
            }
            ?>
            <tr>
              <td><?php echo $i;?></td>
              <td><?php echo $rows['lang'];?></td>
              <td><?php echo $rows2['name'];?></td>
              <td><?php echo $rows['level'];?></td>
              <!-- <td><?php echo $typelink;?></td> -->
              <td><?php echo $rows['code'];?></td>
              <td><?php echo $rows['name'];?></td>
              <td><a href="?nu=menus2_i_u&action=menus2_u_s&id=<?php echo $rows['id']?>">แก้ไข</a> :  <a href="#" onclick="if (confirm('ยืนยันการลบ?')) location.href='?nu=menus2_delete&id=<?php echo $rows['id']?>';">ลบ</a></td>
            </tr>
          <?php
          }
          ?>
        </tbody>
      </table>
    </div>
    <div class="text-right">
      <p><strong><?php echo number_format($num_rows);?> รายการ : <?php echo $num_pages;?> หน้า</strong></p>
      <ul class="pagination justify-content-end">
      <?php
      if($prev_page){
        echo "<li class=\"page-item\"><a class=\"page-link\" href='$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=1&q=$_GET[q]'>หน้าแรก</a></li>";
        echo "<li class=\"page-item\"><a class=\"page-link\" href='$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=$prev_page&q=$_GET[q]'><< ย้อนกลับ</a></li>";
      }

      for($i=1; $i<=$num_pages; $i++){
        if ($i > ($_GET['Page']-5) && $i < ($_GET['Page']+5)) {
          if($i != $page){
            echo "<li class=\"page-item\"><a class=\"page-link\" href='$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=$i&q=$_GET[q]'>$i</a></li>";
          }else{
            echo "<li class=\"page-item active\"><a class=\"page-link\" href=\"$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=$_GET[Page]&q=$_GET[q]#\">$i</a></li>";
          }
        }
      }
      if($page!=$num_pages){
        echo "<li class=\"page-item\"><a class=\"page-link\" href='$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=$next_page&q=$_GET[q]'>ถัดไป>></a></li>";
        echo "<li class=\"page-item\"><a class=\"page-link\" href='$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=$num_pages&q=$_GET[q]'>หน้าสุดท้าย</a></li>";
      }
      ?>
      </ul>
    </div>
  </div>
<?php
}

if($_GET['nu']=="menus2_i_u"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  $sql = "select * from ap_menus2 where id='$_GET[id]'";
  $query = mysqli_query($conn,$sql);
  $rows = mysqli_fetch_array($query);
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">เมนูย่อย 2</h2>
    <div class="table-responsive">
      <form action="?nu=<?php echo $_GET['action']?>" method="post" enctype="multipart/form-data">
      <input type="hidden" name="id" value="<?php echo $rows['id']?>">
      <table class="table table-striped">
        <tbody>
        <tr>
          <td>สำหรับเว็บไซต์ภาษา</td>
          <td><select name="lang" class="form-control">
          <?php
          $sql2 = "select * from ap_langs order by code asc";
          $query2 = mysqli_query($conn,$sql2);
          while($rows2 = mysqli_fetch_array($query2)){
          ?>
            <option value="<?php echo $rows2['code']?>"
            <?php
            if($rows2['code']==$rows['lang']){
              echo "selected='true'";
            }
            ?>
            ><?php echo $rows2['name']?></option>
          <?php
          }
          ?>
          </select></td>
        </tr>
        <tr>
            <td>เมนูหลัก</td>
            <td><select name="code_menus" id="code_menus" class="form-control">
            <?php
            $sql2 = "select * from ap_menus order by level asc";
            $query2 = mysqli_query($conn,$sql2);
            while($rows2 = mysqli_fetch_array($query2)){
            ?>
              <option value="<?php echo $rows2['code']?>"
              <?php
              if($rows2['code']==$rows['code_menus']){
                echo "selected='true'";
              }
              ?>
              ><?php echo $rows2['name']?></option>
            <?php
            }
            ?>
            </select></td>
          </tr>
          <tr>
            <td>ลำดับเมนู</td>
            <td>
            <label for="disabledTextInput">ตัวอย่าง 00</label>
            <input type="text" name="level" value="<?php if(empty($rows['level'])){ echo "00"; }else{ echo $rows['level']; } ?>" class="form-control"></td>
          </tr>
          <tr>
            <td>รหัส</td>
            <td><input type="text" name="code" value="<?php echo $rows['code']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>ชื่อ</td>
            <td><input type="text" name="name" size="30" value="<?php echo $rows['name']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>ประเภท Link</td>
            <td>
            <label for="disabledTextInput">1 = URL Link หรือ 2 = หน้า-โพสต์ หรือ 3 = แค็ตตาล็อก</label>
            <input type="text" name="type_link" size="5" value="<?php echo $rows['type_link']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>URL Link</td>
            <td>
            <label for="disabledTextInput">ถ้าไม่ระบุกรุณาใส่ #</label>
            <input type="text" name="url_link" size="70" value="<?php if(empty($rows['url_link'])){ echo "#"; }else{ echo $rows['url_link']; } ?>" class="form-control"></td>
          </tr>
          <tr>
            <td>หน้า-โพสต์</td>
            <td><select name="url_link2" class="form-control">
            <?php
            $sql2 = "select * from ap_pages order by id desc";
            $query2 = mysqli_query($conn,$sql2);
            while($rows2 = mysqli_fetch_array($query2)){
            ?>
              <option value="<?php echo $rows2['id']?>"
              <?php
              if($rows2['id']==$rows['url_link2']){
                echo "selected='true'";
              }
              ?>
              ><?php echo $rows2['title']?></option>
            <?php
            }
            ?>
            </select></td>
          </tr>
          <tr>
            <td>แค็ตตาล็อก</td>
            <td><select name="url_link3" class="form-control">
            <?php
            $sql3 = "select * from ap_catalogs order by name asc";
            $query3 = mysqli_query($conn,$sql3);
            while($rows3 = mysqli_fetch_array($query3)){
            ?>
              <option value="<?php echo $rows3['code']?>"
              <?php
              if($rows3['code']==$rows['url_link3']){
                echo "selected='true'";
              }
              ?>
              ><?php echo $rows3['name']?></option>
            <?php
            }
            ?>
            </select></td>
          </tr>
          <tr>
            <td>Target</td>
            <td>
            <label for="disabledTextInput">รูปแบบการ Link เช่น _blank</label>
            <input type="text" name="target_link" value="<?php echo $rows['target_link']?>" class="form-control"></td>
          </tr>
          <tr>
            <td colspan="2" align="right"><input type="submit" value="บันทึก" class="btn btn-default"></td>
          </tr>
        </tbody>
      </table>
      </form>
    </div>
  </div>
<?php
}

if($_GET['nu']=="menus2_i_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_menus2";
  $field = "code";
  $field2 = $_POST['code'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2'"));
  if($nr>0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=menus2&action=menus2_i_u&$field=$field2'>";
  }else{
    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    for($i=1; $i<$num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));

      $fields_1 .= "$fields, ";
      $fields_2 .= "'".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $field_1 = substr($fields_1,0,-2);
    $field_2 = substr($fields_2,0,-2);
    mysqli_query($conn,"insert into $table ($field_1) values ($field_2)");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=menus2'>";
  }
  echo "</div>";
}

if($_GET['nu']=="menus2_u_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_menus2";
  $field = "id";
  $field2 = $_POST['id'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2'"));
  if($nr<0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=menus2&action=menus2_i_u&$field=$field2'>";
  }else{
    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    $fields_ = '';
    for($i=1; $i<$num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));

      $fields_ .= "$fields='".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $fields_set = substr($fields_,0,-2);
    mysqli_query($conn,"update $table set $fields_set where $field='$field2' ");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=menus2'>";
  }
  echo "</div>";
}

if ($_GET['nu']=="menus2_delete") {
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_menus2";
  mysqli_query($conn,"delete from $table where id='$_GET[id]'");
  echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
  echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=menus2'>";
  echo "</div>";
}

// menus3
if($_GET['nu']=="menus3"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <div class="row">
      <div class="col-lg-6 col-md-6 col-sm-12">
        <h2 style="margin: 0px">เมนูย่อย 3</h2>
      </div>
      <div class="col-lg-6 col-md-6 col-sm-12 align-text-bottom">
        <form class="form-inline text-right" action="<?php echo $_SERVER[SCRIPT_NAME]; ?>" method="GET">
          <input type="hidden" name="nu" value="<?php echo $_GET['nu'] ?>">
          <div class="form-group">
            <input type="text" name="q" value="<?php echo $_GET['q']; ?>" placeholder="ระบุคำค้น" class="form-control" autofocus>
          </div>
          <button type="submit" class="btn btn-default">ค้นหา</button>
        </form>
      </div>
      <div class="col-lg-12 col-md-12 col-sm-12"><hr></div>
      <p></p>
    </div>
    <div class="table-responsive">
      <table class="table table-striped">
        <thead>
          <tr>
            <th width="70">ลำดับ</th>
            <th>ภาษา</th>
            <th>เมนูย่อย 2</th>
            <th>ลำดับเมนู</th>
            <!-- <th>ประเภท Link</th> -->
            <th>รหัส</th>
            <th>ชื่อ</th>
            <th width="100"><a href="?nu=menus3_i_u&action=menus3_i_s">เพิ่ม</a></th>
          </tr>
        </thead>
        <tbody>
          <?php
          $sql = "select * from ap_menus3 where name like '$_GET[q]%'";
          $query = mysqli_query($conn,$sql);
          $num_rows = mysqli_num_rows($query);

          $per_page = 50;
          $page  = 1;

          if(isset($_GET["Page"])){
            $page = $_GET["Page"];
          }

          $prev_page = $page-1;
          $next_page = $page+1;

          $row_start = (($per_page*$page)-$per_page);
          if($num_rows<=$per_page){
            $num_pages = 1;
          }elseif(($num_rows % $per_page)==0){
            $num_pages = ($num_rows/$per_page) ;
          }else{
            $num_pages =($num_rows/$per_page)+1;
            $num_pages = (int)$num_pages;
          }
          /*$row_end = $per_page * $page;
          if($row_end > $num_rows){
            $row_end = $num_rows;
          }*/
          $row_end = $per_page;

          $sql .= " order by code_menus asc, level asc limit $row_start, $row_end";
          $query = mysqli_query($conn,$sql);

          $i = (($row_start/$per_page)*$per_page);
          while($rows = mysqli_fetch_array($query)){
          $i++;
            $sql2 = "select * from ap_menus2 where code='$rows[code_menus]'";
            $query2 = mysqli_query($conn,$sql2);
            $rows2 = mysqli_fetch_array($query2);

            if($rows['type_link']==1){
              $typelink = "URL Link";
            }
            if($rows['type_link']==2){
              $typelink = "หน้า-โพสต์";
            }
            if($rows['type_link']==3){
              $typelink = "แค็ตตาล็อก";
            }
            ?>
            <tr>
              <td><?php echo $i;?></td>
              <td><?php echo $rows['lang'];?></td>
              <td><?php echo $rows2['name'];?></td>
              <td><?php echo $rows['level'];?></td>
              <!-- <td><?php echo $typelink;?></td> -->
              <td><?php echo $rows['code'];?></td>
              <td><?php echo $rows['name'];?></td>
              <td><a href="?nu=menus3_i_u&action=menus3_u_s&id=<?php echo $rows['id']?>">แก้ไข</a> :  <a href="#" onclick="if (confirm('ยืนยันการลบ?')) location.href='?nu=menus3_delete&id=<?php echo $rows['id']?>';">ลบ</a></td>
            </tr>
          <?php
          }
          ?>
        </tbody>
      </table>
    </div>
    <div class="text-right">
      <p><strong><?php echo number_format($num_rows);?> รายการ : <?php echo $num_pages;?> หน้า</strong></p>
      <ul class="pagination justify-content-end">
      <?php
      if($prev_page){
        echo "<li class=\"page-item\"><a class=\"page-link\" href='$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=1&q=$_GET[q]'>หน้าแรก</a></li>";
        echo "<li class=\"page-item\"><a class=\"page-link\" href='$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=$prev_page&q=$_GET[q]'><< ย้อนกลับ</a></li>";
      }

      for($i=1; $i<=$num_pages; $i++){
        if ($i > ($_GET['Page']-5) && $i < ($_GET['Page']+5)) {
          if($i != $page){
            echo "<li class=\"page-item\"><a class=\"page-link\" href='$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=$i&q=$_GET[q]'>$i</a></li>";
          }else{
            echo "<li class=\"page-item active\"><a class=\"page-link\" href=\"$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=$_GET[Page]&q=$_GET[q]#\">$i</a></li>";
          }
        }
      }
      if($page!=$num_pages){
        echo "<li class=\"page-item\"><a class=\"page-link\" href='$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=$next_page&q=$_GET[q]'>ถัดไป>></a></li>";
        echo "<li class=\"page-item\"><a class=\"page-link\" href='$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=$num_pages&q=$_GET[q]'>หน้าสุดท้าย</a></li>";
      }
      ?>
      </ul>
    </div>
  </div>
<?php
}

if($_GET['nu']=="menus3_i_u"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  $sql = "select * from ap_menus3 where id='$_GET[id]'";
  $query = mysqli_query($conn,$sql);
  $rows = mysqli_fetch_array($query);
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">เมนูย่อย 3</h2>
    <div class="table-responsive">
      <form action="?nu=<?php echo $_GET['action']?>" method="post" enctype="multipart/form-data">
      <input type="hidden" name="id" value="<?php echo $rows['id']?>">
      <table class="table table-striped">
        <tbody>
        <tr>
          <td>สำหรับเว็บไซต์ภาษา</td>
          <td><select name="lang" class="form-control">
          <?php
          $sql2 = "select * from ap_langs order by code asc";
          $query2 = mysqli_query($conn,$sql2);
          while($rows2 = mysqli_fetch_array($query2)){
          ?>
            <option value="<?php echo $rows2['code']?>"
            <?php
            if($rows2['code']==$rows['lang']){
              echo "selected='true'";
            }
            ?>
            ><?php echo $rows2['name']?></option>
          <?php
          }
          ?>
          </select></td>
        </tr>
        <tr>
            <td>เมนูย่อย 2</td>
            <td><select name="code_menus" id="code_menus" class="form-control">
            <?php
            $sql2 = "select * from ap_menus2 order by level asc";
            $query2 = mysqli_query($conn,$sql2);
            while($rows2 = mysqli_fetch_array($query2)){
            ?>
              <option value="<?php echo $rows2['code']?>"
              <?php
              if($rows2['code']==$rows['code_menus']){
                echo "selected='true'";
              }
              ?>
              ><?php echo $rows2['name']?></option>
            <?php
            }
            ?>
            </select></td>
          </tr>
          <tr>
            <td>ลำดับเมนู</td>
            <td>
            <label for="disabledTextInput">ตัวอย่าง 00</label>
            <input type="text" name="level" value="<?php if(empty($rows['level'])){ echo "00"; }else{ echo $rows['level']; } ?>" class="form-control"></td>
          </tr>
          <tr>
            <td>รหัส</td>
            <td><input type="text" name="code" value="<?php echo $rows['code']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>ชื่อ</td>
            <td><input type="text" name="name" size="30" value="<?php echo $rows['name']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>ประเภท Link</td>
            <td>
            <label for="disabledTextInput">1 = URL Link หรือ 2 = หน้า-โพสต์ หรือ 3 = แค็ตตาล็อก</label>
            <input type="text" name="type_link" size="5" value="<?php echo $rows['type_link']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>URL Link</td>
            <td>
            <label for="disabledTextInput">ถ้าไม่ระบุกรุณาใส่ #</label>
            <input type="text" name="url_link" size="70" value="<?php if(empty($rows['url_link'])){ echo "#"; }else{ echo $rows['url_link']; } ?>" class="form-control"></td>
          </tr>
          <tr>
            <td>หน้า-โพสต์</td>
            <td><select name="url_link2" class="form-control">
            <?php
            $sql2 = "select * from ap_pages order by title asc";
            $query2 = mysqli_query($conn,$sql2);
            while($rows2 = mysqli_fetch_array($query2)){
            ?>
              <option value="<?php echo $rows2['id']?>"
              <?php
              if($rows2['id']==$rows['url_link2']){
                echo "selected='true'";
              }
              ?>
              ><?php echo $rows2['title']?></option>
            <?php
            }
            ?>
            </select></td>
          </tr>
          <tr>
            <td>แค็ตตาล็อก</td>
            <td><select name="url_link3" class="form-control">
            <?php
            $sql3 = "select * from ap_catalogs order by name asc";
            $query3 = mysqli_query($conn,$sql3);
            while($rows3 = mysqli_fetch_array($query3)){
            ?>
              <option value="<?php echo $rows3['code']?>"
              <?php
              if($rows3['code']==$rows['url_link3']){
                echo "selected='true'";
              }
              ?>
              ><?php echo $rows3['name']?></option>
            <?php
            }
            ?>
            </select></td>
          </tr>
          <tr>
            <td>Target</td>
            <td>
            <label for="disabledTextInput">รูปแบบการ Link เช่น _blank</label>
            <input type="text" name="target_link" value="<?php echo $rows['target_link']?>" class="form-control"></td>
          </tr>
          <tr>
            <td colspan="2" align="right"><input type="submit" value="บันทึก" class="btn btn-default"></td>
          </tr>
        </tbody>
      </table>
      </form>
    </div>
  </div>
<?php
}

if($_GET['nu']=="menus3_i_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_menus3";
  $field = "code";
  $field2 = $_POST['code'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2'"));
  if($nr>0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=menus3&action=menus_i_u&$field=$field2'>";
  }else{
    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    for($i=1; $i<$num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));

      $fields_1 .= "$fields, ";
      $fields_2 .= "'".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $field_1 = substr($fields_1,0,-2);
    $field_2 = substr($fields_2,0,-2);
    mysqli_query($conn,"insert into $table ($field_1) values ($field_2)");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=menus3'>";
  }
  echo "</div>";
}

if($_GET['nu']=="menus3_u_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_menus3";
  $field = "id";
  $field2 = $_POST['id'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2'"));
  if($nr<0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=menus3&action=menus3_i_u&$field=$field2'>";
  }else{
    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    for($i=1; $i<$num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));

      $fields_ .= "$fields='".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $fields_set = substr($fields_,0,-2);
    mysqli_query($conn,"update $table set $fields_set where $field='$field2' ");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=menus3'>";
  }
  echo "</div>";
}

if ($_GET['nu']=="menus3_delete") {
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_menus3";
  mysqli_query($conn,"delete from $table where id='$_GET[id]'");
  echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
  echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=menus3'>";
  echo "</div>";
}

// menus4
if($_GET['nu']=="menus4"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <div class="row">
      <div class="col-lg-6 col-md-6 col-sm-12">
        <h2 style="margin: 0px">เมนูย่อย 4</h2>
      </div>
      <div class="col-lg-6 col-md-6 col-sm-12 align-text-bottom">
        <form class="form-inline text-right" action="<?php echo $_SERVER[SCRIPT_NAME]; ?>" method="GET">
          <input type="hidden" name="nu" value="<?php echo $_GET['nu'] ?>">
          <div class="form-group">
            <input type="text" name="q" value="<?php echo $_GET['q']; ?>" placeholder="ระบุคำค้น" class="form-control" autofocus>
          </div>
          <button type="submit" class="btn btn-default">ค้นหา</button>
        </form>
      </div>
      <div class="col-lg-12 col-md-12 col-sm-12"><hr></div>
      <p></p>
    </div>
    <div class="table-responsive">
      <table class="table table-striped">
        <thead>
          <tr>
            <th width="70">ลำดับ</th>
            <th>ภาษา</th>
            <th>เมนูย่อย 3</th>
            <th>ลำดับเมนู</th>
            <th>ประเภท Link</th>
            <th>รหัส</th>
            <th>ชื่อ</th>
            <th width="100"><a href="?nu=menus4_i_u&action=menus4_i_s">เพิ่ม</a></th>
          </tr>
        </thead>
        <tbody>
          <?php
          $sql = "select * from ap_menus4 where name like '$_GET[q]%'";
          $query = mysqli_query($conn,$sql);
          $num_rows = mysqli_num_rows($query);

          $per_page = 50;
          $page  = 1;

          if(isset($_GET["Page"])){
            $page = $_GET["Page"];
          }

          $prev_page = $page-1;
          $next_page = $page+1;

          $row_start = (($per_page*$page)-$per_page);
          if($num_rows<=$per_page){
            $num_pages = 1;
          }elseif(($num_rows % $per_page)==0){
            $num_pages = ($num_rows/$per_page) ;
          }else{
            $num_pages =($num_rows/$per_page)+1;
            $num_pages = (int)$num_pages;
          }
          /*$row_end = $per_page * $page;
          if($row_end > $num_rows){
            $row_end = $num_rows;
          }*/
          $row_end = $per_page;

          $sql .= " order by code_menus asc, level asc limit $row_start, $row_end";
          $query = mysqli_query($conn,$sql);

          $i = (($row_start/$per_page)*$per_page);
          while($rows = mysqli_fetch_array($query)){
          $i++;
            $sql2 = "select * from ap_menus3 where code='$rows[code_menus]'";
            $query2 = mysqli_query($conn,$sql2);
            $rows2 = mysqli_fetch_array($query2);

            if($rows['type_link']==1){
              $typelink = "URL Link";
            }
            if($rows['type_link']==2){
              $typelink = "หน้า-โพสต์";
            }
            if($rows['type_link']==3){
              $typelink = "แค็ตตาล็อก";
            }
            ?>
            <tr>
              <td><?php echo $i;?></td>
              <td><?php echo $rows['lang'];?></td>
              <td><?php echo $rows2['name'];?></td>
              <td><?php echo $rows['level'];?></td>
              <td><?php echo $typelink;?></td>
              <td><?php echo $rows['code'];?></td>
              <td><?php echo $rows['name'];?></td>
              <td><a href="?nu=menus4_i_u&action=menus4_u_s&id=<?php echo $rows['id']?>">แก้ไข</a> :  <a href="#" onclick="if (confirm('ยืนยันการลบ?')) location.href='?nu=menus4_delete&id=<?php echo $rows['id']?>';">ลบ</a></td>
            </tr>
          <?php
          }
          ?>
        </tbody>
      </table>
    </div>
    <div class="text-right">
      <p><strong><?php echo number_format($num_rows);?> รายการ : <?php echo $num_pages;?> หน้า</strong></p>
      <ul class="pagination justify-content-end">
      <?php
      if($prev_page){
        echo "<li class=\"page-item\"><a class=\"page-link\" href='$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=1&q=$_GET[q]'>หน้าแรก</a></li>";
        echo "<li class=\"page-item\"><a class=\"page-link\" href='$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=$prev_page&q=$_GET[q]'><< ย้อนกลับ</a></li>";
      }

      for($i=1; $i<=$num_pages; $i++){
        if ($i > ($_GET['Page']-5) && $i < ($_GET['Page']+5)) {
          if($i != $page){
            echo "<li class=\"page-item\"><a class=\"page-link\" href='$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=$i&q=$_GET[q]'>$i</a></li>";
          }else{
            echo "<li class=\"page-item active\"><a class=\"page-link\" href=\"$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=$_GET[Page]&q=$_GET[q]#\">$i</a></li>";
          }
        }
      }
      if($page!=$num_pages){
        echo "<li class=\"page-item\"><a class=\"page-link\" href='$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=$next_page&q=$_GET[q]'>ถัดไป>></a></li>";
        echo "<li class=\"page-item\"><a class=\"page-link\" href='$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=$num_pages&q=$_GET[q]'>หน้าสุดท้าย</a></li>";
      }
      ?>
      </ul>
    </div>
  </div>
<?php
}

if($_GET['nu']=="menus4_i_u"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  $sql = "select * from ap_menus4 where id='$_GET[id]'";
  $query = mysqli_query($conn,$sql);
  $rows = mysqli_fetch_array($query);
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">เมนูย่อย 4</h2>
    <div class="table-responsive">
      <form action="?nu=<?php echo $_GET['action']?>" method="post" enctype="multipart/form-data">
      <input type="hidden" name="id" value="<?php echo $rows['id']?>">
      <table class="table table-striped">
        <tbody>
        <tr>
          <td>สำหรับเว็บไซต์ภาษา</td>
          <td><select name="lang" class="form-control">
          <?php
          $sql2 = "select * from ap_langs order by code asc";
          $query2 = mysqli_query($conn,$sql2);
          while($rows2 = mysqli_fetch_array($query2)){
          ?>
            <option value="<?php echo $rows2['code']?>"
            <?php
            if($rows2['code']==$rows['lang']){
              echo "selected='true'";
            }
            ?>
            ><?php echo $rows2['name']?></option>
          <?php
          }
          ?>
          </select></td>
        </tr>
        <tr>
            <td>เมนูย่อย 3</td>
            <td><select name="code_menus" id="code_menus" class="form-control">
            <?php
            $sql2 = "select * from ap_menus3 order by level asc";
            $query2 = mysqli_query($conn,$sql2);
            while($rows2 = mysqli_fetch_array($query2)){
            ?>
              <option value="<?php echo $rows2['code']?>"
              <?php
              if($rows2['code']==$rows['code_menus']){
                echo "selected='true'";
              }
              ?>
              ><?php echo $rows2['name']?></option>
            <?php
            }
            ?>
            </select></td>
          </tr>
          <tr>
            <td>ลำดับเมนู</td>
            <td>
            <label for="disabledTextInput">ตัวอย่าง 00</label>
            <input type="text" name="level" value="<?php if(empty($rows['level'])){ echo "00"; }else{ echo $rows['level']; } ?>" class="form-control"></td>
          </tr>
          <tr>
            <td>รหัส</td>
            <td><input type="text" name="code" value="<?php echo $rows['code']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>ชื่อ</td>
            <td><input type="text" name="name" size="30" value="<?php echo $rows['name']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>ประเภท Link</td>
            <td>
            <label for="disabledTextInput">1 = URL Link หรือ 2 = หน้า-โพสต์ หรือ 3 = แค็ตตาล็อก</label>
            <input type="text" name="type_link" size="5" value="<?php echo $rows['type_link']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>URL Link</td>
            <td>
            <label for="disabledTextInput">ถ้าไม่ระบุกรุณาใส่ #</label>
            <input type="text" name="url_link" size="70" value="<?php if(empty($rows['url_link'])){ echo "#"; }else{ echo $rows['url_link']; } ?>" class="form-control"></td>
          </tr>
          <tr>
            <td>หน้า-โพสต์</td>
            <td><select name="url_link2" class="form-control">
            <?php
            $sql2 = "select * from ap_pages order by title asc";
            $query2 = mysqli_query($conn,$sql2);
            while($rows2 = mysqli_fetch_array($query2)){
            ?>
              <option value="<?php echo $rows2['id']?>"
              <?php
              if($rows2['id']==$rows['url_link2']){
                echo "selected='true'";
              }
              ?>
              ><?php echo $rows2['title']?></option>
            <?php
            }
            ?>
            </select></td>
          </tr>
          <tr>
            <td>แค็ตตาล็อก</td>
            <td><select name="url_link3" class="form-control">
            <?php
            $sql3 = "select * from ap_catalogs order by name asc";
            $query3 = mysqli_query($conn,$sql3);
            while($rows3 = mysqli_fetch_array($query3)){
            ?>
              <option value="<?php echo $rows3['code']?>"
              <?php
              if($rows3['code']==$rows['url_link3']){
                echo "selected='true'";
              }
              ?>
              ><?php echo $rows3['name']?></option>
            <?php
            }
            ?>
            </select></td>
          </tr>
          <tr>
            <td>Target</td>
            <td>
            <label for="disabledTextInput">รูปแบบการ Link เช่น _blank</label>
            <input type="text" name="target_link" value="<?php echo $rows['target_link']?>" class="form-control"></td>
          </tr>
          <tr>
            <td colspan="2" align="right"><input type="submit" value="บันทึก" class="btn btn-default"></td>
          </tr>
        </tbody>
      </table>
      </form>
    </div>
  </div>
<?php
}

if($_GET['nu']=="menus4_i_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_menus4";
  $field = "code";
  $field2 = $_POST['code'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2'"));
  if($nr>0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=menus4&action=menus_i_u&$field=$field2'>";
  }else{
    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    for($i=1; $i<$num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));

      $fields_1 .= "$fields, ";
      $fields_2 .= "'".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $field_1 = substr($fields_1,0,-2);
    $field_2 = substr($fields_2,0,-2);
    mysqli_query($conn,"insert into $table ($field_1) values ($field_2)");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=menus4'>";
  }
  echo "</div>";
}

if($_GET['nu']=="menus4_u_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_menus4";
  $field = "id";
  $field2 = $_POST['id'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2'"));
  if($nr<0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=menus4&action=menus4_i_u&$field=$field2'>";
  }else{
    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    for($i=1; $i<$num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));

      $fields_ .= "$fields='".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $fields_set = substr($fields_,0,-2);
    mysqli_query($conn,"update $table set $fields_set where $field='$field2' ");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=menus4'>";
  }
  echo "</div>";
}

if ($_GET['nu']=="menus4_delete") {
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_menus4";
  mysqli_query($conn,"delete from $table where id='$_GET[id]'");
  echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
  echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=menus4'>";
  echo "</div>";
}

if($_GET['nu']=="pages"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <div class="row">
      <div class="col-lg-6 col-md-6 col-sm-12">
        <h2 style="margin: 0px">หน้า-โพสต์</h2>
      </div>
      <div class="col-lg-6 col-md-6 col-sm-12 align-text-bottom">
        <form class="form-inline text-right" action="<?php echo $_SERVER[SCRIPT_NAME]; ?>" method="GET">
          <input type="hidden" name="nu" value="<?php echo $_GET['nu'] ?>">
          <div class="form-group">
            <input type="text" name="q" value="<?php echo $_GET['q']; ?>" placeholder="ระบุคำค้น" class="form-control" autofocus>
          </div>
          <button type="submit" class="btn btn-default">ค้นหา</button>
        </form>
      </div>
      <div class="col-lg-12 col-md-12 col-sm-12"><hr></div>
      <p></p>
    </div>
    <div class="table-responsive">
      <table class="table table-striped">
        <thead>
          <tr>
            <th width="70">ลำดับ</th>
            <th>ภาษา</th>
            <!-- <th>วันที่</th> -->
            <th>แค็ตตาล็อก</th>
            <th>เรื่อง</th>
            <!-- <th>Post</th> -->
            <th width="100"><a href="?nu=pages_i_u&action=pages_i_s">เพิ่ม</a></th>
          </tr>
        </thead>
        <tbody>
          <?php
          if($_SESSION['apstatus']=="Admin"){
            $sql = "select * from ap_pages where title like '$_GET[q]%'";
          }else{
            $sql = "select * from ap_pages where title like '$_GET[q]%' and createBy='$_SESSION[user]'";
          }
          $query = mysqli_query($conn,$sql);
          $num_rows = mysqli_num_rows($query);

          $per_page = 50;
          $page  = 1;

          if(isset($_GET["Page"])){
            $page = $_GET["Page"];
          }

          $prev_page = $page-1;
          $next_page = $page+1;

          $row_start = (($per_page*$page)-$per_page);
          if($num_rows<=$per_page){
            $num_pages = 1;
          }elseif(($num_rows % $per_page)==0){
            $num_pages = ($num_rows/$per_page) ;
          }else{
            $num_pages =($num_rows/$per_page)+1;
            $num_pages = (int)$num_pages;
          }
          /*$row_end = $per_page * $page;
          if($row_end > $num_rows){
            $row_end = $num_rows;
          }*/
          $row_end = $per_page;

          $sql .= " order by date_time desc limit $row_start, $row_end";
          $query = mysqli_query($conn,$sql);

          $i = (($row_start/$per_page)*$per_page);
          while($rows = mysqli_fetch_array($query)){
          $i++;
            $sql2 = "select * from ap_catalogs where code='$rows[catalogCode]'";
            $query2 = mysqli_query($conn,$sql2);
            $rows2 = mysqli_fetch_array($query2);

            if(empty($ap_ip)){
              $apdomain = "$ap_site$ap_folder";
            }else{
              $apdomain = str_replace("$ap_ip","$ap_domain","$ap_site$ap_folder");
            }
            
            if(empty($rows['counters']) && $rows['notify']=='1'){

              $rows['info2'] = strip_tags($rows['info2']);
              $rows['info2'] = str_replace( "&nbsp;"," ",$rows['info2']);

              if(empty($rows['images'])){
                $message = array(
                  'message' => "$rows_line[title] --> ".($apdomain)."?page_id=$rows[id]&lang=$_GET[lang]",
                  'stickerPackageId' => '',
                  'stickerId' => ''
                );
              }else{
                $message = array(
                  'message' => "$rows_line[title] $rows[info2] --> ".($apdomain)."?page_id=$rows[id]&lang=$_GET[lang]",
                  'imageThumbnail' => "$rows[images]",
                  'imageFullsize' => "$rows[images]",
                  'imageFile' => 'copy/240.jpg',
                  'stickerPackageId' => '',
                  'stickerId' => ''
                );
              }
              
              if(!empty($line_notify)){
                if($last1!=$line_notify){
                  line_notify_messages($message, $line_notify);
                }
                $last1 = $line_notify;
              }
              if(!empty($line_notify2)){
                if($last2!=$line_notify2){
                  line_notify_messages($message, $line_notify2);
                }
                $last2 = $line_notify2;
              }
              if(!empty($line_notify3)){
                if($last3!=$line_notify3){
                  line_notify_messages($message, $line_notify3);
                }
                $last3 = $line_notify3;
              }
              if(!empty($line_notify4)){
                if($last4!=$line_notify4){
                  line_notify_messages($message, $line_notify4);
                }
                $last4 = $line_notify4;
              }
              if(!empty($line_notify5)){
                if($last5!=$line_notify5){
                  line_notify_messages($message, $line_notify5);
                }
                $last5 = $line_notify5;
              }
              if(!empty($line_notify6)){
                if($last6!=$line_notify6){
                  line_notify_messages($message, $line_notify6);
                }
                $last6 = $line_notify6;
              }
              if(!empty($line_notify7)){
                if($last7!=$line_notify7){
                  line_notify_messages($message, $line_notify7);
                }
                $last7 = $line_notify7;
              }
              if(!empty($line_notify8)){
                if($last8!=$line_notify8){
                  line_notify_messages($message, $line_notify8);
                }
                $last8 = $line_notify8;
              }
              if(!empty($line_notify9)){
                if($last9!=$line_notify9){
                  line_notify_messages($message, $line_notify9);
                }
                $last9 = $line_notify9;
              }

              /* --- */

              $accessToken = LINE_MESSAGE_ACCESS_TOKEN;

              $content = file_get_contents('php://input');
              $arrayJson = json_decode($content, true);

              $arrayHeader = array();
              $arrayHeader[] = "Content-Type: application/json";
              $arrayHeader[] = "Authorization: Bearer {$accessToken}";

              $sql_line = "select * from ap_line_groups where id<2";
              $sql_line = "select * from ap_line_groups where id>2";
              $sql_line = "select * from ap_line_groups";
              $query_line = mysqli_query($conn,$sql_line);
              while($rows_line = mysqli_fetch_array($query_line)){

                $arrayPostData['to'] = "$rows_line[code]";
                
                $ix = 0;
                $arrayPostData['messages'][$ix]['type'] = "flex";
                $arrayPostData['messages'][$ix]['altText'] = "$title";
                $arrayPostData['messages'][$ix]['contents']['type'] = "carousel";

                if($rows['catalogCode']=="Public_Relations"){
                  $sql_linex = "select * from ap_pages where lang='$rows[lang]' and catalogCode='$rows[catalogCode]' and id='$rows[id]'";
                }else{
                  $sql_linex = "select * from ap_pages where lang='$rows[lang]' and catalogCode='$rows[catalogCode]' and id<='$rows[id]' order by id desc limit 10";
                }
                $query_linex = mysqli_query($conn,$sql_linex);
                $ix2 = 0;
                while($rows_linex = mysqli_fetch_array($query_linex)){

                  $arrayPostData['messages'][$ix]['contents']['contents'][$ix2]['type'] = "bubble";

                  $arrayPostData['messages'][$ix]['contents']['contents'][$ix2]['styles']['header']['backgroundColor'] = "#F8F9F9";
                  $arrayPostData['messages'][$ix]['contents']['contents'][$ix2]['styles']['body']['backgroundColor'] = "#F8F9F9";
                  $arrayPostData['messages'][$ix]['contents']['contents'][$ix2]['styles']['body']['separator'] = true;
                  $arrayPostData['messages'][$ix]['contents']['contents'][$ix2]['styles']['body']['separatorColor'] = "#F8F9F9";
                  $arrayPostData['messages'][$ix]['contents']['contents'][$ix2]['styles']['footer']['backgroundColor'] = "#F8F9F9";

                  /*$arrayPostData['messages'][$ix]['contents']['contents'][$ix2]['header']['type'] = "box";
                  $arrayPostData['messages'][$ix]['contents']['contents'][$ix2]['header']['layout'] = "vertical";
                  $arrayPostData['messages'][$ix]['contents']['contents'][$ix2]['header']['contents'][0]['type'] = "text";
                  $arrayPostData['messages'][$ix]['contents']['contents'][$ix2]['header']['contents'][0]['align'] = "center";
                  $arrayPostData['messages'][$ix]['contents']['contents'][$ix2]['header']['contents'][0]['text'] = "$ap_owner_short";*/

                  if($rows['catalogCode']!="Public_Relations"){
                    $arrayPostData['messages'][$ix]['contents']['contents'][$ix2]['hero']['type'] = "image";
                    $arrayPostData['messages'][$ix]['contents']['contents'][$ix2]['hero']['url'] = "$rows_linex[images]";
                    $arrayPostData['messages'][$ix]['contents']['contents'][$ix2]['hero']['size'] = "full";
                    $arrayPostData['messages'][$ix]['contents']['contents'][$ix2]['hero']['aspectRatio'] = "1:1";
                    $arrayPostData['messages'][$ix]['contents']['contents'][$ix2]['hero']['aspectMode'] = "cover";
                  }

                  $arrayPostData['messages'][$ix]['contents']['contents'][$ix2]['body']['type'] = "box";
                  $arrayPostData['messages'][$ix]['contents']['contents'][$ix2]['body']['layout'] = "vertical";
                  $arrayPostData['messages'][$ix]['contents']['contents'][$ix2]['body']['spacing'] = "md";
                  
                  $arrayPostData['messages'][$ix]['contents']['contents'][$ix2]['body']['contents'][0]['type'] = "text";
                  $arrayPostData['messages'][$ix]['contents']['contents'][$ix2]['body']['contents'][0]['size'] = "sm";
                  $arrayPostData['messages'][$ix]['contents']['contents'][$ix2]['body']['contents'][0]['wrap'] = true;
                  $arrayPostData['messages'][$ix]['contents']['contents'][$ix2]['body']['contents'][0]['text'] = "$rows_linex[title] ".$apdomain."?page_id=$rows_linex[id]&lang=$rows_linex[lang]";

                  $arrayPostData['messages'][$ix]['contents']['contents'][$ix2]['body']['contents'][1]['type'] = "button";
                  $arrayPostData['messages'][$ix]['contents']['contents'][$ix2]['body']['contents'][1]['height'] = "sm";
                  $arrayPostData['messages'][$ix]['contents']['contents'][$ix2]['body']['contents'][1]['style'] = "primary";
                  $arrayPostData['messages'][$ix]['contents']['contents'][$ix2]['body']['contents'][1]['action']['type'] = "uri";
                  $arrayPostData['messages'][$ix]['contents']['contents'][$ix2]['body']['contents'][1]['action']['label'] = "ข้อมูลเพิ่มเติม";
                  // $arrayPostData['messages'][$ix]['contents']['contents'][$ix2]['body']['contents'][1]['action']['uri'] = $apdomain."?page_id=$rows_linex[id]&lang=$rows_linex[lang]";
                  $arrayPostData['messages'][$ix]['contents']['contents'][$ix2]['body']['contents'][1]['action']['uri'] = LINE_MESSAGE_LIFF."?page_id=$rows_linex[id]&lang=$rows_linex[lang]";
                      
                  /*$arrayPostData['messages'][$ix]['contents']['contents'][$ix2]['footer']['type'] = "box";
                  $arrayPostData['messages'][$ix]['contents']['contents'][$ix2]['footer']['layout'] = "vertical";
                  $arrayPostData['messages'][$ix]['contents']['contents'][$ix2]['footer']['contents'][0]['type'] = "text";
                  $arrayPostData['messages'][$ix]['contents']['contents'][$ix2]['footer']['contents'][0]['size'] = "sm";
                  $arrayPostData['messages'][$ix]['contents']['contents'][$ix2]['footer']['contents'][0]['align'] = "center";
                  $arrayPostData['messages'][$ix]['contents']['contents'][$ix2]['footer']['contents'][0]['text'] = "©KPRU ARIT";*/


                  if($rows['catalogCode']=="Public_Relations"){
                    if(!empty($rows_linex['images'])){
                      if(empty($rows_linex['title'])){
                        $ix = 0;
                      }else{
                        $ix = 1;
                      }
                      $arrayPostData['messages'][$ix]['type'] = "image";
                      $arrayPostData['messages'][$ix]['originalContentUrl'] = "$rows_linex[images]";
                      $arrayPostData['messages'][$ix]['previewImageUrl'] = "$rows_linex[images]";
                    }
                  }

                  $ix2++;
                }
                
                line_push($arrayHeader, $arrayPostData);
              }
              $accessToken = "";
              $arrayHeader = "";
              $arrayPostData = "";

              mysqli_query($conn,"update ap_pages set counters='1' where id='$rows[id]' ");
            }

            //$loginUrl = $helper->getLoginUrl('http://'.$_SERVER[SERVER_NAME].'/fb-callback-post.php?id='.$rows[id], $permissions);
            
            //if($rows['counters']<1 && $rows['notify']>0){
            /*if($i==1){
              require_once __DIR__ . 'Facebook-SDK/autoload.php';
              $fb = new Facebook\Facebook([
              'app_id' => "$fb_app_id",
              'app_secret' => "$fb_app_secret",
              'default_graph_version' => 'v2.8',
              ]);

              $params = array(
                  "message" => "$rows[title]",
                  "link" => "$ap_site$ap_folder?page_id=$rows[id]&lang=$rows[lang]",
                  "picture" => "$rows[images]",
                  "name" => "$rows[title]",
                  "caption" => "$ap_site$ap_folder",
                  "description" => "$rows[info2]"
              );
              $post = $fb->post("/$fb_page_id/feed",$params, $access_token);
              $post = $post->getGraphNode()->asArray();
            }*/
            ?>
            <tr>
              <td><?php echo $i;?></td>
              <td><?php echo $rows['lang'];?></td>
              <!-- <td><?php echo $rows['date_time'];?></td> -->
              <td><?php echo $rows2['name'];?></td>
              <td><a href="<?php echo $ap_site.$ap_folder ?>?page_id=<?php echo $rows['id'] ?>&lang=<?php echo $rows['lang']; ?>" target="_blank"><?php echo $rows['title'];?></a></td>
              <!-- <td><a href="<?php echo htmlspecialchars($loginUrl)?>" target="_blank">FaceBook</a></td> -->
              <td><?php if($_SESSION['apstatus']=="Admin" || $rows['createBy']==$_SESSION['user']){?><a href="?nu=pages_i_u&action=pages_u_s&id=<?php echo $rows['id']?>">แก้ไข</a> :  <a href="#" onclick="if (confirm('ยืนยันการลบ?')) location.href='?nu=pages_delete&id=<?php echo $rows['id']?>&images=<?php echo $rows['images']?>&images2=<?php echo $rows['images2']?>';">ลบ</a><?php } ?></td>
            </tr>
          <?php
          }
          ?>
        </tbody>
      </table>
    </div>
    <div class="text-right">
      <p><strong><?php echo number_format($num_rows);?> รายการ : <?php echo $num_pages;?> หน้า</strong></p>
      <ul class="pagination justify-content-end">
      <?php
      if($prev_page){
        echo "<li class=\"page-item\"><a class=\"page-link\" href='$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=1&q=$_GET[q]'>หน้าแรก</a></li>";
        echo "<li class=\"page-item\"><a class=\"page-link\" href='$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=$prev_page&q=$_GET[q]'><< ย้อนกลับ</a></li>";
      }

      for($i=1; $i<=$num_pages; $i++){
        if ($i > ($_GET['Page']-5) && $i < ($_GET['Page']+5)) {
          if($i != $page){
            echo "<li class=\"page-item\"><a class=\"page-link\" href='$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=$i&q=$_GET[q]'>$i</a></li>";
          }else{
            echo "<li class=\"page-item active\"><a class=\"page-link\" href=\"$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=$_GET[Page]&q=$_GET[q]#\">$i</a></li>";
          }
        }
      }
      if($page!=$num_pages){
        echo "<li class=\"page-item\"><a class=\"page-link\" href='$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=$next_page&q=$_GET[q]'>ถัดไป>></a></li>";
        echo "<li class=\"page-item\"><a class=\"page-link\" href='$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=$num_pages&q=$_GET[q]'>หน้าสุดท้าย</a></li>";
      }
      ?>
      </ul>
    </div>
  </div>
<?php
}

if($_GET['nu']=="pages_i_u"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  $sql = "select * from ap_pages where id='$_GET[id]'";
  $query = mysqli_query($conn,$sql);
  $rows = mysqli_fetch_array($query);
  if($rows['public_relations']==1){
    $ck = " checked=\"true\"";
  }else{ $ck = ''; }
  if($rows['gallery_status']==1){
    $ck_g = " checked=\"true\"";
  }else{ $ck_g = ''; }
  if($rows['gallery2_status']==1){
    $ck_g2 = " checked=\"true\"";
  }else{ $ck_g2 = ''; }
  if($rows['images2_status']==1){
    $ck_i = " checked=\"true\"";
  }else{ $ck_i = ''; }
  if($rows['notify']==0 || $rows['notify']==""){
    $ck_n1 = " checked=\"true\"";
  }else{ $ck_n1 = ''; }
  if($rows['notify']==1){
    $ck_n2 = " checked=\"true\"";
  }else{ $ck_n2 = ''; }
  ?>
    <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
      <h2 class="sub-header">หน้า-โพสต์</h2>
      <div class="table-responsive">
        <form class="form-horizontal" action="?nu=<?php echo $_GET['action']?>" method="post" enctype="multipart/form-data">
        <input type="hidden" name="id" value="<?php echo $rows['id']?>">
        <input type="hidden" name="likes" value="<?php echo $rows['likes']?>">
        <input type="hidden" name="createBy" value="<?php echo $rows['createBy']?>">
        <input type="hidden" name="createByStatus" value="<?php echo $rows['createByStatus']?>">
          <table class="table table-striped">
            <tbody>
              <tr>
                <td>สำหรับเว็บไซต์ภาษา</td>
                <td><select name="lang" class="form-control">
                <?php
                $sql2 = "select * from ap_langs order by code asc";
                $query2 = mysqli_query($conn,$sql2);
                while($rows2 = mysqli_fetch_array($query2)){
                ?>
                  <option value="<?php echo $rows2['code']?>"
                  <?php
                  if($rows2['code']==$rows['lang']){
                    echo "selected='true'";
                  }
                  ?>
                  ><?php echo $rows2['name']?></option>
                <?php
                }
                ?>
                </select></td>
              </tr>
              <tr>
                <td width="150">วันที่</td>
                <td>
                  <label for="disabledTextInput">ตัวอย่าง : <?php echo date('Ymd');?></label>
                  <input type="text" name="date_time" value="<?php if(empty($rows['date_time'])){ echo date('Ymd'); }else{ echo $rows['date_time']; } ?>" class="form-control"></td>
              </tr>
              <tr>
                <td>ลำดับ หน้า-โพสต์</td>
                <td>
                  <label for="disabledTextInput">ถ้าไม่ระบุกรุณาใส่ 00</label>
                  <input type="text" name="level" value="<?php if(empty($rows['level'])){ echo "00"; }else{ echo $rows['level']; } ?>" class="form-control"></td>
              </tr>
              <tr>
                <td>แค็ตตาล็อก</td>
                <td><select name="catalogCode" class="form-control">
                <?php
                if($_SESSION['apstatus']=="Admin"){
                  $sql2 = "select * from ap_catalogs order by code asc";
                }else{
                  $sql2 = "select * from ap_catalogs where external_status!='1' order by name asc";
                }
                $query2 = mysqli_query($conn,$sql2);
                while($rows2 = mysqli_fetch_array($query2)){
                ?>
                  <option value="<?php echo $rows2['code']?>"
                  <?php
                  if($rows2['code']==$rows['catalogCode']){
                    echo "selected='true'";
                  }
                  ?>
                  ><?php echo $rows2['name']?></option>
                <?php
                }
                ?>
                </select></td>
              </tr>
              <tr>
                <td>เรื่อง</td>
                <td>
                  <label for="disabledTextInput">ต้องการประสัมพันธ์ คลิก check box กรณีไม่ได้อยู่ในแค็ตตาล็อกประชาสัมพันธ์</label>
                  <div class="input-group">
                    <span class="input-group-addon">
                      <input type="checkbox" name="public_relations" value="1"<?php echo $ck;?> aria-label="...">
                    </span>
                    <input type="text" name="title" size="100" value="<?php echo $rows['title']?>" class="form-control">
                  </div>
                </td>
              </tr>
              <tr>
                <td>รูปภาพ</td>
                <td><div class="input-group">
                <input type="text" id="images" name="images" value="<?php echo $rows['images']?>" class="form-control" placeholder="รูปภาพประกอบ">
                <span class="input-group-btn">
                  <button href="tinymce/plugins/filemanager/dialog.php?type=1&editor=mce_0&lang=eng&fldr=&field_id=images&popup=1" class="btn btn-default iframe-btn" type="button">SELECT</button>
                </span>
                </div></td>
              </tr>
              <tr>
                <td>เอกสารประกอบ</td>
                <td>
                  <label for="disabledTextInput">แสดง เรื่อง+เอกสารประกอบ คลิก check box</label>
                  <div class="input-group">
                  <span class="input-group-addon">
                    <input type="checkbox" name="images2_status" value="1"<?php echo $ck_i;?> aria-label="...">
                  </span>
                  <input type="text" id="images2" name="images2" value="<?php echo $rows['images2']?>" class="form-control" placeholder="เอกสารประกอบ">
                  <span class="input-group-btn">
                    <button href="tinymce/plugins/filemanager/dialog.php?type=2&editor=mce_0&lang=eng&fldr=&field_id=images2&popup=1" class="btn btn-default iframe-btn" type="button">SELECT</button>
                  </span>
                  </div></td>
                </td>
              </tr>
              
              <?php
              if($_SESSION['apstatus']=="Admin"){
                $sql2 = "select * from ap_gallery order by date_time desc";
              }else{
                $sql2 = "select * from ap_gallery where createBy='$_SESSION[user]' order by date_time desc";
              }
              $query2 = mysqli_query($conn,$sql2);
              $num_rows2 = mysqli_num_rows($query2);
              if($num_rows2>0){
              ?>
              <tr>
                <td>คลังรูปภาพ</td>
                <td>
                  <label for="disabledTextInput">แสดงคลังรูปภาพ คลิก check box</label>
                  <div class="input-group">
                    <span class="input-group-addon">
                      <input type="checkbox" name="gallery_status" value="1"<?php echo $ck_g;?> aria-label="...">
                    </span>
                    <!-- <select name="gallery" class="form-control"> -->
                    <select name="galleryID" class="form-control">
                    <?php
                    if($_SESSION['apstatus']=="Admin"){
                      $sql2 = "select * from ap_gallery order by date_time desc";
                    }else{
                      $sql2 = "select * from ap_gallery where createBy='$_SESSION[user]' order by date_time desc";
                    }
                    $query2 = mysqli_query($conn,$sql2);
                    while($rows2 = mysqli_fetch_array($query2)){
                    ?>
                      <!-- <option value="<?php echo $rows2['code']?>" -->
                      <option value="<?php echo $rows2['id']?>"
                      <?php
                      if($rows2['id']==$rows['galleryID']){
                      echo " selected='true'";
                      }
                      ?>
                      ><?php echo $rows2['name']?></option>
                    <?php
                    }
                    ?>
                    </select>
                  </div>
                </td>
              </tr>
              <?php
              }

              if($_SESSION['apstatus']=="Admin"){
                $sql2 = "select * from ap_gallery2 order by date_time desc";
              }else{
                $sql2 = "select * from ap_gallery2 where createBy='$_SESSION[user]' order by date_time desc";
              }
              $query2 = mysqli_query($conn,$sql2);
              $num_rows2 = mysqli_num_rows($query2);
              if($num_rows2>0){
              ?>
                <tr>
                  <td>คลังวิดีโอ</td>
                  <td>
                    <label for="disabledTextInput">แสดงคลังวิดีโอ คลิก check box</label>
                    <div class="input-group">
                      <span class="input-group-addon">
                        <input type="checkbox" name="gallery2_status" value="1"<?php echo $ck_g2;?> aria-label="...">
                      </span>
                      <!-- <select name="gallery2" class="form-control"> -->
                      <select name="gallery2ID" class="form-control">
                      <?php
                      while($rows2 = mysqli_fetch_array($query2)){
                      ?>
                        <!-- <option value="<?php echo $rows2['code']?>" -->
                        <option value="<?php echo $rows2['id']?>"
                        <?php
                        if($rows2['code']==$rows['gallery2']){
                        echo "selected='true'";
                        }
                        ?>
                        ><?php echo $rows2['name']?></option>
                      <?php
                      }
                      ?>
                      </select>
                    </div>
                  </td>
                </tr>
              <?php
              } 
              ?>

              <tr>
                <td colspan="2">
                  <label>บทนำ</label>
                  <textarea name="info2" rows="5"><?php echo htmlspecialchars($rows['info2']); ?></textarea>
                </td>
              </tr>
              <tr>
                <td colspan="2">
                  <label>รายละเอียด</label>
                  <textarea name="info" rows="15"><?php echo htmlspecialchars($rows['info']); ?></textarea>
                </td>
              </tr>
              <tr>
                <td>คำสำคัญ (Keywords)</td>
                <td>
                  <label for="disabledTextInput">กรุณาใส่ , คั่นระหว่างคำสำคัญ ตัวอย่าง บรรณารักษ์, ห้องสมุด</label>
                  <input type="text" name="keywords" value="<?php echo $rows['keywords']; ?>" class="form-control"></td>
              </tr>
              <tr>
                <td>ประชาสัมพันธ์</td>
                <td><input type="radio" name="notify" value="0"<?php echo $ck_n1;?>>
                  <label>อยู่ระหว่างดำเนินการ หรือ ไม่ประชาสัมพันธ์</label>
                  <input type="radio" name="notify" value="1"<?php echo $ck_n2;?>>
                  <label>ประชาสัมพันธ์</label></td>
              </tr>
              <tr>
                <td></td>
                <td><div class="row col-xs-5">
                    <label>กรณีประชาสัมพันธ์ซ้ำให้ลบตัวเลขออก</label>
                    <input class="form-control" type="text" name="counters" value="<?php echo $rows['counters']?>">
                  </div>
                </td>
              </tr>
              <tr>
                <td colspan="2" align="right"><input type="submit" value="บันทึก" class="btn btn-default"></td>
              </tr>
            </tbody>
          </table>
        </form>
      </div>
    </div>
<?php
}

if($_GET['nu']=="pages_i_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  if($_POST['createBy']==""){
    $_POST['createBy'] = $_SESSION['user'];
  }
  if($_POST['createByStatus']==""){
    $_POST['createByStatus'] = $_SESSION['apstatus'];
  }
  $table = "ap_pages";
  $query = mysqli_query($conn,"select * from $table");
  $num_fields = mysqli_num_fields($query);
  for($i=1; $i<$num_fields; $i++) {
    $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
    $fields = $field_name[$i];

    if($fields!="info" && $fields!="info2"){
      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));
    }

    $fields_1 .= "$fields, ";
    $fields_2 .= "'".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
  }
  $field_1 = substr($fields_1,0,-2);
  $field_2 = substr($fields_2,0,-2);
  mysqli_query($conn,"insert into $table ($field_1) values ($field_2)");
  echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
  echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=pages'>";
  echo "</div>";
}

if($_GET['nu']=="pages_u_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_pages";
  $field = "id";
  $field2 = $_POST['id'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2'"));
  if($nr<0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=pages&action=pages_i_u&$field=$field2'>";
  }else{
    if($_POST['createBy']==""){
      $_POST['createBy'] = $_SESSION['user'];
    }
    if($_POST['createByStatus']==""){
      $_POST['createByStatus'] = $_SESSION['apstatus'];
    }
    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    for($i=1; $i<$num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      if($fields!="info" && $fields!="info2"){
        $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));
      }

      $fields_ .= "$fields='".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $fields_set = substr($fields_,0,-2);
    mysqli_query($conn,"update $table set $fields_set where $field='$field2' ");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    //echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=pages_i_u&action=pages_u_s&$field=$field2'>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=pages'>";
  }
  echo "</div>";
}

if ($_GET['nu']=="pages_delete") {
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_pages";
  // $images = str_replace("$ap_site$ap_folder", "", "../$_GET[images]");
  // $images2 = str_replace("$ap_site$ap_folder", "", "../$_GET[images2]");
  // unlink($images);
  // unlink($images2);
  mysqli_query($conn,"delete from $table where id='$_GET[id]'");
  echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
  echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=pages'>";
  echo "</div>";
}

if($_GET['nu']=="catalogs"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">แค็ตตาล็อก</h2>
    <div class="table-responsive">
      <table class="table table-striped">
        <thead>
          <tr>
            <th width="70">ลำดับ</th>
            <th>ภาษา</th>
            <th width="150">ลำดับแค็ตตาล็อก</th>
            <th width="120">ให้แสดงหน้าหลัก</th>
            <th width="130">กลุ่ม Link ภายนอก</th>
            <th>รหัส</th>
            <th>ชื่อ</th>
            <th width="100"><a href="?nu=catalogs_i_u&action=catalogs_i_s">เพิ่ม</a></th>
          </tr>
        </thead>
        <tbody>
          <?php
          $sql = "select * from ap_catalogs order by code asc";
          $query = mysqli_query($conn,$sql);
          $i = 0;
          while($rows = mysqli_fetch_array($query)){
          $i++;
          ?>
            <tr>
              <td><?php echo $i;?></td>
              <td><?php echo $rows['lang'];?></td>
              <td align="center"><?php echo $rows['level'];?></td>
              <td align="center"><?php echo $rows['status'];?></td>
              <td align="center"><?php echo $rows['external_status'];?></td>
              <td><?php echo $rows['code'];?></td>
              <td><?php echo $rows['name'];?></td>
              <td align="center"><a href="?nu=catalogs_i_u&action=catalogs_u_s&id=<?php echo $rows['id']?>">แก้ไข</a> :  <a href="#" onclick="if (confirm('ยืนยันการลบ?')) location.href='?nu=catalogs_delete&id=<?php echo $rows['id']?>';">ลบ</a></td>
            </tr>
          <?php
          }
          ?>
        </tbody>
      </table>
    </div>
  </div>
<?php
}

if($_GET['nu']=="catalogs_i_u"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  $sql = "select * from ap_catalogs where id='$_GET[id]'";
  $query = mysqli_query($conn,$sql);
  $rows = mysqli_fetch_array($query);

  if($rows['status']==1){
    $ck = " checked=\"true\"";
  }else{ $ck = ""; }
  if($rows['external_status']==1){
    $ck2 = " checked=\"true\"";
  }else{ $ck2 = ""; }
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">แค็ตตาล็อก</h2>
    <div class="table-responsive">
      <form action="?nu=<?php echo $_GET['action']?>" method="post" enctype="multipart/form-data">
      <input type="hidden" name="id" value="<?php echo $rows['id']?>">
      <input type="hidden" name="code_" value="<?php echo $rows['code']?>">
      <input type="hidden" name="counters" value="<?php echo $rows['counters']?>">
      <table class="table table-striped">
        <tbody>
          <tr>
            <td>สำหรับเว็บไซต์ภาษา</td>
            <td><select name="lang" class="form-control">
            <?php
            $sql2 = "select * from ap_langs order by code asc";
            $query2 = mysqli_query($conn,$sql2);
            while($rows2 = mysqli_fetch_array($query2)){
            ?>
              <option value="<?php echo $rows2['code']?>"
              <?php
              if($rows2['code']==$rows['lang']){
                echo "selected='true'";
              }
              ?>
              ><?php echo $rows2['name']?></option>
            <?php
            }
            ?>
            </select></td>
          </tr>
          <tr>
            <td>ลำดับแค็ตตาล็อก</td>
            <td>
              <label>ระบุลำดับการแสดงแค็ตตาล็อก ต.ย. 00 และทำเครื่องหมายถูกกรณีให้แสดงหน้าหลัก</label>
              <div class="input-group">
                <span class="input-group-addon">
                  <input type="checkbox" name="status" value="1"<?php echo $ck;?> aria-label="...">
                </span>
                <input type="text" name="level" value="<?php if(empty($rows['level'])){ echo "00"; }else{ echo $rows['level']; } ?>" class="form-control">
              </div>
            </td>
          </tr>
          <tr>
            <td>รหัส</td>
            <td><input type="text" name="code" value="<?php echo $rows['code']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>ชื่อ</td>
            <td>
              <label>ทำเครื่องหมายถูกกรณีให้เป็นกลุ่ม Link ภายนอก</label>
              <div class="input-group">
                <span class="input-group-addon">
                  <input type="checkbox" name="external_status" value="1"<?php echo $ck2;?> aria-label="...">
                </span>
                <input type="text" name="name" value="<?php echo $rows['name']?>" class="form-control">
              </div>
            </td>
          </tr>
          <tr>
            <td colspan="2" align="right"><input type="submit" value="บันทึก" class="btn btn-default"></td>
          </tr>
        </tbody>
      </table>
      </form>
    </div>
  </div>
<?php
}

if($_GET['nu']=="catalogs_i_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_catalogs";
  $field = "code";
  $field2 = $_POST['code'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2'"));
  if($nr>0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=catalogs&action=catalogs_i_u&$field=$field2'>";
  }else{
    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    for($i=1; $i<$num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));

      $fields_1 .= "$fields, ";
      $fields_2 .= "'".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $field_1 = substr($fields_1,0,-2);
    $field_2 = substr($fields_2,0,-2);
    mysqli_query($conn,"insert into $table ($field_1) values ($field_2)");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=catalogs'>";
  }
  echo "</div>";
}

if($_GET['nu']=="catalogs_u_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_catalogs";
  $field = "id";
  $field2 = $_POST['id'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2'"));
  if($nr<0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=catalogs&action=catalogs_i_u&$field=$field2'>";
  }else{
    mysqli_query($conn,"update ap_menus set url_link3='$_POST[code]' where url_link3='$_POST[code_]'");
    mysqli_query($conn,"update ap_menus2 set url_link3='$_POST[code]' where url_link3='$_POST[code_]'");
    mysqli_query($conn,"update ap_menus3 set url_link3='$_POST[code]' where url_link3='$_POST[code_]'");
    mysqli_query($conn,"update ap_menus4 set url_link3='$_POST[code]' where url_link3='$_POST[code_]'");
    mysqli_query($conn,"update ap_externalurl set catalogCode='$_POST[code]' where catalogCode='$_POST[code_]'");
    mysqli_query($conn,"update ap_pages set catalogCode='$_POST[code]' where catalogCode='$_POST[code_]'");
    mysqli_query($conn,"update ap_gallery set catalogCode='$_POST[code]' where catalogCode='$_POST[code_]'");
    mysqli_query($conn,"update ap_gallery2 set catalogCode='$_POST[code]' where catalogCode='$_POST[code_]'");
    mysqli_query($conn,"update ap_gallery3 set catalogCode='$_POST[code]' where catalogCode='$_POST[code_]'");
    mysqli_query($conn,"update ap_edms set catalogCode='$_POST[code]' where catalogCode='$_POST[code_]'");
    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    for($i=1; $i<$num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));

      $fields_ .= "$fields='".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $fields_set = substr($fields_,0,-2);
    mysqli_query($conn,"update $table set $fields_set where $field='$field2' ");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=catalogs'>";
  }
  echo "</div>";
}

if ($_GET['nu']=="catalogs_delete") {
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_catalogs";
  mysqli_query($conn,"delete from $table where id='$_GET[id]'");
  echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
  echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=catalogs'>";
  echo "</div>";
}

if($_GET['nu']=="gallery"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">คลังรูปภาพ</h2>
    <div class="table-responsive">
      <table class="table table-striped">
        <thead>
          <tr>
            <th width="70">ลำดับ</th>
            <th>ภาษา</th>
            <th>แค็ตตาล็อก</th>
            <th>รหัส</th>
            <th>ชื่อ</th>
            <th>Photo Slide</th>
            <th width="100"><a href="?nu=gallery_i_u&action=gallery_i_s">เพิ่ม</a></th>
          </tr>
        </thead>
        <tbody>
          <?php
          if($_SESSION['apstatus']=="Admin"){
            $sql = "select * from ap_gallery order by date_time desc";
          }else{
            $sql = "select * from ap_gallery where createBy='$_SESSION[user]' order by date_time desc";
          }
          $query = mysqli_query($conn,$sql);
          $i = 0;
          while($rows = mysqli_fetch_array($query)){
          $i++;
            $sql2 = "select * from ap_catalogs where code='$rows[catalogCode]'";
            $query2 = mysqli_query($conn,$sql2);
            $rows2 = mysqli_fetch_array($query2);
            if($_SESSION['apstatus']!="Admin" && $rows['createBy']==$_SESSION['user']){
              $root = rtrim($_SERVER['DOCUMENT_ROOT'],'/');
              if($ap_folder==""){
                $root = $root;
              }else{
                $root = $root.'\\'.$ap_folder;
              }
              if(!file_exists($root.'\\contents\\'.$_SESSION['user'].'\\'.$rows['code'].'\\')) {
                  mkdir($root.'\\contents\\'.$_SESSION['user'].'\\'.$rows['code'].'\\', 0777);
              }//echo $root.'\\contents\\'.$_SESSION['user'].'\\'.$rows['code'].'\\';
            }
            ?>
            <tr>
              <td><?php echo $i;?></td>
              <td><?php echo $rows['lang'];?></td>
              <td><?php echo $rows2['name'];?></td>
              <td><?php echo $rows['code'];?></td>
              <td><?php echo $rows['name'];?></td>
              <td><a href="<?php echo $ap_site.$ap_folder ?>ap/photo-slide/?nu=&id=<?php echo $rows['id'] ?>" target="_blank">คลิก</a></td>
              <td><?php if($_SESSION['apstatus']=="Admin" || $rows['createBy']==$_SESSION['user']){?><a href="?nu=gallery_i_u&action=gallery_u_s&id=<?php echo $rows['id']?>">แก้ไข</a> :  <a href="#" onclick="if (confirm('ยืนยันการลบ?')) location.href='?nu=gallery_delete&id=<?php echo $rows['id']?>';">ลบ</a><?php } ?></td>
            </tr>
          <?php
          }
          ?>
        </tbody>
      </table>
    </div>
  </div>
<?php
}

if($_GET['nu']=="gallery_i_u"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  $sql = "select * from ap_gallery where id='$_GET[id]'";
  $query = mysqli_query($conn,$sql);
  $rows = mysqli_fetch_array($query);
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">คลังรูปภาพ</h2>
    <div class="table-responsive">
      <form action="?nu=<?php echo $_GET['action']?>" method="post" enctype="multipart/form-data">
      <input type="hidden" name="id" value="<?php echo $rows['id']?>">
      <input type="hidden" name="createBy" value="<?php echo $rows['createBy']?>">
      <input type="hidden" name="createByStatus" value="<?php echo $rows['createByStatus']?>">
      <input type="hidden" name="counters" value="<?php echo $rows['counters']?>">
      <table class="table table-striped">
        <tbody>
          <tr>
            <td>สำหรับเว็บไซต์ภาษา</td>
            <td><select name="lang" class="form-control">
            <?php
            $sql2 = "select * from ap_langs order by code asc";
            $query2 = mysqli_query($conn,$sql2);
            while($rows2 = mysqli_fetch_array($query2)){
            ?>
              <option value="<?php echo $rows2['code']?>"
              <?php
              if($rows2['code']==$rows['lang']){
                echo "selected='true'";
              }
              ?>
              ><?php echo $rows2['name']?></option>
            <?php
            }
            ?>
            </select></td>
          </tr>
          <tr>
            <td>วันที่</td>
            <td>
              <label>ตัวอย่าง : <?php echo date('Ymd')?></label>
              <input type="text" name="date_time" value="<?php if(empty($rows['date_time'])){ echo date('Ymd'); }else{ echo $rows['date_time']; } ?>" class="form-control">
            </td>
          </tr>
          <tr>
            <td>แค็ตตาล็อก</td>
            <td><select name="catalogCode" class="form-control">
            <?php
            if($_SESSION['apstatus']=="Admin"){
              $sql2 = "select * from ap_catalogs where status='1' order by code asc";
            }else{
              $sql2 = "select * from ap_catalogs where status='1' and external_status!='1' order by name asc";
            }
            $query2 = mysqli_query($conn,$sql2);
            while($rows2 = mysqli_fetch_array($query2)){
            ?>
              <option value="<?php echo $rows2['code']?>"
              <?php
              if($rows2['code']==$rows['catalogCode']){
                echo "selected='true'";
              }
              ?>
              ><?php echo $rows2['name']?></option>
            <?php
            }
            ?>
            </select></td>
          </tr>
          <tr>
            <td>รหัส</td>
            <td>
              <label>ตัวอย่าง : <?php echo date('Y.m.d')?></label>
              <input type="text" name="code" value="<?php if(empty($rows['code'])){ echo date('Y.m.d'); }else{ echo $rows['code']; } ?>" class="form-control">
            </td>
          </tr>
          <tr>
            <td>ชื่อ</td>
            <td>
            <label>ตัวอย่าง : "เรื่อง" วันที่ 20 มีนาคม 2522</label>
            <input type="text" name="name" value="<?php echo $rows['name']?>" class="form-control"></td>
          </tr>
          <tr>
            <td colspan="2" align="right"><input type="submit" value="บันทึก" class="btn btn-default"></td>
          </tr>
        </tbody>
      </table>
      </form>
    </div>
  </div>
<?php
}

if($_GET['nu']=="gallery_i_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_gallery";
  $field = "code";
  $field2 = $_POST['code'];
  if($_SESSION['apstatus']=="Admin"){
    $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2'"));
  }else{
    $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where createBy='$_SESSION[user]' and $field='$field2'"));
  }
  if($nr>0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=gallery&action=gallery_i_u&$field=$field2'>";
  }else{
    if($_POST['createBy']==""){
      $_POST['createBy'] = $_SESSION['user'];
    }
    if($_POST['createByStatus']==""){
      $_POST['createByStatus'] = $_SESSION['apstatus'];
    }
    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    for($i=1; $i<$num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));

      $fields_1 .= "$fields, ";
      $fields_2 .= "'".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $field_1 = substr($fields_1,0,-2);
    $field_2 = substr($fields_2,0,-2);
    mysqli_query($conn,"insert into $table ($field_1) values ($field_2)");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=gallery'>";
  }
  echo "</div>";
}

if($_GET['nu']=="gallery_u_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_gallery";
  $field = "id";
  $field2 = $_POST['id'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2'"));
  if($nr<0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=gallery&action=gallery_i_u&$field=$field2'>";
  }else{
    if($_POST['createBy']==""){
      $_POST['createBy'] = $_SESSION['user'];
    }
    if($_POST['createByStatus']==""){
      $_POST['createByStatus'] = $_SESSION['apstatus'];
    }
    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    $fields_ = '';
    for($i=1; $i<$num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));

      $fields_ .= "$fields='".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $fields_set = substr($fields_,0,-2);
    mysqli_query($conn,"update $table set $fields_set where $field='$field2' ");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=gallery'>";
  }
  echo "</div>";
}

if ($_GET['nu']=="gallery_delete") {
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_gallery";
  mysqli_query($conn,"delete from $table where id='$_GET[id]'");
  echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
  echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=gallery'>";
  echo "</div>";
}

if($_GET['nu']=="gallery2"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">คลังวิดีโอ</h2>
    <div class="table-responsive">
      <table class="table table-striped">
        <thead>
          <tr>
            <th width="70">ลำดับ</th>
            <th>ภาษา</th>
            <th>แค็ตตาล็อก</th>
            <th>รหัส</th>
            <th>สถานะ</th>
            <th>ชื่อเรื่อง</th>
            <th width="100"><a href="?nu=gallery2_i_u&action=gallery2_i_s">เพิ่ม</a></th>
          </tr>
        </thead>
        <tbody>
          <?php
          if($_SESSION['apstatus']=="Admin"){
            $sql = "select * from ap_gallery2 order by date_time desc";
          }else{
            $sql = "select * from ap_gallery2 where createBy='$_SESSION[user]' order by date_time desc";
          }
          $query = mysqli_query($conn,$sql);
          $i = 0;
          while($rows = mysqli_fetch_array($query)){
          $i++;
            $sql2 = "select * from ap_catalogs where code='$rows[catalogCode]'";
            $query2 = mysqli_query($conn,$sql2);
            $rows2 = mysqli_fetch_array($query2);
            if($_SESSION['apstatus']!="Admin" && $rows['createBy']==$_SESSION['user']){
              $root = rtrim($_SERVER['DOCUMENT_ROOT'],'/');
              if($ap_folder==""){
                $root = $root;
              }else{
                $root = $root.'\\'.$ap_folder;
              }
              if(!file_exists($root.'\\contents\\'.$_SESSION['user'].'\\'.$rows['code'].'\\')) {
                  mkdir($root.'\\contents\\'.$_SESSION['user'].'\\'.$rows['code'].'\\', 0777);
              }//echo $root.'\\contents\\'.$_SESSION['user'].'\\'.$rows['code'].'\\';
            }
            ?>
            <tr>
              <td><?php echo $i;?></td>
              <td><?php echo $rows['lang'];?></td>
              <td><?php echo $rows2['name'];?></td>
              <td><?php echo $rows['code'];?></td>
              <td><?php if($rows['status']==1){ echo "วิดีโอเด่น"; }?></td>
              <td><?php echo $rows['name'];?></td>
              <td><?php if($_SESSION['apstatus']=="Admin" || $rows['createBy']==$_SESSION['user']){?><a href="?nu=gallery2_i_u&action=gallery2_u_s&id=<?php echo $rows['id']?>">แก้ไข</a> : <a href="#" onclick="if (confirm('คุณแน่ใจหรือไม่? กรุณายืนยันการลบ?')) location.href='?nu=gallery2_delete&id=<?php echo $rows['id']?>';">ลบ</a><?php } ?></td>
            </tr>
          <?php
          }
          ?>
        </tbody>
      </table>
    </div>
  </div>
<?php
}

if($_GET['nu']=="gallery2_i_u"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  $sql = "select * from ap_gallery2 where id='$_GET[id]'";
  $query = mysqli_query($conn,$sql);
  $rows = mysqli_fetch_array($query);

  if($rows['status']==1){
    $ck = " checked=\"true\"";
  }else{ $ck = ""; }
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">คลังวิดีโอ</h2>
    <div class="table-responsive">
      <form action="?nu=<?php echo $_GET['action']?>" method="post" enctype="multipart/form-data">
      <input type="hidden" name="id" value="<?php echo $rows['id']?>">
      <input type="hidden" name="counters" value="<?php echo $rows['counters']?>">
      <table class="table table-striped">
        <tbody>
          <tr>
            <td>สำหรับเว็บไซต์ภาษา</td>
            <td><select name="lang" class="form-control">
            <?php
            $sql2 = "select * from ap_langs order by code asc";
            $query2 = mysqli_query($conn,$sql2);
            while($rows2 = mysqli_fetch_array($query2)){
            ?>
              <option value="<?php echo $rows2['code']?>"
              <?php
              if($rows2['code']==$rows['lang']){
                echo "selected='true'";
              }
              ?>
              ><?php echo $rows2['name']?></option>
            <?php
            }
            ?>
            </select></td>
          </tr>
          <tr>
            <td>วันที่</td>
            <td>
              <label>ตัวอย่าง : <?php echo date('Ymd')?></label>
              <input type="text" name="date_time" value="<?php if(empty($rows['date_time'])){ echo date('Ymd'); }else{ echo $rows['date_time']; } ?>" class="form-control">
            </td>
          </tr>
          <tr>
            <td>แค็ตตาล็อก</td>
            <td><select name="catalogCode" class="form-control">
            <?php
            if($_SESSION['apstatus']=="Admin"){
              $sql2 = "select * from ap_catalogs where status='1' order by code asc";
            }else{
              $sql2 = "select * from ap_catalogs where status='1' and external_status!='1' order by name asc";
            }
            $query2 = mysqli_query($conn,$sql2);
            while($rows2 = mysqli_fetch_array($query2)){
            ?>
              <option value="<?php echo $rows2['code']?>"
              <?php
              if($rows2['code']==$rows['catalogCode']){
                echo "selected='true'";
              }
              ?>
              ><?php echo $rows2['name']?></option>
            <?php
            }
            ?>
            </select></td>
          </tr>
          <tr>
            <td>รหัส</td>
            <td>
              <label>ตัวอย่าง : <?php echo date('Y.m.d')?></label>
              <input type="text" name="code" value="<?php if(empty($rows['code'])){ echo date('Y.m.d'); }else{ echo $rows['code']; } ?>" class="form-control">
            </td>
          </tr>
          <tr>
            <td>ชื่อเรื่อง</td>
            <td>
              <label>ทำเครื่องหมายถูก ตั้งเป็นวิดีโอเด่น และ ตัวอย่าง : "เรื่อง" วันที่ 20 มีนาคม 2522</label>
              <div class="input-group">
                <span class="input-group-addon">
                  <input type="checkbox" name="status" value="1"<?php echo $ck;?> aria-label="...">
                </span>
                <input type="text" name="name" value="<?php echo $rows['name']?>" class="form-control">
              </div>
            </td>
          </tr>
          <tr>
            <td>รูปภาพ</td>
            <td>
            <label for="">อัพโหลดรูปภาพ และ วิดีโอ หลังอัพโหลดเสร็จให้คลิกที่รูปภาพ</label>
            <div class="input-group">
            <input type="text" id="images" name="images" value="<?php echo $rows['images']?>" class="form-control" placeholder="รูปภาพประกอบ">
            <span class="input-group-btn">
              <button href="tinymce/plugins/filemanager/dialog.php?type=1&editor=mce_0&lang=eng&fldr=&field_id=images&popup=1" class="btn btn-default iframe-btn" type="button">SELECT</button>
            </span>
            </div></td>
          </tr>
          <tr>
            <td colspan="2" align="right"><input type="submit" value="บันทึก" class="btn btn-default"></td>
          </tr>
        </tbody>
      </table>
      </form>
    </div>
  </div>
<?php
}

if($_GET['nu']=="gallery2_i_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_gallery2";
  $field = "code";
  $field2 = $_POST['code'];

  if($_SESSION['apstatus']=="Admin"){
    $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2'"));
  }else{
    $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where createBy='$_SESSION[user]' and $field='$field2'"));
  }
  if($nr>0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=gallery2&action=gallery2_i_u&$field=$field2'>";
  }else{
    if($_POST['createBy']==""){
      $_POST['createBy'] = $_SESSION['user'];
    }
    $_POST['createByStatus'] = $_SESSION['apstatus'];
    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    for($i=1; $i<$num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));

      $fields_1 .= "$fields, ";
      $fields_2 .= "'".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $field_1 = substr($fields_1,0,-2);
    $field_2 = substr($fields_2,0,-2);
    mysqli_query($conn,"insert into $table ($field_1) values ($field_2)");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=gallery2'>";
  }
  echo "</div>";
}

if($_GET['nu']=="gallery2_u_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_gallery2";
  $field = "id";
  $field2 = $_POST['id'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2'"));
  if($nr<0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=gallery2&action=gallery2_i_u&$field=$field2'>";
  }else{
    if($_POST['createBy']==""){
      $_POST['createBy'] = $_SESSION['user'];
    }
    $_POST['createByStatus'] = $_SESSION['apstatus'];
    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    $fields_ = '';
    for($i=1; $i<$num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));

      $fields_ .= "$fields='".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $fields_set = substr($fields_,0,-2);
    mysqli_query($conn,"update $table set $fields_set where $field='$field2' ");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=gallery2'>";
  }
  echo "</div>";
}

if ($_GET['nu']=="gallery2_delete") {
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_gallery2";
  mysqli_query($conn,"delete from $table where id='$_GET[id]'");
  echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
  echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=gallery2'>";
  echo "</div>";
}

if($_GET['nu']=="gallery3"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">eBooks Online</h2>
    <div class="table-responsive">
      <table class="table table-striped">
        <thead>
          <tr>
            <th width="70">ลำดับ</th>
            <th>แค็ตตาล็อก</th>
            <th>รหัส</th>
            <th>ชื่อเรื่อง</th>
            <th>สถานะ</th>
            <th width="100"><a href="?nu=gallery3_i_u&action=gallery3_i_s">เพิ่ม</a></th>
          </tr>
        </thead>
        <tbody>
          <?php
          if($_SESSION['apstatus']=="Admin"){
            $sql = "select * from ap_gallery3 order by date_time desc";
          }else{
            $sql = "select * from ap_gallery3 where createBy='$_SESSION[user]' order by date_time desc";
          }
          $query = mysqli_query($conn,$sql);
          $i = 0;
          while($rows = mysqli_fetch_array($query)){
          $i++;
            $sql2 = "select * from ap_catalogs where code='$rows[catalogCode]'";
            $query2 = mysqli_query($conn,$sql2);
            $rows2 = mysqli_fetch_array($query2);
            if($_SESSION['apstatus']!="Admin" && $rows['createBy']==$_SESSION['user']){
              $root = rtrim($_SERVER['DOCUMENT_ROOT'],'/');
              if($ap_folder==""){
                $root = $root;
              }else{
                $root = $root.'\\'.$ap_folder;
              }
              if(!file_exists($root.'\\contents\\'.$_SESSION['user'].'\\'.$rows['code'].'\\')) {
                  mkdir($root.'\\contents\\'.$_SESSION['user'].'\\'.$rows['code'].'\\', 0777);
              }//echo $root.'\\contents\\'.$_SESSION['user'].'\\'.$rows['code'].'\\';
            }
            ?>
            <tr>
              <td><?php echo $i;?></td>
              <td><?php echo $rows2['name'];?></td>
              <td><?php echo $rows['code'];?></td>
              <td><a href="?nu=gallery3_i_u&action=gallery3_i_s&id=<?php echo $rows['id']?>"><?php echo $rows['title'];?></a><?php if($rows['images3']!=""){ ?> / <a href="../ap/ebooks/print.php?nu=&xid=<?php echo $rows['id']?>" target="_blank">[หนังสือยินยอมอนุญาต]</a><?php } ?></td>
              <td><?php echo $rows['status2'];?></td>
              <td><?php if($_SESSION['apstatus']=="Admin" || $rows['createBy']==$_SESSION['user']){?><a href="?nu=gallery3_i_u&action=gallery3_u_s&id=<?php echo $rows['id']?>">แก้ไข</a> :  <a href="#" onclick="if (confirm('ยืนยันการลบ?')) location.href='?nu=gallery3_delete&id=<?php echo $rows['id']?>';">ลบ</a><?php } ?></td>
            </tr>
          <?php
          }
          ?>
        </tbody>
      </table>
    </div>
  </div>
<?php
}

if($_GET['nu']=="gallery3_i_u"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  $sql = "select * from ap_gallery3 where id='$_GET[id]'";
  $query = mysqli_query($conn,$sql);
  $rows = mysqli_fetch_array($query);
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">eBooks Online</h2>
    <div class="table-responsive">
      <form action="?nu=<?php echo $_GET['action']?>" method="post" enctype="multipart/form-data">
      <input type="hidden" name="id" value="<?php echo $rows['id']?>">
      <input type="hidden" name="createBy" value="<?php echo $rows['createBy']?>">
      <input type="hidden" name="createByStatus" value="<?php echo $rows['createByStatus']?>">
      <input type="hidden" name="counters" value="<?php echo $rows['counters']?>">
      <table class="table table-striped">
        <tbody>
          <tr>
            <td>วันที่</td>
            <td>
              <label>ตัวอย่าง : <?php echo date('Ymd')?></label>
              <input type="text" name="date_time" value="<?php if(empty($rows['date_time'])){ echo date('Ymd'); }else{ echo $rows['date_time']; } ?>" class="form-control">
            </td>
          </tr>
          <tr>
            <td>แค็ตตาล็อก</td>
            <td><select name="catalogCode" class="form-control">
            <?php
            $sql2 = "select * from ap_catalogs where code like 'eBooks-%' order by name asc";
            $query2 = mysqli_query($conn,$sql2);
            while($rows2 = mysqli_fetch_array($query2)){
            ?>
              <option value="<?php echo $rows2['code']?>"
              <?php
              if($rows2['code']==$rows['catalogCode']){
                echo "selected='true'";
              }
              ?>
              ><?php echo $rows2['name']?></option>
            <?php
            }
            ?>
            </select></td>
          </tr>
          <tr>
            <td>รหัส</td>
            <td>
              <label>ตัวอย่าง : <?php echo date('Y-m-d')?></label>
              <input type="text" name="code" value="<?php if(empty($rows['code'])){ echo date('Y-m-d'); }else{ echo $rows['code']; } ?>" class="form-control">
            </td>
          </tr>
          <tr>
            <td>ชื่อเรื่อง</td>
            <td>
              <label for="">เผยแพร่ข้อมูลทำเครื่องหมายถูกด้านหน้าชื่อเรื่อง</label>
              <div class="input-group">
                <span class="input-group-addon">
                  <input type="checkbox" name="status2" value="1" <?php if($rows['status2']==1){ echo "checked"; } ?>>
                </span>
                <input placeholder="ชื่อเรื่อง" type="text" name="title" value="<?php echo $rows['title']?>" class="form-control">
              </div>
            </td>
          </tr>
          <tr>
            <td>ผู้แต่ง</td>
            <td><input type="text" name="author" value="<?php echo $rows['author']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>ปีที่จัดทำ</td>
            <td><input type="text" name="years" value="<?php echo $rows['years']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>Keywords</td>
            <td><input type="text" name="keywords" value="<?php echo $rows['keywords']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>รูปภาพ</td>
            <td><div class="input-group">
            <input type="text" id="images" name="images" value="<?php echo $rows['images']?>" class="form-control" placeholder="รูปภาพประกอบ">
            <span class="input-group-btn">
              <button href="tinymce/plugins/filemanager/dialog.php?type=1&editor=mce_0&lang=eng&fldr=&field_id=images&popup=1" class="btn btn-default iframe-btn" type="button">SELECT</button>
            </span>
            </div></td>
          </tr>
          <tr>
            <td>เอกสารประกอบ PDF</td>
            <td><label for="disabledTextInput">ทำเครื่องหมายถูก กรณีให้สามารถดาวห์โหลดเอกสารได้</label>
              <div class="input-group">
              <span class="input-group-addon">
                  <input type="checkbox" name="status" value="1"<?php if($rows['status']=="1"){ echo " checked"; }?> aria-label="...">
                </span>
              <input type="text" id="images2" name="images2" value="<?php echo $rows['images2']?>" class="form-control" placeholder="เอกสารประกอบ">
              <span class="input-group-btn">
                <button href="tinymce/plugins/filemanager/dialog.php?type=2&editor=mce_0&lang=eng&fldr=&field_id=images2&popup=1" class="btn btn-default iframe-btn" type="button">SELECT</button>
              </span>
              </div></td>
            </td>
          </tr>
          <tr>
            <td>ลายเซนต์เจ้าของผลงาน</td>
            <td><div class="input-group">
            <input type="text" id="images3" name="images3" value="<?php echo $rows['images3']?>" class="form-control" placeholder="ลายเซนต์">
            <span class="input-group-btn">
              <button href="tinymce/plugins/filemanager/dialog.php?type=1&editor=mce_0&lang=eng&fldr=&field_id=images3&popup=1" class="btn btn-default iframe-btn" type="button">SELECT</button>
            </span>
            </div></td>
          </tr>
          <tr>
            <td colspan="2" align="right"><input type="submit" value="บันทึก" class="btn btn-default"></td>
          </tr>
        </tbody>
      </table>
      </form>
    </div>
  </div>
<?php
}

if($_GET['nu']=="gallery3_i_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_gallery3";
  $field = "code";
  $field2 = $_POST['code'];
  if($_SESSION['apstatus']=="Admin"){
    $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2'"));
  }else{
    $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where createBy='$_SESSION[user]' and $field='$field2'"));
  }
  if($nr>0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=gallery3&action=gallery3_i_u&$field=$field2'>";
  }else{
    if($_POST['createBy']==""){
      $_POST['createBy'] = $_SESSION['user'];
    }
    if($_POST['createByStatus']==""){
      $_POST['createByStatus'] = $_SESSION['apstatus'];
    }
    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    for($i=1; $i<$num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));

      $fields_1 .= "$fields, ";
      $fields_2 .= "'".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $field_1 = substr($fields_1,0,-2);
    $field_2 = substr($fields_2,0,-2);
    mysqli_query($conn,"insert into $table ($field_1) values ($field_2)");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=gallery3'>";
  }
  echo "</div>";
}

if($_GET['nu']=="gallery3_u_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_gallery3";
  $field = "id";
  $field2 = $_POST['id'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2'"));
  if($nr<0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=gallery3&action=gallery3_i_u&$field=$field2'>";
  }else{
    if($_POST['createBy']==""){
      $_POST['createBy'] = $_SESSION['user'];
    }
    if($_POST['createByStatus']==""){
      $_POST['createByStatus'] = $_SESSION['apstatus'];
    }
    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    $fields_ = '';
    for($i=1; $i<$num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));

      $fields_ .= "$fields='".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $fields_set = substr($fields_,0,-2);
    mysqli_query($conn,"update $table set $fields_set where $field='$field2' ");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=gallery3'>";
  }
  echo "</div>";
}

if ($_GET['nu']=="gallery3_delete") {
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_gallery3";
  mysqli_query($conn,"delete from $table where id='$_GET[id]'");
  echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
  echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=gallery3'>";
  echo "</div>";
}

if($_GET['nu']=="externalurl"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">Link ภายนอก</h2>
    <div class="table-responsive">
      <table class="table table-striped">
        <thead>
          <tr>
            <th width="70">ลำดับ</th>
            <th>ภาษา</th>
            <th>แค็ตตาล็อก</th>
            <th>ลำดับ Link</th>
            <th>รหัส</th>
            <th>ชื่อ</th>
            <th width="100"><a href="?nu=externalurl_i_u&action=externalurl_i_s">เพิ่ม</a></th>
          </tr>
        </thead>
        <tbody>
          <?php
          $sql = "select * from ap_externalurl order by catalogCode asc, level asc";
          $query = mysqli_query($conn,$sql);
          $i = 0;
          while($rows = mysqli_fetch_array($query)){
          $i++;
            $sql2 = "select * from ap_catalogs where code='$rows[catalogCode]'";
            $query2 = mysqli_query($conn,$sql2);
            $rows2 = mysqli_fetch_array($query2);
            ?>
            <tr>
              <td><?php echo $i;?></td>
              <td><?php echo $rows['lang'];?></td>
              <td><?php echo $rows2['name'];?></td>
              <td><?php echo $rows['level'];?></td>
              <td><?php echo $rows['code'];?></td>
              <td><?php echo $rows['name'];?></td>
              <td><a href="?nu=externalurl_i_u&action=externalurl_u_s&id=<?php echo $rows['id']?>">แก้ไข</a> :  <a href="#" onclick="if (confirm('ยืนยันการลบ?')) location.href='?nu=externalurl_delete&id=<?php echo $rows['id']?>';">ลบ</a></td>
            </tr>
          <?php
          }
          ?>
        </tbody>
      </table>
    </div>
  </div>
<?php
}

if($_GET['nu']=="externalurl_i_u"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  $sql = "select * from ap_externalurl where id='$_GET[id]'";
  $query = mysqli_query($conn,$sql);
  $rows = mysqli_fetch_array($query);
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">Link ภายนอก</h2>
    <div class="table-responsive">
      <form action="?nu=<?php echo $_GET['action']?>" method="post" enctype="multipart/form-data">
      <input type="hidden" name="id" value="<?php echo $rows['id']?>">
      <table class="table table-striped">
        <tbody>
        <tr>
          <td>สำหรับเว็บไซต์ภาษา</td>
          <td><select name="lang" class="form-control">
          <?php
          $sql2 = "select * from ap_langs order by code asc";
          $query2 = mysqli_query($conn,$sql2);
          while($rows2 = mysqli_fetch_array($query2)){
          ?>
            <option value="<?php echo $rows2['code']?>"
            <?php
            if($rows2['code']==$rows['lang']){
              echo "selected='true'";
            }
            ?>
            ><?php echo $rows2['name']?></option>
          <?php
          }
          ?>
          </select></td>
        </tr>
        <tr>
            <td>แค็ตตาล็อก</td>
            <td><select name="catalogCode" id="catalogCode" class="form-control">
            <?php
            $sql2 = "select * from ap_catalogs where external_status='1' order by code asc";
            $query2 = mysqli_query($conn,$sql2);
            while($rows2 = mysqli_fetch_array($query2)){
            ?>
              <option value="<?php echo $rows2['code']?>"
              <?php
              if($rows2['code']==$rows['catalogCode']){
                echo "selected='true'";
              }
              ?>
              ><?php echo $rows2['name']?></option>
            <?php
            }
            ?>
            </select></td>
          </tr>
          <tr>
            <td>ลำดับ Link</td>
            <td>
            <label for="disabledTextInput">ตัวอย่าง 00</label>
            <input type="text" name="level" value="<?php if(empty($rows['level'])){ echo "00"; }else{ echo $rows['level']; } ?>" class="form-control"></td>
          </tr>
          <tr>
            <td>รหัส</td>
            <td><input type="text" name="code" value="<?php echo $rows['code']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>ชื่อ</td>
            <td><input type="text" name="name" size="30" value="<?php echo $rows['name']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>URL Link</td>
            <td>
            <label for="disabledTextInput">ถ้าไม่ระบุกรุณาใส่ #</label>
            <input type="text" name="url_link" size="70" value="<?php if(empty($rows['url_link'])){ echo "#"; }else{ echo $rows['url_link']; } ?>" class="form-control"></td>
          </tr>
          <tr>
            <td colspan="2" align="right"><input type="submit" value="บันทึก" class="btn btn-default"></td>
          </tr>
        </tbody>
      </table>
      </form>
    </div>
  </div>
<?php
}

if($_GET['nu']=="externalurl_i_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_externalurl";
  $field = "code";
  $field2 = $_POST['code'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2'"));
  if($nr>0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=externalurl&action=externalurl_i_u&$field=$field2'>";
  }else{
    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    for($i=1; $i<$num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));

      $fields_1 .= "$fields, ";
      $fields_2 .= "'".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $field_1 = substr($fields_1,0,-2);
    $field_2 = substr($fields_2,0,-2);
    mysqli_query($conn,"insert into $table ($field_1) values ($field_2)");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=externalurl'>";
  }
  echo "</div>";
}

if($_GET['nu']=="externalurl_u_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_externalurl";
  $field = "id";
  $field2 = $_POST['id'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2'"));
  if($nr<0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=externalurl&action=externalurl_i_u&$field=$field2'>";
  }else{
    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    $fields_ = '';
    for($i=1; $i<$num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));

      $fields_ .= "$fields='".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $fields_set = substr($fields_,0,-2);
    mysqli_query($conn,"update $table set $fields_set where $field='$field2' ");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=externalurl'>";
  }
  echo "</div>";
}

if ($_GET['nu']=="externalurl_delete") {
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_externalurl";
  mysqli_query($conn,"delete from $table where id='$_GET[id]'");
  echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
  echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=externalurl'>";
  echo "</div>";
}

if($_GET['nu']=="externalbanner"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">แบนเนอร์ ภายนอก</h2>
    <div class="table-responsive">
      <table class="table table-striped">
        <thead>
          <tr>
            <th width="70">ลำดับ</th>
            <th>ภาษา</th>
            <th>แค็ตตาล็อก</th>
            <th>ลำดับ แบนเนอร์</th>
            <th>รหัส</th>
            <th>ชื่อ</th>
            <th width="100"><a href="?nu=externalbanner_i_u&action=externalbanner_i_s">เพิ่ม</a></th>
          </tr>
        </thead>
        <tbody>
          <?php
          $sql = "select * from ap_externalbanner order by code asc, level asc";
          $query = mysqli_query($conn,$sql);
          $i = 0;
          while($rows = mysqli_fetch_array($query)){
            $i++;
            $sql2 = "select * from ap_catalogs where code='$rows[catalogCode]'";
            $query2 = mysqli_query($conn,$sql2);
            $rows2 = mysqli_fetch_array($query2);
            ?>
            <tr>
              <td><?php echo $i;?></td>
              <td><?php echo $rows['lang'];?></td>
              <td><?php echo $rows2['name'];?></td>
              <td><?php echo $rows['level'];?></td>
              <td><?php echo $rows['code'];?></td>
              <td><a href="<?php echo $rows['images']?>" target="_blank"><?php echo $rows['name'];?></a></td>
              <td><a href="?nu=externalbanner_i_u&action=externalbanner_u_s&id=<?php echo $rows['id']?>">แก้ไข</a> :  <a href="#" onclick="if (confirm('ยืนยันการลบ?')) location.href='?nu=externalbanner_delete&id=<?php echo $rows['id']?>';">ลบ</a></td>
            </tr>
          <?php
          }
          ?>
        </tbody>
      </table>
    </div>
  </div>
<?php
}

if($_GET['nu']=="externalbanner_i_u"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  $sql = "select * from ap_externalbanner where id='$_GET[id]'";
  $query = mysqli_query($conn,$sql);
  $rows = mysqli_fetch_array($query);
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">แบนเนอร์ ภายนอก</h2>
    <div class="table-responsive">
      <form action="?nu=<?php echo $_GET['action']?>" method="post" enctype="multipart/form-data">
      <input type="hidden" name="id" value="<?php echo $rows['id']?>">
      <table class="table table-striped">
        <tbody>
      <tr>
          <td>สำหรับเว็บไซต์ภาษา</td>
          <td><select name="lang" class="form-control">
          <?php
          $sql2 = "select * from ap_langs order by code asc";
          $query2 = mysqli_query($conn,$sql2);
          while($rows2 = mysqli_fetch_array($query2)){
          ?>
            <option value="<?php echo $rows2['code']?>"
            <?php
            if($rows2['code']==$rows['lang']){
              echo "selected='true'";
            }
            ?>
            ><?php echo $rows2['name']?></option>
          <?php
          }
          ?>
          </select></td>
        </tr>
        <tr>
          <td>แค็ตตาล็อก</td>
          <td><select name="catalogCode" id="catalogCode" class="form-control">
          <?php
          $sql2 = "select * from ap_catalogs order by code asc";
          $query2 = mysqli_query($conn,$sql2);
          while($rows2 = mysqli_fetch_array($query2)){
          ?>
            <option value="<?php echo $rows2['code']?>"
            <?php
            if($rows2['code']==$rows['catalogCode']){
              echo "selected='true'";
            }
            ?>
            ><?php echo $rows2['name']?></option>
          <?php
          }
          ?>
          </select></td>
        </tr>
        <tr>
          <td>ลำดับ แบนเนอร์</td>
          <td>
          <label for="disabledTextInput">ตัวอย่าง 00</label>
          <input type="text" name="level" value="<?php if(empty($rows['level'])){ echo "00"; }else{ echo $rows['level']; } ?>" class="form-control"></td>
        </tr>
        <tr>
          <td>รหัส</td>
          <td><input type="text" name="code" value="<?php echo $rows['code']?>" class="form-control"></td>
        </tr>
        <tr>
          <td>ชื่อ</td>
          <td><input type="text" name="name" size="30" value="<?php echo $rows['name']?>" class="form-control"></td>
        </tr>
        <tr>
          <td>URL Link</td>
          <td>
          <label for="disabledTextInput">ถ้าไม่ระบุกรุณาใส่ #</label>
          <input type="text" name="url_link" size="70" value="<?php if(empty($rows['url_link'])){ echo "#"; }else{ echo $rows['url_link']; } ?>" class="form-control"></td>
        </tr>
        <tr>
          <td>รูปภาพ</td>
          <td><div class="input-group">
          <input type="text" id="images" name="images" value="<?php echo $rows['images']?>" class="form-control" placeholder="รูปภาพประกอบ">
          <span class="input-group-btn">
            <button href="tinymce/plugins/filemanager/dialog.php?type=1&editor=mce_0&lang=eng&fldr=&field_id=images&popup=1" class="btn btn-default iframe-btn" type="button">SELECT</button>
          </span>
          </div></td>
        </tr>
        <tr>
          <td>สถานะ</td>
          <td>
          <label for="disabledTextInput">0 = ไม่แสดง 1 = แสดง</label>
          <input type="text" name="status" size="5" value="<?php echo $rows['status']?>" class="form-control"></td>
        </tr>
        <tr>
          <td colspan="2" align="right"><input type="submit" value="บันทึก" class="btn btn-default"></td>
        </tr>
        </tbody>
      </table>
      </form>
    </div>
  </div>
<?php
}

if($_GET['nu']=="externalbanner_i_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_externalbanner";
  $field = "code";
  $field2 = $_POST['code'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2'"));
  if($nr>0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=externalbanner&action=externalbanner_i_u&$field=$field2'>";
  }else{
    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    for($i=1; $i<$num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));

      $fields_1 .= "$fields, ";
      $fields_2 .= "'".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $field_1 = substr($fields_1,0,-2);
    $field_2 = substr($fields_2,0,-2);
    mysqli_query($conn,"insert into $table ($field_1) values ($field_2)");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=externalbanner'>";
  }
  echo "</div>";
}

if($_GET['nu']=="externalbanner_u_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_externalbanner";
  $field = "id";
  $field2 = $_POST['id'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2'"));
  if($nr<0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=externalbanner&action=externalbanner_i_u&$field=$field2'>";
  }else{
    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    $fields_ = '';
    for($i=1; $i<$num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));

      $fields_ .= "$fields='".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $fields_set = substr($fields_,0,-2);
    mysqli_query($conn,"update $table set $fields_set where $field='$field2' ");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=externalbanner'>";
  }
  echo "</div>";
}

if ($_GET['nu']=="externalbanner_delete") {
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_externalbanner";
  //$images = str_replace("$ap_site$ap_folder", "", "../$_GET[images]");
  //unlink($images);
  mysqli_query($conn,"delete from $table where id='$_GET[id]'");
  echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
  echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=externalbanner'>";
  echo "</div>";
}

if($_GET['nu']=="themes"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">Themes</h2>
    <div class="table-responsive">
      <table class="table table-striped">
        <thead>
          <tr>
            <th width="70">ลำดับ</th>
            <th>รหัส</th>
            <th>ชื่อ Themes</th>
            <th width="100"><a href="?nu=themes_i_u&action=themes_i_s">เพิ่ม</a></th>
          </tr>
        </thead>
        <tbody>
          <?php
          $sql = "select * from ap_themes order by name asc";
          $query = mysqli_query($conn,$sql);
          $i = 0;
          while($rows = mysqli_fetch_array($query)){
          $i++;
            $sql2 = "select * from ap_catalogs where code='$rows[code]'";
            $query2 = mysqli_query($conn,$sql2);
            $rows2 = mysqli_fetch_array($query2);
            ?>
            <tr>
              <td><?php echo $i;?></td>
              <td><?php echo $rows['code'];?></td>
              <td><?php echo $rows['name'];?></td>
              <td><a href="?nu=themes_i_u&action=themes_u_s&id=<?php echo $rows['id']?>">แก้ไข</a> :  <a href="#" onclick="if (confirm('ยืนยันการลบ?')) location.href='?nu=themes_delete&id=<?php echo $rows['id']?>';">ลบ</a></td>
            </tr>
          <?php
          }
          ?>
        </tbody>
      </table>
    </div>
  </div>
<?php
}

if($_GET['nu']=="themes_i_u"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  $sql = "select * from ap_themes where id='$_GET[id]'";
  $query = mysqli_query($conn,$sql);
  $rows = mysqli_fetch_array($query);
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">Themes</h2>
    <div class="table-responsive">
      <form action="?nu=<?php echo $_GET['action']?>" method="post" enctype="multipart/form-data">
      <input type="hidden" name="id" value="<?php echo $rows['id']?>">
      <table class="table table-striped">
      <tbody>
        <tr>
          <td>รหัส</td>
          <td><input type="text" name="code" value="<?php echo $rows['code']?>" class="form-control"></td>
        </tr>
        <tr>
          <td>ชื่อ Themes</td>
          <td><input type="text" name="name" size="30" value="<?php echo $rows['name']?>" class="form-control"></td>
        </tr>
        <tr>
          <td colspan="2" align="right"><input type="submit" value="บันทึก" class="btn btn-default"></td>
        </tr>
      </tbody>
      </table>
      </form>
    </div>
  </div>
<?php
}

if($_GET['nu']=="themes_i_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_themes";
  $field = "code";
  $field2 = $_POST['code'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2'"));
  if($nr>0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=themes&action=themes_i_u&$field=$field2'>";
  }else{
    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    for($i=1; $i<$num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));

      $fields_1 .= "$fields, ";
      $fields_2 .= "'".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $field_1 = substr($fields_1,0,-2);
    $field_2 = substr($fields_2,0,-2);
    mysqli_query($conn,"insert into $table ($field_1) values ($field_2)");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=themes'>";
  }
  echo "</div>";
}

if($_GET['nu']=="themes_u_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_themes";
  $field = "id";
  $field2 = $_POST['id'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2'"));
  if($nr<0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=themes&action=themes_i_u&$field=$field2'>";
  }else{
    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    $fields_ = '';
    for($i=1; $i<$num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));

      $fields_ .= "$fields='".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $fields_set = substr($fields_,0,-2);
    mysqli_query($conn,"update $table set $fields_set where $field='$field2' ");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=themes'>";
  }
  echo "</div>";
}

if ($_GET['nu']=="themes_delete") {
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_themes";
  mysqli_query($conn,"delete from $table where id='$_GET[id]'");
  echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
  echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=themes'>";
  echo "</div>";
}

if($_GET['nu']=="systems"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">Systems</h2>
    <div class="table-responsive">
      <table class="table table-striped">
        <thead>
          <tr>
            <th width="70">ลำดับ</th>
            <th>รหัส</th>
            <th>Systems</th>
            <th>File Systems</th>
            <th>สถานะ</th>
            <th width="100"><a href="?nu=systems_i_u&action=systems_i_s">เพิ่ม</a></th>
          </tr>
        </thead>
        <tbody>
          <?php
          $sql = "select * from ap_systems order by code asc";
          $query = mysqli_query($conn,$sql);
          $i = 0;
          while($rows = mysqli_fetch_array($query)){
          $i++;
            if($rows['status']==1){
              $ck[$i] = "ทำงาน";
            }
            ?>
            <tr>
              <td><?php echo $i;?></td>
              <td><?php echo $rows['code'];?></td>
              <td><?php echo $rows['name'];?></td>
              <td><?php echo $rows['files'];?></td>
              <td><?php echo $ck[$i];?></td>
              <td><a href="?nu=systems_i_u&action=systems_u_s&id=<?php echo $rows['id']?>">แก้ไข</a> : <a href="#" onclick="if (confirm('ยืนยันการลบ?')) location.href='?nu=systems_delete&id=<?php echo $rows['id']?>';">ลบ</a></td>
            </tr>
          <?php
          }
          ?>
        </tbody>
      </table>
    </div>
  </div>
<?php
}

if($_GET['nu']=="systems_i_u"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  $sql = "select * from ap_systems where id='$_GET[id]'";
  $query = mysqli_query($conn,$sql);
  $rows = mysqli_fetch_array($query);
  if($rows['status']==1){
    $ck = " checked='true'";
  }else{ $ck = ""; }
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">Systems</h2>
    <div class="table-responsive">
      <form action="?nu=<?php echo $_GET['action']?>" method="post" enctype="multipart/form-data">
      <input type="hidden" name="id" value="<?php echo $rows['id']?>">
      <table class="table table-striped">
      <tbody>
        <tr>
          <td>รหัส</td>
          <td><input type="text" name="code" value="<?php echo $rows['code']?>" class="form-control"></td>
        </tr>
        <tr>
          <td>Systems</td>
          <td>
            <label>ต้องการแสดงทำเครื่องหมายถูก</label>
            <div class="input-group">
              <span class="input-group-addon">
                <input type="checkbox" name="status" value="1"<?php echo $ck;?> aria-label="...">
              </span>
              <input type="text" name="name" value="<?php echo $rows['name']?>" class="form-control">
            </div>
          </td>
        </tr>
        <tr>
          <td>File Systems</td>
          <td><input type="text" name="files" size="30" value="<?php echo $rows['files']?>" class="form-control"></td>
        </tr>
        <tr>
            <td>Target</td>
            <td>
            <label for="disabledTextInput">รูปแบบการ Link เช่น _blank</label>
            <input type="text" name="target_link" value="<?php echo $rows['target_link']?>" class="form-control"></td>
          </tr>
        <tr>
          <td colspan="2" align="right"><input type="submit" value="บันทึก" class="btn btn-default"></td>
        </tr>
      </tbody>
      </table>
      </form>
    </div>
  </div>
<?php
}

if($_GET['nu']=="systems_i_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_systems";
  $field = "code";
  $field2 = $_POST['code'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2'"));
  if($nr>0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=systems&action=systems_i_u&$field=$field2'>";
  }else{
    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    for($i=1; $i<$num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));

      $fields_1 .= "$fields, ";
      $fields_2 .= "'".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $field_1 = substr($fields_1,0,-2);
    $field_2 = substr($fields_2,0,-2);
    mysqli_query($conn,"insert into $table ($field_1) values ($field_2)");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=systems'>";
  }
  echo "</div>";
}

if($_GET['nu']=="systems_u_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_systems";
  $field = "id";
  $field2 = $_POST['id'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2'"));
  if($nr<0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=systems&action=systems_i_u&$field=$field2'>";
  }else{
    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    for($i=1; $i<$num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));

      $fields_ .= "$fields='".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $fields_set = substr($fields_,0,-2);
    mysqli_query($conn,"update $table set $fields_set where $field='$field2' ");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=systems'>";
  }
  echo "</div>";
}

if ($_GET['nu']=="systems_delete") {
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_systems";
  mysqli_query($conn,"delete from $table where id='$_GET[id]'");
  echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
  echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=systems'>";
  echo "</div>";
}

if($_GET['nu']=="status"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">สถานะ</h2>
    <div class="table-responsive">
      <table class="table table-striped">
        <thead>
          <tr>
            <th width="70">ลำดับ</th>
            <th>รหัส</th>
            <th>สถานะ</th>
            <th width="100"><a href="?nu=status_i_u&action=status_i_s">เพิ่ม</a></th>
          </tr>
        </thead>
        <tbody>
          <?php
          $sql = "select * from ap_status order by code asc";
          $query = mysqli_query($conn,$sql);
          $i = 0;
          while($rows = mysqli_fetch_array($query)){
          $i++;
            ?>
            <tr>
              <td><?php echo $i;?></td>
              <td><?php echo $rows['code'];?></td>
              <td><?php echo $rows['name'];?></td>
              <td><a href="?nu=status_i_u&action=status_u_s&id=<?php echo $rows['id']?>">แก้ไข</a> :  <a href="#" onclick="if (confirm('ยืนยันการลบ?')) location.href='?nu=status_delete&id=<?php echo $rows['id']?>';">ลบ</a></td>
            </tr>
          <?php
          }
          ?>
        </tbody>
      </table>
    </div>
  </div>
<?php
}

if($_GET['nu']=="status_i_u"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  $sql = "select * from ap_status where id='$_GET[id]'";
  $query = mysqli_query($conn,$sql);
  $rows = mysqli_fetch_array($query);
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">สถานะ</h2>
    <div class="table-responsive">
      <form action="?nu=<?php echo $_GET['action']?>" method="post" enctype="multipart/form-data">
      <input type="hidden" name="id" value="<?php echo $rows['id']?>">
      <table class="table table-striped">
      <tbody>
        <tr>
          <td>รหัส</td>
          <td><input type="text" name="code" value="<?php echo $rows['code']?>" class="form-control"></td>
        </tr>
        <tr>
          <td>สถานะ</td>
          <td><input type="text" name="name" size="30" value="<?php echo $rows['name']?>" class="form-control"></td>
        </tr>
        <tr>
          <td colspan="2" align="right"><input type="submit" value="บันทึก" class="btn btn-default"></td>
        </tr>
      </tbody>
      </table>
      </form>
    </div>
  </div>
<?php
}

if($_GET['nu']=="status_i_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_status";
  $field = "code";
  $field2 = $_POST['code'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2'"));
  if($nr>0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=status&action=status_i_u&$field=$field2'>";
  }else{
    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    for($i=1; $i<$num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));

      $fields_1 .= "$fields, ";
      $fields_2 .= "'".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $field_1 = substr($fields_1,0,-2);
    $field_2 = substr($fields_2,0,-2);
    mysqli_query($conn,"insert into $table ($field_1) values ($field_2)");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=status'>";
  }
  echo "</div>";
}

if($_GET['nu']=="status_u_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_status";
  $field = "id";
  $field2 = $_POST['id'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2'"));
  if($nr<0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=status&action=status_i_u&$field=$field2'>";
  }else{
    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    $fields_ = '';
    for($i=1; $i<$num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));

      $fields_ .= "$fields='".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $fields_set = substr($fields_,0,-2);
    mysqli_query($conn,"update $table set $fields_set where $field='$field2' ");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=status'>";
  }
  echo "</div>";
}

if ($_GET['nu']=="status_delete") {
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_status";
  mysqli_query($conn,"delete from $table where id='$_GET[id]'");
  echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
  echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=status'>";
  echo "</div>";
}

if($_GET['nu']=="edms"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <div class="row">
      <div class="col-lg-6 col-md-6 col-sm-12">
        <h2 style="margin: 0px">บริหารจัดการเอกสารอิเล็กทรอนิกส์</h2>
      </div>
      <div class="col-lg-6 col-md-6 col-sm-12 align-text-bottom">
        <form class="form-inline text-right" action="<?php echo $_SERVER[SCRIPT_NAME]; ?>" method="GET">
          <input type="hidden" name="nu" value="<?php echo $_GET['nu'] ?>">
          <div class="form-group">
            <input type="text" name="q" value="<?php echo $_GET['q']; ?>" placeholder="ระบุคำค้น" class="form-control" autofocus>
          </div>
          <button type="submit" class="btn btn-default">ค้นหา</button>
        </form>
      </div>
      <div class="col-lg-12 col-md-12 col-sm-12"><hr></div>
      <p></p>
    </div>
    <div class="table-responsive">
      <table class="table table-striped">
        <thead>
          <tr>
            <th width="70">ลำดับ</th>
            <th>ภาษา</th>
            <th>วันที่</th>
            <th>หมวด</th>
            <th>ปี</th>
            <th>ชื่อเอกสาร</th>
            <th>ผู้จัดทำ</th>
            <th width="100"><a href="?nu=edms_i_u&action=edms_i_s">เพิ่ม</a></th>
          </tr>
        </thead>
        <tbody>
          <?php
          if($_SESSION['apstatus']=="Admin"){
            $sql = "select * from ap_edms where name like '$_GET[q]%'";
          }else{
            $sql = "select * from ap_edms where createBy='$_SESSION[user]' and name like '$_GET[q]%'";
          }
          $query = mysqli_query($conn,$sql);
          $num_rows = mysqli_num_rows($query);

          $per_page = 50;
          $page  = 1;

          if(isset($_GET["Page"])){
            $page = $_GET["Page"];
          }

          $prev_page = $page-1;
          $next_page = $page+1;

          $row_start = (($per_page*$page)-$per_page);
          if($num_rows<=$per_page){
            $num_pages = 1;
          }elseif(($num_rows % $per_page)==0){
            $num_pages = ($num_rows/$per_page) ;
          }else{
            $num_pages =($num_rows/$per_page)+1;
            $num_pages = (int)$num_pages;
          }
          /*$row_end = $per_page * $page;
          if($row_end > $num_rows){
            $row_end = $num_rows;
          }*/
          $row_end = $per_page;

          $sql .= " order by id desc, date_time desc limit $row_start, $row_end";
          $query = mysqli_query($conn,$sql);

          $i = (($row_start/$per_page)*$per_page);
          while($rows = mysqli_fetch_array($query)){
          $i++;
            $sql2 = "select * from ap_catalogs where code='$rows[catalogCode]'";
            $query2 = mysqli_query($conn,$sql2);
            $rows2 = mysqli_fetch_array($query2);
            
            if($rows['notify']=="1"){
            //if($rows['id']=="134"){
              if(empty($ap_ip)){
                $apdomain = "$ap_site$ap_folder";
              }else{
                $apdomain = str_replace("$ap_ip","$ap_domain","$ap_site$ap_folder");
              }
              if($last!=$apdomain){
                $message = array(
                'message' => "$rows[name] "." --> ".($apdomain)."ap/edms/?nu=&catalogCode=$rows[catalogCode]&lang=$rows[lang]",
                'imageThumbnail' => '',
                'imageFullsize' => '',
                'stickerPackageId' => '',
                'stickerId' => ''
                );

                if($_SESSION['apstatus']=="ClericalOfficer"){
                  $line_notify = $line_notify3;
                  $token = "tn4tthSdCi521nNZg0ahWFZZtEHUJW7UoD2Qa7X6K2Q"; // Anucha Puangpaka
                  //line_notify($message, $token, $rows_line['id']);
                  //$line_notify = $token;
                  if(!empty($line_notify)){
                    if($last1!=$line_notify){
                      line_notify_messages($message, $line_notify);
                    }
                    $last1 = $line_notify;
                  }
                }
                if($_SESSION['apstatus']=="PublicRelations"){
                  if(!empty($line_notify)){
                    if($last1!=$line_notify){
                      line_notify_messages($message, $line_notify);
                    }
                    $last1 = $line_notify;
                  }
                  if(!empty($line_notify2)){
                    if($last2!=$line_notify2){
                      line_notify_messages($message, $line_notify2);
                    }
                    $last2 = $line_notify2;
                  }
                  if(!empty($line_notify3)){
                    if($last3!=$line_notify3){
                      line_notify_messages($message, $line_notify3);
                    }
                    $last3 = $line_notify3;
                  }
                  if(!empty($line_notify4)){
                    if($last4!=$line_notify4){
                      line_notify_messages($message, $line_notify4);
                    }
                    $last4 = $line_notify4;
                  }
                  if(!empty($line_notify5)){
                    if($last5!=$line_notify5){
                      line_notify_messages($message, $line_notify5);
                    }
                    $last5 = $line_notify5;
                  }
                  if(!empty($line_notify6)){
                    if($last6!=$line_notify6){
                      line_notify_messages($message, $line_notify6);
                    }
                    $last6 = $line_notify6;
                  }
                  if(!empty($line_notify7)){
                    if($last7!=$line_notify7){
                      line_notify_messages($message, $line_notify7);
                    }
                    $last7 = $line_notify7;
                  }
                  if(!empty($line_notify8)){
                    if($last8!=$line_notify8){
                      line_notify_messages($message, $line_notify8);
                    }
                    $last8 = $line_notify8;
                  }
                  if(!empty($line_notify9)){
                    if($last9!=$line_notify9){
                      line_notify_messages($message, $line_notify9);
                    }
                    $last9 = $line_notify9;
                  }
                }
                
                mysqli_query($conn,"update ap_edms set notify='2' where id='$rows[id]' ");
              }
              $last = $apdomain;
            }
            ?>
            <tr>
              <td><?php echo $i;?></td>
              <td><?php echo $rows['lang'];?></td>
              <td><?php echo $rows['date_time'];?></td>
              <td><a href="<?php echo $ap_site.$ap_folder ?>ap/edms/?nu=&catalogCode=<?php echo $rows['catalogCode'] ?>&lang=<?php echo $rows['lang'] ?>" target="_blank"><?php echo $rows2['name'];?></a></td>
              <td><?php echo $rows['years'];?></td>
              <td><?php if(!empty($rows['num'])){ echo $rows['num'].". "; }?><?php echo $rows['name'];?></td>
              <td><?php echo $rows['author'];?></td>
              <td><?php if($_SESSION['apstatus']=="Admin" || $rows['createBy']==$_SESSION['user']){?><a href="?nu=edms_i_u&action=edms_u_s&id=<?php echo $rows['id']?>">แก้ไข</a> :  <a href="#" onclick="if (confirm('ยืนยันการลบ?')) location.href='?nu=edms_delete&id=<?php echo $rows['id']?>&file=<?php echo $rows['files']?>';">ลบ</a><?php } ?></td>
            </tr>
          <?php
          }
          ?>
        </tbody>
      </table>
    </div>
    <div class="text-right">
      <p><strong><?php echo number_format($num_rows);?> รายการ : <?php echo $num_pages;?> หน้า</strong></p>
      <ul class="pagination justify-content-end">
      <?php
      if($prev_page){
        echo "<li class=\"page-item\"><a class=\"page-link\" href='$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=1&q=$_GET[q]'>หน้าแรก</a></li>";
        echo "<li class=\"page-item\"><a class=\"page-link\" href='$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=$prev_page&q=$_GET[q]'><< ย้อนกลับ</a></li>";
      }

      for($i=1; $i<=$num_pages; $i++){
        if ($i > ($_GET['Page']-5) && $i < ($_GET['Page']+5)) {
          if($i != $page){
            echo "<li class=\"page-item\"><a class=\"page-link\" href='$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=$i&q=$_GET[q]'>$i</a></li>";
          }else{
            echo "<li class=\"page-item active\"><a class=\"page-link\" href=\"$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=$_GET[Page]&q=$_GET[q]#\">$i</a></li>";
          }
        }
      }
      if($page!=$num_pages){
        echo "<li class=\"page-item\"><a class=\"page-link\" href='$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=$next_page&q=$_GET[q]'>ถัดไป>></a></li>";
        echo "<li class=\"page-item\"><a class=\"page-link\" href='$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=$num_pages&q=$_GET[q]'>หน้าสุดท้าย</a></li>";
      }
      ?>
      </ul>
    </div>
  </div>
<?php
}

if($_GET['nu']=="edms_i_u"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  $sql = "select * from ap_edms where id='$_GET[id]'";
  $query = mysqli_query($conn,$sql);
  $rows = mysqli_fetch_array($query);
  if($rows['watermark']==1){
    $ck = " checked='true'";
  }
  $sql_2 = "select * from ap_users where user='$_SESSION[user]'";
  $query_2 = mysqli_query($conn,$sql_2);
  $rows_2 = mysqli_fetch_array($query_2);
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">บริหารจัดการเอกสารอิเล็กทรอนิกส์</h2>
    <div class="table-responsive">
      <form action="?nu=<?php echo $_GET['action']?>" method="post" enctype="multipart/form-data">
      <input type="hidden" name="id" value="<?php echo $rows['id']?>">
      <input type="hidden" name="createBy" value="<?php echo $rows['createBy']?>">
      <input type="hidden" name="createByStatus" value="<?php echo $rows['createByStatus']?>">
      <input type="hidden" name="counters" value="<?php echo $rows['counters']?>">
      <input type="hidden" name="files_" value="<?php echo $rows['files']?>">
      <table class="table table-striped">
        <tbody>
          <tr>
            <td>ภาษา</td>
            <td>
              <select name="lang" class="form-control">
              <?php
              $sql2 = "select * from ap_langs order by code asc";
              $query2 = mysqli_query($conn,$sql2);
              while($rows2 = mysqli_fetch_array($query2)){
              ?>
                <option value="<?php echo $rows2['code']?>" 
                <?php
                if($rows2['code']==$rows['lang']){
                  echo "selected='true'";
                }
                ?>
                ><?php echo $rows2['name']?></option>
              <?php
              }
              ?>
              </select>
            </td>
          </tr>
          <tr>
            <td>หมวด</td>
            <td><select name="catalogCode" class="form-control">
            <?php
            $sql2 = "select * from ap_catalogs where code like 'EDMS%' order by name asc";
            $query2 = mysqli_query($conn,$sql2);
            while($rows2 = mysqli_fetch_array($query2)){
            ?>
              <option value="<?php echo $rows2['code']?>"
              <?php
              if($rows2['code']==$rows['catalogCode']){
                echo "selected='true'";
              }
              ?>
              ><?php echo $rows2['name']?></option>
            <?php
            }
            ?>
            </select></td>
          </tr>
          <tr>
            <td>วันที่</td>
            <td><input placeholder="" type="text" name="date_time" value="<?php if(empty($rows['date_time'])){ echo date('Y-m-d H:i:s'); }else{ echo $rows['date_time']; } ?>" class="form-control">
            </td>
          </tr>
          <tr>
            <td>ลำดับเอกสาร</td>
            <td><input placeholder="ลำดับเอกสาร" type="text" name="num" value="<?php echo $rows['num']?>" class="form-control">
            </td>
          </tr>
          <tr>
            <td>ชื่อเอกสาร</td>
            <td><input placeholder="ชื่อเอกสาร" type="text" name="name" value="<?php echo $rows['name']?>" class="form-control">
            </td>
          </tr>
          <tr>
            <td>ผู้จัดทำ</td>
            <td><input type="text" name="author" value="<?php if($rows['author']==""){ echo "$rows_2[fullname]"; }else{ echo $rows['author']; } ?>" class="form-control"></td>
          </tr>
          <tr>
            <td>ปีที่จัดทำ</td>
            <td><input type="text" name="years" value="<?php if($rows['years']==""){ echo date('Y'); }else{ echo $rows['years']; } ?>" class="form-control"></td>
          </tr>
          <tr>
            <td>ไฟล์เอกสาร</td>
            <td>
              <label>ทำเครื่องถูก กรณีเป็นไฟล์ PDF ที่ต้องการทำลายน้ำเอกสาร</label>
              <div class="input-group">
                <span class="input-group-addon">
                  <input type="checkbox" name="watermark" value="1"<?php echo $ck;?> aria-label="...">
                </span>
                <input type="file" name="files" accept="application/pdf,application/vnd.ms-word,application/vnd.ms-excel,application/vnd.ms-powerpoint,image/gif,image/jpeg,image/png,application/zip" class="form-control" placeholder="ไฟล์เอกสาร">
              </div>
              <small>*หมายเหตุ เลือกไฟล์เอกสร หรือ URL Link</small>
            </td>
          </tr>
          <tr>
            <td>URL Link</td>
            <td>
              <input placeholder="URL" type="text" name="url" value="<?php echo $rows['url']?>" class="form-control">
              <small>*หมายเหตุ เลือกไฟล์เอกสร หรือ URL Link</small>
            </td>
          </tr>
          <?php 
          if($_SESSION['apstatus']=="Admin" || $_SESSION['apstatus']=="ClericalOfficer" || $_SESSION['apstatus']=="PublicRelations"){ 
          ?>
          <tr>
            <td>ประชาสัมพันธ์ผ่าน Line?</td>
            <td>
              <label for="">ต้องการประชาสัมพันธ์ผ่าน Line กรอกเลข 1</label>
              <input placeholder="" type="text" name="notify" value="<?php echo $rows['notify']?>" class="form-control">
            </td>
          </tr>
          <?php } ?>
          <tr>
            <td colspan="2" align="right"><input type="submit" value="บันทึก" class="btn btn-default"></td>
          </tr>
        </tbody>
      </table>
      </form>
    </div>
  </div>
<?php
}

if($_GET['nu']=="edms_i_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_edms";

  if($_POST['createBy']==""){
    $_POST['createBy'] = $_SESSION['user'];
  }
  if($_POST['createByStatus']==""){
    $_POST['createByStatus'] = $_SESSION['apstatus'];
  }

  $_POST['date_time'] = date('Y-m-d H:i:s');

  if(empty($_FILES['files']['name'])){
    $_POST['files'] = "";
  }else{
    if(in_array(substr(strrchr($_FILES['files']['name'],'.'),1),$filesAllowed)){
      $in = "../contents/edms/";
      $namefile = $_POST['catalogCode']."-".date('Ymd').time();
      if(strlen($_FILES['files']['tmp_name'])!=0){
        move_uploaded_file($_FILES['files']['tmp_name'], "$in$namefile" . strrchr($_FILES['files']['name'], "."));
      }
      $_POST['files'] = $namefile.strrchr($_FILES['files']['name'], ".");
    }
  }

  $query = mysqli_query($conn,"select * from $table");
  $num_fields = mysqli_num_fields($query);
  for($i=1; $i<$num_fields; $i++) {
    $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
    $fields = $field_name[$i];

    $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));

    $fields_1 .= "$fields, ";
    $fields_2 .= "'".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
  }
  $field_1 = substr($fields_1,0,-2);
  $field_2 = substr($fields_2,0,-2);
  mysqli_query($conn,"insert into $table ($field_1) values ($field_2)");
  echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
  echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=edms'>";
  echo "</div>";
}

if($_GET['nu']=="edms_u_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_edms";
  $field = "id";
  $field2 = $_POST['id'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2'"));
  if($nr<0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=edms&action=edms_i_u&$field=$field2'>";
  }else{
    if($_POST['createBy']==""){
      $_POST['createBy'] = $_SESSION['user'];
    }
    if($_POST['createByStatus']==""){
      $_POST['createByStatus'] = $_SESSION['apstatus'];
    }

    //$_POST['date_time'] = date('Y-m-d H:i:s');
  
    if($_FILES['files']['name']==''){
      $_POST['files'] = $_POST['files_'];
    }else{
      if(in_array(substr(strrchr($_FILES['files']['name'],'.'),1),$filesAllowed)){
        $in = "../contents/edms/";
        unlink("$in$_POST[files_]");
        $namefile = $_POST['catalogCode']."-".date('Ymd').time();
        if(strlen($_FILES['files']['tmp_name'])!=0){
          move_uploaded_file($_FILES['files']['tmp_name'], "$in$namefile" . strrchr($_FILES['files']['name'], "."));
        }
        $_POST['files'] = $namefile.strrchr($_FILES['files']['name'], ".");
      }
    }

    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    $fields_ = '';
    for($i=1; $i<$num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));

      $fields_ .= "$fields='".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $fields_set = substr($fields_,0,-2);
    mysqli_query($conn,"update $table set $fields_set where $field='$field2' ");
    
    //print_r('$_post);
    //echo "update $table set $fields_set where $field='$field2' ";
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=edms'>";
  }
  echo "</div>";
}

if ($_GET['nu']=="edms_delete") {
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_edms";
  mysqli_query($conn,"delete from $table where id='$_GET[id]'");
  unlink("../contents/edms/$_GET[file]");
  echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
  echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=edms'>";
  echo "</div>";
}

if($_GET['nu']=="messages"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <div class="row">
      <div class="col-lg-6 col-md-6 col-sm-12">
        <h2 style="margin: 0px">ระบบ ส่งข้อความ</h2>
      </div>
      <div class="col-lg-6 col-md-6 col-sm-12 align-text-bottom">
        <form class="form-inline text-right" action="<?php echo $_SERVER[SCRIPT_NAME]; ?>" method="GET">
          <input type="hidden" name="nu" value="<?php echo $_GET['nu'] ?>">
          <div class="form-group">
            <input type="text" name="q" value="<?php echo $_GET['q']; ?>" placeholder="ระบุคำค้น" class="form-control" autofocus>
          </div>
          <button type="submit" class="btn btn-default">ค้นหา</button>
        </form>
      </div>
      <div class="col-lg-12 col-md-12 col-sm-12"><hr></div>
      <p></p>
    </div>
    <div class="table-responsive">
      <table class="table table-striped">
      <thead>
        <tr>
          <th width="80">ลำดับ</th>
          <th width="100">สถานะ</th>
          <th>ข้อความ</th>
          <th width="100"><a href="?nu=messages-i-u&action=messages-i-s">เพิ่ม</a></th>
        </tr>
      </thead>
      <tbody>
      <?php
      if($_SESSION['apstatus']=="Admin"){
        $sql = "select * from ap_messages where title like '$_GET[q]%'";
      }else{
        $sql = "select * from ap_messages where title like '$_GET[q]%' and staff='$_SESSION[user]'";
      }
      
      $query = mysqli_query($conn,$sql);
      $num_rows = mysqli_num_rows($query);

      $per_page = 100;
      $page  = 1;
      
      if(isset($_GET["Page"])){
        $page = $_GET["Page"];
      }

      $prev_page = $page-1;
      $next_page = $page+1;

      $row_start = (($per_page*$page)-$per_page);
      if($num_rows<=$per_page){
        $num_pages =1;
      }elseif(($num_rows % $per_page)==0){
        $num_pages =($num_rows/$per_page) ;
      }else{
        $num_pages =($num_rows/$per_page)+1;
        $num_pages = (int)$num_pages;
      }
      /*$row_end = $per_page * $page;
      if($row_end > $num_rows){
        $row_end = $num_rows;
      }*/
      $row_end = $per_page;

      $sql .= " order by id desc limit $row_start, $row_end";
      $query = mysqli_query($conn,$sql);

      $i = (($row_start/$per_page)*$per_page);
      while($rows = mysqli_fetch_array($query)){
      $i++;
        if($rows['notify']=="1"){
          /*$message = array(
          'message' => "$rows[title]",
          'imageThumbnail' => '',
          'imageFullsize' => '',
          'stickerPackageId' => '',
          'stickerId' => ''
          );*/

          if(empty($rows['files'])){
            $message = array(
              'message' => "$rows[title]",
              'stickerPackageId' => '',
              'stickerId' => ''
            );
          }else{
            $message = array(
              'message' => "$rows[title]",
              'imageThumbnail' => "$ap_site$ap_folder"."contents/messages/$rows[files]",
              'imageFullsize' => "$ap_site$ap_folder"."contents/messages/$rows[files]",
              'imageFile' => 'copy/240.jpg',
              'stickerPackageId' => '',
              'stickerId' => ''
            );
          }
          
          if(!empty($line_notify)){
            if($last1!=$line_notify){
              line_notify_messages($message, $line_notify);
            }
            $last1 = $line_notify;
          }
          if(!empty($line_notify2)){
            if($last2!=$line_notify2){
              line_notify_messages($message, $line_notify2);
            }
            $last2 = $line_notify2;
          }
          if(!empty($line_notify3)){
            if($last3!=$line_notify3){
              line_notify_messages($message, $line_notify3);
            }
            $last3 = $line_notify3;
          }
          if(!empty($line_notify4)){
            if($last4!=$line_notify4){
              line_notify_messages($message, $line_notify4);
            }
            $last4 = $line_notify4;
          }
          if(!empty($line_notify5)){
            if($last5!=$line_notify5){
              line_notify_messages($message, $line_notify5);
            }
            $last5 = $line_notify5;
          }
          if(!empty($line_notify6)){
            if($last6!=$line_notify6){
              line_notify_messages($message, $line_notify6);
            }
            $last6 = $line_notify6;
          }
          if(!empty($line_notify7)){
            if($last7!=$line_notify7){
              line_notify_messages($message, $line_notify7);
            }
            $last7 = $line_notify7;
          }
          if(!empty($line_notify8)){
            if($last8!=$line_notify8){
              line_notify_messages($message, $line_notify8);
            }
            $last8 = $line_notify8;
          }
          if(!empty($line_notify9)){
            if($last9!=$line_notify9){
              line_notify_messages($message, $line_notify9);
            }
            $last9 = $line_notify9;
          }

          /* ---- */

          $accessToken = LINE_MESSAGE_ACCESS_TOKEN;

          $content = file_get_contents('php://input');
          $arrayJson = json_decode($content, true);

          $arrayHeader = array();
          $arrayHeader[] = "Content-Type: application/json";
          $arrayHeader[] = "Authorization: Bearer {$accessToken}";
          
          $sql_linex = "select * from ap_line_groups where id<2";
          $sql_linex = "select * from ap_line_groups where id>2";
          $sql_linex = "select * from ap_line_groups";
          $query_linex = mysqli_query($conn,$sql_linex);
          while($rows_linex = mysqli_fetch_array($query_linex)){
            $arrayPostData['to'] = "$rows_linex[code]";

            /*$arrayPostData['messages'][0]['type'] = "flex";
            $arrayPostData['messages'][0]['altText'] = "$title";

            $arrayPostData['messages'][0]['contents']['type'] = "bubble"; 

            $arrayPostData['messages'][0]['contents']['styles']['header']['backgroundColor'] = "#F8F9F9";
            $arrayPostData['messages'][0]['contents']['styles']['body']['backgroundColor'] = "#F8F9F9";
            $arrayPostData['messages'][0]['contents']['styles']['body']['separator'] = true;
            $arrayPostData['messages'][0]['contents']['styles']['body']['separatorColor'] = "#F8F9F9";
            $arrayPostData['messages'][0]['contents']['styles']['footer']['backgroundColor'] = "#F8F9F9";*/
            
            /* old */
            /*$arrayPostData['messages'][0]['contents']['header']['type'] = "box";
            $arrayPostData['messages'][0]['contents']['header']['layout'] = "vertical";
            $arrayPostData['messages'][0]['contents']['header']['contents'][0]['type'] = "text";
            $arrayPostData['messages'][0]['contents']['header']['contents'][0]['align'] = "center";
            $arrayPostData['messages'][0]['contents']['header']['contents'][0]['text'] = "$ap_owner_short";*/

            /*$arrayPostData['messages'][0]['contents']['hero']['type'] = "image";
            $arrayPostData['messages'][0]['contents']['hero']['url'] = "$ap_site$ap_folder"."contents/KPRU-ARIT_512.jpg";
            $arrayPostData['messages'][0]['contents']['hero']['size'] = "full";
            $arrayPostData['messages'][0]['contents']['hero']['aspectRatio'] = "2:1";
            $arrayPostData['messages'][0]['contents']['hero']['aspectMode'] = "cover";

            $arrayPostData['messages'][0]['contents']['body']['type'] = "box";
            $arrayPostData['messages'][0]['contents']['body']['layout'] = "vertical";
            $arrayPostData['messages'][0]['contents']['body']['spacing'] = "md";*/
            
            if(!empty($rows['title'])){
              /*$arrayPostData['messages'][0]['contents']['body']['contents'][0]['type'] = "text";
              $arrayPostData['messages'][0]['contents']['body']['contents'][0]['size'] = "sm";
              $arrayPostData['messages'][0]['contents']['body']['contents'][0]['wrap'] = true;
              $arrayPostData['messages'][0]['contents']['body']['contents'][0]['text'] = "$rows[title]";*/

              $arrayPostData['messages'][0]['type'] = "text";
              $arrayPostData['messages'][0]['text'] = "$rows[title]";
            }

            /*$arrayPostData['messages'][0]['contents']['body']['contents'][1]['type'] = "button";
            $arrayPostData['messages'][0]['contents']['body']['contents'][1]['height'] = "sm";
            $arrayPostData['messages'][0]['contents']['body']['contents'][1]['style'] = "primary";
            $arrayPostData['messages'][0]['contents']['body']['contents'][1]['action']['type'] = "uri";
            $arrayPostData['messages'][0]['contents']['body']['contents'][1]['action']['label'] = "อ่านข้อมูลเพิ่มเติม";
            $arrayPostData['messages'][0]['contents']['body']['contents'][1]['action']['uri'] = "$apdomain"."ap/edms/?nu=&catalogCode=$rows[catalogCode]&lang=$rows[lang]";*/
            
            if(!empty($rows['files'])){
              if(empty($rows['title'])){
                $x = 0;
              }else{
                $x = 1;
              }
              $arrayPostData['messages'][$x]['type'] = "image";
              $arrayPostData['messages'][$x]['originalContentUrl'] = "$ap_site$ap_folder"."contents/messages/$rows[files]";
              $arrayPostData['messages'][$x]['previewImageUrl'] = "$ap_site$ap_folder"."contents/messages/$rows[files]";
            }
                
            /*$arrayPostData['messages'][0]['contents']['footer']['type'] = "box";
            $arrayPostData['messages'][0]['contents']['footer']['layout'] = "vertical";
            $arrayPostData['messages'][0]['contents']['footer']['contents'][0]['type'] = "text";
            $arrayPostData['messages'][0]['contents']['footer']['contents'][0]['size'] = "sm";
            $arrayPostData['messages'][0]['contents']['footer']['contents'][0]['align'] = "center";
            $arrayPostData['messages'][0]['contents']['footer']['contents'][0]['text'] = "©KPRU ARIT";*/

            line_push($arrayHeader, $arrayPostData);
          }

          mysqli_query($conn,"update ap_messages set notify='2' where id='$rows[id]' ");
        }
        ?>
        <tr>
          <td><?php echo $i;?>.</td>
          <td><?php if($rows['notify']=="2"){ echo "ส่งแล้ว"; }else{ echo "-"; } ?></td>
          <td><?php echo nl2br($rows['title']); ?></td>
          <td align="center"><a href="?nu=messages-i-u&action=messages-u-s&id=<?php echo $rows['id']?>">แก้ไข</a> : <a href="#" onclick="if (confirm('ยืนยันการลบ?')) location.href='?nu=messages-delete&id=<?php echo $rows['id']?>';">ลบ</a></td>
        </tr>
      <?php 
      } 
      ?>
      </tbody>
      </table>
    </div>
    <div class="text-right">
      <?php if($_GET['Page']==""){ $_GET['Page'] = $page; } ?>
      <p class="text-right"><strong><?php echo number_format($num_rows);?> รายการ : <?php echo $num_pages;?> หน้า</strong></p>
      <ul class="pagination justify-content-end">
      <?php
      if($prev_page){
        echo "<li class=\"page-item\"><a class=\"page-link\" href='$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=1&q=$_GET[q]'>หน้าแรก</a></li>";
        echo "<li class=\"page-item\"><a class=\"page-link\" href='$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=$prev_page&q=$_GET[q]'><< ย้อนกลับ</a></li>";
      }

      for($i=1; $i<=$num_pages; $i++){
        if ($i > ($_GET['Page']-10) && $i < ($_GET['Page']+10)) {
          if($i != $page){
            echo "<li class=\"page-item\"><a class=\"page-link\" href='$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=$i&q=$_GET[q]'>$i</a></li>";
          }else{
            echo "<li class=\"page-item active\"><a class=\"page-link\" href=\"$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=$_GET[Page]&q=$_GET[q]#\">$i</a></li>";
          }
        }
      }
      if($page!=$num_pages){
        echo "<li class=\"page-item\"><a class=\"page-link\" href='$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=$next_page&q=$_GET[q]'>ถัดไป>></a></li>";
        echo "<li class=\"page-item\"><a class=\"page-link\" href='$_SERVER[SCRIPT_NAME]?nu=$_GET[nu]&Page=$num_pages&q=$_GET[q]'>หน้าสุดท้าย</a></li>";
      }
      ?>
      </ul>
    </div>
  </div>
<?php
}

if($_GET['nu']=="messages-i-u"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  $sql = "select * from ap_messages where id='$_GET[id]'";
  $query = mysqli_query($conn,$sql);
  $rows = mysqli_fetch_array($query);
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <div class="row">
      <div class="col-lg-12 col-md-12 col-sm-12">
        <h2 style="margin: 0px">ระบบ ส่งข้อความ</h2>
      </div>
      <form action="?nu=<?php echo $_GET['action']?>" method="post" enctype="multipart/form-data">
      <input type="hidden" name="id" value="<?php echo $rows['id']?>">
      <input type="hidden" name="files_" value="<?php echo $rows['files']?>">
      <table class="table table-striped">
      <tbody>
        <tr>
          <td width="250">ข้อความ</td>
          <td><textarea name="title" id="title" cols="30" rows="10" class="form-control"><?php echo htmlspecialchars($rows['title']); ?></textarea></td>
        </tr>
        <tr>
          <td>รูปภาพ</td>
          <td><?php if(!empty($rows['files'])){ ?><p><a href="?nu=messages-delete2&id=<?php echo $rows['id']; ?>&files=<?php echo $rows['files']; ?>">ลบ</a></p><?php } ?><input type="file" name="files" class="form-control"></td>
        </tr>
        <tr>
          <td><p>สถานะ</p>0 = ไม่ส่งข้อความ<br>1 = ส่งข้อความ<br>2 = ส่งข้อความแล้ว</td>
          <td><input type="text" name="notify" value="<?php if($rows['notify']==""){ echo "0"; }else{ echo $rows['notify']; } ?>" class="form-control"></td>
        </tr>
        <tr>
          <td colspan="2" align="right"><input type="submit" value="บันทึก" class="btn btn-primary"></td>
        </tr>
      </tbody>
      </table>
      </form>
    </div>
  </div>
<?php
}

if($_GET['nu']=="messages-i-s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  $table = "ap_messages";

  $_POST['dateTime'] = date('Y-m-d H:i:s');
  $_POST['staff'] = $_SESSION['user'];

  if($_FILES['files']['name']==''){
    $_POST['files'] = "";
  }else{
    if(in_array(substr(strrchr($_FILES['files']['name'],'.'),1),$filesAllowed)){
      $in = "../contents/messages/";
      $namefile = $_SESSION['user']."-".date('Ymd').time();
      if(strlen($_FILES['files']['tmp_name'])!=0){
        move_uploaded_file($_FILES['files']['tmp_name'], "$in$namefile" . strrchr($_FILES['files']['name'], "."));
      }
      $_POST['files'] = $namefile.strrchr($_FILES['files']['name'], ".");
    }
  }

  $query = mysqli_query($conn,"select * from $table");
  $num_fields = mysqli_num_fields($query);
  for($i=1; $i<$num_fields; $i++) {
    $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
    $fields = $field_name[$i];

    // $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));
    $_POST[$fields] = strip_tags(trim($_POST[$fields]));

    $fields_1 .= "$fields, ";
    $fields_2 .= "'".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
  }
  $field_1 = substr($fields_1,0,-2);
  $field_2 = substr($fields_2,0,-2);
  mysqli_query($conn,"insert into $table ($field_1) values ($field_2)");
  echo "<div class=\"content\">";
  echo "<br><p class='text-center'><B>กรุณารอสักครู่</B></p>";
  echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=messages'>";
  echo "</div>";
}

if($_GET['nu']=="messages-u-s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_messages";
  $field = "id";
  $field2 = $_POST['id'];

  $_POST['dateTime'] = date('Y-m-d H:i:s');
  $_POST['staff'] = $_SESSION['user'];

  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2' "));
  if($nr<0){
    echo "<p class='mt-5 text-center'><B>ไม่สามารถบันทึกข้อมูลได้</B></p>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=messages&action=messages-i-u&$field=$field2'>";
  }else{
    if($_FILES['files']['name']==''){
      $_POST['files'] = $_POST['files_'];
    }else{
      if(in_array(substr(strrchr($_FILES['files']['name'],'.'),1),$filesAllowed)){
        $in = "../contents/messages/";
        unlink("$in/$_POST[files_]");
        $namefile = $_SESSION['user']."-".date('Ymd').time();
        if(strlen($_FILES['files']['tmp_name'])!=0){
          move_uploaded_file($_FILES['files']['tmp_name'], "$in$namefile" . strrchr($_FILES['files']['name'], "."));
        }
        $_POST['files'] = $namefile.strrchr($_FILES['files']['name'], ".");
      }
    }

    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    for($i=1; $i<$num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      // $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));
      $_POST[$fields] = strip_tags(trim($_POST[$fields]));

      $fields_ .= "$fields='".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $fields_set = substr($fields_,0,-2);
    mysqli_query($conn,"update $table set $fields_set where $field='$field2' ");
    echo "<br><p class='text-center'><B>กรุณารอสักครู่</B></p>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=messages'>";
  }
  echo "</div>";
}

if ($_GET['nu']=="messages-delete") {
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }

  $table = "ap_messages";
  mysqli_query($conn,"delete from $table where id='$_GET[id]'");
  $in = "../contents/messages/";
  if(!empty($_GET['files'])){
    unlink("$in/$_GET[files]");
  }
  echo "<div class=\"content\">";
  echo "<br><p class='text-center'><B>กรุณารอสักครู่</B></p>";
  echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=messages'>";
  echo "</div>";
}

if ($_GET['nu']=="messages-delete2") {
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  $table = "ap_messages";
  $in = "../contents/messages/";
  if(!empty($_GET['files'])){
    unlink("$in/$_GET[files]");
    mysqli_query($conn,"update $table set files='' where id='$_GET[id]'");
  }
  echo "<p class='mt-5 text-center'><B>กรุณารอสักครู่</B></p>";
  echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=messages-i-u&action=messages-u-s&id=$_GET[id]'>";
}

if($_GET['nu']=="products"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  if($_GET['type']=="Questionnaire"){
    $ptitle = "แบบสอบถาม";
  }
  if($_GET['type']=="Quiz"){
    $ptitle = "ข้อสอบ";
  }
  if($_GET['type']=="Register"){
    $ptitle = "ลงทะเบียน";
  }
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header"><?php echo $ptitle; ?>ออนไลน์</h2>
    <div class="table-responsive">
      <table class="table table-striped">
        <thead>
          <tr>
            <th width="70">ลำดับ</th>
            <th width="100">วันที่</th>
            <th width="130">รหัส</th>
            <th><?php echo $ptitle;?></th>
            <?php
            if($_GET['type']=="Quiz"){ 
            ?>
            <th width="180">จำนวนข้อสอบที่ผ่านเกณฑ์</th>
            <th width="100">จำนวนข้อสอบ</th>
            <?php 
            }
            ?>
            <?php
            if($_GET['type']=="Register"){ 
            ?>
            <th width="130">จำนวนผู้เข้าร่วม</th>
            <?php 
            }
            ?>
            <th width="150">ผู้ดูแล</th>
            <th width="100"><?php /*if($_SESSION['apstatus']=="Admin1"){*/ ?><a href="?nu=products_i_u&action=products_i_s&type=<?php echo $_GET['type']?>">เพิ่ม</a><?php /*}*/ ?></th>
          </tr>
        </thead>
        <tbody>
          <?php
          $days = date(Ymd); /*date(Ymd,strtotime("+30 day"));*/
          if($_SESSION['apstatus']=="Admin"){
            $sql = "select * from ap_products where type='$_GET[type]' order by users asc, code desc";
            $sql = "select * from ap_products where type='$_GET[type]' order by date_expire desc, code desc";
          }else{
            $sql = "select * from ap_products where type='$_GET[type]' and users='$_SESSION[user]' order by date_expire desc, code desc";
            $sql = "select * from ap_products where type='$_GET[type]' order by date_expire desc, code desc";
          }
          $query = mysqli_query($conn,$sql);
          $i = 0;
          while($rows = mysqli_fetch_array($query)){
            $i++;
            if($days>=$rows['date_start'] && $days<=$rows['date_expire']){
              $warning = " class=\"label label-success\"";
            }else{
              $warning = " class=\"label label-danger\"";
            }
            $sql1 = "select * from ap_quiz_question where code='$rows[code]'";
            $query1 = mysqli_query($conn,$sql1);
            $num_rows1 = mysqli_num_rows($query1);

            $sql2 = "select * from ap_users where user='$rows[users]'";
            $query2 = mysqli_query($conn,$sql2);
            $rows2 = mysqli_fetch_array($query2);
            ?>
            <tr>
              <td><?php echo $i;?></td>
              <td><a href="#" <?php echo $warning;?>><?php date_reformat_year($rows['date_start']);?> - <?php date_reformat($rows['date_expire']);?></a></td>
              <td><?php echo $rows['code'];?>
                <?php
                if($_GET['type']=="Quiz"){
                  echo " : <a href=\"?nu=quiz_question&code=$rows[code]\" target=\"_blank\">Q</a> ";
                }?>
              </td>
              <td><?php
                if($_GET['type']=="Questionnaire"){
                  echo "[ <a href=\"$ap_site$ap_folder"."ap/questionnaire/?nu=products&type=Questionnaire\" target=\"_blank\">หน้าหลัก</a>] ";
                  echo "<a href=\"../ap/questionnaire/?xcode=$rows[code]\" target=\"_blank\">$rows[name]</a> ";
                  echo "[ <a href=\"?nu=questionnaire_part&code=$rows[code]\" target=\"_blank\">ตอนที่</a> ] ";
                  echo "[ <a href=\"../ap/questionnaire/?nu=result&xcode=$rows[code]\" target=\"_blank\">สรุปผล</a> ] ";
                }elseif($_GET['type']=="Quiz"){
                  echo "<a href=\"../ap/quiz/?xcode=$rows[code]\" target=\"_blank\">$rows[name]</a> / <a href=\"../ap/quiz/?nu=info&xcode=$rows[code]\" target=\"_blank\">รายชื่อ</a> ";
                }else{
                  echo "<a href=\"?nu=products&type=Register&regCode=$rows[code]\">เขตข้อมูล</a> / <a href=\"../ap/register/?xcode=$rows[code]\" target=\"_blank\">$rows[name]</a> / <a href=\"../ap/register/?nu=info&xcode=$rows[code]\" target=\"_blank\">รายชื่อ</a> ";
                }
                ?>
              </td>
              <?php
              if($_GET['type']=="Quiz"){
              ?>
                <td><?php echo $rows['score'].'/'.$rows['numexam']; ?></td>
                <td><?php echo $num_rows1 ?></td>
              <?php 
              }
              ?>
              <?php
              if($_GET['type']=="Register"){
                $sql21 = "select * from ap_register where code='$rows[code]'";
                $query21 = mysqli_query($conn,$sql21);
                $num_rows21 = mysqli_num_rows($query21);
                $rows21 = mysqli_fetch_array($query21);
              ?>
                <td><?php echo number_format($num_rows21).'/'.number_format($rows['xlimit']); ?></td>
              <?php 
              }
              ?>
              <td><?php echo $rows2['fullname'] ?></td>
              <td><?php if($_SESSION['apstatus']=="Admin" || $_SESSION['user']==$rows['users']){?><a href="?nu=products_i_u&action=products_u_s&id=<?php echo $rows['id']?>&type=<?php echo $_GET['type']?>">แก้ไข</a> :  <a href="#" onclick="if (confirm('ยืนยันการลบ?')) location.href='?nu=products_delete&id=<?php echo $rows['id']?>&type=<?php echo $_GET['type']?>&code=<?php echo $rows['code']?>';">ลบ</a><?php } ?></td>
            </tr>
          <?php
          }
          ?>
        </tbody>
      </table>
      <?php 
      if($_GET['type']=="Register"){
        if(!empty($_GET['regCode'])){
          $sql = "select * from ap_register_form where id='$_GET[xid]'";
          $query = mysqli_query($conn,$sql);
          $rows = mysqli_fetch_array($query);
          if($_GET['xid']==""){
            $job = 1;
          }else{
            $job = 2;
          }
          ?>
          <hr>
          <form method="post" action="?nu=products&type=Register&regCode=<?php echo $_GET['regCode']; ?>&job=<?php echo $job; ?>">
          <input type="hidden" name="xid" value="<?php echo $_GET['xid']; ?>">
          <table class="table table-striped">
          <tbody>
            <tr>
              <td>รหัส</td>
              <td><input type="text" name="regCode" value="<?php echo $_GET['regCode']?>" class="form-control"></td>
            </tr>
            <tr>
              <td>ประเภท Field</td>
              <td><select name="regTypeFieldCode" class="form-control">
                <option value="text" <?php if($rows['regTypeFieldCode']=="text"){ echo "selected='true'"; } ?>>Text</option>
                <option value="radio" <?php if($rows['regTypeFieldCode']=="radio"){ echo "selected='true'"; } ?>>Radio</option>
                <option value="checkbox" <?php if($rows['regTypeFieldCode']=="checkbox"){ echo "selected='true'"; } ?>>Checkbox</option>
              </select></td>
            </tr>
            <tr>
              <td>รหัส Field</td>
              <td><select name="regFieldCode" class="form-control">
                <?php
                $sql2 = "select * from ap_register_fields order by code asc";
                $query2 = mysqli_query($conn,$sql2);
                while($rows2 = mysqli_fetch_array($query2)){
                ?>
                  <option value="<?php echo $rows2['code']?>"
                  <?php
                  if($rows2['code']==$rows['regFieldCode']){
                    echo "selected='true'";
                  }
                  ?>
                  ><?php echo $rows2['code'].' '.$rows2['name']?></option>
                <?php
                }
                ?>
                </select>
            </tr>
            <tr>
              <td>ชื่อ Field</td>
              <td><input type="text" name="regNameFieldCode" value="<?php echo $rows['regNameFieldCode']; ?>" class="form-control"></td>
            </tr>
            <tr>
              <td>ชื่อเรียก Field</td>
              <td><label for="">เฉพาะประเภท Field Checkbox และ Radio</label>
                <input type="text" name="regAlias" value="<?php echo $rows['regAlias']; ?>" class="form-control"></td>
            </tr>
            <tr>
              <td>แสดงผล</td>
              <td><div class="checkbox"><label for=""><input type="checkbox" name="regStatus" value="Show" <?php if($rows['regStatus']=="Show"){ echo "checked"; } ?>> หน้าลงทะเบียน</label></div></td>
            </tr>
            <tr>
              <td>ความต้องการ</td>
              <td><div class="checkbox"><label for=""><input type="checkbox" name="required" value="required" <?php if($rows['required']=="required"){ echo "checked"; } ?>> เฉพาะประเภท Field Text</label></div></td>
            </tr>
            <tr>
              <td colspan="2" align="right"><input type="submit" value="บันทึก" class="btn btn-default"></td>
            </tr>
          </tbody>
          </table>
          </form>
            
          <table class="table table-striped">
          <thead>
            <tr>
              <th width="100">ลำดับ</th>
              <th>ประเภท Field</th>
              <th>รหัส Field</th>
              <th>เขตข้อมูล</th>
              <th>แสดงผล</th>
              <th>ความต้องการ</th>
              <th></th>
            </tr>
          </thead>
          <tbody>
          <?php
          $sql = "select * from ap_register_form where regCode='$_GET[regCode]' order by regFieldCode asc";
          $query = mysqli_query($conn,$sql);
          $i = 0;
          while($rows = mysqli_fetch_array($query)){
          $i++;
          ?>
            <tr>
              <td><?php echo $i;?></td>
              <td><?php echo $rows['regTypeFieldCode'];?></td>
              <td><?php echo $rows['regFieldCode'];?></td>
              <td><?php echo $rows['regNameFieldCode'];?></td>
              <td><?php echo $rows['regStatus'];?></td>
              <td><?php echo $rows['required'];?></td>
              <td><?php if($_SESSION['apstatus']=="Admin" || $_SESSION['user']==$rows['users']){ ?><a href="?nu=products&type=<?php echo $_GET['type']?>&regCode=<?php echo $_GET['regCode'] ?>&xid=<?php echo $rows['id']?>">แก้ไข</a> :  <a href="#" onclick="if (confirm('ยืนยันการลบ?')) location.href='?nu=<?php echo $_GET['nu'] ?>&type=<?php echo $_GET['type']?>&regCode=<?php echo $_GET['regCode'] ?>&job=3&xid=<?php echo $rows['id']?>';">ลบ</a><?php } ?></td>
            </tr>
          <?php
          }
          ?>
          </tbody>
          </table>
          <?php
          if($_GET['job']==1){
            $num_rows3 = mysqli_num_rows(mysqli_query($conn,"select * from ap_register_form where regCode='$_POST[regCode]' and regFieldCode='$_POST[regFieldCode]' "));
            if($num_rows3<1){
              mysqli_query($conn,"insert into ap_register_form (regCode,regTypeFieldCode,regFieldCode,regNameFieldCode,regStatus,required) values ('$_POST[regCode]','$_POST[regTypeFieldCode]','$_POST[regFieldCode]','$_POST[regNameFieldCode]','$_POST[regStatus]','$_POST[required]')");
            }
            echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=products&type=$_GET[type]&regCode=$_POST[regCode]'>";
          }
          if($_GET['job']==2){
            mysqli_query($conn,"update ap_register_form set regCode='$_POST[regCode]',regTypeFieldCode='$_POST[regTypeFieldCode]',regFieldCode='$_POST[regFieldCode]',regNameFieldCode='$_POST[regNameFieldCode]',regStatus='$_POST[regStatus]',required='$_POST[required]' where id='$_POST[xid]'");
            echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=products&type=$_GET[type]&regCode=$_POST[regCode]&xid=$_POST[xid]'>";
          }
          if($_GET['job']==3){
            mysqli_query($conn,"delete from ap_register_form where id='$_GET[xid]'");
            echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=products&type=$_GET[type]&regCode=$_GET[regCode]'>";
          }
        }
      }
      ?>
    </div>
  </div>
<?php
}

if($_GET['nu']=="products_i_u"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  $sql = "select * from ap_products where id='$_GET[id]'";
  $query = mysqli_query($conn,$sql);
  $rows = mysqli_fetch_array($query);
  if(empty($rows['type'])){
    $rows['type'] = $_GET['type'];
  }
  if($_GET['type']=="Questionnaire"){
    $ptitle = "แบบสอบถาม";
  }
  if($_GET['type']=="Quiz"){
    $ptitle = "ข้อสอบ";
  }
  if($_GET['type']=="Register"){
    $ptitle = "ลงทะเบียน";
  }

  if(empty($rows['users'])){
    $rows['users'] = $_SESSION['user'];
  }
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header"><?php echo $ptitle; ?></h2>
    <div class="table-responsive">
      <form action="?nu=<?php echo $_GET['action']?>" method="post" enctype="multipart/form-data">
      <input type="hidden" name="id" value="<?php echo $rows['id']?>">
      <input type="hidden" name="counters" value="<?php echo $rows['counters']?>">
      <input type="hidden" name="type" value="<?php echo $rows['type']?>">
      <input type="hidden" name="code_" value="<?php echo $rows['code']?>">
      <table class="table table-striped">
      <tbody>
        
        <tr>
          <td>ผู้ดูแล ชุด<?php echo $ptitle; ?></td>
          <td><?php if($_SESSION['apstatus']=="Admin"){ ?><select name="users" id="code_users" class="form-control">
          <?php
          $sql2 = "select * from ap_users order by user asc";
          $query2 = mysqli_query($conn,$sql2);
          while($rows2 = mysqli_fetch_array($query2)){
          ?>
            <option value="<?php echo $rows2['user']?>"
            <?php
            if($rows2['user']==$rows['users']){
              echo "selected='true'";
            }
            ?>
            ><?php echo $rows2['fullname']?></option>
          <?php
          }
          ?>
          </select><?php }else{ ?><input type="text" name="users" value="<?php echo $rows['users']?>" class="form-control" readonly><?php } ?></td>
        </tr>
        
        
        
        <tr>
          <td>วันที่ให้บริการ</td>
          <td><input<?php /*if($_SESSION['apstatus']!="Admin"){ ?> readonly="true" <?php }*/ ?> type="text" name="date_start" value="<?php if(empty($rows['date_start'])){ echo date('Ymd'); }else{ echo $rows['date_start']; } ?>" class="form-control"></td>
        </tr>
        <tr>
          <td>วันที่หมดอายุ</td>
          <td><input<?php /*if($_SESSION['apstatus']!="Admin"){ ?> readonly="true" <?php }*/ ?> type="text" name="date_expire" value="<?php if(empty($rows['date_expire'])){ echo date('Ymd'); }else{ echo $rows['date_expire']; } ?>" class="form-control"></td>
        </tr>
        <tr>
          <td>รหัส</td>
          <td><input<?php /*if($_SESSION['apstatus']!="Admin"){ ?> readonly="ture" <?php }*/ ?> type="text" name="code" value="<?php echo $rows['code']?>" class="form-control"></td>
        </tr>
        <tr>
          <td><?php echo $ptitle; ?></td>
          <td><input type="text" name="name" value="<?php echo $rows['name']?>" class="form-control"></td>
        </tr>
        <?php
        if($_GET['type']=="Quiz"){
        ?>
        <tr>
          <td>จำนวนข้อสอบที่ผ่านเกณฑ์</td>
          <td><input type="text" name="score" value="<?php echo $rows['score']?>" class="form-control"></td>
        </tr>
        <tr>
          <td>จำนวนข้อสอบ</td>
          <td><input type="text" name="numexam" value="<?php echo $rows['numexam']?>" class="form-control"></td>
        </tr>
        <?php
        }
        if($_GET['type']=="Register"){
        ?>
        <tr>
          <td>จำนวนรับลงทะเบียน</td>
          <td><input type="text" name="xlimit" value="<?php echo $rows['xlimit']?>" class="form-control"></td>
        </tr>
        <?php }else{ ?>
        <input type="hidden" name="xlimit" value="<?php echo $rows['xlimit']?>">
        <?php } ?>
        <tr>
          <td>เลขที่เกียรติบัตร</td>
          <td><input type="text" name="certificateCode" value="<?php echo $rows['certificateCode']?>" class="form-control"></td>
        </tr>
        <tr>
          <td colspan="2">
            <label>รายละเอียด</label>
            <textarea name="info" rows="5"><?php echo htmlspecialchars($rows['info']); ?></textarea>
          </td>
        </tr>
        <tr>
          <td>แบนเนอร์</td>
          <td><div class="input-group">
          <input type="text" id="photo" name="photo" value="<?php echo $rows['photo']?>" class="form-control" placeholder="">
          <span class="input-group-btn">
            <button href="tinymce/plugins/filemanager/dialog.php?type=1&editor=mce_0&lang=eng&fldr=&field_id=photo&popup=1" class="btn btn-default iframe-btn" type="button">SELECT</button>
          </span>
          </div></td>
          </tr>
        <tr>
          <td>Line Notify</td>
          <td><input type="text" name="linenotify" value="<?php echo $rows['linenotify']?>" class="form-control"></td>
        </tr>
        <tr>
          <td>Line Group</td>
          <td><input type="text" name="linegroup" value="<?php echo $rows['linegroup']?>" class="form-control"></td>
        </tr>
        <tr>
          <td colspan="2" align="right"><input type="submit" value="บันทึก" class="btn btn-default"></td>
        </tr>
      </tbody>
      </table>
      </form>
    </div>
  </div>
<?php
}

if($_GET['nu']=="products_i_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_products";
  $field = "code";
  $field2 = $_POST['code'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2'"));
  if($nr>0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=products&action=products_i_u&$field=$field2&type=$_POST[type]'>";
  }else{
    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    for($i=1; $i < $num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      if($fields!="info"){
        $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));
      }

      $fields_1 .= "$fields, ";
      $fields_2 .= "'".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $field_1 = substr($fields_1,0,-2);
    $field_2 = substr($fields_2,0,-2);
    mysqli_query($conn,"insert into $table ($field_1) values ($field_2)");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=products&type=$_POST[type]'>";
  }
  echo "</div>";
}

if($_GET['nu']=="products_u_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_products";
  $field = "id";
  $field2 = $_POST['id'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2'"));
  if($nr<0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=products&action=products_i_u&$field=$field2&type=$_POST[type]'>";
  }else{
    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    $fields_ = '';
    for($i=1; $i < $num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      if($fields!="info"){
        $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));
      }

      $fields_ .= "$fields='".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $fields_set = substr($fields_,0,-2);
    mysqli_query($conn,"update $table set $fields_set where $field='$field2' ");
    if($_POST['type']=="Questionnaire"){
      mysqli_query($conn,"update ap_questionnaire_part set code='$_POST[code]' where code='$_POST[code_]'");
      mysqli_query($conn,"update ap_questionnaire_question set code='$_POST[code]' where code='$_POST[code_]'");
      mysqli_query($conn,"update ap_questionnaire_answer set code='$_POST[code]' where code='$_POST[code_]'");
    }
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    //echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=products&type=$_POST[type]'>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=products&action=products_i_u&$field=$field2&type=$_POST[type]'>";
  }
  echo "</div>";
}

if ($_GET['nu']=="products_delete") {
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_products";
  mysqli_query($conn,"delete from $table where id='$_GET[id]'");
  if($_GET['type']=="Questionnaire"){
    mysqli_query($conn,"delete from ap_questionnaire_part where code='$_GET[code]'");
    mysqli_query($conn,"delete from ap_questionnaire_question where code='$_GET[code]'");
    mysqli_query($conn,"delete from ap_questionnaire_answer where code='$_GET[code]'");
  }
  echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
  echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=products&type=$_GET[type]'>";
  echo "</div>";
}

if($_GET['nu']=="questionnaire_part"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">แบบสอบถามออนไลน์ <?php echo $_GET['code']?> ตอนที่ ?</h2>
    <div class="table-responsive">
      <table class="table table-striped">
        <thead>
          <tr>
            <th width="70">ลำดับ</th>
            <th>รหัส</th>
            <th>ตอนที่</th>
            <th>ชื่อตอน</th>
            <th width="100"><a href="?nu=questionnaire_part_i_u&action=questionnaire_part_i_s&code=<?php echo $_GET[code];?>">เพิ่ม</a></th>
          </tr>
        </thead>
        <tbody>
          <?php
          $sql = "select * from ap_questionnaire_part where code='$_GET[code]' order by code_part asc";
          $query = mysqli_query($conn,$sql);
          $i = 0;
          while($rows = mysqli_fetch_array($query)){
            $i++;
            ?>
            <tr>
              <td><?php echo $i;?></td>
              <td><?php echo $rows['code'];?></td>
              <td><?php echo $rows['code_part'];?> [ <a href="?nu=questionnaire_question&code=<?php echo $rows[code];?>&code_part=<?php echo $rows[code_part];?>" target="_blank\">คำถาม</a> ]
              </td>
              <td><?php echo $rows['name'];?></td>
              <td><a href="?nu=questionnaire_part_i_u&action=questionnaire_part_u_s&code=<?php echo $rows['code']?>&id=<?php echo $rows['id']?>">แก้ไข</a> :  <a href="#" onclick="if (confirm('ยืนยันการลบ?')) location.href='?nu=questionnaire_part_delete&id=<?php echo $rows['id']?>&code=<?php echo $rows['code']?>&code_part=<?php echo $rows['code_part']?>';">ลบ</a></td>
            </tr>
          <?php
          }
          ?>
        </tbody>
      </table>
    </div>
  </div>
<?php
}

if($_GET['nu']=="questionnaire_part_i_u"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  $sql = "select * from ap_questionnaire_part where id='$_GET[id]'";
  $query = mysqli_query($conn,$sql);
  $rows = mysqli_fetch_array($query);
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">แบบสอบถามออนไลน์ <?php echo $_GET['code']?> ตอนที่ ?</h2>
    <div class="table-responsive">
      <form action="?nu=<?php echo $_GET['action']?>" method="post" enctype="multipart/form-data">
      <input type="hidden" name="id" value="<?php echo $rows['id']?>">
      <input type="hidden" name="code" value="<?php echo $_GET['code']?>">
      <table class="table table-striped">
      <tbody>
        <tr>
          <td>ตอนที่</td>
          <td><input type="text" name="code_part" value="<?php echo $rows['code_part']?>" class="form-control"></td>
        </tr>
        <tr>
          <td>ชื่อตอน</td>
          <td><input type="text" name="name" value="<?php echo $rows['name']?>" class="form-control"></td>
        <tr>
          <td colspan="2">
          <label>คำชี้แจง</label>
          <textarea name="details" class="form-control" style="height: 300px"><?php echo htmlspecialchars($rows['details']); ?></textarea></td>
        </tr>
        <tr>
          <td colspan="2" align="right"><input type="submit" value="บันทึก" class="btn btn-default"></td>
        </tr>
      </tbody>
      </table>
      </form>
    </div>
  </div>
<?php
}

if($_GET['nu']=="questionnaire_part_i_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_questionnaire_part";
  /*$field = "code_part";
  $field2 = $_POST['code_part'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2'"));
  if($nr>0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=questionnaire_part&action=questionnaire_part_i_u&$field=$field2'>";
  }else{*/
    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    for($i=1; $i < $num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      if($fields!="details"){
        $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));
      }

      $fields_1 .= "$fields, ";
      $fields_2 .= "'".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $field_1 = substr($fields_1,0,-2);
    $field_2 = substr($fields_2,0,-2);
    mysqli_query($conn,"insert into $table ($field_1) values ($field_2)");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=questionnaire_part&code=$_POST[code]'>";
  //}
  echo "</div>";
}

if($_GET['nu']=="questionnaire_part_u_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_questionnaire_part";
  $field = "id";
  $field2 = $_POST['id'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2'"));
  if($nr<0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=questionnaire_part&action=questionnaire_part_i_u&$field=$field2&code=$_POST[code]'>";
  }else{
    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    $fields_ = '';
    for($i=1; $i < $num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      if($fields!="details"){
        $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));
      }

      $fields_ .= "$fields='".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $fields_set = substr($fields_,0,-2);
    mysqli_query($conn,"update $table set $fields_set where $field='$field2' ");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=questionnaire_part&code=$_POST[code]'>";
  }
  echo "</div>";
}

if ($_GET['nu']=="questionnaire_part_delete") {
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_questionnaire_part";
  mysqli_query($conn,"delete from $table where id='$_GET[id]'");
  mysqli_query($conn,"delete from ap_questionnaire_question where code_part='$_GET[code_part]'");
  echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
  echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=questionnaire_part&code=$_GET[code]'>";
  echo "</div>";
}

if($_GET['nu']=="questionnaire_question"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">แบบสอบถามออนไลน์ <?php echo $_GET['code']?> ตอนที่ <?php echo $_GET['code_part']?></h2>
    <div class="table-responsive">
      <table class="table table-striped">
        <thead>
          <tr>
            <th width="70">ลำดับ</th>
            <th>ตอนที่</th>
            <th>รหัส</th>
            <th>ข้อคำถาม</th>
            <th width="100"><a href="?nu=questionnaire_question_i_u&action=questionnaire_question_i_s&code=<?php echo $_GET[code];?>&code_part=<?php echo $_GET[code_part]?>">เพิ่ม</a></th>
          </tr>
        </thead>
        <tbody>
          <?php
          $sql = "select * from ap_questionnaire_question where code='$_GET[code]' and code_part='$_GET[code_part]' order by code_question asc";
          $query = mysqli_query($conn,$sql);
          $i = 0;
          while($rows = mysqli_fetch_array($query)){
            $i++;
            ?>
            <tr>
              <td><?php echo $i;?></td>
              <td><?php echo $rows['code_part'];?></td>
              <td><?php echo $rows['code_question'];?></td>
              <td><?php echo $rows['title'];?></td>
              <td><a href="?nu=questionnaire_question_i_u&action=questionnaire_question_u_s&code=<?php echo $rows['code']?>&code_part=<?php echo $_GET[code_part]?>&id=<?php echo $rows['id']?>">แก้ไข</a> : <a href="#" onclick="if (confirm('ยืนยันการลบ?')) location.href='?nu=questionnaire_question_delete&id=<?php echo $rows['id']?>&code=<?php echo $rows['code']?>&code_part=<?php echo $rows['code_part']?>';">ลบ</a></td>
            </tr>
          <?php
          }
          ?>
        </tbody>
      </table>
    </div>
  </div>
<?php
}

if($_GET['nu']=="questionnaire_question_i_u"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  $sql = "select * from ap_questionnaire_question where id='$_GET[id]'";
  $query = mysqli_query($conn,$sql);
  $rows = mysqli_fetch_array($query);

  if($rows['format_status']==1){
    $ck99 = " checked='true'";
  }
  if($rows['title_status']==1){
    $ck0 = " checked='true'";
  }
  if($rows['choice_1_status']==1){
    $ck1 = " checked='true'";
  }
  if($rows['choice_2_status']==1){
    $ck2 = " checked='true'";
  }
  if($rows['choice_3_status']==1){
    $ck3 = " checked='true'";
  }
  if($rows['choice_4_status']==1){
    $ck4 = " checked='true'";
  }
  if($rows['choice_5_status']==1){
    $ck5 = " checked='true'";
  }
  if($rows['choice_6_status']==1){
    $ck6 = " checked='true'";
  }
  if($rows['choice_7_status']==1){
    $ck7 = " checked='true'";
  }
  if($rows['choice_8_status']==1){
    $ck8 = " checked='true'";
  }
  if($rows['choice_9_status']==1){
    $ck9 = " checked='true'";
  }
  if($rows['choice_10_status']==1){
    $ck10 = " checked='true'";
  }
  if($rows['choice_11_status']==1){
    $ck11 = " checked='true'";
  }
  if($rows['choice_12_status']==1){
    $ck12 = " checked='true'";
  }
  if($rows['choice_13_status']==1){
    $ck13 = " checked='true'";
  }
  if($rows['choice_14_status']==1){
    $ck14 = " checked='true'";
  }
  if($rows['choice_15_status']==1){
    $ck15 = " checked='true'";
  }
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">แบบสอบถามออนไลน์ <?php echo $_GET['code']?> ตอนที่ <?php echo $_GET['code_part']?></h2>
    <div class="table-responsive">
      <form action="?nu=<?php echo $_GET['action']?>" method="post" enctype="multipart/form-data">
      <input type="hidden" name="id" value="<?php echo $rows['id']?>">
      <input type="hidden" name="code" value="<?php echo $_GET[code]?>">
      <input type="hidden" name="code_part" value="<?php echo $_GET[code_part]?>">
      <table class="table table-striped">
      <tbody>
        <tr>
          <td>รหัส</td>
          <td><input type="text" name="code_question" value="<?php echo $rows['code_question']?>" class="form-control"></td>
        </tr>
        <?php /* ?><tr>
          <td>รูปแบบ</td>
          <td>
          <label>แบบที่ 1 = 1, แบบที่ 2 = 2, แบบที่ 3 = 3</label>
          <input type="text" name="format" value="<?php echo $rows['format']?>" class="form-control"></td>
        </tr><?php */ ?><tr>
        <tr>
          <td>รูปแบบ</td>
          <td>
            <label>แบบที่ 1 = 1, แบบที่ 2 = 2, แบบที่ 3 = 3 และ ทำเครื่องหมายถูก กรณีเป็นปลายเปิด</label>
            <div class="input-group">
              <span class="input-group-addon">
                <input type="checkbox" name="format_status" value="1"<?php echo $ck99;?> aria-label="...">
              </span>
              <input type="text" name="format" value="<?php echo $rows['format']?>" class="form-control">
            </div>
          </td>
        </tr>
        <tr>
          <td>ข้อคำถาม</td>
          <td>
            <label>ทำเครื่องหมายถูก กรณีเป็นหัวข้อย่อย</label>
            <div class="input-group">
              <span class="input-group-addon">
                <input type="checkbox" name="title_status" value="1"<?php echo $ck0;?> aria-label="...">
              </span>
              <input type="text" name="title" value="<?php echo $rows['title']?>" class="form-control">
            </div>
          </td>
        </tr>
        <tr>
          <td>ตัวเลือกที่ 1</td>
          <td>
            <label>ทำเครื่องหมายถูก กรณีต้องการให้กรอกข้อมูลเพิ่ม</label>
            <div class="input-group">
              <span class="input-group-addon">
                <input type="checkbox" name="choice_1_status" value="1"<?php echo $ck1;?> aria-label="...">
              </span>
              <input type="text" name="choice_1" value="<?php echo $rows['choice_1']?>" class="form-control">
            </div>
          </td>
        </tr>
        <tr>
          <td>ตัวเลือกที่ 2</td>
          <td>
            <label>ทำเครื่องหมายถูก กรณีต้องการให้กรอกข้อมูลเพิ่ม</label>
            <div class="input-group">
              <span class="input-group-addon">
                <input type="checkbox" name="choice_2_status" value="1"<?php echo $ck2;?> aria-label="...">
              </span>
              <input type="text" name="choice_2" value="<?php echo $rows['choice_2']?>" class="form-control">
            </div>
          </td>
        </tr>
        <tr>
          <td>ตัวเลือกที่ 3</td>
          <td>
            <label>ทำเครื่องหมายถูก กรณีต้องการให้กรอกข้อมูลเพิ่ม</label>
            <div class="input-group">
              <span class="input-group-addon">
                <input type="checkbox" name="choice_3_status" value="1"<?php echo $ck3;?> aria-label="...">
              </span>
              <input type="text" name="choice_3" value="<?php echo $rows['choice_3']?>" class="form-control">
            </div>
          </td>
        </tr>
        <tr>
          <td>ตัวเลือกที่ 4</td>
          <td>
            <label>ทำเครื่องหมายถูก กรณีต้องการให้กรอกข้อมูลเพิ่ม</label>
            <div class="input-group">
              <span class="input-group-addon">
                <input type="checkbox" name="choice_4_status" value="1"<?php echo $ck4;?> aria-label="...">
              </span>
              <input type="text" name="choice_4" value="<?php echo $rows['choice_4']?>" class="form-control">
            </div>
          </td>
        </tr>
        <tr>
          <td>ตัวเลือกที่ 5</td>
          <td>
            <label>ทำเครื่องหมายถูก กรณีต้องการให้กรอกข้อมูลเพิ่ม</label>
            <div class="input-group">
              <span class="input-group-addon">
                <input type="checkbox" name="choice_5_status" value="1"<?php echo $ck5;?> aria-label="...">
              </span>
              <input type="text" name="choice_5" value="<?php echo $rows['choice_5']?>" class="form-control">
            </div>
          </td>
        </tr>
        <tr>
          <td>ตัวเลือกที่ 6</td>
          <td>
            <label>ทำเครื่องหมายถูก กรณีต้องการให้กรอกข้อมูลเพิ่ม</label>
            <div class="input-group">
              <span class="input-group-addon">
                <input type="checkbox" name="choice_6_status" value="1"<?php echo $ck6;?> aria-label="...">
              </span>
              <input type="text" name="choice_6" value="<?php echo $rows['choice_6']?>" class="form-control">
            </div>
          </td>
        </tr>
        <tr>
          <td>ตัวเลือกที่ 7</td>
          <td>
            <label>ทำเครื่องหมายถูก กรณีต้องการให้กรอกข้อมูลเพิ่ม</label>
            <div class="input-group">
              <span class="input-group-addon">
                <input type="checkbox" name="choice_7_status" value="1"<?php echo $ck7;?> aria-label="...">
              </span>
              <input type="text" name="choice_7" value="<?php echo $rows['choice_7']?>" class="form-control">
            </div>
          </td>
        </tr>
        <tr>
          <td>ตัวเลือกที่ 8</td>
          <td>
            <label>ทำเครื่องหมายถูก กรณีต้องการให้กรอกข้อมูลเพิ่ม</label>
            <div class="input-group">
              <span class="input-group-addon">
                <input type="checkbox" name="choice_8_status" value="1"<?php echo $ck8;?> aria-label="...">
              </span>
              <input type="text" name="choice_8" value="<?php echo $rows['choice_8']?>" class="form-control">
            </div>
          </td>
        </tr>
        <tr>
          <td>ตัวเลือกที่ 9</td>
          <td>
            <label>ทำเครื่องหมายถูก กรณีต้องการให้กรอกข้อมูลเพิ่ม</label>
            <div class="input-group">
              <span class="input-group-addon">
                <input type="checkbox" name="choice_9_status" value="1"<?php echo $ck9;?> aria-label="...">
              </span>
              <input type="text" name="choice_9" value="<?php echo $rows['choice_9']?>" class="form-control">
            </div>
          </td>
        </tr>
        <tr>
          <td>ตัวเลือกที่ 10</td>
          <td>
            <label>ทำเครื่องหมายถูก กรณีต้องการให้กรอกข้อมูลเพิ่ม</label>
            <div class="input-group">
              <span class="input-group-addon">
                <input type="checkbox" name="choice_10_status" value="1"<?php echo $ck10;?> aria-label="...">
              </span>
              <input type="text" name="choice_10" value="<?php echo $rows['choice_10']?>" class="form-control">
            </div>
          </td>
        </tr>
        <tr>
          <td>ตัวเลือกที่ 11</td>
          <td>
            <label>ทำเครื่องหมายถูก กรณีต้องการให้กรอกข้อมูลเพิ่ม</label>
            <div class="input-group">
              <span class="input-group-addon">
                <input type="checkbox" name="choice_11_status" value="1"<?php echo $ck11;?> aria-label="...">
              </span>
              <input type="text" name="choice_11" value="<?php echo $rows['choice_11']?>" class="form-control">
            </div>
          </td>
        </tr>
        <tr>
          <td>ตัวเลือกที่ 12</td>
          <td>
            <label>ทำเครื่องหมายถูก กรณีต้องการให้กรอกข้อมูลเพิ่ม</label>
            <div class="input-group">
              <span class="input-group-addon">
                <input type="checkbox" name="choice_12_status" value="1"<?php echo $ck12;?> aria-label="...">
              </span>
              <input type="text" name="choice_12" value="<?php echo $rows['choice_12']?>" class="form-control">
            </div>
          </td>
        </tr>
        <tr>
          <td>ตัวเลือกที่ 13</td>
          <td>
            <label>ทำเครื่องหมายถูก กรณีต้องการให้กรอกข้อมูลเพิ่ม</label>
            <div class="input-group">
              <span class="input-group-addon">
                <input type="checkbox" name="choice_13_status" value="1"<?php echo $ck13;?> aria-label="...">
              </span>
              <input type="text" name="choice_13" value="<?php echo $rows['choice_13']?>" class="form-control">
            </div>
          </td>
        </tr>
        <tr>
          <td>ตัวเลือกที่ 14</td>
          <td>
            <label>ทำเครื่องหมายถูก กรณีต้องการให้กรอกข้อมูลเพิ่ม</label>
            <div class="input-group">
              <span class="input-group-addon">
                <input type="checkbox" name="choice_14_status" value="1"<?php echo $ck14;?> aria-label="...">
              </span>
              <input type="text" name="choice_14" value="<?php echo $rows['choice_14']?>" class="form-control">
            </div>
          </td>
        </tr>
        <tr>
          <td>ตัวเลือกที่ 15</td>
          <td>
            <label>ทำเครื่องหมายถูก กรณีต้องการให้กรอกข้อมูลเพิ่ม</label>
            <div class="input-group">
              <span class="input-group-addon">
                <input type="checkbox" name="choice_15_status" value="1"<?php echo $ck15;?> aria-label="...">
              </span>
              <input type="text" name="choice_15" value="<?php echo $rows['choice_15']?>" class="form-control">
            </div>
          </td>
        </tr>
        <tr>
          <td colspan="2" align="right"><input type="submit" value="บันทึก" class="btn btn-default"></td>
        </tr>
      </tbody>
      </table>
      </form>
    </div>
  </div>
<?php
}

if($_GET['nu']=="questionnaire_question_i_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_questionnaire_question";
  $field = "code_question";
  $field2 = $_POST['code_question'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where code='$_POST[code]' and code_part='$_POST[code_part]' and $field='$field2'"));
  if($nr>0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=questionnaire_question&action=questionnaire_question_i_u&code=$_POST[code]&code_part=$_POST[code_part]'>";
  }else{
    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    for($i=1; $i < $num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));

      $fields_1 .= "$fields, ";
      $fields_2 .= "'".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $field_1 = substr($fields_1,0,-2);
    $field_2 = substr($fields_2,0,-2);
    mysqli_query($conn,"insert into $table ($field_1) values ($field_2)");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=questionnaire_question&code=$_POST[code]&code_part=$_POST[code_part]'>";
  }
  echo "</div>";
}

if($_GET['nu']=="questionnaire_question_u_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_questionnaire_question";
  $field = "id";
  $field2 = $_POST['id'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2'"));
  if($nr<0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=questionnaire_question&action=questionnaire_question_i_u&$field=$field2&code=$_POST[code]&code_part=$_POST[code_part]'>";
  }else{
    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    $fields_ = '';
    for($i=1; $i < $num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));

      $fields_ .= "$fields='".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $fields_set = substr($fields_,0,-2);
    mysqli_query($conn,"update $table set $fields_set where $field='$field2' ");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=questionnaire_question&code=$_POST[code]&code_part=$_POST[code_part]'>";
  }
  echo "</div>";
}

if ($_GET['nu']=="questionnaire_question_delete") {
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_questionnaire_question";
  mysqli_query($conn,"delete from $table where id='$_GET[id]'");
  echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
  echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=questionnaire_question&code=$_GET[code]&code_part=$_GET[code_part]'>";
  echo "</div>";
}

if($_GET['nu']=="quiz_question"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  $sql2 = "select * from ap_products where code='$_GET[code]'";
  $query2 = mysqli_query($conn,$sql2);
  $rows2 = mysqli_fetch_array($query2);
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">ข้อสอบ <?php echo $_GET['code']?></h2>
    <div class="table-responsive">
      <table class="table table-striped">
        <thead>
          <tr>
            <th width="70">ลำดับ</th>
            <th>คำถาม</th>
            <th width="100"><?php if($_SESSION['apstatus']=="Admin" || $_SESSION['user']==$rows2['users']){ ?><a href="?nu=quiz_question_i_u&action=quiz_question_i_s&code=<?php echo $_GET['code']?>">เพิ่ม</a><?php } ?></th>
          </tr>
        </thead>
        <tbody>
          <?php
          $sql = "select * from ap_quiz_question where code='$_GET[code]' order by id desc";
          $query = mysqli_query($conn,$sql);
          $i = 0;
          while($rows = mysqli_fetch_array($query)){
            $i++;
            if(empty($rows['users'])){
              mysqli_query($conn,"update ap_quiz_question set users='$rows2[users]' where id='$rows[id]'");
            }
            ?>
            <tr>
              <td><?php echo $i;?></td>
              <td><?php echo $rows['question'];?> <br><b>ตอบข้อ</b> <?php echo $rows['answer'];?> : <?php $a = "choice$rows[answer]"; if("choice$rows[answer]"=="$a"){ echo "$rows[$a]"; } ?></br></td>
              <td><?php if($_SESSION['apstatus']=="Admin" || $_SESSION['user']==$rows['users']){ ?><a href="?nu=quiz_question_i_u&action=quiz_question_u_s&id=<?php echo $rows['id']?>&code=<?php echo $_GET['code']?>">แก้ไข</a> :  <a href="#" onclick="if (confirm('ยืนยันการลบ?')) location.href='?nu=quiz_question_delete&id=<?php echo $rows['id']?>&code=<?php echo $_GET['code']?>';">ลบ</a><?php } ?></td>
            </tr>
          <?php
          }
          ?>
        </tbody>
      </table>
    </div>
  </div>
<?php
}

if($_GET['nu']=="quiz_question_i_u"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  $sql = "select * from ap_quiz_question where id='$_GET[id]'";
  $query = mysqli_query($conn,$sql);
  $rows = mysqli_fetch_array($query);

  $sql2 = "select * from ap_products where code='$_GET[code]'";
  $query2 = mysqli_query($conn,$sql2);
  $rows2 = mysqli_fetch_array($query2);
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">ข้อสอบ</h2>
    <div class="table-responsive">
      <form action="?nu=<?php echo $_GET['action']?>" method="post" enctype="multipart/form-data">
      <input type="hidden" name="id" value="<?php echo $rows['id']?>">
      <input type="hidden" name="users" value="<?php echo $rows2['users']; ?>">
      <?php
      if($rows['code']==""){?>
        <input type="hidden" name="code" value="<?php echo $_GET['code']?>">
      <?php }else{ ?>
        <input type="hidden" name="code" value="<?php echo $rows['code']?>">
      <?php } ?>
      <table class="table table-striped">
      <tbody>
        <tr>
          <td>คำถาม</td>
          <td><input type="text" name="question" value="<?php echo $rows['question']?>" class="form-control"></td>
        </tr>
        <tr>
          <td>คำตอบที่ 1 (ก,A)</td>
          <td><input type="text" name="choice1" size="30" value="<?php echo $rows['choice1']?>" class="form-control"></td>
        </tr>
        <tr>
          <td>คำตอบที่ 2 (ข,B)</td>
          <td><input type="text" name="choice2" size="30" value="<?php echo $rows['choice2']?>" class="form-control"></td>
        </tr>
        <tr>
          <td>คำตอบที่ 3 (ค,C)</td>
          <td><input type="text" name="choice3" size="30" value="<?php echo $rows['choice3']?>" class="form-control"></td>
        </tr>
        <tr>
          <td>คำตอบที่ 4 (ง,D)</td>
          <td><input type="text" name="choice4" size="30" value="<?php echo $rows['choice4']?>" class="form-control"></td>
        </tr>
        <tr>
          <td>คำตอบที่ 5 (จ,E)</td>
          <td><input type="text" name="choice5" size="30" value="<?php echo $rows['choice5']?>" class="form-control"></td>
        </tr>
        <tr>
          <td>คำตอบที่ถูกต้อง</td>
          <td><input type="text" name="answer" size="30" value="<?php echo $rows['answer']?>" class="form-control"></td>
        </tr>
        <tr>
          <td colspan="2" align="right"><input type="submit" value="บันทึก" class="btn btn-default"></td>
        </tr>
      </tbody>
      </table>
      </form>
    </div>
  </div>
<?php
}

if($_GET['nu']=="quiz_question_i_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_quiz_question";
  $field = "question";
  $field2 = $_POST['question'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where code='$_POST[code]' and $field='$field2'"));
  if($nr>0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=quiz_question&action=quiz_question_i_u&code=$_POST[code]'>";
  }else{
    $_POST['date_time'] = date('Y-m-d H:i:s');
    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    for($i=1; $i < $num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));

      $fields_1 .= "$fields, ";
      $fields_2 .= "'".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $field_1 = substr($fields_1,0,-2);
    $field_2 = substr($fields_2,0,-2);
    mysqli_query($conn,"insert into $table ($field_1) values ($field_2)");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=quiz_question&code=$_POST[code]'>";
  }
  echo "</div>";
}

if($_GET['nu']=="quiz_question_u_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_quiz_question";
  $field = "id";
  $field2 = $_POST['id'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where code='$_POST[code]' and $field='$field2'"));
  if($nr<0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=quiz_question&action=quiz_question_i_u&$field=$field2&code=$_POST[code]'>";
  }else{
    $_POST['date_time'] = date('Y-m-d H:i:s');
    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    $fields_ = '';
    for($i=1; $i < $num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));

      $fields_ .= "$fields='".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $fields_set = substr($fields_,0,-2);
    mysqli_query($conn,"update $table set $fields_set where $field='$field2' ");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=quiz_question&code=$_POST[code]'>";
  }
  echo "</div>";
}

if ($_GET['nu']=="quiz_question_delete") {
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_quiz_question";
  mysqli_query($conn,"delete from $table where id='$_GET[id]'");
  echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
  echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=quiz_question&code=$_GET[code]'>";
  echo "</div>";
}

if($_GET['nu']=="qa"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">บริการตอบคำถาม</h2>
    <div class="table-responsive">
      <table class="table table-striped">
        <thead>
          <tr>
            <th width="70">ลำดับ</th>
            <th>ภาษา</th>
            <th>กลุ่มคำถาม</th>
            <th>กลุ่มย่อยคำถาม</th>
            <th>คำถาม</th>
            <th width="50%">คำตอบ</th>
            <th width="100"><a href="?nu=qa_i_u&action=qa_i_s">เพิ่ม</a></th>
          </tr>
        </thead>
        <tbody>
          <?php
          $sql = "select * from ap_question_answer order by type asc,xgroup asc,question asc ,dateTime asc";
          $query = mysqli_query($conn,$sql);
          $i = 0;
          while($rows = mysqli_fetch_array($query)){
          $i++;
          if($rows['type']=="1"){
            $qa1 = "คำถามที่พบบ่อย";
          }elseif($rows['type']=="2"){
            $qa1 = "คำถามทั่วไป";
          }else{
            $qa1 = "BOT";
          }
          if($rows['xgroup']=="1"){
            $qa2 = "ข้มูล";
          }elseif($rows['xgroup']=="2"){
            $qa2 = "บริการ";
          }elseif($rows['xgroup']=="3"){
            $qa2 = "ทรัพยากร";
          }else{
            $qa2 = "BOT";
          }
          ?>
            <tr>
              <td><?php echo $i;?></td>              
              <td><?php echo $rows['lang'];?></td>
              <td><?php echo $qa1;?></td>
              <td><?php echo $qa2;?></td>
              <td><b><a href="<?php echo $ap_site.$ap_folder; ?>ap/question-answer/" target="_blank"><?php echo $rows['question'];?></a></b></td>
              <td><?php echo $rows['answer'];?></td>
              <!-- <td><a href="<?php echo htmlspecialchars($loginUrl)?>" target="_blank">FaceBook</a></td> -->
              <td><a href="?nu=qa_i_u&action=qa_u_s&id=<?php echo $rows['id']?>">แก้ไข</a> :  <a href="#" onclick="if (confirm('ยืนยันการลบ?')) location.href='?nu=qa_delete&id=<?php echo $rows['id']?>';">ลบ</a></td>
            </tr>
          <?php
          }
          ?>
        </tbody>
      </table>
    </div>
  </div>
<?php
}

if($_GET['nu']=="qa_i_u"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  $sql = "select * from ap_question_answer where id='$_GET[id]'";
  $query = mysqli_query($conn,$sql);
  $rows = mysqli_fetch_array($query);
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
  <h2 class="sub-header">บริการตอบคำถาม</h2>
  <form class="form-horizontal" action="?nu=<?php echo $_GET['action']?>" method="post" enctype="multipart/form-data">
  <input type="hidden" name="id" value="<?php echo $rows['id']?>">
  <input type="hidden" name="dateTime" value="<?php echo $rows['dateTime']?>">
  <input type="hidden" name="createBy" value="<?php echo $rows['createBy']?>">
  <input type="hidden" name="createByStatus" value="<?php echo $rows['createByStatus']?>">
  <div class="from-group">
    <label for="">ภาษา</label>
    <select name="lang" class="form-control">
    <?php
    $sql2 = "select * from ap_langs order by code asc";
    $query2 = mysqli_query($conn,$sql2);
    while($rows2 = mysqli_fetch_array($query2)){
    ?>
      <option value="<?php echo $rows2['code']?>" 
      <?php
      if($rows2['code']==$rows['lang']){
        echo "selected='true'";
      }
      ?>
      ><?php echo $rows2['name']?></option>
    <?php
    }
    ?>
    </select>
  </div>
  <div class="from-group">
    <p></p><label for="">กลุ่มคำถาม</label><p></p>
    <input type="radio" name="type" value="" <?php if($rows['type']==""){ echo "selected checked"; } ?>> BOT
    <input type="radio" name="type" value="1" <?php if($rows['type']==1){ echo "selected checked"; } ?>> คำถามที่พบบ่อย
    <input type="radio" name="type" value="2" <?php if($rows['type']==2){ echo "selected checked"; } ?>> คำถามทั่วไป
  </div>
  <div class="from-group">
    <p></p><label for="">กลุ่มย่อยคำถาม</label><p></p>
    <input type="radio" name="xgroup" value="" <?php if($rows['xgroup']==""){ echo "selected checked"; } ?>> BOT
    <input type="radio" name="xgroup" value="1" <?php if($rows['xgroup']==1){ echo "selected checked"; } ?>> ข้อมูล
    <input type="radio" name="xgroup" value="2" <?php if($rows['xgroup']==2){ echo "selected checked"; } ?>> บริการ
    <input type="radio" name="xgroup" value="3" <?php if($rows['xgroup']==3){ echo "selected checked"; } ?>> ทรัพยากร
  </div>
  <div class="from-group">
    <p></p><label for="">คำถาม</label>
    <input type="text" name="question" value="<?php echo $rows['question']?>" class="form-control">
  </div>
  <div class="from-group">
    <p></p><label for="">คำตอบ</label>
    <textarea name="answer" cols="30" rows="10" class="form-control"><?php echo htmlspecialchars($rows['answer']); ?></textarea>
  </div>
  <p></p><p align="right"><input type="submit" value="บันทึก" class="btn btn-default"></p>
  </form>
    </div>
<?php
}

if($_GET['nu']=="qa_i_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $_POST['dateTime'] = date('Y-m-d H:i:s');
  if($_POST['createBy']==""){
    $_POST['createBy'] = $_SESSION['user'];
  }
  if($_POST['createByStatus']==""){
    $_POST['createByStatus'] = $_SESSION['apstatus'];
  }
  $table = "ap_question_answer";
  $query = mysqli_query($conn,"select * from $table");
  $num_fields = mysqli_num_fields($query);
  for($i=1; $i<$num_fields; $i++) {
    $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
    $fields = $field_name[$i];

    if($fields!="answer"){
      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));
    }

    $fields_1 .= "$fields, ";
    $fields_2 .= "'".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
  }
  $field_1 = substr($fields_1,0,-2);
  $field_2 = substr($fields_2,0,-2);
  mysqli_query($conn,"insert into $table ($field_1) values ($field_2)");
  echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
  echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=qa'>";
  echo "</div>";
}

if($_GET['nu']=="qa_u_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_question_answer";
  $field = "id";
  $field2 = $_POST['id'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2'"));
  if($nr<0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=qa&action=qa_i_u&$field=$field2'>";
  }else{
    if($_POST['createBy']==""){
      $_POST['createBy'] = $_SESSION['user'];
    }
    if($_POST['createByStatus']==""){
      $_POST['createByStatus'] = $_SESSION['apstatus'];
    }
    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    for($i=1; $i<$num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      if($fields!="answer"){
        $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));
      }

      $fields_ .= "$fields='".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $fields_set = substr($fields_,0,-2);
    mysqli_query($conn,"update $table set $fields_set where $field='$field2' ");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=qa'>";
  }
  echo "</div>";
}

if ($_GET['nu']=="qa_delete") {
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_question_answer";
  mysqli_query($conn,"delete from $table where id='$_GET[id]'");
  echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
  echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=qa'>";
  echo "</div>";
}

if($_GET['nu']=="users_job_description"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <div class="row">
      <div class="col-lg-6 col-md-6 col-sm-12">
        <?php 
        if(empty($_GET['code']) || $_GET['code']=="IndDev"){ 
          echo "<p class=\"h4\">ระบบ การพัฒนาบุคลากร</p>";
        }else{
          echo "<p class=\"h4\">ระบบ การบริการวิชาการ</p>";
        }
        ?>        
      </div>
      <div class="col-lg-6 col-md-6 col-sm-12">
        <form class="form-inline text-right" action="print.php" method="GET" target="_blank">
          <input type="hidden" name="nu" value="">
          <div class="form-group">
            <input type="text" name="q" value="<?php echo date('Y-m'); ?>" placeholder="ระบุคำค้น" class="form-control" autofocus>
          </div>
          <button type="submit" class="btn btn-default">ค้นหา</button>
        </form>
      </div>
    </div>
    <hr>
    <div class="table-responsive">
      <table class="table table-striped">
        <thead>
          <tr>
            <th width="70">ลำดับ</th>
            <th width="150">วันที่</th>
            <th>เรื่อง</th>
            <th>สถานที่</th>
            <th width="100">เอกสาร</th>
            <th width="100"><a href="?nu=users_job_description_i_u&action=users_job_description_i_s&code=<?php echo $_GET['code']; ?>">เพิ่ม</a></th>
          </tr>
        </thead>
        <tbody>
          <?php
          if($_SESSION['apstatus']=="Admin"){
            $sql = "select * from ap_users_job_description where code='$_GET[code]' order by date_time desc";
          }else{
            $sql = "select * from ap_users_job_description where createBy='$_SESSION[user]' and code='$_GET[code]' order by date_time desc";
          }
          $query = mysqli_query($conn,$sql);
          $i = 0;
          while($rows = mysqli_fetch_array($query)){
          
            $sql2 = "select * from ap_users where user='$rows[createBy]'";
            $query2 = mysqli_query($conn,$sql2);
            $rows2 = mysqli_fetch_array($query2);
            if($last!=$rows['createBy']){
              $i = 0;
              ?>
              <tr>
                <td colspan="6"><?php echo $rows2['fullname'] ?></td>
              </tr>
            <?php
            }
            $last = $rows['createBy'];

            $i++;
            ?>
            <tr>
              <td><?php echo $i;?>.</td>
              <td><?php echo $rows['dateTime'];?></td>
              <td><a href="<?php echo $ap_site.$ap_folder; ?>ap/job-description/?nu=&code=<?php echo $_GET['code']; ?>" target="_blank"><?php echo $rows['title'];?></a></td>
              <td><?php echo $rows['location'];?></td>
              <td><a href="?nu=edms2&catalogCode=<?php echo $rows['id'] ?>&code=<?php echo $_GET['code']; ?>" target="_blank">อัพโหลด</a></td>
              <td><?php if($_SESSION['apstatus']=="Admin" || $rows['createBy']==$_SESSION['user']){?><a href="?nu=users_job_description_i_u&action=users_job_description_u_s&id=<?php echo $rows['id']?>&code=<?php echo $_GET['code']; ?>">แก้ไข</a> :  <a href="#" onclick="if (confirm('ยืนยันการลบ?')) location.href='?nu=users_job_description_delete&id=<?php echo $rows['id']?>&code=<?php echo $_GET['code']; ?>';">ลบ</a><?php } ?></td>
            </tr>
          <?php
          }
          ?>
        </tbody>
      </table>
    </div>
  </div>
<?php
}

if($_GET['nu']=="users_job_description_i_u"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  $sql = "select * from ap_users_job_description where id='$_GET[id]'";
  $query = mysqli_query($conn,$sql);
  $rows = mysqli_fetch_array($query);
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <!-- <h2 class="sub-header">ระบบ การพัฒนาบุคลากร</h2> -->
    <?php 
    if(empty($_GET['code']) || $_GET['code']=="IndDev"){ 
      echo "<p class=\"h4\">ระบบ การพัฒนาบุคลากร</p>";
    }else{
      echo "<p class=\"h4\">ระบบ การบริการวิชาการ</p>";
    }
    ?>
    <div class="table-responsive">
      <form action="?nu=<?php echo $_GET['action']?>" method="post" enctype="multipart/form-data">
      <input type="hidden" name="id" value="<?php echo $rows['id']?>">
      <input type="hidden" name="code" value="<?php echo $rows['code']?>">
      <input type="hidden" name="createBy" value="<?php echo $rows['createBy']?>">
      <input type="hidden" name="createByStatus" value="<?php echo $rows['createByStatus']?>">
      <input type="hidden" name="counters" value="<?php echo $rows['counters']?>">
      <input type="hidden" name="files_" value="<?php echo $rows['files']?>">
      <input type="hidden" name="files2_" value="<?php echo $rows['files2']?>">
      <input type="hidden" name="files3_" value="<?php echo $rows['files3']?>">
      <table class="table table-striped">
        <tbody>
          <tr style="display: none">
            <td>รหัส</td>
            <td><input placeholder="" type="text" name="code" value="<?php if($rows['code']==""){ echo $_GET['code']; }else{ echo $rows['code']; } ?>" class="form-control" readonly>
            </td>
          </tr>
          <tr>
            <td>วันที่บันทึก</td>
            <td><input placeholder="" type="text" name="date_time" value="<?php if($rows['date_time']==""){ echo date('Y-m-d H:i:s'); }else{ echo $rows['date_time']; } ?>" class="form-control">
            </td>
          </tr>
          <tr>
            <td>วันที่ </td>
            <td>
              <div class="row input-group">
                <div class="col-lg-12 col-md-12 col-sm-12">
                  <label for="">ตัวอย่าง 15 - 20 มี.ค. <?php echo date('Y')+543; ?></label>
                  <input placeholder="" type="text" name="dateTime" value="<?php echo $rows['dateTime']; ?>" class="form-control">
                </div>
              </div>
            </td>
          </tr>
          <tr>
            <td>เรื่อง</td>
            <td><input type="text" name="title" value="<?php echo $rows['title']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>สถานที่</td>
            <td><input type="text" name="location" value="<?php echo $rows['location']; ?>" class="form-control"></td>
          </tr>
          <tr>
            <td>จังหวัด</td>
            <td><input type="text" name="province" value="<?php echo $rows['province']; ?>" class="form-control"></td>
          </tr>
          <tr>
            <td colspan="2">
              <label>รายชื่อผู้เข้าร่วม</label>
              <textarea name="listname"><?php echo htmlspecialchars($rows['listname']); ?></textarea>
            </td>
          </tr>
          <tr>
            <td colspan="2">
              <?php 
              if(!empty($rows['files'])){
              ?>
                <a href="../../contents/edms2/<?php echo $rows['files']; ?>" target="_blank"><img src="../../contents/edms2/<?php echo $rows['files']; ?>" width="100"></a><p></p>
              <?php
              } 
              ?>
              <label>รูปภาพ</label>
              <input type="file" name="files" class="form-control" placeholder="ไฟล์เอกสาร">
            </td>
          </tr>
          <?php /* ?><tr>
            <td colspan="2">
              <label>รูปภาพ2</label>
              <input type="file" name="files2" class="form-control" placeholder="ไฟล์เอกสาร 2">
            </td>
          </tr>
          <tr>
            <td colspan="2">
              <label>รูปภาพ 3</label>
              <input type="file" name="files3" class="form-control" placeholder="ไฟล์เอกสาร 3">
            </td>
          </tr><?php */ ?>
          <tr>
            <td colspan="2">
              <label>สรุปเนื้อหา</label>
              <textarea name="description"><?php echo htmlspecialchars($rows['description']); ?></textarea>
            </td>
          </tr>
          <tr>
            <td colspan="2">
              <label>คาดว่าจะนำไปใช้ประโยชน์</label>
              <textarea name="description2"><?php echo htmlspecialchars($rows['description2']); ?></textarea>
            </td>
          </tr>
          <tr>
            <td colspan="2" align="right"><input type="submit" value="บันทึก" class="btn btn-default"></td>
          </tr>
        </tbody>
      </table>
      </form>
    </div>
  </div>
<?php
}

if($_GET['nu']=="users_job_description_i_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_users_job_description";

  if($_POST['createBy']==""){
    $_POST['createBy'] = $_SESSION['user'];
  }
  if($_POST['createByStatus']==""){
    $_POST['createByStatus'] = $_SESSION['apstatus'];
  }

  // $_POST['date_time'] = date('Y-m-d H:i:s');

  if($_FILES['files']['name']==''){
    $_POST['files'] = '';
  }else{
    if(in_array(substr(strrchr($_FILES['files']['name'],'.'),1),$filesAllowed)){
      $in = "../contents/edms2/";
      $namefile = $_POST['createBy']."-img-".date('Ymd').time();
      if(strlen($_FILES['files']['tmp_name'])!=0){
        move_uploaded_file($_FILES['files']['tmp_name'], "$in$namefile" . strrchr($_FILES['files']['name'], "."));
      }
      $_POST['files'] = $namefile.strrchr($_FILES['files']['name'], ".");
    }
  }

  if($_FILES['files2']['name']==''){
    $_POST['files2'] = '';
  }else{
    if(in_array(substr(strrchr($_FILES['files2']['name'],'.'),1),$filesAllowed)){
      $in = "../contents/edms2/";
      $namefile = $_POST['createBy']."-img2-".date('Ymd').time();
      if(strlen($_FILES['files2']['tmp_name'])!=0){
        move_uploaded_file($_FILES['files2']['tmp_name'], "$in$namefile" . strrchr($_FILES['files2']['name'], "."));
      }
      $_POST['files2'] = $namefile.strrchr($_FILES['files2']['name'], ".");
    }
  }

  if($_FILES['files3']['name']==''){
    $_POST['files3'] = '';
  }else{
    if(in_array(substr(strrchr($_FILES['files3']['name'],'.'),1),$filesAllowed)){
      $in = "../contents/edms2/";
      $namefile = $_POST['createBy']."-img3-".date('Ymd').time();
      if(strlen($_FILES['files3']['tmp_name'])!=0){
        move_uploaded_file($_FILES['files3']['tmp_name'], "$in$namefile" . strrchr($_FILES['files3']['name'], "."));
      }
      $_POST['files3'] = $namefile.strrchr($_FILES['files3']['name'], ".");
    }
  }

  $query = mysqli_query($conn,"select * from $table");
  $num_fields = mysqli_num_fields($query);
  for($i=1; $i<$num_fields; $i++) {
    $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
    $fields = $field_name[$i];

    if($fields!="description" && $fields!="description2"){
      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));
    }

    $fields_1 .= "$fields, ";
    $fields_2 .= "'".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
  }
  $field_1 = substr($fields_1,0,-2);
  $field_2 = substr($fields_2,0,-2);
  mysqli_query($conn,"insert into $table ($field_1) values ($field_2)");
  echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
  echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=users_job_description&code=$_POST[code]'>";
  echo "</div>";
}

if($_GET['nu']=="users_job_description_u_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_users_job_description";
  $field = "id";
  $field2 = $_POST['id'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2'"));
  if($nr<0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=users_job_description&action=users_job_description_i_u&$field=$field2&code=$_POST[code]'>";
  }else{
    if($_POST['createBy']==""){
      $_POST['createBy'] = $_SESSION['user'];
    }
    if($_POST['createByStatus']==""){
      $_POST['createByStatus'] = $_SESSION['apstatus'];
    }

    // $_POST['date_time'] = date('Y-m-d H:i:s');

    if($_FILES['files']['name']==''){
      $_POST['files'] = $_POST['files_'];
    }else{
      if(in_array(substr(strrchr($_FILES['files']['name'],'.'),1),$filesAllowed)){
        $in = "../contents/edms2/";
        unlink("$in$rows[files]");
        $namefile = $_POST['createBy']."-img-".date('Ymd').time();
        if(strlen($_FILES['files']['tmp_name'])!=0){
          move_uploaded_file($_FILES['files']['tmp_name'], "$in$namefile" . strrchr($_FILES['files']['name'], "."));
        }
        $_POST['files'] = $namefile.strrchr($_FILES['files']['name'], ".");
      }
    }

    if($_FILES['files2']['name']==''){
      $_POST['files2'] = $_POST['files2_'];
    }else{
      if(in_array(substr(strrchr($_FILES['files2']['name'],'.'),1),$filesAllowed)){
        $in = "../contents/edms2/";
        unlink("$in$rows[files2]");
        $namefile = $_POST['createBy']."-img2-".date('Ymd').time();
        if(strlen($_FILES['files2']['tmp_name'])!=0){
          move_uploaded_file($_FILES['files2']['tmp_name'], "$in$namefile" . strrchr($_FILES['files2']['name'], "."));
        }
        $_POST['files2'] = $namefile.strrchr($_FILES['files2']['name'], ".");
      }
    }

    if($_FILES['files3']['name']==''){
      $_POST['files3'] = $_POST['files3_'];
    }else{
      if(in_array(substr(strrchr($_FILES['files3']['name'],'.'),1),$filesAllowed)){
        $in = "../contents/edms2/";
        unlink("$in$rows[files3]");
        $namefile = $_POST['createBy']."-img3-".date('Ymd').time();
        if(strlen($_FILES['files3']['tmp_name'])!=0){
          move_uploaded_file($_FILES['files3']['tmp_name'], "$in$namefile" . strrchr($_FILES['files3']['name'], "."));
        }
        $_POST['files3'] = $namefile.strrchr($_FILES['files3']['name'], ".");
      }
    }

    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    $fields_ = '';
    for($i=1; $i<$num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      if($fields!="description" && $fields!="description2"){
        $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));
      }

      $fields_ .= "$fields='".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $fields_set = substr($fields_,0,-2);
    mysqli_query($conn,"update $table set $fields_set where $field='$field2' ");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=users_job_description&code=$_POST[code]'>";
  }
  echo "</div>";
}

if ($_GET['nu']=="users_job_description_delete") {
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_users_job_description";
  $table2 = "ap_edms2";

  $sql = "select * from $table2 where catalogCode=$_GET[id]";
  $query = mysqli_query($conn,$sql);
  while($rows = mysqli_fetch_array($query)){
    unlink("../contents/edms2/$rows[files]");
    unlink("../contents/edms2/$rows[files2]");
    unlink("../contents/edms2/$rows[files3]");
  }
  mysqli_query($conn,"delete from $table where id='$_GET[id]'");
  mysqli_query($conn,"delete from $table2 where catalogCode='$_GET[id]'");
  unlink("../contents/edms2/$_GET[id]");
  echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
  echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=users_job_description&code=$_GET[code]'>";
  echo "</div>";
}

if($_GET['nu']=="edms2"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }

  $sql = "select * from ap_users_job_description where id='$_GET[catalogCode]'";
  $query = mysqli_query($conn,$sql);
  $rows = mysqli_fetch_array($query);
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">เอกสารอิเล็กทรอนิกส์ - <?php echo $rows['title']; ?></h2>
    <div class="table-responsive">
      <table class="table table-striped">
        <thead>
          <tr>
            <th width="70">ลำดับ</th>
            <th>วันที่</th>
            <!-- <th>เรื่อง</th> -->
            <th>ชื่อเอกสาร</th>
            <th width="100"><a href="?nu=edms2_i_u&action=edms2_i_s&catalogCode=<?php echo $_GET['catalogCode']; ?>">เพิ่ม</a></th>
          </tr>
        </thead>
        <tbody>
          <?php
          if($_SESSION['apstatus']=="Admin"){
            $sql = "select * from ap_edms2 where catalogCode='$_GET[catalogCode]' order by id asc";
          }else{
            $sql = "select * from ap_edms2 where createBy='$_SESSION[user]' and catalogCode='$_GET[catalogCode]' order by id asc";
          }
          $query = mysqli_query($conn,$sql);
          $i = 0;
          while($rows = mysqli_fetch_array($query)){
          $i++;
            $sql2 = "select * from ap_users_job_description where createBy='$rows[createBy]'";
            $query2 = mysqli_query($conn,$sql2);
            $rows2 = mysqli_fetch_array($query2);
            ?>
            <tr>
              <td><?php echo $i;?></td>
              <td><?php echo $rows['date_time'];?></td>
              <!-- <td><?php echo $rows2['title'];?></td> -->
              <td><a href="../contents/edms2/<?php echo $rows['files'] ?>" target="_blank"><?php echo $rows['name'];?></a></td>
              <td><?php if($_SESSION['apstatus']=="Admin" || $rows['createBy']==$_SESSION['user']){?><a href="?nu=edms2_i_u&action=edms2_u_s&id=<?php echo $rows['id']?>&catalogCode=<?php echo $rows['catalogCode']?>">แก้ไข</a> :  <a href="#" onclick="if (confirm('ยืนยันการลบ?')) location.href='?nu=edms2_delete&id=<?php echo $rows['id']?>&file=<?php echo $rows['files']?>&catalogCode=<?php echo $rows['catalogCode']?>';">ลบ</a><?php } ?></td>
            </tr>
          <?php
          }
          ?>
        </tbody>
      </table>
    </div>
  </div>
<?php
}

if($_GET['nu']=="edms2_i_u"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  $sql = "select * from ap_edms2 where id='$_GET[id]'";
  $query = mysqli_query($conn,$sql);
  $rows = mysqli_fetch_array($query);
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">เอกสารอิเล็กทรอนิกส์</h2>
    <div class="table-responsive">
      <form action="?nu=<?php echo $_GET['action']?>" method="post" enctype="multipart/form-data">
      <input type="hidden" name="id" value="<?php echo $rows['id']?>">
      <input type="hidden" name="catalogCode" value="<?php echo $_GET['catalogCode']?>">
      <input type="hidden" name="createBy" value="<?php echo $rows['createBy']?>">
      <input type="hidden" name="createByStatus" value="<?php echo $rows['createByStatus']?>">
      <input type="hidden" name="counters" value="<?php echo $rows['counters']?>">
      <input type="hidden" name="files_" value="<?php echo $rows['files']?>">
      <table class="table table-striped">
        <tbody>
          <tr>
            <td>ชื่อเอกสาร</td>
            <td><input placeholder="ชื่อเอกสาร" type="text" name="name" value="<?php echo $rows['name']?>" class="form-control">
            </td>
          </tr>
          <tr>
            <td>ไฟล์เอกสาร</td>
            <td><input type="file" name="files" class="form-control" placeholder="ไฟล์เอกสาร"></td>
          </tr>
          <tr>
            <td colspan="2" align="right"><input type="submit" value="บันทึก" class="btn btn-default"></td>
          </tr>
        </tbody>
      </table>
      </form>
    </div>
  </div>
<?php
}

if($_GET['nu']=="edms2_i_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_edms2";

  if($_POST['createBy']==""){
    $_POST['createBy'] = $_SESSION['user'];
  }
  if($_POST['createByStatus']==""){
    $_POST['createByStatus'] = $_SESSION['apstatus'];
  }

  $_POST['date_time'] = date('Y-m-d H:i:s');

  if($_FILES['files']['name']==''){
    $_POST['files'] = "";
  }else{
    if(in_array(substr(strrchr($_FILES['files']['name'],'.'),1),$filesAllowed)){
      $in = "../contents/edms2/";
      $namefile = $_POST['createBy']."-".$_POST['catalogCode']."-".date('Ymd').time();
      if(strlen($_FILES['files']['tmp_name'])!=0){
        move_uploaded_file($_FILES['files']['tmp_name'], "$in$namefile" . strrchr($_FILES['files']['name'], "."));
      }
      $_POST['files'] = $namefile.strrchr($_FILES['files']['name'], ".");
    }
  }

  $query = mysqli_query($conn,"select * from $table");
  $num_fields = mysqli_num_fields($query);
  for($i=1; $i<$num_fields; $i++) {
    $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
    $fields = $field_name[$i];

    $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));

    $fields_1 .= "$fields, ";
    $fields_2 .= "'".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
  }
  $field_1 = substr($fields_1,0,-2);
  $field_2 = substr($fields_2,0,-2);
  mysqli_query($conn,"insert into $table ($field_1) values ($field_2)");
  echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
  echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=edms2&catalogCode=$_POST[catalogCode]'>";
  echo "</div>";
}

if($_GET['nu']=="edms2_u_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_edms2";
  $field = "id";
  $field2 = $_POST['id'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2'"));
  if($nr<0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=edms2&action=edms2_i_u&$field=$field2&catalogCode=$_POST[catalogCode]'>";
  }else{
    if($_POST['createBy']==""){
      $_POST['createBy'] = $_SESSION['user'];
    }
    if($_POST['createByStatus']==""){
      $_POST['createByStatus'] = $_SESSION['apstatus'];
    }

    $_POST['date_time'] = date('Y-m-d H:i:s');
  
    if($_FILES['files']['name']==''){
      $_POST['files'] = $_POST['files_'];
    }else{
      if(in_array(substr(strrchr($_FILES['files']['name'],'.'),1),$filesAllowed)){
        $in = "../contents/edms2/";
        unlink("$in$_POST[files_]");
        $namefile = $_POST['createBy']."-".$_POST['catalogCode']."-".date('Ymd').time();
        if(strlen($_FILES['files']['tmp_name'])!=0){
          move_uploaded_file($_FILES['files']['tmp_name'], "$in$namefile" . strrchr($_FILES['files']['name'], "."));
        }
        $_POST['files'] = $namefile.strrchr($_FILES['files']['name'], ".");
      }
    }

    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    $fields_ = '';
    for($i=1; $i<$num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));

      $fields_ .= "$fields='".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $fields_set = substr($fields_,0,-2);
    mysqli_query($conn,"update $table set $fields_set where $field='$field2' ");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=edms2&catalogCode=$_POST[catalogCode]'>";
  }
  echo "</div>";
}

if ($_GET['nu']=="edms2_delete") {
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_edms2";
  mysqli_query($conn,"delete from $table where id='$_GET[id]'");
  unlink("../contents/edms2/$_GET[file]");
  echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
  echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=edms2&catalogCode=$_GET[catalogCode]'>";
  echo "</div>";
}

if($_GET['nu']=="contacts"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">ระบบ ติดต่อเรา</h2>
    <div class="table-responsive">
      <table class="table table-striped">
        <thead>
          <tr>
            <th width="70">ลำดับ</th>
            <th>ชื่อ-นามสกุล/ฝ่ายงาน</th>
            <th>ตำแหน่ง</th>
            <th>หมายเลขโทรศัพท์</th>
            <th>หน่วยงาน/สังกัด</th>
            <th>สถานะ</th>
            <th width="100"><a href="?nu=contacts_i_u&action=contacts_i_s">เพิ่ม</a></th>
          </tr>
        </thead>
        <tbody>
          <?php
          $sql = "select * from ap_contacts order by status desc,location asc,level asc";
          $query = mysqli_query($conn,$sql);
          $i = 0;
          while($rows = mysqli_fetch_array($query)){
          $i++;
            ?>
            <tr>
              <td><?php echo $i;?></td>
              <td><a href="../ap/contacts/" target="_blank"><?php echo $rows['name'];?></a></td>
              <td><?php echo $rows['position'];?></td>
              <td><?php echo $rows['phone'];?></td>
              <td><?php echo $rows['department'];?></td>
              <td align="center"><?php if($rows['status']=="1"){ echo "/"; }?></td>
              <td><a href="?nu=contacts_i_u&action=contacts_u_s&id=<?php echo $rows['id']?>">แก้ไข</a> :  <a href="#" onclick="if (confirm('ยืนยันการลบ?')) location.href='?nu=contacts_delete&id=<?php echo $rows['id']?>';">ลบ</a></td>
            </tr>
          <?php
          }
          ?>
        </tbody>
      </table>
    </div>
  </div>
<?php
}

if($_GET['nu']=="contacts_i_u"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  $sql = "select * from ap_contacts where id='$_GET[id]'";
  $query = mysqli_query($conn,$sql);
  $rows = mysqli_fetch_array($query);
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">ระบบ ติดต่อเรา</h2>
    <div class="table-responsive">
      <form action="?nu=<?php echo $_GET['action']?>" method="post" enctype="multipart/form-data">
      <input type="hidden" name="id" value="<?php echo $rows['id']?>">
      <table class="table table-striped">
      <tbody>
        <tr>
          <td>สถานะ * 1=แสดง 0=ไม่แสดง</td>
          <td><input type="text" name="status" value="<?php echo $rows['status']?>" class="form-control"></td>
        </tr>
        <tr>
          <td>ตำแหน่งการจัดเรียงข้อมูล</td>
          <td><input type="text" name="location" value="<?php echo $rows['location']?>" class="form-control"></td>
        </tr>
        <tr>
          <td>ลำดับข้อมูล</td>
          <td><input type="text" name="level" value="<?php echo $rows['level']?>" class="form-control"></td>
        </tr>
        <tr>
          <td>ตำแหน่ง</td>
          <td><input type="text" name="position" size="30" value="<?php echo $rows['position']?>" class="form-control"></td>
        </tr>
        <tr>
          <td>ชื่อ-นามสกุล/ฝ่ายงาน</td>
          <td><input type="text" name="name" size="30" value="<?php echo $rows['name']?>" class="form-control"></td>
        </tr>
        <tr>
          <td>หมายเลขโทรศัพท์</td>
          <td><input type="text" name="phone" size="30" value="<?php echo $rows['phone']?>" class="form-control"></td>
        </tr>
        <tr>
          <td>หน่วยงาน/สังกัด</td>
          <td><input type="text" name="department" size="30" value="<?php echo $rows['department']?>" class="form-control"></td>
        </tr>
        <tr>
          <td colspan="2" align="right"><input type="submit" value="บันทึก" class="btn btn-default"></td>
        </tr>
      </tbody>
      </table>
      </form>
    </div>
  </div>
<?php
}

if($_GET['nu']=="contacts_i_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_contacts";
  $field = "phone";
  $field2 = $_POST['phone'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2'"));
  /*if($nr>0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=contacts&action=contacts_i_u&$field=$field2'>";
  }else{*/
    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    for($i=1; $i<$num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));

      $fields_1 .= "$fields, ";
      $fields_2 .= "'".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $field_1 = substr($fields_1,0,-2);
    $field_2 = substr($fields_2,0,-2);
    mysqli_query($conn,"insert into $table ($field_1) values ($field_2)");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=contacts'>";
  /*}*/
  echo "</div>";
}

if($_GET['nu']=="contacts_u_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_contacts";
  $field = "id";
  $field2 = $_POST['id'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2'"));
  if($nr<0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=contacts&action=contacts_i_u&$field=$field2'>";
  }else{
    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    $fields_ = '';
    for($i=1; $i<$num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));

      $fields_ .= "$fields='".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $fields_set = substr($fields_,0,-2);
    mysqli_query($conn,"update $table set $fields_set where $field='$field2' ");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=contacts'>";
  }
  echo "</div>";
}

if ($_GET['nu']=="contacts_delete") {
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_contacts";
  mysqli_query($conn,"delete from $table where id='$_GET[id]'");
  echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
  echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=contacts'>";
  echo "</div>";
}

if($_GET['nu']=="days_type"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">ประเภทปฏิทิน</h2>
    <div class="table-responsive">
      <table class="table table-striped">
        <thead>
          <tr>
            <th width="70">ลำดับ</th>
            <th>รหัส</th>
            <th>ประเภท</th>
            <th>รหัสสี</th>
            <th width="100"><a href="?nu=days_type_i_u&action=days_type_i_s">เพิ่ม</a></th>
          </tr>
        </thead>
        <tbody>
          <?php
          $sql = "select * from ap_days_type order by code asc";
          $query = mysqli_query($conn,$sql);
          $i = 0;
          while($rows = mysqli_fetch_array($query)){
          $i++;
            ?>
            <tr>
              <td><?php echo $i;?></td>
              <td><?php echo $rows['code'];?></td>
              <td><?php echo $rows['name'];?></td>
              <td><table><tr><td bgcolor="<?php echo $rows['color'];?>" width="10"></td><td>&nbsp;&nbsp;&nbsp;<?php echo $rows['color'];?></td></tr></table></td>
              <td><a href="?nu=days_type_i_u&action=days_type_u_s&id=<?php echo $rows['id']?>">แก้ไข</a> :  <a href="#" onclick="if (confirm('ยืนยันการลบ?')) location.href='?nu=days_type_delete&id=<?php echo $rows['id']?>';">ลบ</a></td>
            </tr>
          <?php
          }
          ?>
        </tbody>
      </table>
    </div>
  </div>
<?php
}

if($_GET['nu']=="days_type_i_u"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  $sql = "select * from ap_days_type where id='$_GET[id]'";
  $query = mysqli_query($conn,$sql);
  $rows = mysqli_fetch_array($query);
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">ประเภทปฏิทิน</h2>
    <div class="table-responsive">
      <form action="?nu=<?php echo $_GET['action']?>" method="post" enctype="multipart/form-data">
      <input type="hidden" name="id" value="<?php echo $rows['id']?>">
      <table class="table table-striped">
      <tbody>
        <tr>
          <td>รหัส</td>
          <td><input type="text" name="code" value="<?php echo $rows['code']?>" class="form-control"></td>
        </tr>
        <tr>
          <td>ชื่อ</td>
          <td><input type="text" name="name" size="30" value="<?php echo $rows['name']?>" class="form-control"></td>
        </tr>
        <tr>
          <td>รหัสสี</td>
          <td><input type="text" name="color" size="30" value="<?php echo $rows['color']?>" class="form-control"></td>
        </tr>
        <tr>
          <td colspan="2" align="right"><input type="submit" value="บันทึก" class="btn btn-default"></td>
        </tr>
      </tbody>
      </table>
      </form>
    </div>
  </div>
<?php
}

if($_GET['nu']=="days_type_i_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_days_type";
  $field = "code";
  $field2 = $_POST['code'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2'"));
  if($nr>0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=days_type&action=days_type_i_u&$field=$field2'>";
  }else{
    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    for($i=1; $i<$num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));

      $fields_1 .= "$fields, ";
      $fields_2 .= "'".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $field_1 = substr($fields_1,0,-2);
    $field_2 = substr($fields_2,0,-2);
    mysqli_query($conn,"insert into $table ($field_1) values ($field_2)");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=days_type'>";
  }
  echo "</div>";
}

if($_GET['nu']=="days_type_u_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_days_type";
  $field = "id";
  $field2 = $_POST['id'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2'"));
  if($nr<0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=days_type&action=days_type_i_u&$field=$field2'>";
  }else{
    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    $fields_ = '';
    for($i=1; $i<$num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));

      $fields_ .= "$fields='".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $fields_set = substr($fields_,0,-2);
    mysqli_query($conn,"update $table set $fields_set where $field='$field2' ");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=days_type'>";
  }
  echo "</div>";
}

if ($_GET['nu']=="days_type_delete") {
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_days_type";
  mysqli_query($conn,"delete from $table where id='$_GET[id]'");
  echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
  echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=days_type'>";
  echo "</div>";
}

if($_GET['nu']=="days"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <div class="row">
      <div class="col-lg-6 col-md-6 col-sm-12">
        <p class="h4">ระบบ ปฏิทินวันหยุด, ปฏิทินวันสำคัญ, ปฏิทินโครงการ/กิจกรรม</p>
      </div>
      <div class="col-lg-6 col-md-6 col-sm-12">
        <form class="form-inline text-right" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="GET">
          <input type="hidden" name="nu" value="<?php echo $_GET['nu'] ?>">
          <div class="form-group">
            กรุณาระบุ เดือน ต.ย. <?php echo date('Y-m'); ?>
            <input type="text" name="q" value="<?php if(empty($_GET['q'])){ echo date('Y-m'); }else{ echo $_GET['q']; } ?>" placeholder="" class="form-control" autofocus>
          </div>
          <button type="submit" class="btn btn-default">ค้นหา</button>
        </form>
      </div>
    </div>
    <hr>
    <div class="table-responsive">
      <table class="table table-striped">
        <thead>
          <tr>
            <th width="70">ลำดับ</th>
            <th>วันที่</th>
            <th>วัน</th>
            <th width="100"><a href="?nu=days_i_u&action=days_i_s">เพิ่ม</a></th>
          </tr>
        </thead>
        <tbody>
          <?php
          // $sql = "select * from ap_days order by apType asc,apYear desc,apMount asc,apDay asc";
          if(empty($_GET['q'])){
            // $sql = "select * from ap_days where apYear='".date('Y')."' and apMount='".date('m')."' order by apType asc,apYear desc,apMount asc,apDay asc";
            $sql = "select * from ap_days where apMount='".date('m')."' order by apType asc,apYear desc,apMount asc,apDay asc";
          }else{
            $d = explode("-", $_GET['q']);
            $sql = "select * from ap_days where apYear='$d[0]' and apMount='$d[1]' order by apType asc,apYear desc,apMount asc,apDay asc";
          }

          $query = mysqli_query($conn,$sql);
          $i = 0;
          while($rows = mysqli_fetch_array($query)){
            $sql2 = "select * from ap_days_type where code='$rows[apType]'";
            $query2 = mysqli_query($conn,$sql2);
            $rows2 = mysqli_fetch_array($query2);
            if($last!=$rows['apType']){
              $i = 0;
              ?>
              <tr>
                <td colspan="4"><?php echo $rows2['name']; ?></td>
              </tr>
            <?php
            }
            $last = $rows['apType'];

            $i++;

            if(empty($rows['apYear'])){
              $day = "แสดงในปฏิทินทุกปี ".$rows['apMount']."-".$rows['apDay'];
            }else{
              $day = $rows['apYear']."-".$rows['apMount']."-".$rows['apDay'];
            }
            ?>
            <tr>
              <td><?php echo $i;?>.</td>
              <td><?php echo $day;?></td>
              <td><a href="../ap/calendar/" target="_blank"><?php echo $rows['apTitle'];?></a></td>
              <td><a href="?nu=days_i_u&action=days_u_s&id=<?php echo $rows['id']?>">แก้ไข</a> :  <a href="#" onclick="if (confirm('ยืนยันการลบ?')) location.href='?nu=days_delete&id=<?php echo $rows['id']?>';">ลบ</a></td>
            </tr>
          <?php
          }
          ?>
        </tbody>
      </table>
    </div>
  </div>
<?php
}

if($_GET['nu']=="days_i_u"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  $sql = "select * from ap_days where id='$_GET[id]'";
  $query = mysqli_query($conn,$sql);
  $rows = mysqli_fetch_array($query);
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">ระบบ ปฏิทิน</h2>
    <div class="table-responsive">
      <form action="?nu=<?php echo $_GET['action']?>" method="post" enctype="multipart/form-data">
      <input type="hidden" name="id" value="<?php echo $rows['id']?>">
      <table class="table table-striped">
        <tbody>
          <tr>
            <td>ประเภท</td>
            <td>
              <select class="form-control" name="apType">
              <?php 
              $sql2 = "select * from ap_days_type order by code asc";
              $query2 = mysqli_query($conn,$sql2);
              $i = 0;
              while($rows2 = mysqli_fetch_array($query2)){
              $i++;
              ?>
                <option value="<?php echo $rows2['code']; ?>" <?php if($rows2['code']=="$rows[apType]"){ echo "selected"; } ?>><?php echo $rows2['code']." ".$rows2['name']; ?></option>
              <?php 
              } 
              ?>
              </select>
            </td>
          </tr>
          <tr>
            <td>วันที่ ปี เดือน วัน</td>
            <td>
              <div class="row">
                <div class="col-lg-3 col-md-3 col-sm-12">
                  <label for="">ต.ย. ปี ระบุ <?php echo date('Y'); ?> หรือ ไม่ระบุกรณีมีทุกๆ ปี</label>
                  <input placeholder="" type="text" name="apYear" value="<?php echo $rows['apYear']; ?>" class="form-control">
                </div>
                <div class="col-lg-3 col-md-3 col-sm-12">
                  <label for="">ต.ย. เดือน <?php echo dateThaiMonth(date('Y-m-d')); ?> ระบุ <?php echo date('m'); ?></label>
                  <input placeholder="" type="text" name="apMount" value="<?php echo $rows['apMount']; ?>" class="form-control" required>
                </div>
                <div class="col-lg-6 col-md-6 col-sm-12">
                  <label for="">ต.ย. ระบุวันที่ <?php echo date('d'); ?> หรือ ระบุ <?php echo date('d').','.date('d', strtotime(date('Y-m-d'). ' + 1 days')).','.date('d', strtotime(date('Y-m-d'). ' + 2 days')); ?> </label>
                  <input placeholder="" type="text" name="apDay" value="<?php echo $rows['apDay']; ?>" class="form-control" required>
                </div>
              </div>
            </td>
          </tr>
          <tr>
            <td>ชื่อ</td>
            <td><input type="text" name="apTitle" value="<?php echo $rows['apTitle']?>" class="form-control" required></td>
          </tr>
          <tr>
            <td colspan="2">
              <label>เนื้อหา</label>
              <textarea name="apInfo"><?php echo htmlspecialchars($rows['apInfo']); ?></textarea>
            </td>
          </tr>
          <tr>
            <td>รูปภาพ</td>
            <td>
              <div class="input-group">
                <input type="text" id="apImages" name="apImages" value="<?php echo $rows['apImages']?>" class="form-control" placeholder="รูปภาพประกอบ">
                <span class="input-group-btn">
                <button href="tinymce/plugins/filemanager/dialog.php?type=1&editor=mce_0&lang=eng&fldr=&field_id=apImages&popup=1" class="btn btn-default iframe-btn" type="button">SELECT</button>
                </span>
              </div>
            </td>
          </tr>
          <tr>
            <td>URL ข้อมูลประกอบ</td>
            <td>
              <div class="input-group">
                <input type="text" id="apURL" name="apURL" value="<?php echo $rows['apURL']?>" class="form-control" placeholder="">
                <span class="input-group-btn">
                <button href="tinymce/plugins/filemanager/dialog.php?type=0&editor=mce_0&lang=eng&fldr=&field_id=apURL&popup=1" class="btn btn-default iframe-btn" type="button">SELECT</button>
                </span>
              </div>
            </td>
          </tr>
          <tr>
            <td colspan="2"><strong>สำหรับ แสดงหน้าหลักเว็บไชต์</strong></td>
          </tr>
          <tr>
            <td>วันที่เริ่มต้นและวันที่สุดท้ายของการแสดงหน้าเว็บ กรณีแสดงทุกปีให้ระบุ 0000 แทน ปี</td>
            <td>
              <div class="row">
                <div class="col-lg-3 col-md-3 col-sm-12">
                  <label for="">ต.ย. <?php echo date('Ymd'); ?></label>
                  <input placeholder="" type="text" name="apDateS" value="<?php echo $rows['apDateS']; ?>" class="form-control">
                </div>
                <div class="col-lg-3 col-md-3 col-sm-12">
                  <label for="">ต.ย. <?php echo date('Ymd'); ?></label>
                  <input placeholder="" type="text" name="apDateE" value="<?php echo $rows['apDateE']; ?>" class="form-control">
                </div>
              </div>
            </td>
          </tr>
          <tr>
            <td>รูปภาพ</td>
            <td>
              <div class="input-group">
                <input type="text" id="apImage" name="apImage" value="<?php echo $rows['apImage']?>" class="form-control" placeholder="">
                <span class="input-group-btn">
                <button href="tinymce/plugins/filemanager/dialog.php?type=1&editor=mce_0&lang=eng&fldr=&field_id=apImage&popup=1" class="btn btn-default iframe-btn" type="button">SELECT</button>
                </span>
              </div>
            </td>
          </tr>
          <tr>
            <td>พื้นหลัง</td>
            <td>
              <div class="input-group">
                <input type="text" id="apImage_bg" name="apImage_bg" value="<?php echo $rows['apImage_bg']?>" class="form-control" placeholder="">
                <span class="input-group-btn">
                <button href="tinymce/plugins/filemanager/dialog.php?type=2&editor=mce_0&lang=eng&fldr=&field_id=apImage_bg&popup=1" class="btn btn-default iframe-btn" type="button">SELECT</button>
                </span>
              </div>
            </td>
          </tr>
          <tr>
            <td>ข้อความบรรทัดที่ 1 ต.ย. ๒๘ กรกฏาคม</td>
            <td><input type="text" name="apText_1" value="<?php echo $rows['apText_1']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>ข้อความบรรทัดที่ 2 ต.ย. วันคล้ายวันเฉลิมพระชนมพรรษา,พระบาทสมเด็จพระปรเมนทรรามาธิบดีศรีสินทรมหาวชิราลงกรณ พระวชิรเกล้าเจ้าอยู่หัว</td>
            <td><input type="text" name="apText_2" value="<?php echo $rows['apText_2']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>ข้อความบรรทัดที่ 3 ต.ย. ขอพระองค์ทรงพระเจริญ</td>
            <td><input type="text" name="apText_3" value="<?php echo $rows['apText_3']?>" class="form-control"></td>
          </tr>
          <tr>
            <td>ข้อความบรรทัดที่ 4 ต.ย. ข้าพระพุทธเจ้า ผู้บริหาร และบุคลากร,<?php echo $title; ?></td>
            <td><input type="text" name="apText_4" value="<?php echo $rows['apText_4']?>" class="form-control"></td>
          </tr>
          <tr>
            <td colspan="2" align="right"><input type="submit" value="บันทึก" class="btn btn-default"></td>
          </tr>
        </tbody>
      </table>
      </form>
    </div>
  </div>
<?php
}

if($_GET['nu']=="days_i_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_days";

  if($_POST['users']==""){
    $_POST['users'] = $_SESSION['user'];
  }
  $_POST['dateTime'] = date('Y-m-d H:i:s');

  $query = mysqli_query($conn,"select * from $table");
  $num_fields = mysqli_num_fields($query);
  for($i=1; $i<$num_fields; $i++) {
    $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
    $fields = $field_name[$i];

    if($fields!="apInfo"){
      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));
    }

    $fields_1 .= "$fields, ";
    $fields_2 .= "'".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
  }
  $field_1 = substr($fields_1,0,-2);
  $field_2 = substr($fields_2,0,-2);
  mysqli_query($conn,"insert into $table ($field_1) values ($field_2)");
  echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
  echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=days'>";
  echo "</div>";
}

if($_GET['nu']=="days_u_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_days";
  $field = "id";
  $field2 = $_POST['id'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2'"));
  if($nr<0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=days&action=days_i_u&$field=$field2'>";
  }else{

    if($_POST['users']==""){
      $_POST['users'] = $_SESSION['user'];
    }
    $_POST['dateTime'] = date('Y-m-d H:i:s');

    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    $fields_ = '';
    for($i=1; $i<$num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      if($fields!="apInfo"){
        $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));
      }

      $fields_ .= "$fields='".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $fields_set = substr($fields_,0,-2);
    mysqli_query($conn,"update $table set $fields_set where $field='$field2' ");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=days'>";
  }
  echo "</div>";
}

if ($_GET['nu']=="days_delete") {
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_days";
  mysqli_query($conn,"delete from $table where id='$_GET[id]'");
  echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
  echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=days'>";
  echo "</div>";
}

if($_GET['nu']=="maps"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">แผนที่</h2>
    <div class="table-responsive">
      <table class="table table-striped">
        <thead>
          <tr>
            <th width="70">ลำดับ</th>
            <th>รหัส</th>
            <th>ชื่อ</th>
            <th>รูปภาพ</th>
            <th>Website</th>
            <th>Latitude</th>
            <th>Longitude</th>
            <th width="100"><a href="?nu=maps_i_u&action=maps_i_s">เพิ่ม</a></th>
          </tr>
        </thead>
        <tbody>
          <?php
          $sql = "select *,CONVERT(code,UNSIGNED INTEGER) as code2 from ap_maps order by code2 asc";
          $query = mysqli_query($conn,$sql);
          $i = 0;
          while($rows = mysqli_fetch_array($query)){
          $i++;
            ?>
            <tr>
              <td><?php echo $i;?></td>
              <td><?php echo $rows['code'];?></td>
              <td><a href="../ap/maps/" target="_blank"><?php echo $rows['name'];?></a></td>
              <td><?php if(!empty($rows['images'])){ ?><a href="<?php echo $rows['images'] ?>" target="_blank">คลิก!</a><?php }else{ echo ""; } ?></td>
              <td><?php if(!empty($rows['url'])){ ?><a href="<?php echo $rows['url'] ?>" target="_blank">คลิก!</a><?php }else{ echo ""; } ?></td>
              <td><?php echo $rows['lat'];?></td>
              <td><?php echo $rows['lng'];?></td>
              <td><a href="?nu=maps_i_u&action=maps_u_s&id=<?php echo $rows['id']?>">แก้ไข</a> :  <a href="#" onclick="if (confirm('ยืนยันการลบ?')) location.href='?nu=maps_delete&id=<?php echo $rows['id']?>';">ลบ</a></td>
            </tr>
          <?php
          }
          ?>
        </tbody>
      </table>
    </div>
  </div>
<?php
}

if($_GET['nu']=="maps_i_u"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  $sql = "select * from ap_maps where id='$_GET[id]'";
  $query = mysqli_query($conn,$sql);
  $rows = mysqli_fetch_array($query);
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">แผนที่</h2>
    <div class="table-responsive">
      <form action="?nu=<?php echo $_GET['action']?>" method="post" enctype="multipart/form-data">
      <input type="hidden" name="id" value="<?php echo $rows['id']?>">
      <table class="table table-striped">
      <tbody>
        <tr>
          <td>รหัส</td>
          <td><input type="text" name="code" value="<?php echo $rows['code']?>" class="form-control" required></td>
        </tr>
        <tr>
          <td>ชื่อ</td>
          <td><input type="text" name="name" size="30" value="<?php echo $rows['name']?>" class="form-control" required></td>
        </tr>
        <tr>
          <td>รูปภาพ</td>
          <td>
            <div class="input-group">
              <input type="text" id="images" name="images" value="<?php echo $rows['images']?>" class="form-control" placeholder="รูปภาพประกอบ">
              <span class="input-group-btn">
              <button href="tinymce/plugins/filemanager/dialog.php?type=1&editor=mce_0&lang=eng&fldr=&field_id=images&popup=1" class="btn btn-default iframe-btn" type="button">SELECT</button>
              </span>
            </div>
          </td>
        </tr>
        <tr>
          <td>Website</td>
          <td><input type="text" name="url" size="30" value="<?php echo $rows['url']?>" class="form-control"></td>
        </tr>
        <tr>
          <td>Latitude</td>
          <td><input type="text" id="lat" name="lat" size="30" value="<?php echo $rows['lat']?>" class="form-control" required></td>
        </tr>
        <tr>
          <td>Longitude</td>
          <td><input type="text" id="lng" name="lng" size="30" value="<?php echo $rows['lng']?>" class="form-control" required></td>
        </tr>
        <tr>
          <td colspan="2" align="right"><input type="submit" value="บันทึก" class="btn btn-default"></td>
        </tr>
      </tbody>
      </table>
      </form>

      <div id="map" style="width: 100%; height: 600px;"></div>
      <script>
      function geocodePosition(pos) {
        geocoder.geocode({
          latLng: pos
        }, function(responses) {
          if (responses && responses.length > 0) {
            updateMarkerAddress(responses[0].formatted_address);
          } else {
            updateMarkerAddress('Cannot determine address at this location.');
          }
        });
      }

      function updateMarkerStatus(str) {
        //document.getElementById('markerStatus').innerHTML = str;
      }

      function updateMarkerPosition(latLng) {
        document.getElementById('info').innerHTML = [
          latLng.lat(),
          latLng.lng()
        ].join(', ');
        document.getElementById('lat').value = latLng.lat();
        document.getElementById('lng').value = latLng.lng();
      }

      function updateMarkerAddress(str) {
        document.getElementById('address2').innerHTML = str;
      }

      var geocoder;
      var map;

      function initMap() {
        geocoder = new google.maps.Geocoder();
        <?php if(empty($rows['lat']) && empty($rows['lng'])){ ?>
        var latlng = new google.maps.LatLng(<?php echo $_COOKIE['lat']?>,<?php echo $_COOKIE['lng']?>);
        <?php }else{ ?>
        var latlng = new google.maps.LatLng(<?php echo $rows['lat'] ?>, <?php echo $rows['lng'] ?>);
        <?php } ?>
        var myOptions = {
          zoom: 8,
          center: latlng,
          mapTypeId: google.maps.MapTypeId.HYBRID
        }
        map = new google.maps.Map(document.getElementById("map"), myOptions);

        var input = document.getElementById('address');
        var searchBox = new google.maps.places.SearchBox(input);
        map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);

        map.addListener('bounds_changed', function() {
          searchBox.setBounds(map.getBounds());
        });

        var markers = [];
        searchBox.addListener('places_changed', function() {
          var places = searchBox.getPlaces();

          if (places.length == 0) {
            return;
          }

          markers.forEach(function(marker) {
            marker.setMap(null);
          });
          markers = [];

          var bounds = new google.maps.LatLngBounds();
          places.forEach(function(place) {
            if (!place.geometry) {
              console.log("Returned place contains no geometry");
              return;
            }
            var icon = {
              url: place.icon,
              size: new google.maps.Size(71, 71),
              origin: new google.maps.Point(0, 0),
              anchor: new google.maps.Point(17, 34),
              scaledSize: new google.maps.Size(25, 25)
            };

            markers.push(new google.maps.Marker({
              map: map,
              icon: icon,
              title: place.name,
              position: place.geometry.location
            }));

            if (place.geometry.viewport) {
              bounds.union(place.geometry.viewport);
            } else {
              bounds.extend(place.geometry.location);
            }
          });
          map.fitBounds(bounds);
        });
      }

      function codeAddress() {
        var address = document.getElementById("address").value;
        geocoder.geocode( { 'address': address}, function(results, status) {
          if (status == google.maps.GeocoderStatus.OK) {
            map.setCenter(results[0].geometry.location);
            var marker = new google.maps.Marker({
              map: map,
              draggable: true,
              position: results[0].geometry.location,
            });
            
            updateMarkerPosition(results[0].geometry.location);
            geocodePosition(results[0].geometry.location);

            google.maps.event.addListener(marker, 'dragstart', function() {
              updateMarkerAddress('กำลังกำหนดพิกัด');
            });

            google.maps.event.addListener(marker, 'drag', function() {
              updateMarkerStatus('กำลังกำหนดพิกัด');
              updateMarkerPosition(marker.getPosition());
            });

            google.maps.event.addListener(marker, 'dragend', function() {
              updateMarkerStatus('กำหนดพิกัดเรียบร้อย');
              geocodePosition(marker.getPosition());
            });

          } else {
            /*alert("Geocode was not successful for the following reason: " + status);*/
          }
        });
      }

      <?php if(!empty($rows['lat']) && !empty($rows['lng'])){ ?>

      <?php } ?>
      </script>
      <script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCFfrtIXXeUtabOdKthKxVy6R5pl7IXRQ0&libraries=places&callback=initMap"></script>

      <div class="md-form pt-3">
        <input type="text" name="address" value="<?php if(!empty($rows['lat']) && !empty($rows['lng'])){ echo "$rows[lat], $rows[lng]"; } ?>" id="address" class="form-control" style="width: 450px;margin-top: 12px" placeholder="ค้นหา ชื่อสถานที่">
        <div class="input-group-append">
          <p></p><input type="button" value="แสดงตำแหน่ง หรือ ปรับแต่งพิกัด" id="Geocode" onclick="codeAddress()" class="btn btn-primary">
        </div>
        <div class="pt-1" id="markerStatus"></div>
        <p><div id="info"></div></p>
        <p><div id="address2"></div></p>
      </div>
      
    </div>
  </div>
<?php
}

if($_GET['nu']=="maps_i_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_maps";
  $field = "code";
  $field2 = $_POST['code'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2'"));
  if($nr>0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=maps&action=maps_i_u&$field=$field2'>";
  }else{

    $_POST['users'] = $_SESSION['user'];
    $_POST['dateTime'] = date('Y-m-d H:i:s');

    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    for($i=1; $i<$num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));

      $fields_1 .= "$fields, ";
      $fields_2 .= "'".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $field_1 = substr($fields_1,0,-2);
    $field_2 = substr($fields_2,0,-2);
    mysqli_query($conn,"insert into $table ($field_1) values ($field_2)");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=maps'>";
  }
  echo "</div>";
}

if($_GET['nu']=="maps_u_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_maps";
  $field = "id";
  $field2 = $_POST['id'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2'"));
  if($nr<0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=maps&action=maps_i_u&$field=$field2'>";
  }else{

    $_POST['users'] = $_SESSION['user'];
    $_POST['dateTime'] = date('Y-m-d H:i:s');

    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    $fields_ = '';
    for($i=1; $i<$num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));
      
      $fields_ .= "$fields='".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $fields_set = substr($fields_,0,-2);
    mysqli_query($conn,"update $table set $fields_set where $field='$field2' ");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=maps'>";
  }
  echo "</div>";
}

if ($_GET['nu']=="maps_delete") {
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_maps";
  mysqli_query($conn,"delete from $table where id='$_GET[id]'");
  echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
  echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=maps'>";
  echo "</div>";
}

if($_GET['nu']=="certificate"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">ระบบ เกียรติบัตร</h2>
    <div class="table-responsive">
      <table class="table table-striped">
        <thead>
          <tr>
            <th width="70">ลำดับ</th>
            <th>รหัส</th>
            <th>ชื่อ</th>
            <th>รูปแบบตัวอักษร</th>
            <th>รูปภาพ</th>
            <th>ข้อมูลเกียรติบัตร</th>
            <th>จำนวน</th>
            <th width="100"><a href="?nu=certificate_i_u&action=certificate_i_s">เพิ่ม</a></th>
          </tr>
        </thead>
        <tbody>
          <?php
          $sql = "select * from ap_certificate order by code asc";
          $query = mysqli_query($conn,$sql);
          $i = 0;
          while($rows = mysqli_fetch_array($query)){
          $i++;
            $sql2 = "select count(*) as total from ap_certificate_users where codeCertificate='$rows[code]'";
            $query2 = mysqli_query($conn,$sql2);
            $rows2 = mysqli_fetch_array($query2);
            ?>
            <tr>
              <td><?php echo $i;?></td>
              <td><?php echo $rows['code'];?></td>
              <td><a href="../ap/certificate/?codeCertificate=<?php echo $rows['code']; ?>" target="_blank"><?php echo $rows['name'];?></a></td>
              <td><?php echo $rows['fonts'];?></td>
              <td><?php if(!empty($rows['files'])){ ?> <a href="<?php echo "../contents/certificate/$rows[code]/$rows[files]"; ?>" target="_blank"><img src="<?php echo "../contents/certificate/$rows[code]/$rows[files]"; ?>" width="80px" alt="<?php echo $rows['name']; ?>"></a><?php } ?></td>
              <td><a href="?nu=certificate_users&codeCertificate=<?php echo "$rows[code]"; ?>">คลิก!!</a></td>
              <td><?php echo number_format($rows2['total']); ?></td>
              <td><a href="?nu=certificate_i_u&action=certificate_u_s&id=<?php echo $rows['id']?>">แก้ไข</a> :  <a href="#" onclick="if (confirm('ยืนยันการลบ?')) location.href='?nu=certificate_delete&id=<?php echo $rows['id']; ?>&files=<?php echo $rows['files']; ?>';">ลบ</a></td>
            </tr>
          <?php
          }
          ?>
        </tbody>
      </table>
    </div>
  </div>
<?php
}

if($_GET['nu']=="certificate_i_u"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  $sql = "select * from ap_certificate where id='$_GET[id]'";
  $query = mysqli_query($conn,$sql);
  $rows = mysqli_fetch_array($query);
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">ระบบ เกียรติบัตร</h2>
    <div class="table-responsive">
      <form action="?nu=<?php echo $_GET['action']?>" method="post" enctype="multipart/form-data">
      <input type="hidden" name="id" value="<?php echo $rows['id']?>">
      <input type="hidden" name="files_" value="<?php echo $rows['files']?>">
      <table class="table table-striped">
      <tbody>
        <tr>
          <td>รหัส</td>
          <td><input type="text" name="code" value="<?php echo $rows['code']; ?>" class="form-control"></td>
        </tr>
        <tr>
          <td>ชื่อ</td>
          <td><input type="text" name="name" size="30" value="<?php echo $rows['name']; ?>" class="form-control"></td>
        </tr>
        <tr>
          <td>รูปแบบตัวอังษร (THNiramit, Simsun)</td>
          <td><input type="text" name="fonts" size="30" value="<?php echo $rows['fonts']; ?>" class="form-control"></td>
        </tr>
        <tr>
          <td>รูปภาพ (WxH 2000x1414) <?php if(!empty($rows["files"])){ ?>[<a href="../contents/certificate/<?php echo "$rows[code]"; ?>/<?php echo $rows['files']; ?>" target="_blank">ตรวจรูปภาพ</a>]<?php } ?></td>
          <td><input type="file" class="form-control" id="files" name="files"></td>
        </tr>
        <tr>
          <td colspan="2" align="right"><input type="submit" value="บันทึก" class="btn btn-default"></td>
        </tr>
      </tbody>
      </table>
      </form>
    </div>
  </div>
<?php
}

if($_GET['nu']=="certificate_i_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_certificate";
  $field = "code";
  $field2 = $_POST['code'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2'"));
  if($nr>0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=certificate&action=certificate_i_u&$field=$field2'>";
  }else{
    if(!file_exists("../contents/certificate/$_POST[code]/")) {
      mkdir("../contents/certificate/$_POST[code]/", 0777, true);
    }

    if($_FILES['files']['name']==""){
      $_POST['files'] = "";
    }else{
      $in = "../contents/certificate/$_POST[code]/";
      $namefile = $_POST['code']."-".date('Ymd').time();
      if(strlen($_FILES['files']['tmp_name'])!=0){
        move_uploaded_file($_FILES['files']['tmp_name'], "$in$namefile" . strrchr($_FILES['files']['name'], "."));
      }
      $_POST['files'] = $namefile.strrchr($_FILES['files']['name'], ".");
    }

    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    for($i=1; $i<$num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));

      $fields_1 .= "$fields, ";
      $fields_2 .= "'".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $field_1 = substr($fields_1,0,-2);
    $field_2 = substr($fields_2,0,-2);
    mysqli_query($conn,"insert into $table ($field_1) values ($field_2)");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=certificate'>";
  }
  echo "</div>";
}

if($_GET['nu']=="certificate_u_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_certificate";
  $field = "id";
  $field2 = $_POST['id'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2'"));
  if($nr<0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=certificate&action=certificate_i_u&$field=$field2'>";
  }else{
    if(!file_exists("../contents/certificate/$_POST[code]/")) {
      mkdir("../contents/certificate/$_POST[code]/", 0777, true);
    }

    if($_FILES['files']['name']==""){
      $_POST['files'] = $_POST['files_'];
    }else{
      unlink("../contents/certificate/$_POST[code]/$_POST[files_]");

      $in = "../contents/certificate/$_POST[code]/";
      $namefile = $_POST['code']."-".date('Ymd').time();
      if(strlen($_FILES['files']['tmp_name'])!=0){
        move_uploaded_file($_FILES['files']['tmp_name'], "$in$namefile" . strrchr($_FILES['files']['name'], "."));
      }
      $_POST['files'] = $namefile.strrchr($_FILES['files']['name'], ".");
    }

    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    $fields_ = '';
    for($i=1; $i<$num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));

      $fields_ .= "$fields='".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $fields_set = substr($fields_,0,-2);
    mysqli_query($conn,"update $table set $fields_set where $field='$field2' ");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=certificate'>";
  }
  echo "</div>";
}

if ($_GET['nu']=="certificate_delete") {
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  echo "<div class=\"content\">";
  $table = "ap_certificate";
  mysqli_query($conn,"delete from $table where id='$_GET[id]'");
  unlink("../contents/certificate/$_GET[code]/$_GET[files]");
  echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
  echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=certificate'>";
  echo "</div>";
}

if($_GET['nu']=="certificate_users"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }

  $sql = "select * from ap_certificate where code='$_GET[codeCertificate]'";
  $query = mysqli_query($conn,$sql);
  $rows = mysqli_fetch_array($query);
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <div class="row">
      <div class="col-lg-6 col-md-6 col-sm-12">
        <p class="h4">ข้อมูลเกียรติบัตร : <a href="?nu=certificate"><u><?php echo $rows['name']; ?></u></a></p>
      </div>
      <div class="col-lg-6 col-md-6 col-sm-12">
        <form class="form-inline text-right" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="GET">
          <input type="hidden" name="nu" value="<?php echo $_GET['nu']; ?>">
          <input type="hidden" name="codeCertificate" value="<?php echo $_GET['codeCertificate']; ?>">
          <div class="form-group">
            กรุณาระบุ ชื่อ หรือ นามสกุล 
            <input type="text" name="q" value="<?php if(!empty($_GET['q'])){ echo $_GET['q']; } ?>" placeholder="" class="form-control" autofocus>
          </div>
          <button type="submit" class="btn btn-default">ค้นหา</button>
        </form>
      </div>
    </div>
    <hr>
    <div class="table-responsive">
      <table class="table table-striped">
        <thead>
          <tr>
            <th width="70">ลำดับ</th>
            <th>รหัส</th>
            <th>ชื่อ-นามสกุล</th>
            <th width="100"><a href="?nu=certificate_users_i_u&action=certificate_users_i_s&codeCertificate=<?php echo $_GET['codeCertificate']; ?>">เพิ่ม</a></th>
          </tr>
        </thead>
        <tbody>
          <?php
          if(empty($_GET['q'])){
            $sql = "select * from ap_certificate_users where codeCertificate='$_GET[codeCertificate]' order by code asc, fullname asc";
          }else{
            $sql = "select * from ap_certificate_users where codeCertificate='$_GET[codeCertificate]' and fullname like '%$_GET[q]%' order by code asc, fullname asc";
          }
          $query = mysqli_query($conn,$sql);
          $i = 0;
          while($rows = mysqli_fetch_array($query)){
          $i++;
          ?>
            <tr>
              <td><?php echo $i; ?>.</td>
              <td><?php echo $rows['code']; ?></td>
              <td><a href="../ap/certificate/?nu=certificate&codeCertificate=<?php echo $_GET['codeCertificate']; ?>&code=<?php echo $rows['code']; ?>" target="_blank"><?php echo $rows['fullname']; ?></a></td>
              <td><a href="?nu=certificate_users_i_u&action=certificate_users_u_s&id=<?php echo $rows['id']; ?>&codeCertificate=<?php echo $_GET['codeCertificate']; ?>">แก้ไข</a> :  <a href="#" onclick="if (confirm('ยืนยันการลบ?')) location.href='?nu=certificate_users_delete&id=<?php echo $rows['id']; ?>&codeCertificate=<?php echo $_GET['codeCertificate']; ?>';">ลบ</a></td>
            </tr>
          <?php
          }
          ?>
        </tbody>
      </table>
    </div>
  </div>
<?php
}

if($_GET['nu']=="certificate_users_i_u"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  
  $sql = "select * from ap_certificate_users where id='$_GET[id]'";
  $query = mysqli_query($conn,$sql);
  $rows = mysqli_fetch_array($query);
  ?>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">ข้อมูลเกียรติบัตร</h2>
    <div class="table-responsive">
      <form action="?nu=<?php echo $_GET['action']; ?>" method="post" enctype="multipart/form-data">
      <input type="hidden" name="codeCertificate" value="<?php echo $_GET['codeCertificate']; ?>">
      <input type="hidden" name="id" value="<?php echo $rows['id']; ?>">
      <table class="table table-striped">
        <tbody>
          <tr>
            <td>รหัส</td>
            <td><input type="text" name="code" value="<?php echo $rows['code']; ?>" class="form-control" required></td>
          </tr>
          <tr>
            <td>ชื่อ-นามสกุล</td>
            <td><input type="text" name="fullname" value="<?php echo $rows['fullname']; ?>" class="form-control" required></td>
          </tr>
          <tr>
            <td colspan="2" align="right"><input type="submit" value="บันทึก" class="btn btn-default"></td>
          </tr>
        </tbody>
      </table>
      </form>
    </div>
  </div>
<?php
}

if($_GET['nu']=="certificate_users_i_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  
  echo "<div class=\"content\">";
  $table = "ap_certificate_users";

  $_POST['dateTime'] = date('Y-m-d H:i:s');

  $query = mysqli_query($conn,"select * from $table");
  $num_fields = mysqli_num_fields($query);
  for($i=1; $i<$num_fields; $i++) {
    $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
    $fields = $field_name[$i];
    
    $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));

    $fields_1 .= "$fields, ";
    $fields_2 .= "'".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
  }
  $field_1 = substr($fields_1,0,-2);
  $field_2 = substr($fields_2,0,-2);
  mysqli_query($conn,"insert into $table ($field_1) values ($field_2)");
  echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
  echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=certificate_users&codeCertificate=$_POST[codeCertificate]'>";
  echo "</div>";
}

if($_GET['nu']=="certificate_users_u_s"){
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }
  
  echo "<div class=\"content\">";
  $table = "ap_certificate_users";
  $field = "id";
  $field2 = $_POST['id'];
  $nr = mysqli_num_rows(mysqli_query($conn,"select * from $table where $field='$field2'"));
  if($nr<0){
    echo "<center><b>ไม่สามารถบันทึกข้อมูลได้</b></center>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=certificate_users&action=certificate_users_i_u&$field=$field2&codeCertificate=$_POST[codeCertificate]'>";
  }else{

    if($_POST['users']==""){
      $_POST['users'] = $_SESSION['user'];
    }
    $_POST['dateTime'] = date('Y-m-d H:i:s');

    $query = mysqli_query($conn,"select * from $table");
    $num_fields = mysqli_num_fields($query);
    $fields_ = '';
    for($i=1; $i<$num_fields; $i++) {
      $field_name[$i] = mysqli_fetch_field_direct($query,$i)->name;
      $fields = $field_name[$i];

      $_POST[$fields] = htmlspecialchars(strip_tags(trim($_POST[$fields])));

      $fields_ .= "$fields='".mysqli_real_escape_string($conn,$_POST[$fields])."', ";
    }
    $fields_set = substr($fields_,0,-2);
    mysqli_query($conn,"update $table set $fields_set where $field='$field2' ");
    echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
    echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=certificate_users&codeCertificate=$_POST[codeCertificate]'>";
  }
  echo "</div>";
}

if ($_GET['nu']=="certificate_users_delete") {
  login_($_SESSION['id'],$_SESSION['user'],$_SESSION['password']);
  if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("location:index.php?nu=logout");
    exit;
  }

  echo "<div class=\"content\">";
  $table = "ap_certificate_users";
  mysqli_query($conn,"delete from $table where id='$_GET[id]'");
  echo "<br><CENTER><B>กรุณารอสักครู่</B></CENTER>";
  echo "<meta http-equiv='refresh' content='1;URL=index.php?nu=certificate_users&codeCertificate=$_GET[codeCertificate]'>";
  echo "</div>";
}

mysqli_close($conn);
// }

include('foot.php');
?>