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


<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <script type="text/javascript">
     
        var clsid = "0";
        var yrsid = "0";
        var divsid = "0";
        $(document).ready(function () {
            datatable();
            let today2 = new Date().toISOString().substr(0, 10);
            $("#divssave").show();
            document.querySelector("#dp-6").value = today2;
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "scholershipmaster.aspx/PopulateDropDownList3",
                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 ddyear2 = value.DeptName;
                        var ddyear3 = value.DeptIds;
                        var dtyear1 = new Date(ddyear2);
                        var dtyear2 = new Date(ddyear3);
                        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(yrsid).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$=ddlyears]').change(function () {
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "scholershipmaster.aspx/PopulateDropDownList4",
                    data: "",
                    dataType: "json",
                    success: function (result) {
                        $('select[id$=ddlclassty]').empty();
                        $('select[id$=ddlclassty]').append("<option value='0'>Select Class</option>");
                        $.each(result.d, function (key, value) {
                            var dd = value.DeptName;
                            $('select[id$=ddlclassty]').append($("<option data-toggle='tooltip' data-placement='top' title='" + value.DeptName + "'></option>").val(value.DeptId).html(dd));
                            //console.log(value.DeptName);
                        });
                     
                            $('select[id$=ddlclassty]').val(clsid).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$=ddlclassty]').change(function () {
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "scholershipmaster.aspx/PopulateDropDownList5",
                    data: "{classesid :'" + $('select[id$=ddlclassty]').val() + "'}",
                    dataType: "json",
                    success: function (result) {
                        $('select[id$=txtdivi]').empty();
                        $('select[id$=txtdivi]').append("<option value='0'>Select Division</option>");
                        $.each(result.d, function (key, value) {
                            var dd = value.DeptName;
                            $('select[id$=txtdivi]').append($("<option data-toggle='tooltip' data-placement='top' title='" + value.DeptName + "'></option>").val(value.DeptId).html(dd));
                            //console.log(value.DeptName);
                        });
                     
                            $('select[id$=txtdivi]').val(divsid).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: "scholershipmaster.aspx/PopulateDropDownList7",
                data: "",
                dataType: "json",
                success: function (result) {
                    $('select[id$=ddlcategory]').empty();
                    $('select[id$=ddlcategory]').append("<option value='0'>Select Category</option>");
                    $.each(result.d, function (key, value) {
                        var dd = value.DeptName;
                        $('select[id$=ddlcategory]').append($("<option data-toggle='tooltip' data-placement='top' title='" + value.DeptName + "'></option>").val(value.DeptId).html(dd));
                        //console.log(value.DeptName);
                    });
                    $('select[id$=ddlcategory]').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 savedata() {
            if ($('select[id$=ddlyears]').val() == "0") {
                noty({ text: 'Please Select Year', layout: 'topRight', type: 'information' });
                setTimeout(function () { $.noty.closeAll(); }, 3000);
                return false;
            }
            if ($('select[id$=ddlclassty]').val() == "0") {
                noty({ text: 'Please Select Class', layout: 'topRight', type: 'information' });
                setTimeout(function () { $.noty.closeAll(); }, 3000);
                return false;
            }
            if ($("#dp-6").val() == "") {
                noty({ text: 'Please Select Scheme Date', layout: 'topRight', type: 'information' });
                setTimeout(function () { $.noty.closeAll(); }, 3000);
                return false;
            }
            if ($('select[id$=txtdivi]').val() == "0") {
                noty({ text: 'Please Select Division', layout: 'topRight', type: 'information' });
                setTimeout(function () { $.noty.closeAll(); }, 3000);
                return false;
            }
            if ($("#txtschemenm").val() == "") {
                noty({ text: 'Please Enter Scheme Name', layout: 'topRight', type: 'information' });
                setTimeout(function () { $.noty.closeAll(); }, 3000);
                return false;
            }
            if ($('select[id$=ddlcategory]').val() == "0") {
                noty({ text: 'Please Select Category', layout: 'topRight', type: 'information' });
                setTimeout(function () { $.noty.closeAll(); }, 3000);
                return false;
            }
          

            if ($("select[id$=ddlstatus]").val() == "") {
                noty({ text: 'Please Select Status', layout: 'topRight', type: 'information' });
                setTimeout(function () { $.noty.closeAll(); }, 3000);
                return false;
            } 
            if($("select[id$=ddlinst]").val() == ""){
                noty({ text: 'Please Select Installement', layout: 'topRight', type: 'information' });
                setTimeout(function () { $.noty.closeAll(); }, 3000);
                return false;
            }
            var obj = {};
            obj.yrsid = $('select[id$=ddlyears]').val();
            obj.clsid = $('select[id$=ddlclassty]').val();
            obj.divsid = $('select[id$=txtdivi]').val();
            obj.scheme = $("#txtschemenm").val();
            obj.schdate = $("#dp-6").val();
            obj.catids = $('select[id$=ddlcategory]').val();
            obj.scholtutionamt = $("#txttutionamount").val();
            obj.scholdevelopamt = $("#txtdevelopamount").val();
            obj.itype = $("select[id$=ddlinst]").val();
            obj.amount = $("#txtamount").val();
            obj.statuss = $("select[id$=ddlstatus]").val();
            $.ajax({
                type: "POST",
                url: "scholershipmaster.aspx/SavaData",
                data: JSON.stringify(obj),
                contentType: "application/JSON; charset=utf-8",
                datatype: "JSON",
                success: function (response) {
                    if (response.d == "Insert") {
                        noty({ text: 'Insert Successfully', layout: 'topRight', type: 'information' });
                        setTimeout(function () { $.noty.closeAll(); }, 3000);
                     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 clear() {
            $('select[id$=ddlyears]').val("0").change();
            $('select[id$=ddlclassty]').val("0").change();
           $('select[id$=txtdivi]').val("0").change();
           $("#txtschemenm").val("");
           let today2 = new Date().toISOString().substr(0, 10);
                     
           document.querySelector("#dp-6").value = today2;
        
           $('select[id$=ddlcategory]').val("0").change();
           $("#txttutionamount").val("");
           $("#txtdevelopamount").val("");
          $("select[id$=ddlinst]").val("").change();
           $("#txtamount").val("");
            $("select[id$=ddlstatus]").val("").change();
        } 
        function datatable() {
            $.ajax({
                type: "POST",
                url: "scholershipmaster.aspx/GetData",
                contentType: "application/JSON; charset=utf-8",
                data: "{}",
                datatype: "JSON",
                success: function (data) {
                    var table = $('#dataemp').DataTable();
                    data = JSON.parse(data.d);
                    table.clear();
                    for (var i = 0; i < data.length; i++) {
                        table.row.add([
                            data[i].clsname, data[i].divsname, data[i].schemename, data[i].feesname,data[i].installtype,data[i].amount,data[i].status,"<button  style='height:35px; width: 35px; padding-left:7px;' type='button' onclick=editdata('" + data[i].scholid + "') 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].scholid + "') id='btndelete' class='btn btn-danger'><i class='fadeIn animated bx bx-trash-alt'></i></button>"

                        ]).draw(true);

                    }
                }
            });
        }
        function deletedata(id1){
            $.ajax({
                url: "scholershipmaster.aspx/DeleteData",
                contentType: "application/JSON; charset=utf-8",
                method: "post",
                datatype: "JSON",
                data: "{id:'" + id1 + "'}",
                success: function (data) {
                    datatable();
                    noty({ text: 'Delete Successfully', layout: 'topRight', type: 'information' });
                    setTimeout(function () { $.noty.closeAll(); }, 3000);
                }
            });
        }
        var schid = "";
        function editdata(sids){
           
            $.ajax({
                url: "scholershipmaster.aspx/EditData",
                contentType: "application/JSON; charset=utf-8",
                method: "post",
                datatype: "JSON",
                data: "{ids:'" + sids + "'}",
                success: function (data) {
                    if(data.d != "")
                    {
                        data = JSON.parse(data.d);
                        console.log(data);
                        $("#divssave").hide();
                        $("#divsup").show();
                   
                        yrsid = data[0].yearsid;
                        $("#ddlyears").val(yrsid).change();
                        clsid = data[0].classid;
                    
                    
                       
                        divsid = data[0].divisionid;
                        $("#ddlyears").trigger("change");

                        $("#txtschemenm").val(data[0].schemename);
                        let today3 = new Date(data[0].scholdate).toISOString().substr(0, 10);
                     
                        document.querySelector("#dp-6").value = today3;
                        // $("#dp-6").val(data[0].scholdate);
                        $('select[id$=ddlcategory]').val(data[0].categoryid).change();
                  
                        $("select[id$=ddlinst]").val(data[0].installtype).change();
                        $("#txtamount").val(data[0].amount);
                        $("select[id$=ddlstatus]").val(data[0].status).change();
                        schid = data[0].id;
                  
                    }

                },
                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 updatedata() {

            var obj1 = {};
            obj1.sids = schid;
            obj1.yrsid = $('select[id$=ddlyears]').val();
            obj1.clsid = $('select[id$=ddlclassty]').val();
            obj1.divsid = $('select[id$=txtdivi]').val();
            obj1.scheme = $("#txtschemenm").val();
            obj1.schdate = $("#dp-6").val();
            obj1.catids = $('select[id$=ddlcategory]').val();
            obj1.scholtutionamt = $("#txttutionamount").val();
            obj1.scholdevelopamt = $("#txtdevelopamount").val();
            obj1.itype = $("select[id$=ddlinst]").val();
            obj1.amount = $("#txtamount").val();
            obj1.statuss = $("select[id$=ddlstatus]").val();
            $.ajax({
                url: "scholershipmaster.aspx/UpdateData",
                contentType: "application/JSON; charset=utf-8",
                method: "post",
                datatype: "JSON",
                data: JSON.stringify(obj1),
                success: function (data) {
                    datatable();
                    clear();
                 
                    $("#divssave").show();
                    $("#divsup").hide();
                    noty({ text: 'Update Successfully', layout: 'topRight', type: 'information' });
                    setTimeout(function () { $.noty.closeAll(); }, 3000);

                }
            });
        }
    </script>
<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>Scholership</h6>
						</div>
						<hr />
						
						
						<form class="row g-2">
							  <div class="col-md-3">
								<label>Year</label>
							<select id="ddlyears" class="form-select">
								
								</select>
							</div>
                            <div class="col-md-3">
								<label>Class</label>
							<select id="ddlclassty" class="form-select">
								
								</select>
							</div>
							<div class="col-md-3">
								<label>Division</label>
								<select id="txtdivi" class="form-select">
								
								</select>
							</div>
                              <div class="col-md-3">
								<label>Scheme Name</label>
							<input class="form-control mb-3" type="text" id="txtschemenm" placeholder="Enter Scheme"
								aria-label="default input example" />
							</div>
                              <div class="col-md-3">
								<label>Scheme Date</label>
							<input class="form-control mb-3" type="date" id="dp-6"
								aria-label="default input example" />
							</div>
                             <div class="col-md-3">
								<label>Category</label>
								<select id="ddlcategory" class="form-select">
								
								</select>
							</div>
                               <div class="col-md-3" style="display:none">
								<label>Tutuion Fee</label>
							<input class="form-control mb-3" type="text" id="txttutionamount" placeholder="Enter Amount"
								aria-label="default input example" />
							</div> 

                            <div class="col-md-3" style="display:none">
								<label>Development Fee</label>
							<input class="form-control mb-3" type="text" id="txtdevelopamount" placeholder="Enter Amount"
								aria-label="default input example" />
							</div> 
                             <div class="col-md-3">
								<label>No. Of Installment</label>
							<select id="ddlinst" class="form-select">
                                <option value="">select </option>
								<option value="firstinst">First Installment</option>
                                	<option value="secondinst">Second  Installment</option>
								</select>
							</div>
                            <div class="col-md-3">
								<label>Amount</label>
							<input class="form-control mb-3" type="text" id="txtamount" placeholder="Enter Amount"
								aria-label="default input example" />
							</div> 
                              <div class="col-md-3">
								<label>Status</label>
							<select id="ddlstatus" class="form-select">
                                <option value="">select status</option>
								<option value="Active">Active</option>
                                	<option value="InActive">InActive</option>
								</select>
							</div>
					<div class="col-md-2" style="margin-top: 4.7vh;display:none" id="divssave">
			        <button type="button" id="btnadd" value="Submit" onclick="savedata();" class="btn btn-primary"><i class='bx bx-plus-circle'></i>Add</button>
							
											
						   </div>
						   <div class="col-md-2" style="margin-top: 4.7vh;display:none" id="divsup">
			        <button type="button" id="Button1" value="Submit" onclick="updatedata();" class="btn btn-primary"><i class='bx bx-plus-circle'></i>Update</button>
							
											
						   </div>
                           </form>
                       </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>Class</th>
										   <th>Division</th>
										   <th>Scheme Name</th>  
                                           <th>Category</th>
                                           <th>Insttalement </th>
                                           <th>Amount</th>
                                           <th>Status</th>
										   <th style="background-color: white;">Action</th>
									   </tr>
								   </thead>
								   <%--<tbody>
									   <tr>
										   <td>1</td>
										   <td>Admission Fee</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>&nbsp;
											<button  style="height:35px; width: 35px; padding-left:7px;" type="button" class="btn btn-primary"><i class="lni lni-save"></i>
											</button>&nbsp;
											<button  style="height:35px; width: 35px; padding-left:7px;" type="button" class="btn btn-dark"><i class='bx bx-x'></i></span>
											</button>
											</td>
										</div>	
										</div>		
										</div>								
										   </tr>
									   </tbody>--%>
							   </table>
						   </div>
					   </div>
				   </div>
			   </div>
			   </div>
                    </div>
                </div>
            </div>
    </asp:Content>