<?php
   include_once("../include/main.inc.php");
   check_session('MyCpanel','index.php');
   ################ Paging Starts From Here ##################
   @extract($_REQUEST);

   $heading="Manage Subdomain Content";
   $record_type="Content";
   $script_name="manage_location_content.php";
   $table_name="location_content_tbl";
   ####################################################
   
   if($id=="add_record") {
       $location_name = array();
   	foreach($location_id as $key => $location){
           $location_sql = mysqli_query($dbconn,"SELECT * FROM $table_name where location_id='$location' AND category_id='$category_id'");
           if(mysqli_num_rows($location_sql)==0){
            $location = implode(',',$location_ids);
               $sql=db_query("insert into $table_name set category_id='$category_id', location_id='0',
               location_ids='$location', meta_keyword='$meta_keyword', meta_description='$meta_description', meta_title='$meta_title',description='$description',description2='$description2',specification='$specification',short_description='$short_description',keyword1='$keyword1',keyword2='$keyword2',keyword3='$keyword3'");
           } else{ 
              $check=true;
               $location_name[] = get_location_name($location); 
           }
   		
   	}
       if($check==true){
           $location_string = implode(',', $location_name);
            $_SESSION['session_message']=$record_type.' for '.$location_string.' is Already Submitted.';
   		header("Location: ".SITE_ADMIN_URL."/".$script_name."?category_id=".$category_id);
   		exit;
       }
   		$_SESSION['session_message']='New '.$record_type.' has been added successfully';
   		header("Location: ".SITE_ADMIN_URL."/".$script_name."?category_id=".$category_id);
   		exit;
   	}
   if($id=="update_record") {
        $location = implode(',',$location_ids);
   		updateTable($table_name,"location_id='0',location_ids='$location', category_id='$category_id', meta_keyword='$meta_keyword', meta_description='$meta_description', meta_title='$meta_title', description='$description',description2='$description2',specification='$specification', short_description='$short_description', keyword1='$keyword1',keyword2='$keyword2', keyword3='$keyword3' where id='$recordID'");
   	
   		$_SESSION['session_message']=$record_type.' has been updated successfully';
   		header("Location: ".SITE_ADMIN_URL."/".$script_name."?category_id=".$category_id);
   		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);
   	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;
 while($arr=mysqli_fetch_array($qry)) {
    $location_name = ($arr['location_id'] == 0)?'All':get_location_name($arr['location_id']);
    $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;?>"><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 status='Y' and parentID ='0'");
   $category_name = ($category_id == 0)?'Home':get_category_name($category_id);
   ?>
    <div class="form-group">
        <label>Category</label>
        <input type="text" name="category_name" value="<?= $category_name ?>" readonly class="form-control" Required>
    </div>
    <div class="form-group">
        <label>Location</label>
        <select name="location_id[]" multiple class="form-control">
            <option value='0' selected>All Location</option>
            <?php while($location = mysqli_fetch_array($location_sql)){
   if($location['locationID'] == $res['location_id']){$sel = 'selected';}else{$sel = '';}
   $parentID = $location['locationID'];
   $sub_location_sql = mysqli_query($dbconn,"SELECT * FROM location_tbl WHERE status='Y' and parentID='$parentID'");
   ?>
            <option style="font-weight:bold;" value="<?= $location['locationID']?>" <?= $sel?>> <?= $location['locationName']?></option>
            <?php 
   if(mysqli_num_rows($sub_location_sql)>0){ 
   	while($sub_location = mysqli_fetch_array($sub_location_sql)){
          if($sub_location['locationID'] == $res['location_id']){$sel = 'selected';}else{$sel = '';}
        $subparentID = $sub_location['locationID'];
        $sub_sub_location_sql = mysqli_query($dbconn,"SELECT * FROM location_tbl WHERE status='Y' and parentID='$subparentID'");
      ?>
            <option <?=$sel?> value="<?= $sub_location['locationID']?>"> --<?= $sub_location['locationName']?></option>
            <?php 
        while($cityloc = mysqli_fetch_assoc($sub_sub_location_sql))
        {if($cityloc['locationID'] == $res['location_id']){$sel = 'selected';}else{$sel = '';}
       ?>
            <option <?= $sel ?> value="<?=$cityloc['locationID']?>"> --<?=$cityloc['locationName']?></option>
            <?php } }
   } }?>
        </select>
    </div>
    <div class="form-group">
        <label>Meta Title</label>
        <input type="text" name="meta_title" value="<?=$res['meta_title'];?>" class="form-control" Required>
    </div>
    <div class="form-group">
        <label>Meta Keywords</label>
        <input type="text" name="meta_keyword" value="<?= $res['meta_keyword'] ?>" class="form-control" Required>
    </div>
    <div class="form-group">
        <label>Meta Description</label>
        <textarea rows="5" cols="60" name="meta_description" Required class="form-control"><?= $res['meta_description'] ?></textarea>
    </div>
    <div class="form-group">
        <label>Location For Description 2</label>
        <select name="location_ids[]" multiple class="form-control">
            <option>--Select Location--</option>
            <?php 
    $location_sql = mysqli_query($dbconn,"SELECT * FROM location_tbl WHERE status='Y' and parentID!='0'");
    while($location = mysqli_fetch_array($location_sql)){
        $loc = explode(',',$res['location_ids']);
        if(in_array($location['locationID'],$loc)){$sel = 'selected';$col="background-color:blue;color:white;";}else{$sel = '';$col='';}
   ?>
            <option style="<?=$col?>" <?=$sel?> value="<?=$location['locationID'];?>">-----<?=$location['locationName'];?></option>
            <?php  }?>
        </select>
    </div>
    <div class="form-group">
        <label>Short Description</label>
        <textarea rows="5" cols="60" name="short_description" Required class="form-control editor"><?=$res['short_description'] ?></textarea>
    </div>
    <div class="form-group">
        <label>Description</label>
        <textarea rows="5" cols="60"  name="description" Required class="form-control editor"><?=$res['description'] ?></textarea>
    </div>
    <div class="form-group">
        <label>Description 2</label>
        <textarea rows="5" cols="60" name="description2" class="form-control editor"><?=$res['description2'] ?></textarea>
    </div>
    <div class="form-group">
        <label>Specification</label>
        <textarea rows="5" cols="60" name="specification" class="form-control editor"><?=$res['specification'] ?></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>
</div>
<?php
   admin_footer();
   exit;
   ?>
