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

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

    <script src="../assets/js/jquery.min.js"></script>
    <script type="text/javascript">
        var orderi = 1;
        var amount = 0;
        var subtotal = 0;
        var existamount = 0;
        var existamount1 = 0;
        var existamount2 = 0;
        var existamount3 = 0;
        var existamount4 = 0;  var existamount5 = 0;
        var existamount6 = 0;
        var existamount7 = 0;
        var amountarray = [];
        var amountarray11 = [];
        var amountarray22 = []; var row = 0;
        $(document).ready(function () {
            let today2 = new Date().toISOString().substr(0, 10);
                     
            document.querySelector("#incomedate").value = today2;

            $("#btnadd").click(function () {
                if ($("#btnadd").val() == "Submit") {
                    SaveData();
                }
                else if ($("#btnadd").val() == "Update") {
                    updatedata();
                }
            });
            $("select[id$=ddlpaymode]").change(function () {
                if( $("select[id$=ddlpaymode]").val() == "online"){
                    $("#divutrn").show();
                } 
                else  if($("select[id$=ddlpaymode]").val() == "cash"){
                    $("#divutrn").hide();
                }
            });
            datatable();Getcode();
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "expensessetup.aspx/PopulateDropDownList4",
                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$=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: "expensessetup.aspx/PopulateDropDownList56",
                data: "",
                dataType: "json",
                success: function (result) {
                    $('select[id$=ddlclasses]').empty();
                    $('select[id$=ddlclasses]').append("<option value='0'>Select Class</option>");
                    $.each(result.d, function (key, value) {
                      

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

                        $('select[id$=Select1]').append($("<option data-toggle='tooltip' data-placement='top' title='" + value.DeptName + "'></option>").val(value.DeptId).html(value.DeptName));
                        //console.log(value.DeptName);
                    });
                    $('select[id$=Select1]').val('0').change();
                   
                },
                error: function ajaxError(result) {
                    console.log(result.status + ' : ' + result.statusText);
                }

            });
        });

        var fill = "";
        var re2 = 0;
        function Getcode() {
         
            $.ajax({
                url: 'expensessetup.aspx/GetEmpData',
                type: 'post',
                contentType: 'application/json;charset=utf-8',
                dataType: 'json',
                data: "{}",
                success: function (data) {

                    data = JSON.parse(data.d);

                    var nn = data[0].expcounts;

                 
                    if(nn == 0){
                        re2 = parseInt(nn) + 1;
                    }else{
                        re2 = parseInt(nn) + 1;
                    }
                    var name = "REC";

                    fill =  re2;
                    $("#lblrecpt").html(fill); 
                    //  $('#pre').html(fill);
                },

                    
                 
                error: function (error) {
                    console.log(JSON.stringify(error));
                }
            });
        }
        function SaveData() {
            var customers = new Array();
            var table = document.getElementById("tt12");
            for (var i = 1; i < table.rows.length; i++) {
                var row = table.rows[i]; 
               
                var customer = {};
                customer.exepensenm =  row.cells[0].innerText;
                customer.particular =  row.cells[1].innerText;
                customer.amounts =  row.cells[2].innerText;
             
                customers.push(customer);console.log(customers);
            }
            var obj = {};
            obj.narrationss = $("#txtnarrations").val();
           
            obj.expnm = $('select[id$=Select1]').val();
            obj.amount = $("#txtcredit").val();
            obj.reptsnos = $("#lblrecpt").html();
            obj.years = $("select[id$=ddlyears]").val();
            obj.clsids = $("select[id$=ddlclasses]").val();
            obj.paydate = $("#incomedate").val();
            obj.paymode = $("select[id$=ddlpaymode]").val();
            obj.utrnos  = $("#txtutrnos").val();
            obj.array = JSON.stringify(customers);
            $.ajax({
                type: "POST",
                url: "expensessetup.aspx/savedata", //It calls our web method.
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(obj),
                dataType: "json",
                success: function (data) {
                    console.log(JSON.stringify(data));

                    noty({ text: 'Saved Successfully', layout: 'topRight', type: 'information' });
                    setTimeout(function () { $.noty.closeAll(); }, 3000);
                    Getcode();
                    datatable();
                    Clear();
                },
                error: function (error) {
                    console.log(JSON.stringify(error));
                    //alert("Error in insert");
                    noty({ text: 'Error in insert', layout: 'topRight', type: 'information' });
                    setTimeout(function () { $.noty.closeAll(); }, 3000);

                }
            });

        }
        function Addrow(){
            var obj = {}; var markup = "";
          
              
            amount = $("#txtcredit").val();
                   
            if (orderi == 1) {
                $("#subtotal").text(amount);
                existamount = $("#subtotal").text();
                $("#total").text(amount);
                      
                existamount1 = $("#total").text();
                amountarray[orderi] = amount;
                amountarray11[orderi] = amount;
            } 
            if (orderi > 1) {
                     
                existamount = $("#subtotal").text();
                $("#total").text(amount);
                var total1 = parseFloat(existamount) + parseFloat(amount);
                var total2 = parseFloat(existamount1) + parseFloat(amount);
                total1 = total1.toFixed(2);
                total2 = total2.toFixed(2);
                $("#subtotal").text(total1);
                existamount = $("#subtotal").text();
                $("#total").text(total2);
                existamount1 = $("#total").text();
                     
                      
            } 
            markup = "<tr><td style='text-align:center;'>"+ $("select[id$=Select1]").val() +"</td><td style='text-align:center;'>"+ $("#txtnarrations").val() +"</td><td style='text-align:center;'>"+ $("#txtcredit").val() +"</td><td  align='center'><input value='X' id=" + orderi + " class='btn active deleterow' type=button style='color:#FF0000;' /></td></tr>";
            $(".addrow").append(markup); 
            orderi++;
        }  
        function datatable() {
            $.ajax({
                type: "POST",
                url: "expensessetup.aspx/GetData",
                contentType: "application/JSON; charset=utf-8",

                datatype: "JSON",
                success: function (data) {
                    $("#dataemp").find("tbody").empty();
                    var table = $('#dataemp').DataTable();
                    data = JSON.parse(data.d);
                    table.clear();
                    for (var i = 0,count = 1; i < data.length; i++,count++) {
                        table.row.add([
                             count, data[i].rectpsnos,data[i].expname, data[i].exedate,  data[i].totalamounts,data[i].narrations,
                             "<button  style='height:35px; width: 35px; padding-left:7px;display:none' type='button' onclick=editdata('" + data[i].rectpsnos + "') id='btnedit' class='btn btn-success'  ><i class='bx bx-message-square-edit'></i></button> <button  style='height:35px; width: 35px; padding-left:7px;' type='button' onclick=deletedata('" + data[i].rectpsnos + "') id='btndelete' class='btn btn-danger'><i class='fadeIn animated bx bx-trash-alt'></i></button> <button  style='height:35px; width: 35px; padding-left:7px;' type='button' onclick=printdata('" + data[i].rectpsnos + "') class='btn btn-primary'><i class='fadeIn animated bx bx-printer'></i></button>"

                        ]).draw(true);

                    }
                }
            });
        }
        function printdata(recpt) {
           
            $("#printabel3").remove();
            loadpage1(recpt);
        }
        function loadpage1(recept) {
            setTimeout(function () {
                $("<iframe id='printabel3'>")

            .attr("src", "expensesreceiptsprint.aspx?receipts=" + recept)
                
            .appendTo("body");
                $("#printabel3").hide();
            }, 2000);
          
        }
        var id1 = "";
        function editdata(id) {
            $.ajax({

                url: "expensessetup.aspx/EditData",
                contentType: "application/JSON; charset=utf-8",
                method: "post",
                datatype: "JSON",
                data: "{id:'" + id + "'}",
                success: function (data) {
                    data = JSON.parse(data.d);
                    console.log(data);
                    $("#txtname").val(data[0].name);
                    $('select[id$=ddlsubtype]').val(data[0].subjecttype);
                    $("#txtsubcode").val(data[0].subjectcode);
                    $("#txtcredit").val(data[0].creditpoint);

                    id1 = id;

                   
                    $("#btnadd").val('Update');

                }
            });
        }
        function updatedata() {


            $.ajax({
                url: "expensessetup.aspx/UpdateData",
                contentType: "application/JSON; charset=utf-8",
                method: "post",
                datatype: "JSON",
                data: "{id:'" + id1 + "',name:'" + $("#txtname").val() + "',subjectcode:'" + $("#txtsubcode").val() + "',subjecttype:'" + $('select[id$=ddlsubtype]').val() + "',creditpoint:'" + $("#txtcredit").val() + "' }",
                success: function (data) {
                    datatable();
                    Clear();
                    Getcode();
                    $("#btnadd").val("Update");
                    noty({ text: 'Update Successfully', layout: 'topRight', type: 'information' });
                    setTimeout(function () { $.noty.closeAll(); }, 3000);

                }
            });
        }

        function deletedata(id1) {

            $.ajax({
                url: "expensessetup.aspx/DeleteData",
                contentType: "application/JSON; charset=utf-8",
                method: "post",
                datatype: "JSON",
                data: "{id:'" + id1 + "'}",
                success: function (data) {
                    datatable();Getcode(); Clear();
                    $("#dataemp").find("tbody").empty();
                    noty({ text: 'Delete Successfully', layout: 'topRight', type: 'information' });
                    setTimeout(function () { $.noty.closeAll(); }, 3000);
                }
            });
        }

        function Clear() {
            $("select[id$=ddlyears]").val("0").change();
            $('select[id$=ddlclasses]').val("0").change();
           
            $(".addrow").find("tbody").empty();
           $("#txtnarrations").val("");
           
         $('select[id$=Select1]').val();
            $("#txtcredit").val("");
           
        
            obj.paydate = $("#incomedate").val();
            $("select[id$=ddlpaymode]").val("").change();
          $("#txtutrnos").val("");
        }
    </script>


    <!--start page wrapper -->
		<div class="page-wrapper">
			<div class="page-content">
				<div class="row">
					<div class="col-md-12 mx-auto" >
					
				<div class="card">
					<div class="card-body">
						<div class="card-title d-flex align-items-center">

							<h6>Expenses Setup</h6>
						</div>
						<hr/>
						
						
						<form class="row g-2">
				
                            <div class="col-md-2">
								<label> Receipt No. :&nbsp;<label id="lblrecpt"></label> </label>
								
							</div>
                            <div class="col-md-2">
									<label>Years<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="ddlclasses" class="form-select">
								
									</select> 
                                    
								</div>
                            	<div class="col-md-2">
								<label> Paydate</label>
							<input type="date" id="incomedate" class="form-control" />
							</div>
                            <div class="col-md-2">
								<label>Exepnses</label>
								<select class="form-select mb-3" id="Select1" aria-label="Default select example">
								
									</select>
							</div>
							<div class="col-md-2">
								<label>Narration</label>
								<input class="form-control mb-3" id="txtnarrations" type="text" placeholder="Narration"
									aria-label="default input example" />
							</div>
							
							

							<div class="col-md-1">
								<label>Amount</label>
								<input class="form-control mb-3" id="txtcredit" type="text" placeholder="Amount"
									aria-label="default input example">
							</div>
						
                                    <div class="col-md-2">
									<label class="control-label">Pay Mode<font color="#000000">*</font></label>
									<select id="ddlpaymode" class="form-select">
                                            <option value="">Select Pay Mode</option>
                                    <option value="cash">Cash</option>
                                        <option value="online">Online</option> 
									</select>
								</div> 
                            	<div class="col-md-2" style="display:none" id="divutrn">
                                        <label>UTR Nos.<font color="#000000">*</font> </label>
									<input type="text" id="txtutrnos" class="form-control" />
                                        </div>
								<div class="col-md-1" id="divaddrow" style="padding-top:16px;">
									<div class="btn btn-primary px-4" onclick="Addrow()">  <i class="fadeIn animated bx bx-plus"></i>
									</div>

								</div>
                               </form>
							  
                    
                   </div>
				<div class="col-md-12">
                     <table id="tt12" class="addrow"  width="100%" border="0">
                    <thead>
                        <!--  <H6>Vendor List</H6> -->
                        <tr style="background-color:#f0f0f0; font-size:12px; height:30px;">

                            
                            <th width="20%" style="text-align:center"> Particular</th>
                             <th style="text-align:center">Narration</th>
                            <th style="text-align:center"> Amount(Dr) </th>
                         
                        </tr>
                    </thead>
                    <tbody>

                    </tbody>

                </table>
				</div>
