<?php

include_once("../include/main.inc.php");

check_session('MyCpanel','index.php');

################ Paging Starts From Here ##################

@extract($_REQUEST);

if($parentID=="") {
	$parentID=0;
}

$heading="Manage Locations";

$record_type="Location";

$script_name="manage-location.php";

$table_name="location_tbl";

#################### Add Record ##################

if($_REQUEST['action']=="add_record" && $catName!="") {
	$sql=db_query("select * from $table_name where locationName='$catName' and parentID='$parentID'");
	$num=mysqli_num_rows($sql);
	if($num>0) {
		$_SESSION['session_message']=$record_type.' already exist';
	}
	else{
		$sql=db_query("insert into $table_name set parentID='$parentID', locationName='$catName'");
		$_SESSION['session_message']=$record_type.' has been added successfully';
		header("Location: ".SITE_ADMIN_URL."/".$script_name."?parentID=".$parentID."&locationID=".$catID);
		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 locationID = '$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);
  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 locationID = '$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);
  exit;
}

if (isset($_POST['delete'])) {
  if (!empty($_POST['cat_arr'])) {
      $catid = $_POST['cat_arr'];
      foreach ($catid as $id) {
          mysqli_query($dbconn, "DELETE FROM $table_name WHERE locationID = '$id'");
      }
      // $_SESSION['session_message'] = '<div class="btn btn-success">' . $record_type . ' has been deleted successfully</div>';
  } else {
      // $_SESSION['session_message'] = '<div class="btn btn-danger">No Category selected</div>';
  }
  header("Location: ".SITE_ADMIN_URL."/".$script_name);
  exit;
}
########################################################
admin_header("Manage $record_type");
$qry= db_query("select * from $table_name where parentID='$parentID' order by locationType asc, locationName asc");	
?>
<div class="content-wrapper">
          <div class="page-header">
            <h3 class="page-title">
			<?=$heading;?>
            </h3>
            <nav aria-label="breadcrumb">
              <ol class="breadcrumb">
			  <li class="nav-item d-none d-lg-flex">
            <a class="nav-link" href="<?=admin_url?>addeditlocation.php?parentID=<?=$parentID?>">
              <span class="btn btn-primary">+ Add New Location</span>
            </a>
          </li>
              </ol>
            </nav>
          </div>
          <div class="card">
            <div class="card-body">
              <div class="row">
                <div class="col-12">
                <form action="" method="post">
                  <div class="table-responsive">
                     <table id="order-listing" class="table table-bordered table-hover">
                      <thead>
                        <tr>
                        <th>
                            <div class="form-check mt-0">
										<label class="form-check-label">
										<input type="checkbox" class="form-check-input" name="check_all" id="check_all" value="1" onclick="checkall(this.form)">
										<i class="input-helper"></i>
										</label>
                                    </div>
                            </th>
							<th>S.No.</th>
                            <th>Location Name</th>
                            <th>Status</th>
                            <th>Actions</th>
                        </tr>
                      </thead>
                      <tbody>
					  <?php
 $sl=$start+1;
 while($arr=mysqli_fetch_array($qry)) {
?>

 <tr>
                            <td><input type="checkbox" name="cat_arr[]" value="<?= $arr['locationID'] ?>"></td>
                            <td><?=$sl?></td>
     <td><p class="ordern"><a href="<?=SITE_ADMIN_URL;?>/<?=$script_name;?>?parentID=<?=$arr['locationID'];?>"><?=$arr['locationName'];?></a></p></td>
							<td><?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 } ?>
                            </td>
                           <td> <p class="download-dcs">
                            <a href="<?=SITE_ADMIN_URL?>/addeditlocation.php?catID=<?=$arr['locationID'];?>&parentID=<?=$parentID;?>"><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></p>
                            </td>
						</tr>

 <?php  $sl++; }?>
                       
                      </tbody>
                    </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>
                        <button type="submit" name="delete" onclick="return select_chk()" class="btn btn-danger btn-icon-text btn-sm">
							<i class="fas fa-trash-alt mr-1"></i>Delete
                        </button>
                        </div>
                  </div>
                </div>
                      </form>
              </div>
            </div>
          </div>
        </div>
        <script language="javascript">

// checked=false;
// function checkall(frm1){
// 	var aa= frm1;
// 	if(checked == false){
// 		checked = true
// 	}
// 	else{
// 		checked = false
// 	}
// 	for (var i =0; i < aa.elements.length; i++){
// 		aa.elements[i].checked = checked;
// 	}
// } 
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>
                  <?php
                  admin_footer();
                  exit;
                  ?>