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

if($_REQUEST['action'] == 'Save'){
$Name = addslashes(trim($_REQUEST['Name']));
$Power = addslashes(trim($_REQUEST['Power']));
$Quantity = addslashes(trim($_REQUEST['Quantity']));
$Narration = addslashes(trim($_REQUEST['Narration']));
$AddMinus = addslashes(trim($_REQUEST['AddMinus']));
$CreatedDate = date('Y-m-d');
 $Status = 1;
$ModifiedBy = 0;
$ModifiedDate = null;

/*$sql = "INSERT INTO tbl_lens SET Name='$Name',Power='$Power',Quantity='$Quantity',Narration='$Narration',AddMinus='$AddMinus',CreatedBy='$user_id',CreatedDate='$CreatedDate'";
  $conn->query($sql);
  $PostId = mysqli_insert_id($conn);*/
try {
   $sql = "INSERT INTO tbl_lens (Name,Power,Quantity,Narration,AddMinus,CreatedBy,CreatedDate,ModifiedBy,ModifiedDate) VALUES (:Name,:Power,:Quantity,:Narration,:AddMinus,:CreatedBy,:CreatedDate,:ModifiedBy,:ModifiedDate)";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':Name', $Name);
$stmt->bindParam(':Power', $Power);
$stmt->bindParam(':Quantity', $Quantity);
$stmt->bindParam(':Narration', $Narration);
$stmt->bindParam(':AddMinus', $AddMinus);
$stmt->bindParam(':CreatedBy', $user_id);
$stmt->bindParam(':CreatedDate', $CreatedDate);
$stmt->bindParam(':ModifiedBy', $ModifiedBy);
$stmt->bindParam(':ModifiedDate', $ModifiedDate);
if ($stmt->execute()) {
 $PostId = $conn->lastInsertId();
  if($AddMinus == '+'){
   /* $sql2 = "INSERT INTO tbl_item_ledger SET PostId='$PostId',FrameName='$Name',Power='$Power',Qty='$Quantity',CrDr='Dr',Status='1',CreatedBy='$user_id',CreatedDate='$CreatedDate',Narration='$Narration',Type='Lens'";
  $conn->query($sql2);*/
    try {
      $CrDr = "Dr";
      $Type = "Lense";
      $MrdNo = 0;
      $FrameType = 0;
   $sql = "INSERT INTO tbl_item_ledger (PostId,FrameName,Power,Qty,CrDr,Narration,Type,Status,CreatedBy,CreatedDate,MrdNo,FrameType) VALUES (:PostId,:FrameName,:Power,:Qty,:CrDr,:Narration,:Type,:Status,:CreatedBy,:CreatedDate,:MrdNo,:FrameType)";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':PostId', $PostId);
$stmt->bindParam(':FrameName', $Name);
$stmt->bindParam(':Power', $Power);
$stmt->bindParam(':Qty', $Quantity);
$stmt->bindParam(':CrDr', $CrDr);
$stmt->bindParam(':Narration', $Narration);
$stmt->bindParam(':Type', $Type);
$stmt->bindParam(':Status', $Status);
$stmt->bindParam(':CreatedBy', $user_id);
$stmt->bindParam(':CreatedDate', $CreatedDate);
$stmt->bindParam(':MrdNo', $MrdNo);
$stmt->bindParam(':FrameType', $FrameType);
if ($stmt->execute()) {
  
  }
}
catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

  }
  else{
  /* $sql2 = "INSERT INTO tbl_item_ledger SET  PostId='$PostId',FrameName='$Name',Power='$Power',Qty='$Quantity',CrDr='Cr',Status='1',CreatedBy='$user_id',CreatedDate='$CreatedDate',Narration='$Narration',Type='Lens'";
  $conn->query($sql2); */
   try {
      $CrDr = "Cr";
      $Type = "Lense";
       $MrdNo = 0;
      $FrameType = 0;
   $sql = "INSERT INTO tbl_item_ledger (PostId,FrameName,Power,Qty,CrDr,Narration,Type,Status,CreatedBy,CreatedDate,MrdNo,FrameType) VALUES (:PostId,:FrameName,:Power,:Qty,:CrDr,:Narration,:Type,:Status,:CreatedBy,:CreatedDate,:MrdNo,:FrameType)";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':PostId', $PostId);
$stmt->bindParam(':FrameName', $Name);
$stmt->bindParam(':Power', $Power);
$stmt->bindParam(':Qty', $Quantity);
$stmt->bindParam(':CrDr', $CrDr);
$stmt->bindParam(':Narration', $Narration);
$stmt->bindParam(':Type', $Type);
$stmt->bindParam(':Status', $Status);
$stmt->bindParam(':CreatedBy', $user_id);
$stmt->bindParam(':CreatedDate', $CreatedDate);
$stmt->bindParam(':MrdNo', $MrdNo);
$stmt->bindParam(':FrameType', $FrameType);
if ($stmt->execute()) {
  
  }
}
catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}
  }
  echo 1;
}
}
catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

	
 
}

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


}

if($_POST['action'] == 'Edit') {
     $id = $_POST['id'];
$Name = addslashes(trim($_REQUEST['Name']));
$Power = addslashes(trim($_REQUEST['Power']));
$Quantity = addslashes(trim($_REQUEST['Quantity']));
$Narration = addslashes(trim($_REQUEST['Narration']));
$AddMinus = addslashes(trim($_REQUEST['AddMinus']));
$ModifiedDate = date('Y-m-d');

  $query2 = "UPDATE tbl_lens SET Name='$Name',Power='$Power',Quantity='$Quantity',Narration='$Narration',AddMinus='$AddMinus',ModifiedDate='$ModifiedDate',ModifiedBy='$user_id' WHERE id = '$id'";
 	$conn->query($query2);
  $PostId = $id;
  if($AddMinus == '+'){
  $sql2 = "UPDATE tbl_item_ledger SET Qty='$Qty',Rate='$Rate',CrDr='Dr',Status='1',CreatedBy='$user_id',CreatedDate='$CreatedDate',Narration='Stock Added' WHERE FrameName='$PostId'";
  $conn->query($sql2);
  }
  else{
   $sql2 = "UPDATE tbl_item_ledger SET Qty='$Qty',Rate='$Rate',CrDr='Cr',Status='1',CreatedBy='$user_id',CreatedDate='$CreatedDate',Narration='Stock Added' WHERE FrameName='$PostId'";
  $conn->query($sql2); 
  }
  echo 1;
}

  if($_POST['action'] == 'delete') {
   
      $id = $_POST['id'];
      $query = "DELETE FROM tbl_lens WHERE id = '$id'";
      $conn->query($query);
      $query2 = "DELETE FROM tbl_item_ledger WHERE PostId = '$id' AND Type='Lens'";
      $conn->query($query2);
      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>Lens Name</th>
             <th>Lens Power</th>
             <th>Quantity</th>
              <th>CrDr</th>
             <th>Action</th>
            </tr>
        </thead>
        <tbody>
          <?php 
 $srno = 1;
  $sql = "SELECT * FROM tbl_lens ORDER BY id DESC";
   $rx = getList($sql);
  foreach($rx as $nx){
  ?>
           <tr>
             <td><?php echo $srno; ?></td>
           
              <td><?php echo $nx['Name']; ?></td>
          <td><?php echo $nx['Power']; ?></td>
          <td><?php echo $nx['Quantity']; ?></td>
             <td><?php if($nx['AddMinus']=='+'){echo "<span style='color:green;'>Credit</span>";} else { echo "<span style='color:red;'>Debit</span>";} ?></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 }
?>