<?php
include_once("../include/main.inc.php");
check_session('MyCpanel','index.php');

################ Paging Starts From Here ##################
@extract($_REQUEST);
$start = (intval($start)==0 or $start=="")?$start=0:$start;
$pagesize = intval($pagesize)==0?$pagesize=100:$pagesize;
if($parentID=="") {
	$parentID=1;
}
$heading="Manage All pages Meta tags";
$record_type="static";
$script_name="manage_allpagesmeta.php";
$table_name="wps_meta_tags";
// getCount($table_name, " where entity_name='static'");
// if($id=="add_record") {
//     $entity_id= getCount($table_name, " where entity_name='static'")+1;
//     $numCnt = getCount($table_name, " where page_url='$page_url'");
//     if ($numCnt > 0) {
//       $_SESSION['session_message'] = '<div class="btn btn-warning">' . $record_type . ' already exist  </div>';
//     } else {

//     $sql=db_query("insert into $table_name set page_url='$page_url',page_heading='$page_name',meta_description='$meta_description',meta_keyword='$meta_keyword',meta_title='$meta_title',entity_type='index/static',
// entity_name='static',entity_id='$entity_id'");
    
// 		$_SESSION['session_message']='<div class="btn btn-success">New '.$record_type.' has been added successfully</div>';
// 		header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
// 		exit;

// 	}
// }

  
if($id=="update_record") {
    $numCnt = getCount($table_name, " where page_url='$page_url' and meta_id!='$recordID'");
  if ($numCnt > 0) {
    $_SESSION['session_message'] = '<div class="btn btn-warning">' . $record_type . ' already exist  </div>';
    header("Location: " . SITE_ADMIN_URL . "/" . $script_name);
    exit;
  }else{

  
		updateTable($table_name, "meta_description='$meta_description',meta_keyword='$meta_keyword',meta_title='$meta_title' where meta_id='$recordID'");
		
		$_SESSION['session_message']='<div class="btn btn-success">'.$record_type.' has been updated successfully</div>';
		header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
		exit;
}
}	

if (isset($_POST['active'])) {
    if (!empty($_POST['cat_arr'])) {
        $catid = $_POST['cat_arr'];
        foreach ($catid as $id) {
            
            mysqli_query($dbconn, "UPDATE $table_name SET status = 'Y'  WHERE meta_id='$id'");
        }
        //$_SESSION['session_message'] = '<div class="btn btn-success">' . $record_type . ' has been updated successfully</div>';
    } else {
        //$_SESSION['session_message'] = '<div class="btn btn-danger">No Category selected</div>';
    }
    header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&parentID=" . $parentID . "&catID=" . $catID . "&pagesize=" . $pagesize);
    exit;
}

if (isset($_POST['inactive'])) {
    if (!empty($_POST['cat_arr'])) {
        $catid = $_POST['cat_arr'];
        foreach ($catid as $id) {
           
            mysqli_query($dbconn, "UPDATE $table_name SET status = 'N'  WHERE meta_id='$id' ");
        }
        //$_SESSION['session_message'] = '<div class="btn btn-success">' . $record_type . ' has been updated successfully</div>';
    } else {
        //$_SESSION['session_message'] = '<div class="btn btn-danger">No Category selected</div>';
    }
    header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&parentID=" . $parentID . "&catID=" . $catID . "&pagesize=" . $pagesize);
    exit;
}

// if(isset($feature_pro)){
// 	foreach($pro_arr as $id){
// 		mysqli_query($dbconn,"UPDATE $table_name SET product_theme = '1' where meta_id='$id'");
// 	}
// 	$_SESSION['session_message']='<div class="btn btn-warning">' . $record_type . ' has been set as Feature Product</div>';
// 		header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize);
// 		exit;
// }

admin_header("Manage $record_type");
?>
<style>
.display-img ul {
    list-style: none;
    padding: 0;
}

.display-img li {
    display: inline-block;
    margin: 5px;
    cursor: move;
    position: relative;
}

.display-img img {
    width: 100px;
    height: auto;
}

