<?php
   include_once("../include/main.inc.php");
   check_session('MyCpanel','index.php');
   ################ Paging Starts From Here ##################
   @extract($_REQUEST);
   $start = (intval($start)<0 or $start=="")?0:$start;
   $pagesize = intval($pagesize)==0?$pagesize=20:$pagesize;
   
   $heading="Manage Subdomain Content";
   $record_type="Content";
   $script_name="manage_piles_subdomaincontent.php";
   $table_name="location_pilescontent_tbl";
   ####################################################
   
   if($id=="add_record") {
    if(!empty($location_ids)){
     $location_name = array();
     $loc = implode(',',$location_ids);
     // print_r($loc);die;
         $location_sql = mysqli_query($dbconn,"SELECT * FROM $table_name where location_ids='$loc' AND category_id='$category_id'");
         if(mysqli_num_rows($location_sql)==0){
            $short_description = htmlentitiesDecode($short_description);
            $meta_title = htmlentitiesDecode($meta_title);
            $meta_keyword = htmlentitiesDecode($meta_keyword);
            $meta_description = htmlentitiesDecode($meta_description);
            $subheadingtwo = htmlentitiesDecode($subheadingtwo);
            $short_description = str_ireplace("'", "&apos;", $short_description);
            $meta_title = str_ireplace("'", "&apos;", $meta_title);
            $meta_keyword = str_ireplace("'", "&apos;", $meta_keyword);
            $meta_description = str_ireplace("'", "&apos;", $meta_description);
            $headingtwo = str_ireplace("'", "&apos;", $headingtwo );
            
             $sql=db_query("insert into $table_name set category_id='$category_id', location_id='0', location_ids='$loc', meta_keyword='$meta_keyword',subheadingtwo='$subheadingtwo', meta_description='$meta_description', meta_title='$meta_title', description='$description', description2='$description2',description3='$description3',short_description='$short_description'");
         } else{ 
             $check=true;
             $location_name[] = get_location_name($location); 
         }
     if($check==true){
             $_SESSION['session_message']=$record_type.' for these locations is Already Created.';
         header("Location: ".SITE_ADMIN_URL."/".$script_name."?category_id=".$category_id."&start=".$start."&pagesize=".$pagesize);
         exit;
     }
     }else{
         $_SESSION['session_message']='Please Select Location';
         header("Location: ".SITE_ADMIN_URL."/".$script_name."?category_id=".$category_id."&start=".$start."&pagesize=".$pagesize);
         exit;
     }
        $_SESSION['session_message']='New '.$record_type.' has been added successfully';
        header("Location: ".SITE_ADMIN_URL."/".$script_name."?category_id=".$category_id."&start=".$start."&pagesize=".$pagesize);
        exit;
    }
