<?php 
session_start();
include_once '../config.php';
$user_id = $_SESSION['Admin']['id'];
$CurrDate = date('dmY');

if($_REQUEST['action'] == 'Save'){
$FreqName = addslashes(trim($_REQUEST['FreqName']));
$Morning = addslashes(trim($_REQUEST['Morning']));
$Afternoon = addslashes(trim($_REQUEST['Afternoon']));
$Evening = addslashes(trim($_REQUEST['Evening']));
$CreatedDate = date('Y-m-d');
$Status = 1;
$ModifiedBy = 0;
$ModifiedDate = null;
$sql = "SELECT * FROM tbl_freq WHERE FreqName='$FreqName'";
$rncnt = getRow($sql);
if($rncnt > 0){
	echo 0;
}
else{
/*	$sql = "INSERT INTO tbl_freq SET Name='$Name',Amount='$Amount',CreatedBy='$user_id',CreatedDate='$CreatedDate'";
	$conn->query($sql);
	echo 1;*/

  try {
   $sql = "INSERT INTO tbl_freq (FreqName,Morning,Afternoon,Evening) VALUES (:FreqName,:Morning,:Afternoon,:Evening)";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':FreqName', $FreqName);
$stmt->bindParam(':Morning', $Morning);
$stmt->bindParam(':Afternoon', $Afternoon);
$stmt->bindParam(':Evening', $Evening);
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_freq WHERE id = '$id'";
   $row = getRecord($query);
    echo json_encode($row);


}

if($_POST['action'] == 'Edit') {
     $id = $_POST['id'];
$FreqName = addslashes(trim($_REQUEST['FreqName']));
$Morning = addslashes(trim($_REQUEST['Morning']));
$Afternoon = addslashes(trim($_REQUEST['Afternoon']));
$Evening = addslashes(trim($_REQUEST['Evening']));
$ModifiedDate = date('Y-m-d');
$query = "SELECT * FROM tbl_freq WHERE FreqName='$FreqName' AND id != '$id'";
$row_cnt = getRow($query);
if($row_cnt > 0){
  echo 0;
}
else{
  try{
  $query2 = "UPDATE tbl_freq SET FreqName='$FreqName',Morning='$Morning',Afternoon='$Afternoon',Evening='$Evening' 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_freq 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>Frequency</th>
             <th>Morning</th>
             <th>Afternoon</th>
             <th>Evening</th>
             <th>Action</th>
            </tr>
        </thead>
        <tbody>
          <?php 
 $srno = 1;
  $sql = "SELECT * FROM tbl_freq ORDER BY id DESC";
 $rx = getList($sql);
  foreach($rx as $nx){

  ?>
           <tr>
             <td><?php echo $srno; ?></td>
           
              <td><?php echo $nx['FreqName']; ?></td>
               <td><?php echo $nx['Morning']; ?></td>
               <td><?php echo $nx['Afternoon']; ?></td>
               <td><?php echo $nx['Evening']; ?></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 }
?>