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

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

    <script src="../assets/js/jquery.min.js"></script>
    <script type="text/javascript">

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


        function SaveData() {

            var obj = {};
            obj.subjectcode = $("#txtsubcode").val()
            obj.name = $("#txtname").val()
            obj.subjecttype = $('select[id$=ddlsubtype]').val()
            obj.creditpoint = $("#txtcredit").val()


            $.ajax({
                type: "POST",
                url: "schemelist.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: '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 datatable() {
            $.ajax({
                type: "POST",
                url: "schemelist.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; i < data.length; i++) {
                        table.row.add([
                             data[i].id, data[i].name, data[i].subjectcode, data[i].subjecttype, data[i].creditpoint,
                             "<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);

                    }
                }
            });
        }

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

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

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

                }
            });
        }
        function updatedata() {

           
            $.ajax({
                url: "schemelist.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();
                    $("#btnadd").val("Update");
                    noty({ text: 'Update Successfully', layout: 'topRight', type: 'information' });
                    setTimeout(function () { $.noty.closeAll(); }, 3000);

                }
            });
        }

        function deletedata(id1) {

            $.ajax({
                url: "schemelist.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);
                }
            });
        }

        function Clear() {

            $("#txtname").val("");
            $("#txtsubcode").val("");
            $('select[id$=ddlsubtype]').val("").change();
            $("#txtcredit").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>Scheme List</h6>
						</div>
						<hr>
						
						
						<form class="row g-2">
							<div class="col-md-2">
								<label> Name</label>
								<input class="form-control mb-3" id="txtname" type="text" placeholder=" Name"
									aria-label="default input example">
							</div>
							<div class="col-md-2">
								<label>Subject Code</label>
								<input class="form-control mb-3" id="txtsubcode"  type="text" placeholder="Subject Code"
									aria-label="default input example">
							</div>
							
							<div class="col-md-2">
								<label>Subject Type</label>
								<select class="form-select mb-3" id="ddlsubtype" aria-label="Default select example">
									<option selected>Select</option>
									<option>Theory</option>
									<option>Practical</option>
									</select>
							</div>


							<div class="col-md-2">
								<label>Credit Point</label>
								<input class="form-control mb-3" id="txtcredit" type="text" placeholder="Credit Point"
									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>Scheme List</th>
										   <th>Subject Code</th>  
										   <th>Student type</th>
										   <th>Credit Point</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></span>
											</button>
											</td>
										</div>	
										</div>		
										</div>								
										   </tr>
									   </tbody>--%>
							   </table>
						   </div>
					   </div>
				   </div>
			   </div>
			   </div>
</asp:Content>