if($id=="update_record") {
 if(!empty($location_ids)){
     // print_r($locations);die;
         $loc = implode(',',$location_ids);
         $short_description = str_ireplace("'", "&apos;", $short_description);
         $meta_title = str_ireplace("'", "&apos;", $meta_title);
         $meta_keyword = str_ireplace("'", "&apos;", $meta_keyword);
         $meta_description = str_ireplace("'", "&apos;", $meta_description);
         $subheadingtwo = str_ireplace("'", "&apos;", $subheadingtwo);

         updateTable($table_name,"location_id='0',location_ids='$loc', category_id='$category_id', subheadingtwo='$subheadingtwo',  meta_keyword='$meta_keyword', meta_description='$meta_description', meta_title='$meta_title', description='$description', short_description='$short_description',description2='$description2',description3='$description3' where id='$recordID'");
 }else{
     $_SESSION['session_message']='Please Select Location';
     header("Location: ".SITE_ADMIN_URL."/".$script_name."?category_id=".$category_id."&id=edit_record&start=".$start."&pagesize=".$pagesize."&recordID=".$recordID);
     exit;
 }
 $_SESSION['session_message']=$record_type.' has been updated successfully';
 header("Location: ".SITE_ADMIN_URL."/".$script_name."?category_id=".$category_id."&start=".$start."&pagesize=".$pagesize);
 exit;
}
if($id=="delete_record") {
    db_query("delete from $table_name where id='$recordID'");
    $_SESSION['session_message']=$record_type.' has been deleted successfully';
    header("Location: ".SITE_ADMIN_URL."/".$script_name."?category_id=".$category_id."&start=".$start."&pagesize=".$pagesize);
    exit;
}
admin_header("");
$qry= db_query("select * from $table_name where category_id='$category_id'");
?>
<style>
select[multiple],
select[size] {
    height: 200px;
}
</style>
<div class="content-wrapper">
    <div class="page-header">
        <h3 class="page-title">
            <?=$heading;?>
        </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"><?=$heading;?></li>
            </ol>
        </nav>
    </div>
    <div class="card">
        <div class="card-body">
            <div class="row">
                <div class="col-12">
                    <div class="table-responsive">
                        <table id="order-listing" class="table table-bordered table-hover">
                            <thead>
                                <tr>
                                    <th>S.No.</th>
                                    <th>Category Name</th>
                                    <th>Location</th>
                                    <th>Actions</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
 $sl=$start+1;
 $sno = 0;
 while($arr=mysqli_fetch_array($qry)) {
    $sno++;
    // $location_name = ($arr['location_id'] == 0)?'All':get_location_name($arr['location_id']);
    $location_name = ($sno == 1)?'All': 'Selected Locations';
    $category_name = ($arr['category_id'] == 0)?'Home':get_category_name($arr['category_id']); ?>
                                <tr>
                                    <td><?=$sl?></td>
                                    <td>
                                        <p class="ordern"><?=$category_name;?></p>
                                    </td>
                                    <td>
                                        <p class="dongle-dcs"> <?=$location_name;?></p>
                                    </td>
                                    <td>
                                        <p class="download-dcs">
                                            <a
                                                href="<?=$script_name;?>?category_id=<?= $category_id ?>&id=edit_record&recordID=<?=$arr['id'];?>#bot">
                                                <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24"
                                                    viewBox="0 0 24 24" fill="none" stroke="currentColor"
                                                    stroke-width="2" stroke-linecap="round" stroke-linejoin="round"
                                                    class="feather feather-edit-3">
                                                    <path d="M12 20h9"></path>
                                                    <path d="M16.5 3.5a2.121 2.121 0 0 1 3 3L7 19l-4 1 1-4L16.5 3.5z">
                                                    </path>
                                                </svg>
                                            </a>
                                            <a href="<?=$script_name;?>?category_id=<?= $category_id?>&id=delete_record&recordID=<?=$arr['id'];?>"
                                                onClick="return confirm('Are you sure to delete this record...');">
                                                <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16"
                                                    fill="currentColor" class="bi bi-trash" viewBox="0 0 16 16">
                                                    <path
                                                        d="M5.5 5.5A.5.5 0 0 1 6 6v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm2.5 0a.5.5 0 0 1 .5.5v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm3 .5a.5.5 0 0 0-1 0v6a.5.5 0 0 0 1 0V6z" />
                                                    <path fill-rule="evenodd"
                                                        d="M14.5 3a1 1 0 0 1-1 1H13v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V4h-.5a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1H6a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1h3.5a1 1 0 0 1 1 1v1zM4.118 4 4 4.059V13a1 1 0 0 0 1 1h6a1 1 0 0 0 1-1V4.059L11.882 4H4.118zM2.5 3V2h11v1h-11z" />
                                                </svg></a>
                                        </p>
                                    </td>
                                </tr>
                                <?php  $sl++; }?>
                            </tbody>
                        </table>

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


                    <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="text-center">
                            <h3>Add New <?=$record_type;?></h3>
                        </div>
                        <?php
}
else {
 ?>
                        <div align="left" height="20">
                            <h3>Edit <?=$record_type;?></h3>
                        </div>
                        <div align="right" height="20"><a class="btn btn-primary text-light"
                                href="<?=$script_name.'?category_id='.$category_id;?>"><b>Add New
                                    <?=$record_type;?></b></a></div>
                        <br />
                        <?php
$gid = $_GET['recordID'];
}
$res=getResult($table_name," where id='$gid'");
$location_sql = mysqli_query($dbconn,"SELECT * FROM location_tbl WHERE parentID ='0' order by locationName asc");
$category_name = ($category_id == 0)?'Home':get_category_name($category_id);
?>
                        <div class="form-group">
                            <label>Category<span>*</span></label>
                            <input type="text" name="category_name" value="<?= $category_name ?>" readonly
                                class="form-control" />
                        </div>
                        <div class="form-group">
                            <!-- <div class="col-sm-6"> -->
                            <label>Location<span>*</span></label>
                            <label><input type="checkbox" name="check_all" id="checkAll"> All locations</label>
                            <!-- </div> -->
                            <div class="col-sm-6" style="float:right;">
                                <label><input type="checkbox" name="check_half" id="checkHalf"> 50% Locations</label>
                            </div>
                            <div class="market-locations" style="height:400px;overflow-y:scroll;">
                                <?php while($location = mysqli_fetch_array($location_sql)){ 
         $selct_cat_query = "select * from $table_name WHERE id ='$gid'";
         $res_cat = $dbconn->query($selct_cat_query);
         $cat_loca = $res_cat->fetch_assoc();
         $loca_cat = explode(",", $cat_loca['location_ids']);
         // print_r($loca_cat);die; 
         $checked = '';
         
             if(in_array($location['locationID'], $loca_cat)){
                 $checked = 'checked';
             }else{
                 $checked = '';
             }
         
         ?>
                                <div class="row">
                                    <div class="col-sm-12 location-cls checked-loca">
                                        <?php if($location['status'] == 'Y') { ?><input type="checkbox"
                                            name="location_ids[]" value="<?= $location['locationID']; ?>"
                                            <?= $checked; ?>> <strong><?= $location['locationName']; ?></strong>
                                        <?php } else { ?><label><?= $location['locationName']; ?></label>
                                        <?php } ?>
                                    </div>
                                    <?php $parentID = $location['locationID'];
             $sub_location_sql = mysqli_query($dbconn,"SELECT * FROM location_tbl WHERE  parentID='$parentID'  order by locationName asc"); 
             $total_sub_loca = mysqli_num_rows($sub_location_sql);
             
             if(mysqli_num_rows($sub_location_sql)>0){ 
                 while($sub_location = mysqli_fetch_array($sub_location_sql)){ 
                     $selct_cat_query = "select * from $table_name WHERE id='$gid'";
                     $res_cat = $dbconn->query($selct_cat_query);
                     $cat_loca = $res_cat->fetch_assoc();
                     $loca_cat = explode(",", $cat_loca['location_ids']);
                     $checked = '';
                     if(in_array($sub_location['locationID'], $loca_cat)){
                         $checked = 'checked';
                     }else{
                         $checked = '';
                     }
                     ?>
                                    <div class="col-sm-12 location-cls checked-loca">
                                        <input type="checkbox" name="location_ids[]"
                                            value="<?= $sub_location['locationID']; ?>" <?= $checked; ?>>
                                        <label><?= $sub_location['locationName']; ?></label>
                                    </div>
                                    <?php $subsub_location_sql = mysqli_query($dbconn,"SELECT * FROM location_tbl WHERE  parentID='".$sub_location['locationID']."'  order by locationName asc"); 
                        $total_subsub_loca = mysqli_num_rows($subsub_location_sql);
                        $half_subsub_loca = 0;
                        if($half_subsub_loca % 2 == 0){
                            $half_subsub_loca = $total_subsub_loca / 2;
                        }else{
                            $half_subsub_loca = ($total_subsub_loca + 1) / 2;
                        }
                        if(mysqli_num_rows($subsub_location_sql)>0){ 
                            $counting = 0;
                            while($subsub_location = mysqli_fetch_array($subsub_location_sql)){ 
                                $selct_cat_query = "select * from $table_name WHERE id='$gid'";
                                $res_cat = $dbconn->query($selct_cat_query);
                                $cat_loca = $res_cat->fetch_assoc();
                                $loca_cat = explode(",", $cat_loca['location_ids']);
                                $checked = '';
                                if(in_array($subsub_location['locationID'], $loca_cat)){
                                    $checked = 'checked';
                                }else{
                                    $checked = '';
                                }
                                $counting++;
                                ?>
                                    <div
                                        class="col-sm-3 location-cls <?php if($counting <= $half_subsub_loca) { echo 'checked-loca'; } ?>">
                                        <input type="checkbox" name="location_ids[]"
                                            value="<?= $subsub_location['locationID']; ?>" <?= $checked; ?>>
                                        <?= $subsub_location['locationName']; ?>
                                    </div>
                                    <?php } } } } ?>
                                </div><?php }
     $location_sql = mysqli_query($dbconn,"SELECT * FROM location_tbl WHERE status='Y' and parentID ='0' order by locationName asc");
     ?>
                            </div>
                        </div>
                        <div class="form-group">
                            <label>Sub-Domain Heading H2 <span>*</span></label>
                            <input type="text" name="subheadingtwo" value="<?=$res['subheadingtwo'];?>"
                                class="form-control" />
                        </div>

                        <div class="form-group">
                            <label>Meta Title <span>*</span></label>
                            <input type="text" name="meta_title" value="<?=$res['meta_title'];?>"
                                class="form-control" />
                        </div>
                        <div class="form-group">
                            <label>Meta Keywords<span>*</span></label>
                            <input type="text" name="meta_keyword" value="<?= $res['meta_keyword'] ?>"
                                class="form-control" />
                        </div>
                        <div class="form-group">
                            <label>Meta Description<span>*</span></label>
                            <textarea rows="5" cols="60" name="meta_description"
                                class="form-control"><?= $res['meta_description'] ?></textarea>
                        </div>
                        <div class="form-group">
                            <label>Short Description<span>*</span></label>
                            <textarea rows="5" cols="60" name="short_description"
                                class="form-control"><?=$res['short_description'] ?></textarea>
                        </div>
                        <div class="form-group">
                            <label>Description<span>*</span></label>
                            <textarea rows="5" cols="60" id="description" name="description"
                                class="form-control editor"><?=$res['description'] ?></textarea>
                        </div>
                        <div class="form-group">
                            <label>Description 2<span>*</span></label>
                            <textarea rows="5" cols="60" id="description2" name="description2"
                                class="form-control editor"><?=$res['description2'] ?></textarea>
                        </div>
                        <!-- <div class="form-group">
                            <label>Description 3<span>*</span></label>
                            <textarea rows="5" cols="60" id="description3" name="description3"
                                class="form-control editor"><?=$res['description3'] ?></textarea>
                        </div> -->
                        <div class="form-group">
                            <?php
if($recordID !="") {
 ?>
                            <input type="submit" name="submit" value="Update <?=$record_type;?>"
                                class="button btn btn-success">
                            <input type="hidden" name="recordID" value="<?=$res['id'];?>">
                            <input type="hidden" name="id" value="update_record">
                            <?php	   
}
else{
 ?>
                            <input type="submit" name="submit" value="Add New <?=$record_type;?>"
                                class="button btn btn-success">
                            <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>
                    <script src="jquery.min.js"></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);

                            }

                        })

                    });
                    $(document).ready(function() {
                        $('#checkAll').change(function() {
                            if (this.checked) {
                                $('.location-cls > input:checkbox').attr('checked', 'checked');
                            } else {
                                $('.location-cls > input:checkbox').attr('checked', '');
                            }
                        });
                        $('#checkHalf').change(function() {
                            if (this.checked) {
                                $('.checked-loca > input:checkbox').attr('checked', 'checked');
                            } else {
                                $('.checked-loca > input:checkbox').attr('checked', '');
                            }
                        })
                    })
                    </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;
?>