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

<asp:Content ID="Content1" 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: "incomereport.aspx/PopulateDropDownListyras",
                data: "",
                dataType: "json",
                success: function (result) {
                    $('select[id$=ddlyears]').empty();
                    $('select[id$=ddlyears]').append("<option value='0'>Select Year</option>");
                    $.each(result.d, function (key, value) {
                        var ddyear1 = value.DeptName;
                        var ddyear2 = value.year1;
                        var dtyear1 = new Date(ddyear1);
                        var dtyear2 = new Date(ddyear2);
                        var stryr = dtyear1.getFullYear() + "-" + dtyear2.getFullYear();

                        $('select[id$=ddlyears]').append($("<option data-toggle='tooltip' data-placement='top' title='" + stryr + "'></option>").val(value.DeptId).html(stryr));
                        //console.log(value.DeptName);
                    });
                    $('select[id$=ddlyears]').val('0').change();
                    $('select[id$=ddlyears]').trigger("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: "incomereport.aspx/PopulateDropDownList4",
            data: "",
            dataType: "json",
            success: function (result) {
                $('select[id$=ddlincome]').empty();
                $('select[id$=ddlincome]').append("<option value='0'>Select income</option>");
                $.each(result.d, function (key, value) {
                

                    $('select[id$=ddlincome]').append($("<option data-toggle='tooltip' data-placement='top' title='" + value.DeptName + "'></option>").val(value.DeptId).html(value.DeptName));
                    //console.log(value.DeptName);
                });
                $('select[id$=ddlincome]').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: "incomereport.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);
            }

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


                        $('select[id$=ddldivis]').append($("<option data-toggle='tooltip' data-placement='top' title='" + dd + "'></option>").val(value.DeptId).html(dd));
                        //console.log(value.DeptName);
                    });
                    $('select[id$=ddldivis]').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);
                }

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


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


                   console.log(data.d);
                    //$("#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": "10%" },
                                     { "width": "14%" },
                                     { "width": "10%" },
                                     { "width": "7%" },
                                     { "width": "6%" },
                                     { "width": "10%" },
                                     { "width": "7%" },
                               { "width": "7%" },  { "width": "7%" },

                            ]
                        });
                 
                        $('#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].recptnos, data[i].incnm,data[i].studid, fullname, data[i].clsnm, data[i].divnm, data[i].incpaydt, data[i].paymodes, data[i].utrnos,  data[i].amount,data[i].narrations
                            ]).draw(false);
                            total = total + parseFloat(data[i].amount);

                        }
                        if (total > 0) {
                            $("#totalamt").html(total);
                        }

                    }
                    else{
                        table = $('#dataemp').DataTable();
                        table.destroy();
                       clear();
                        $('#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$=ddlincome]").val();
            obj.clsid = $("select[id$=ddlclass]").val();
            obj.diviions = $("select[id$=ddldivis]").val();
            obj.yearsids = $("select[id$=ddlyears]").val();
            if ($("#incomedate").val() != "") {alert($("select[id$=ddlincome]").val());
                $.ajax({
                    url: 'incomereport.aspx/GetDataExports',
                    method: 'post',
                    contentType: 'application/json;charset=utf-8',
                    data: JSON.stringify(obj),
                    dataType: 'json',
                    success: function (data) {alert(data.d);
                      var  table = $('#dataemp').DataTable();
                        table.destroy();
                        if (data.d != "") {
                            data = JSON.parse(data.d);
                        }
                      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: 'Income Receipt',
                                download: 'open',
                                orientation: 'landscape',
                                exportOptions: {
                                    columns: ':visible'
                                }
                            }],
                            "columns": [
                                 { "width": "5%" },
                                 { "width": "8%" },
                                 { "width": "10%" },
                                 { "width": "14%" },
                                 { "width": "10%" },
                                 { "width": "7%" },
                                 { "width": "6%" },
                                 { "width": "10%" },
                                 { "width": "7%" },
                                 { "width": "7%" },
                                  { "width": "7%" },
                            ]
                        });

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

                        for (var i = 0; i < data.length; i++) {
                            fullname2 = "";
                            fullname2 = data[i].title + "." + data[i].fname + " " + data[i].mname + " " + data[i].lname;
                            table.row.add([
                             data[i].recptnos, data[i].incnm,data[i].studid, fullname2, data[i].clsnm, data[i].divnm, data[i].incpaydt, data[i].paymodes, data[i].utrnos,  data[i].amount,data[i].narrations,
                            ]).draw(false);
                          
                        }
                    },
                    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 clear(){
            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;
            $("select[id$=ddlincome]").val("0").change();
            $('select[id$=ddlclass]').val("0").change();
            $("select[id$=ddldivis]").val("0").change();
            $('select[id$=ddlyears]').val("0").change();
        }
    </script>
      <div class="page-wrapper">
			<div class="page-content">
                <h6>INCOME  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-1" style="margin-top:10px">
                                      OR
                                      </div>
                                  <div class="col-md-2">
									<label>Income <font color="#000000">*</font></label>
									<select id="ddlincome" class="form-select">
                                    
                                         
									</select>
                                      </div>
                                  <div class="col-md-2" style="display:none">
									<label>Year <font color="#000000">*</font></label>
									<select id="ddlyears" 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-2" style="display:none">
									<label>Division <font color="#000000">*</font></label>
									<select id="ddldivis" class="form-select">
                                    
                                         
									</select>
								</div>
                                <div class="col-md-1" style="margin-top : 20px;">
								<div class="btn btn-primary">
									<div class="ms-2" id="" onclick="getdata12();"> Search </div></div>
								

							</div>
                                </div> 
                                </div> 
                            </form>
                            </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>Income Head</th>
                                           <th>Student ID</th>	
									       <th>Student Name</th>  
										   <th>Class Name</th>
										   <th>Divison</th>
										   <th>Payment Date</th>
                                           <th>Payment Mode</th>
                                           <th>UTR No.</th>
									       <th>Paid Amount</th>
                                          <th>Narration</th>
                                       
									   </tr>
								   </thead>
                                
								
							   </table>
                    </div>
                            </div>
					</div>
                </div> 
          </div>
</asp:Content>