<?php 
session_start();
require_once 'config.php';
require_once 'auth.php';
$MainPage="OPD";
$Page = "OPD-Receipt";
?>
<!DOCTYPE html>

<html lang="en" class="default-style">

<head>
    <title><?php echo $Proj_Title; ?></title>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
    <meta name="description"
        content="" />
    <meta name="keywords" content="">
    <meta name="author" content="Codedthemes" />
    <?php include_once 'header_script.php'; ?>
</head>

<body>
    <!-- [ Preloader ] Start -->
    <div class="page-loader">
        <div class="bg-primary"></div>
    </div>
    <!-- [ Preloader ] End -->
   <style>
table, td, th {  
  border: 1px solid #ddd;
  text-align: left;
}

table {
  border-collapse: collapse;
  width: 100%;
}

th, td {
  padding: 15px;
}
</style>
    <!-- [ Layout wrapper ] Start -->
   <div class="layout-wrapper layout-1 layout-without-sidenav">
        <div class="layout-inner">
         
         <?php include 'sidenavi.php'; ?>

<div class="layout-container">
                   <div class="layout-content">

                    <!-- [ content ] Start -->
                    <div class="container-fluid flex-grow-1 container-p-y">
                        
<?php 
$currDate = date('Y-m-d');
$sql = "SELECT * FROM tbl_opd_receipts WHERE CreatedDate='$currDate'";
$rncnt = getRow($sql);
$sql2 = "SELECT * FROM tbl_opd_receipts";
$rncnt2 = getRow($sql2);
 ?>
                        <div class="row">
                            <!-- customar project  start -->
                            <div class="col-xl-12">
                                <div class="card">
                                    <div class="card-body">
                                        <div class="row align-items-center m-l-0">
                                            
                                             
                                            
                                            <div class="col-sm-2 text-left">
                                                <button class="btn btn-success btn-sm mb-3 btn-round" data-toggle="modal" data-target="#modal-report" id="add_button"><i class="feather icon-plus"></i> Add Receipt</button>
                                            </div>
                                             <div class="col-sm-5 text-right">
                                                <button class="btn btn-danger">Today's Total Receipt : <span id="cntid"><?php echo $rncnt; ?></span> </button>
                                            </div>
                                            <div class="col-sm-5 text-right">
                                                <button class="btn btn-success"> Total Receipt : <span id="cntid"><?php echo $rncnt2; ?></span> </button>
                                            </div>
                                            
                                           
                                        </div>
                                        <hr>
                                        <div class="table-responsive" id="custresult">
                                            
                                        </div>
                                       
                                    </div>
                                </div>
                            </div>
                            <!-- customar project  end -->
                        </div>
                    </div>
                    <!-- [ content ] End -->
                    <!-- [ Layout footer ] Start -->
                  <?php include 'footer.php'; ?>
                </div>

                
                <!-- [ Layout content ] Start -->
            </div>
            <!-- [ Layout container ] End -->
        </div>
        <!-- Overlay -->
        <div class="layout-overlay layout-sidenav-toggle"></div>
    </div>


    <!-- [ Layout wrapper] End -->
    <div class="modal fade" id="modal-report" tabindex="-1" role="dialog" aria-labelledby="myExtraLargeModalLabel" aria-hidden="true">
    <div class="modal-dialog" style="max-width: 70rem; ">
        <div class="modal-content">
          
            
            <div class="modal-header">
                <h5 class="modal-title">Add Receipt</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <form id="validation-form" method="POST">
                     <input type="hidden" name="action" id="action" value="Save">
                       <input type="hidden" name="id" id="id" /> 
                    <div class="row">
                        <div class="col-sm-5">
                            <div class="form-group">
                                <label>Patient <span class="text-danger">*</span></label>

                                <select class="select2-demo form-control" style="width: 100%" data-allow-clear="true" name="PatientId" id="PatientId">
                                    <option>Search Patient</option>
                                    <?php 
                                        $sql4 = "SELECT MrdNo,PatientName FROM tbl_patients";
                                        $row4 = getList($sql4);
                                        foreach($row4 as $result){
                                         ?>
                                    <option value="<?php echo $result['MrdNo']; ?>"><?php echo $result['PatientName']."/".$result['MrdNo']; ?></option>
                                <?php } ?>
                                  
                                </select>
                            </div>
                        </div>
                        <div class="col-sm-2">
                            <div class="form-group">
                            <label>Mrd. No <span class="text-danger">*</span></label>
                                <input type="text" class="form-control" name="MrdNo" id="MRD_NO" placeholder="" readonly="">
                            </div>
                        </div>
                         <div class="col-sm-5">
                            <div class="form-group">
                                <label class="floating-label" for="Name">Patient Name <span class="text-danger">*</span></label>
                                <input type="text" class="form-control" name="PatientName" id="PatientName" placeholder="">
                            </div>
                        </div>
                        
                      <!--   <div class="col-sm-3">
                            <div class="form-group">
                                <label class="floating-label" for="gender">Select Department <span class="text-danger">*</span></label>
                                   <select class="form-control" style="width: 100%" name="ConsultantId" id="ConsultantId">
                                   
                                     <option value="" disabled="" selected="">Select Department</option>
                                     <?php 
                                        $sql4 = "SELECT Name,id FROM tbl_users WHERE Type='Department'";
                                        $row4 = getList($sql4);
                                        foreach($row4 as $result){
                                         ?>
                                    <option value="<?php echo $result['id']; ?>"><?php echo $result['Name']; ?></option>
                                <?php } ?>                                   
                                </select>
                            </div>
                        </div> -->
                       <!-- <div class="col-sm-4">
                            <div class="form-group fill">
                                <label class="floating-label" for="Birth">Procedure <span class="text-danger">*</span></label>
                               <select class="form-control" style="width: 100%" name="Procedure" id="Procedure">
                                   
                                     <option value="" disabled="" selected="">Select Procedure</option>
                                     <?php 
                                        $sql4 = "SELECT DISTINCT(Name) AS ProcName FROM tbl_procedures";
                                        $row4 = getList($sql4);
                                        foreach($row4 as $result){
                                         ?>
                                    <option value="<?php echo $result['ProcName']; ?>"><?php echo $result['ProcName']; ?></option>
                                <?php } ?>                                   
                                </select>
                            </div>
                        </div>
                        <div class="col-sm-2">
                            <div class="form-group">
                                <label class="floating-label" for="Phone">Amount <span class="text-danger">*</span></label>
                                <input type="text" class="form-control" id="Amount" name="Amount" placeholder="">
                            </div>
                        </div> -->
                       <div class="col-sm-3">
                            <div class="form-group">
                                <label class="floating-label" for="gender">Payment Mode <span class="text-danger">*</span></label>
                                <select class="form-control" id="PayMode" name="PayMode">
                                    <option value=""></option>
                                  <option value="Cash">Cash</option>
                                    <option value="Card">Card</option>
                                    <option value="Mobile Payment">Mobile Payment</option>
                                </select>
                            </div>
                        </div>
                         <div class="col-sm-3">
                            <div class="form-group">
                                <label class="floating-label" for="Phone">Cheque No</label>
                                <input type="text" class="form-control" id="ChequeNo" Name="ChequeNo" placeholder="">
                            </div>
                        </div>
                         <div class="col-sm-3">
                            <div class="form-group">
                                <label class="floating-label" for="gender">Narration</label>
                                <input type="text" class="form-control" id="Narration" Name="Narration" placeholder="">
                            </div>
                        </div>
                         <div class="col-sm-12" id="custresult2">
                        
    </div>
                       
                     
                        
                        <div class="col-sm-12" style="padding-top: 25px;">
                           <button class="btn btn-primary" type="submit" name="submit" id="submit">Submit</button>
                            <button class="btn btn-danger" type="reset">Clear</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<?php include_once 'footer_script.php'; ?>