<div class="row">
					       <div class="col-md-2" style="margin-top: 4.7vh;float:right">
						   <button type="button" id="btnadd" value="Submit" class="btn btn-primary"><i class='bx bx-plus-circle'></i>Add</button>
							
											
						   </div>
						   
                        </div>
                           </div>
				   <div class="overlay toggle-icon"></div>
				   <hr/>
				   <div class="col-md-12 mx-auto">
				   <div class="card">
					   <div class="card-body">
						   <div class="table-responsive">
							   <table id="dataemp" class="table table-striped table-bordered">
								   <thead>
									   <tr>
										   <th>Sr.no</th>	
										   <th>Receipt Nos</th>
                                             <th>Expenses</th>
										   <th>Pay Date</th>  
										   <th>Total Amount</th>
										<th>Narration</th>
										   <th style="background-color: white;">Action</th>
									   </tr>
								   </thead>
								 <tbody>
									    <%-- <tr>
										   <td>1</td>
										   <td></td>	
										   <td>8989</td>
										   <td>IT</td>
										   <td></td>

										   <td style="background-color: white;">
																	
											<button style="height:35px; width: 35px; padding-left:7px;" type="button" class="btn btn-success"  ><i class='bx bx-message-square-edit'></i>
											</button>&nbsp;<button style="height:35px; width: 35px; padding-left:7px;" type="button" class="btn btn-danger"><i class="fadeIn animated bx bx-trash-alt"></i>
											</button>
											<button style="height:35px; width: 35px; padding-left:7px;" type="button" class="btn btn-primary"><i class="lni lni-save"></i>
											</button>
											<button style="height:35px; width: 35px; padding-left:7px;" type="button" class="btn btn-dark"><i class='bx bx-x'></i>
											</button>
											</td>
											</tr>--%>
										</tbody>
							   </table>
						   </div>
					   </div>
				   </div>
			   </div>
			   </div>
		   </div>			
   
				   
		 
				
				<!--end page wrapper -->
				<!--start overlay-->
				
		<!--end page wrapper -->
		<!--start overlay-->
		<div class="overlay toggle-icon"></div>
		<!--end overlay-->
		<!--Start Back To Top Button--> <a href="javaScript:;" class="back-to-top"><i class='bx bxs-up-arrow-alt'></i></a>
		<!--End Back To Top Button-->
		
	</div>
            </div>
</asp:Content>