.ui-sortable-placeholder {
    background: #f0f0f0;
    border: 2px dashed #ccc;
    width: 100px;
    /* Adjust according to your image size */
    height: auto;
    visibility: visible !important;
}
</style>
<div class="content-wrapper">
    <div class="page-header">
        <?php
           if($_GET['recordID']=="" || $id!="edit_record") {
	          ?>
        <!-- <h3 class="page-title">Add New <?=$record_type;?></h3>
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb">
                <li class="breadcrumb-item"><a href="<?admin_url?>welcome.php">Admin</a></li>
                <li class="breadcrumb-item active" aria-current="page">Add New <?=$record_type;?></li>
            </ol>
        </nav> -->
        <?php }else{ ?>
        <h3 class="page-title">Edit <?=$record_type;?></h3>
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb">
                <li class="breadcrumb-item"><a href="<?admin_url?>welcome.php">Admin</a></li>
                <li class="breadcrumb-item active" aria-current="page">Edit <?=$record_type;?></li>
            </ol>
        </nav>
        <?php } ?>
    </div>

    <?php
                  if($_GET['recordID']!="" || $id=="edit_record") {
                    ?>
    <div class="row">
        <div class="col-12 grid-margin stretch-card">
            <div class="card">
                <div class="card-body">
                    <form name="bannerFrm" method=post action="" enctype="multipart/form-data"
                        onsubmit="return validate('bannerFrm') && chk_valid_date();">
                        <?php
                  if($_GET['recordID']=="" || $id!="edit_record") {
                    ?>
                        <!-- <div class="form-group">
                            <h3>Add New <?=$record_type;?></h3>
                        </div> -->
                        <?php
                            }
                            else {
                                ?>
                        <div class="form-group">
                            <h3>Edit <?=$record_type;?></h3>
                        </div>
                        <!-- <div class="form-group text-right">
                            <a href="<?=$script_name;?>" class="btn btn-primary text-white"><b>Add New
                                    <?=$record_type;?></b></a>
                        </div> -->
                        <?php
                                }
                                $res=getResult($table_name," where meta_id='$recordID'");
                                ?>

                                <?php
                                if($_GET['recordID']!="" || $id=="edit_record") {
                                    ?>                    
                       
                        <div class="form-group">
                            <label for="cate">Page Meta Tags <font color="red">*</font></label>

                            <input type="text" name="meta_title" class="form-control" value="<?=$res['meta_title']?>"
                                placeholder="Page Meta Tags">
                           
                        </div>
                        <div class="form-group">
                            <label for="cate">Page Meta Description <font color="red">*</font></label>


                            <textarea name="meta_description" class="form-control" value=""
                                placeholder="Page Meta Description"><?=$res['meta_description']?></textarea>
                         
                        </div>

                        <div class="form-group">
                            <label for="cate">Page Meta Keywords <font color="red">*</font></label>
                      

                            <textarea name="meta_keyword" class="form-control" value=""
                                placeholder="Page Meta Keywords"><?=$res['meta_keyword']?></textarea>
                           
                        </div>
                      
                        <div class="form-group">
                            <?php
                            if($recordID !="") {
                                ?>
                            <input type="submit" class="btn btn-primary" name="submit" value="Update <?=$record_type;?>"
                                class="button">
                            <input type="hidden" name="recordID" value="<?=$res['meta_id'];?>">
                            <input type="hidden" name="id" value="update_record">
                            <?php	   
                                }
                                else{
                                    ?>
                            <input type="submit" class="btn btn-primary" name="submit"
                                value="Add New <?=$record_type;?>" class="button">
                            <input type="hidden" name="id" value="add_record">
                            <?php
                                }
                                }
                                ?>
                            <input type="hidden" name="start" value="<?=$start;?>">
                            <input type="hidden" name="pagesize" value="<?=$pagesize;?>">
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
    <?php } ?>
    <div class="page-header">
        <h3 class="page-title">
            <?=$heading;?>
        </h3>
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb">
                <li class="breadcrumb-item"><a href="welcome.php">Admin</a></li>
                <li class="breadcrumb-item active" aria-current="page">Products</li>
            </ol>
        </nav>
    </div>

    <div class="card">
        <div class="card-body">
            <div class="row">
                <div class="col-12">

                    <?php
                        $qry1= mysqli_query($dbconn,"select * from $table_name ");
                        $reccnt = mysqli_num_rows($qry1);
                        $qry= mysqli_query($dbconn,"select * from $table_name order by meta_id desc");
                        // echo pageNavigation($reccnt,$pagesize,$start);
                        print_message();
                        ?>
                    <form name="formn" method="post" action="" enctype="multipart/form-data">
                        <div class="table-responsive">
                            <table id="order-listing" class="table table-bordered table-hover">
                                <?php
                        if($reccnt>0) {
             ?>
                                <thead>
                                    <tr>
                                        <th class="_reab">#</th>
                                        <th>S.No.</th>
                                        <th>Page Name</th>
                                        <th>Page Url</th>
                                        <th>Entity Name</th>
                                        <th>Status</th>
                                        
                                        <th>Options</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
	 $sl=$start+1;
      while($arr=mysqli_fetch_array($qry)) { 
      ?>
                                    <tr>
                                        <td><label class="container1"><input type="checkbox" name="cat_arr[]"
                                                    value="<?= $arr['meta_id'] ?>" /> <span
                                                    class="checkmark"></span></label></td>
                                        <td><?=$sl;?></td>
                                        <td><?=$arr['page_heading'];?></td>
                                        <td><?=$arr['page_url'];?></td>
                                        <td>
                                        <?=$arr['entity_name'];?>
                                        </td>
                                        <td>
                                         
                                            <p class="status-dcs"><?php if ($arr['status'] == 'Y') { ?><label class="badge badge-success badge-pill">Active</label><?php } else if ($arr['status'] == 'N') { ?> <label class="badge badge-danger badge-pill">In-Active</label>
                                                <?php } ?></p>
                                        </td>
                                      
                                        <td>&nbsp;<a
                                                href="<?=$script_name;?>?id=edit_record&start=<?=$start;?>&pagesize=<?=$pagesize?>&recordID=<?=$arr['meta_id'];?>#bot"><i
                                                    class="fa fa-edit"></i></a>
                                        </td>
                                    </tr>
                                    <?php $sl++; }  ?>
                                </tbody>
                                <?php } else { ?> <tr>
                                    <td>
                                        <h5>Not Record Found ... </h5>
                                    </td>
                                </tr><?php } ?>
                            </table>
                            <div class="btn-grp">
                                <button type="submit" name="active" onclick="return select_chk()" class="btn btn-success btn-icon-text btn-sm">
                                    <i class="fas fa-toggle-on mr-1"></i>Activate
                                </button>
                                <button type="submit" name="inactive" onclick="return select_chk()" class="btn btn-warning btn-icon-text btn-sm">
                                    <i class="far fa-times-circle mr-1"></i>Deactivate
                                </button>
                                </div>
                        </div>
                        <!-- <div class="text-right">
                            <input type="submit" name="feature_pro" value="feature Product" class="btn btn-primary">
                        </div> -->
                    </form>
                </div>
            </div>

        </div>
    </div>

    <link rel='stylesheet' href='calendar.css' title='calendar'>
    <script language="javascript" src="calendar.js"></script>
    <script src="jquery.min.js"></script>
    <!-- <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> -->
    <!-- <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script> -->
    <script language="javascript">
    function select_chk() {
        var chks = document.getElementsByName('cat_arr[]');
        var hasChecked = false;
        for (var i = 0; i < chks.length; i++) {
            if (chks[i].checked) {
                hasChecked = true;
                break;
            }
        }
        if (hasChecked == false) {
            alert("Please Select At Least One.");
            return false;
        }
    }
