<%@ Page Language="C#" AutoEventWireup="true" CodeFile="classmaster.aspx.cs" MasterPageFile="~/module/dashboardPage.master" Inherits="module_classmaster" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

      
    <script type="text/javascript">

        $(document).ready(function () {
            $("#btnadd").click(function () {
                if ($("#btnadd").val() == "Submit") {
                    btnadd();
                }
                else if ($("#btnadd").val() == "Update") {
                    updatedata();
                }
            });
            datatable();
        });

        function btnadd() {
            $.ajax({
                type: "POST",
                url: "classmaster.aspx/SavaData",
                data: "{ classs:'" + $("#txtclass").val() + "',srtcode :'"+ $("#txtcode").val() +"'}",
                contentType: "application/JSON; charset=utf-8",
                datatype: "JSON",
                success: function (response) {
                    //window.location.reload();
                    $("#txtclass").val(""); $("#txtcode").val("");
                    datatable();
                    noty({ text: 'Insert Successfully', layout: 'topRight', type: 'information' });
                    setTimeout(function () { $.noty.closeAll(); }, 3000);
                },
                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);

                }

            });
        }
        var id1 = "";
        function editdata(id) {
            $.ajax({

                url: "classmaster.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);
                    $("#txtclass").val(data[0].class);
                    $("#txtcode").val(data[0].shortcode);
                    id1 = id;

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

                }
            });
        }
        
        function datatable() {
            $.ajax({
                type: "POST",
                url: "classmaster.aspx/GetData",
                contentType: "application/JSON; charset=utf-8",

                datatype: "JSON",
                success: function (data) {
                    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].class,data[i].shortcode,
                             "<button  style='height:35px; width: 35px; padding-left:7px;' type='button' onclick=editdata('" + data[i].id + "') 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].id + "') 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' 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></span></button>"

                        ]).draw(true);

                    }
                }
            });
        }
        function updatedata() {
            alert(id1);
            $.ajax({

                url: "classmaster.aspx/UpdateData",
                contentType: "application/JSON; charset=utf-8",
                method: "post",
                datatype: "JSON",
                data: "{uid:'" + id1 + "',classs:'" + $("#txtclass").val() + "',srtcode :'" + $("#txtcode").val() + "'}",
                success: function (data) {


                    datatable();

                    $("#txtclass").val("");
                    $("#txtcode").val("");
                    $("#btnadd").val("Update");
                    noty({ text: 'Update Successfully', layout: 'topRight', type: 'information' });
                    setTimeout(function () { $.noty.closeAll(); }, 3000);
                }
            });
        }
        function deletedata(id1) {

            $.ajax({

                url: "classmaster.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);

                }
            });
        }

    </script>
 	<div class="page-wrapper">
			<div class="page-content">
				<div class="row">
					<div class="col-md-8 mx-auto" >
					
				<div class="card">
					<div class="card-body">
						<div class="card-title d-flex align-items-center">

							<h6>Class</h6>
						</div>
						<hr>
						
						
						<form class="row g-2">
							<div class="col-md-2"></div>
							<div class="col-md-3">
								<label>Class</label>
								<input class="form-control mb-3" type="text" id="txtclass" placeholder="Enter Class"
								aria-label="default input example" />
							</div>
                            	<div class="col-md-3">
								<label>Short Code</label>
								<input class="form-control mb-3" type="text" id="txtcode" placeholder="Enter ShortCode"
								aria-label="default input example" />
							</div>
					<div class="col-md-2" style="margin-top: 4.7vh;">
			        <button type="button" id="btnadd" value="Submit" class="btn btn-primary"><i class='bx bx-plus-circle'></i>Add</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>Sr.no</th>	
										   <th>Class</th>
										   	   <th>Short Code</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>