<script type="text/javascript">

     $(document).ready(function() {
      $('#example').DataTable({
      
        "scrollX": true
        
    });
    $('#example2').DataTable({
      
        "scrollX": true,
       "order": [ 0, 'asc' ],
        "info": false,
        'paging': false
    });

   
    
});
     function isNumberKey(evt){ 
    var charCode = (evt.which) ? evt.which : evt.keyCode
        if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57))
        return false;
    return true;
}

function success_toast(){
    var isRtl = $('body').attr('dir') === 'rtl' || $('html').attr('dir') === 'rtl';
   $.growl.notice({
      title:    'Success',
      message:  'New Receipt Added Successfully!',
      location: isRtl ? 'tl' : 'tr'
    });
  }
  function error_toast(){
    var isRtl = $('body').attr('dir') === 'rtl' || $('html').attr('dir') === 'rtl';
   $.growl.error({
      title:    'Error',
      message:  'Receipt Already Exists',
      location: isRtl ? 'tl' : 'tr'
    });
  }

  function procedure_lists(){
  var action = 'view';
      $.ajax({
  type: "POST",
  url: "ajax_files/ajax_opd_receipt.php",
   data:{action:action},  
  success: function(data){
      $('#custresult').html(data);
  }
  });
    }

    function getProcRec2(id){
      var action = 'viewProcRec2';
      $.ajax({
  type: "POST",
  url: "ajax_files/ajax_opd_receipt.php",
   data:{action:action,id:id},  
  success: function(data){
      $('#custresult2').html(data);
  }
  });
    }

    function getProcRec(){
      var action = 'viewProcRec';
      $.ajax({
  type: "POST",
  url: "ajax_files/ajax_opd_receipt.php",
   data:{action:action},  
  success: function(data){
      $('#custresult2').html(data);
  }
  });
    }

     $(document).ready(function() {
        procedure_lists();

$(document).on("change", "#ConsultantId", function(event){
  var val = this.value;
   var action = "getConsultantDetails";
    $.ajax({
    url:"ajax_files/ajax_queue_management.php",
    method:"POST",
    data : {action:action,id:val},
    dataType:"json",  
    success:function(data)
    {
        $('#ConsFees').val(data.Fees);
    }
    });

 });

     $(document).on("change", "#PatientId", function(event){
  var val = this.value;
   var action = "getNewPatientDetails2";
    $.ajax({
    url:"ajax_files/ajax_queue_management.php",
    method:"POST",
    data : {action:action,id:val},
    dataType:"json",  
    success:function(data)
    {
        //alert(data);
        console.log(data);
        $('#MRD_NO').val(data.MrdNo);
        $('#PatientName').val(data.PatientName);
      //$('#PartStateId').html(data);
    }
    });

 });

     $(document).on("change", "#Procedure", function(event){
  var val = this.value;
   var action = "getProcedureDetails";
    $.ajax({
    url:"ajax_files/ajax_queue_management.php",
    method:"POST",
    data : {action:action,id:val},
    dataType:"json",  
    success:function(data)
    {
        $('#Amount').val(data.Amount);
    }
    });

 });

        $('#add_button').click(function(){  
           $('.modal-title').html("Add <span class='font-weight-light'>Receipt</span>");  
getProcRec();
           $('#action').val("Save");  
           $('#id').val('');

     $('#Name').val('');  
                $('#Amount').val('');
       $('#submit').text('Submit');
          
      }) 

      $('#validation-form').on('submit', function(e){
      e.preventDefault();  
          if ($('#validation-form').valid()){ 
         $.ajax({  
                url :"ajax_files/ajax_opd_receipt.php",  
                method:"POST",  
                data:new FormData(this),  
                contentType:false,  
                processData:false,  
                 beforeSend:function(){
     $('#submit').attr('disabled','disabled');
     $('#submit').text('Please Wait...');
    },
                success:function(data){ 
                 
                    //console.log(data);exit();
                   var res = JSON.parse(data);
                   var status = res.status;
                   var count = res.count;
                   var id = res.id;
                    if(status == 1){
                        $('#modal-report').modal('hide'); 
                         success_toast();
                         procedure_lists();
                         $('#cntid').html(count);
                         window.location.href="opdreceipt.php";
                         window.open('opd-receipt-print.php?id='+id,'_blank');
                    }
                    else{
                        error_toast();
                        $('#modal-report').modal('show'); 
                    }
                     $('#submit').attr('disabled',false);
                       $('#submit').text('Submit');
                }
                });
     }
      });


      $(document).on("click", ".update", function(event){
 event.preventDefault();
 event.stopPropagation();
 var id = $(this).attr("data-id");
 var action = "fetch_record";
 $.ajax({  
                url:"ajax_files/ajax_opd_receipt.php",  
                method:"POST",  
                data:{action:action,id:id},  
                dataType:"json",  
                success:function(data){  
                $('#PatientId').val(data.PatientId).attr("selected",true);  
                $('#MRD_NO').val(data.MrdNo);  
                $('#PatientName').val(data.PatientName);  
                $('#ConsultantId').val(data.ConsultantId).attr("selected",true);  
                $('#Procedure').val(data.Procedures).attr("selected",true);  
                $('#Amount').val(data.Amount);
                $('#PayMode').val(data.PayMode).attr("selected",true);  
                $('#ChequeNo').val(data.ChequeNo);
                $('#Narration').val(data.Narration);
                $('#action').val('Edit'); 
                   
                       $('#id').val(id);  
                       getProcRec2(id);
                       $('#submit').text("Update");   
                       $('#modal-report').modal('show');
                         $('.modal-title').html("Update <span class='font-weight-light'>Receipt</span>"); 
                     
                }  
           });
});



 $(document).on("click", ".delete", function(event){
 event.preventDefault();
 var id = $(this).attr("data-id");
 var action = "delete";
 //alert(id);
   swal({
            title: "Are you sure?",
            text: "You will not be able to recover this Receipt!",
            type: "warning",
            showCancelButton: true,
            confirmButtonClass: "btn-danger",
            confirmButtonText: "Yes, delete",
            cancelButtonText: "No, cancel",
            closeOnConfirm: false,
            closeOnCancel: false
        }, function(isConfirm) {
            if (isConfirm) {
                 $.ajax({  
                url:"ajax_files/ajax_opd_receipt.php",  
                method:"POST",  
                data:{action:action,id:id},  
               
                success:function(data){
                      var res = JSON.parse(data);
                   var status = res.status;
                   var count = res.count;
                    $('#cntid').html(count);
              swal("Deleted!", "Receipt has been deleted.", "success");
              
              procedure_lists();

                     }  
           });
                
            } else {
                swal("Cancelled", "Receipt is safe :)", "error");
            }
        });

           
 });
    }); 
</script>
</body>

</html>
