<?php 
session_start();
include_once '../config.php';
$user_id = $_SESSION['Admin']['id'];
$CurrDate = date('dmY');
if($_REQUEST['action'] == 'checkUsername'){
$Username = addslashes(trim($_REQUEST['Username']));
$sql = "SELECT * FROM tbl_users WHERE Username='$Username'";
$rncnt = getRow($sql);
if($rncnt > 0){
	echo 1;
}
else{
	echo 0;
}
}

if($_REQUEST['action'] == 'Save'){
$Name = addslashes(trim($_REQUEST['Name']));
$Username = addslashes(trim($_REQUEST['Username']));
$Password = addslashes(trim($_REQUEST['Password']));
$Type = addslashes(trim($_REQUEST['Type']));
$Phone = addslashes(trim($_REQUEST['Phone']));
$RegNo = addslashes(trim($_REQUEST['RegNo']));
$Fees = addslashes(trim($_REQUEST['Fees']));
$CreatedDate = date('Y-m-d');
$Status = 1;
$ModifiedBy = 0;
$ModifiedDate = null;
$sql = "SELECT * FROM tbl_users WHERE Username='$Username'";
$rncnt = getRow($sql);
if($rncnt > 0){
	echo 0;
}
else{
	/*$sql = "INSERT INTO tbl_users SET Name='$Name',Username='$Username',Password='$Password',Type='$Type',Phone='$Phone',RegNo='$RegNo',Fees='$Fees',CreatedBy='$CreatedBy',CreatedDate='$CreatedDate'";
	$conn->query($sql);
	echo 1;*/
  try {
   $sql = "INSERT INTO tbl_users (Name,Username,Password,Type,Phone,RegNo,Fees,CreatedBy,CreatedDate,ModifiedBy,ModifiedDate) VALUES (:Name,:Username,:Password,:Type,:Phone,:RegNo,:Fees,:CreatedBy,:CreatedDate,:ModifiedBy,:ModifiedDate)";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':Name', $Name);
$stmt->bindParam(':Username', $Username);
$stmt->bindParam(':Password', $Password);
$stmt->bindParam(':Type', $Type);
$stmt->bindParam(':Phone', $Phone);
$stmt->bindParam(':RegNo', $RegNo);
$stmt->bindParam(':Fees', $Fees);
$stmt->bindParam(':CreatedBy', $user_id);
$stmt->bindParam(':CreatedDate', $CreatedDate);
$stmt->bindParam(':ModifiedBy', $ModifiedBy);
$stmt->bindParam(':ModifiedDate', $ModifiedDate);
if ($stmt->execute()) {
  echo 1;
}
}
catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}
}
}

if($_POST['action'] == 'fetch_record'){
 $id = $_POST['id'];
    $query = "SELECT * FROM tbl_users WHERE id = '$id'";
   $row = getRecord($query);
    echo json_encode($row);


}

if($_POST['action'] == 'Edit') {
     $id = $_POST['id'];
$Name = addslashes(trim($_REQUEST['Name']));
$Username = addslashes(trim($_REQUEST['Username']));
$Password = addslashes(trim($_REQUEST['Password']));
$Type = addslashes(trim($_REQUEST['Type']));
$Phone = addslashes(trim($_REQUEST['Phone']));
$RegNo = addslashes(trim($_REQUEST['RegNo']));
$Fees = addslashes(trim($_REQUEST['Fees']));
$ModifiedDate = date('Y-m-d');
$query = "SELECT * FROM tbl_users WHERE Username='$Username' AND id != '$id'";
$row_cnt = getRow($query);
if($row_cnt > 0){
  echo 0;
}
else{
  try{
  $query2 = "UPDATE tbl_users SET Name='$Name',Username='$Username',Password='$Password',Type='$Type',Phone='$Phone',RegNo='$RegNo',Fees='$Fees',ModifiedDate='$ModifiedDate',ModifiedBy='$user_id' WHERE id = '$id'";
 	$conn->query($query2);
  echo 1;
}
catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

}
}

  if($_POST['action'] == 'delete') {
   
      $id = $_POST['id'];
      $query = "DELETE FROM tbl_users WHERE id = '$id'";
      $conn->query($query);
      echo "Delete Successfully";

  }

 if($_POST['action']=='view'){?>
<table id="example" class="table table-striped table-bordered dt-responsive nowrap" style="width:100%">
        <thead>
            <tr>
             <th>Sr.No</th>
             <th>username</th>
             <th>Name</th>
             <th>type</th>
             <th>Mobile</th>
             <th>Registration No</th>
             <th>Fees</th>
             <th>Action</th>
            </tr>
        </thead>
        <tbody>
          <?php 
 $srno = 1;
  $sql = "SELECT * FROM tbl_users ORDER BY id DESC";
 $rx = getList($sql);
  foreach($rx as $nx){
      /*if($nx['Type'] == 'Consultant'){
        $Suffix = "Dr.";
      }
      else{
        $Suffix = "";
      }*/
  ?>
           <tr>
             <td><?php echo $srno; ?></td>
             <td><?php echo $nx['Username']; ?></td>
              <td><?php echo $Suffix." ".$nx['Name']; ?></td>
             <td><?php echo $nx['Type']; ?></td>
              <td><?php echo $nx['Phone']; ?></td>
              <td><?php echo $nx['RegNo']; ?></td>
             <td>&#8377;<?php echo number_format($nx['Fees'],2); ?></td>
             <td><a data-id="<?php echo $nx['id']; ?>" href='javascript:void(0);' data-toggle="tooltip" data-placement="top" title="Edit" data-original-title="Edit" class="update"><i class="lnr lnr-pencil mr-2"></i></a>&nbsp;&nbsp;<a data-id="<?php echo $nx['id']; ?>" href='javascript:void(0);' data-toggle="tooltip" data-placement="top" title="Delete" data-original-title="Delete" class="delete" id="bootbox-confirm"><i class="lnr lnr-trash text-danger"></i></a>
             </td>
            </tr>
             <?php $srno++;} ?>
        </tbody>
    </table>
    <script type="text/javascript">
      $(document).ready(function() {
      $('#example').DataTable( {
        responsive: true
      });
      });
    </script>
 <?php }
?>