</script>
    <script>
        
    $(document).ready(function() {
        $('#product_title').on('input blur', function() {
            let input = $(this).val();
            let inputResponse = removeSpecialChars(input);

            $('#product_name').val(inputResponse);


            // $.ajax({
            //     url: 'your-server-endpoint', // Replace with your server endpoint
            //     type: 'POST',
            //     data: { value: inputValue },
            //     success: function(response){
            //         $('#result').html(response);
            //     },
            //     error: function(xhr, status, error){
            //         console.error('AJAX Error: ' + status + error);
            //     }
            // });
        });
    });
    </script>
    <script type="text/javascript">
    $("#quickCity").change(function() {

        var cityid = $('#quickCity').val();

        $.ajax({

            url: "ajax99.php", //The url where the server req would we made.

            async: false,

            type: "POST", //The type which you want to use: GET/POST

            data: "cityid=" + cityid, //The variables which are going.

            dataType: "html", //Return data type (what we expect).

            //This is the function which will be called if ajax call is successful.

            success: function(data) {

                //data is the html of the page where the request is made.

                $('#locatity').html(data);

            }

        })

    });
    </script>
    <script type="text/javascript">
    $("#locatity").change(function() {

        var locatityid = $('#locatity').val();

        $.ajax({

            url: "ajax999.php", //The url where the server req would we made.

            async: false,

            type: "POST", //The type which you want to use: GET/POST

            data: "locatityid=" + locatityid, //The variables which are going.

            dataType: "html", //Return data type (what we expect).

            //This is the function which will be called if ajax call is successful.

            success: function(data) {

                //data is the html of the page where the request is made.

                $('#asd').html(data);

            }

        })

    });
    </script>
    <script type="text/javascript">
    $("#asd").change(function() {

        var asdid = $('#asd').val();

        $.ajax({

            url: "ajax9999.php", //The url where the server req would we made.

            async: false,

            type: "POST", //The type which you want to use: GET/POST

            data: "asdid=" + asdid, //The variables which are going.

            dataType: "html", //Return data type (what we expect).

            //This is the function which will be called if ajax call is successful.

            success: function(data) {

                //data is the html of the page where the request is made.

                $('#asd1').html(data);

            }

        })

    });
    </script>
    <iframe width=174 height=189 name="gToday:normal:agenda.js" id="gToday:normal:agenda.js"
        src="<?=SITE_URL_PATH;?>/js/calendar/themes/Normal/ipopeng.htm" scrolling="no" frameborder="0"
        style="visibility:visible; z-index:999; position:absolute; top:-500px; left:-500px;">
    </iframe>

    <?php
admin_footer();
exit;
?>