<%@ Page Language="C#" AutoEventWireup="true" CodeFile="expensereport.aspx.cs" MasterPageFile="~/module/dashboardPage.master" Inherits="module_expensereport" %>

<asp:Content ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
       <script type="text/javascript">
           $(document).ready(function () {

               let today2 = new Date().toISOString().substr(0, 10);
                     
               document.querySelector("#incomedate").value = today2;
               let today3 = new Date().toISOString().substr(0, 10);
                     
               document.querySelector("#incomedate1").value = today3;
               $.ajax({
                   type: "POST",
                   contentType: "application/json; charset=utf-8",
                   url: "expensereport.aspx/PopulateDropDownList4",
                   data: "",
                   dataType: "json",
                   success: function (result) {
                       $('select[id$=ddlexpense]').empty();
                       $('select[id$=ddlexpense]').append("<option value='0'>Select Expense</option>");
                       $.each(result.d, function (key, value) {
                

                           $('select[id$=ddlexpense]').append($("<option data-toggle='tooltip' data-placement='top' title='" + value.DeptName + "'></option>").val(value.DeptId).html(value.DeptName));
                           //console.log(value.DeptName);
                       });
                       $('select[id$=ddlexpense]').val('0').change();
                       //$('select[id$=cat] option:eq(0)').attr("selected", true);
                       //  $('select[id$=ddlclassty]').selectpicker('refresh');
                       //  hdndoc=$('select[id$=cat] option:eq(1)').val();
                   },
                   error: function ajaxError(result) {
                       console.log(result.status + ' : ' + result.statusText);
                   }

               });
               $.ajax({
                   type: "POST",
                   contentType: "application/json; charset=utf-8",
                   url: "expensereport.aspx/PopulateDropDownList5",
                   data: "",
                   dataType: "json",
                   success: function (result) {
                       $('select[id$=ddlclass]').empty();
                       $('select[id$=ddlclass]').append("<option value='0'>Select class</option>");
                       $.each(result.d, function (key, value) {
                           var dd = value.DeptName;
                         

                           $('select[id$=ddlclass]').append($("<option data-toggle='tooltip' data-placement='top' title='" + dd + "'></option>").val(value.DeptId).html(dd));
                           //console.log(value.DeptName);
                       });
                       $('select[id$=ddlclass]').val('0').change();
                       //$('select[id$=cat] option:eq(0)').attr("selected", true);
                       //  $('select[id$=ddlclassty]').selectpicker('refresh');
                       //  hdndoc=$('select[id$=cat] option:eq(1)').val();
                   },
                   error: function ajaxError(result) {
                       console.log(result.status + ' : ' + result.statusText);
                   }

               });
           });
           var total = 0;
           function getdata12() {
               var totalpaid = 0;
             
               var remainsamt = 0;
               var totalremainsamt = 0;


               $.ajax({
                   url: "expensereport.aspx/Getdata",
                   method: "post",
                   contentType: "application/json; charset=utf-8",
                   data: "{pdate:'" + $('#incomedate').val() + "',pdate1:'" + $("#incomedate1").val() + "', incomeid:'" + $("select[id$=ddlexpense]").val() + "',clsid :'"+ $('select[id$=ddlclass]').val() +"'}",
                   dataType: "json",
                   success: function (data) {



                       //$("#dataemp").find("tbody").empty();
                       // $("#dataemp").find("tbody").empty();

                       table = $('#dataemp').DataTable();
                       table.destroy();
                       if (data.d != "") {
                           data = JSON.parse(data.d);
                     
                           var table = $('#dataemp').DataTable({
                               //"aaSorting": [[0, "desc"]],
                               //"ordering": false,
                               "pageLength": 10,
                               "columns": [
                                        { "width": "5%" },
                                        { "width": "8%" },
                                          { "width": "8%" },
                                        { "width": "10%" },
                                        { "width": "11%" },
                                        { "width": "10%" },
                                        { "width": "7%" },
                                
                                 { "width": "10%" },
                               ]
                           });
                 
                           $('#dataemp').DataTable().clear().draw();
                           for (var i = 0; i < data.length ; i++) {
                       
                               fullname = data[i].title + "." + data[i].fname + " " + data[i].mname + " " + data[i].lname;
                               totalremainsamt = totalremainsamt + parseFloat(data[i].amount);

                               table.row.add([
                                   data[i].rectpsnos, data[i].expnm,data[i].clsnm, data[i].incpaydt, data[i].pymodes, data[i].utrnos,  data[i].amounts,data[i].narrations
                               ]).draw(false);
                               total = total + parseFloat(data[i].amounts);

                           }
                           if (total > 0) {
                               $("#totalamt").html(total);
                           }
                       }else{
                           table = $('#dataemp').DataTable();
                           table.destroy();
                           total = 0;
                           $('#dataemp').DataTable().clear().draw();
                           noty({ text: 'No Records Found!!', layout: 'topRight', type: 'information' });
                           setTimeout(function () { $.noty.closeAll(); }, 3000);
                       }
                   },
                   error: function () {
                       //alert("Error in display");
                       noty({ text: 'Get Error', layout: 'topRight', type: 'error' });
                       setTimeout(function () { $.noty.closeAll(); }, 3000);
                   }

               });
           } 
           function ExportDataExam(){
               var obj = {};
               obj.pdate= $('#incomedate').val();
               obj.pdate1 = $("#incomedate1").val();
               obj.incomeid = $("select[id$=ddlexpense]").val();
               obj.clsid = $('select[id$=ddlclass]').val();
         
               if ($("#incomedate").val() != "") {
                   $.ajax({
                       url: 'expensereport.aspx/GetDataExports',
                       method: 'post',
                       contentType: 'application/json;charset=utf-8',
                       data: JSON.stringify(obj),
                       dataType: 'json',
                       success: function (data) {
                           table = $('#dataemp').DataTable();
                           table.destroy();Clearex();
                           if (data.d != "") {
                               data = JSON.parse(data.d);
                         
                               var table = $('#dataemp').DataTable({

                                   "order": [[0, "asc"]],
                                   "ordering": false,
                                   "pageLength": 10,
                                   //"lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
                                   "dom": 'lBfrtip',
                          
                                   //responsive: true,
                                   //select: {
                                   //    style: 'single'
                                   //},
                                   buttons: [
                                   {
                                       extend: 'excelHtml5',
                                       text: 'Export to Excel',
                                       title: 'Expense Receipt',
                                       download: 'open',
                                       orientation: 'landscape',
                                       exportOptions: {
                                           columns: ':visible'
                                       }
                                   }],
                                   "columns": [
                                        { "width": "5%" },
                                        { "width": "8%" },
                                        { "width": "8%" },
                                        { "width": "10%" },
                                        { "width": "11%" },
                                        { "width": "10%" },
                                        { "width": "7%" },
                                
                                        { "width": "10%" },

                                   ]
                               });

                               $('#dataemp').DataTable().clear().draw();

                               for (var i = 0; i < data.length; i++) {
                           
                                   table.row.add([
                                    data[i].rectpsnos, data[i].expnm,data[i].clsnm, data[i].incpaydt, data[i].pymodes, data[i].utrnos,  data[i].amounts,data[i].narrations
                                   ]).draw(false);
                          
                               }
                           }else{
                               table = $('#dataemp').DataTable();
                               table.destroy();
                               Clearex();
                               $('#dataemp').DataTable().clear().draw();
                               noty({ text: 'No Records Found!!', layout: 'topRight', type: 'information' });
                               setTimeout(function () { $.noty.closeAll(); }, 3000);
                           }
                       },
                       error: function () {


                           noty({ text: 'Get Error', layout: 'topRight', type: 'error' });
                           setTimeout(function () { $.noty.closeAll(); }, 3000);
                       }
                   });
               }
               else {
                   noty({ text: 'Please Select Payment Date', layout: 'topRight', type: 'information' });
                   setTimeout(function () { $.noty.closeAll(); }, 3000);
                       
               } 
           } 
           function Clearex(){
             
               $("select[id$=ddlexpense]").val("0").change();
               $('select[id$=ddlclass]').val("0").change();
               let today2 = new Date().toISOString().substr(0, 10);
                     
               document.querySelector("#incomedate").value = today2;
               let today3 = new Date().toISOString().substr(0, 10);
                     
               document.querySelector("#incomedate1").value = today3;
           }
    </script>
     <div class="page-wrapper">
			<div class="page-content">
                <h6>Expense  Report</h6>

				<div class="card">
					<div class="card-body">
						<div class="card-title d-flex align-items-center">

						</div>
						<hr />
						<form class="row g-2">
							

						<%--	<h6 style="background-color:#00a65a; color: white;text-align:center;font-size:16px">Other Income Voucher</h6>--%>
							<div class="col-md-2"></div>
							<div style="">

							<div class="row">
                                 <div class="col-md-2">
									<label> From<font color="#000000">*</font> </label>
									<input type="date" id="incomedate" class="form-control" />
								</div>
                                 <div class="col-md-2">
									<label>To<font color="#000000">*</font> </label>
									<input type="date" id="incomedate1" class="form-control" />
								</div>
                                   <div class="col-md-2" style="margin-top:5px">
                                       OR
                                       </div>
                                  <div class="col-md-2">
									<label>Expense <font color="#000000">*</font></label>
									<select id="ddlexpense" class="form-select">
                                    
                                         
									</select>
								</div>
                                   <div class="col-md-2">
									<label>Class <font color="#000000">*</font></label>
									<select id="ddlclass" class="form-select">
                                    
                                         
									</select>
								</div>
                                <div class="col-md-1" style="">
								<div class="btn btn-primary px-4">
									<div class="ms-2" id="" onclick="getdata12();"> Search </div></div>
								

							</div>
                                </div> 
                                 <hr />
                  <div class="card">
                     
					<div class="card-body">
                    <div class="card-title d-flex align-items-center">
                      
                        
                     </div>
                         <div class="row">
                    <div class="col-md-12">
                            <div class="col-md-3">
                        <label>Total Cash :</label> &nbsp; <label id="totalamt"></label>
                        </div>
                            <div class="col-md-4"></div>
                         <div class="col-md-3" style="float:right;margin-bottom:3px">
                              <button class="btn btn-primary" type="button" style=""  onclick="ExportDataExam();"> Export Data</button>
                             </div>
					
						</div>
						</div>
                               <div class="table-responsive">
                                    <table id="dataemp" class="table table-striped table-bordered">
								   <thead>
									   <tr>
										   <th>Receipt No.</th>	
                                           <th>Expense Head</th>
                                         <th>Class</th>
										   <th>Payment Date</th>
                                            <th>Payment Mode</th>
                                            <th>UTR No.</th>
									       <th> Amount</th>
                                         <th> Narration</th>
                                       
									   </tr>
								   </thead>
                                
								
							   </table>
                    </div>
                            </div>
					</div>
                                </div> 
                            </form>
                            </div>
                    </div> 
                </div> 
          </div>
</asp:Content>