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

<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();
                }
            });
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "division.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('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);
                }

            });

            datatable();
        });

        function btnadd() {
            $.ajax({
                type: "POST",
                url: "division.aspx/SavaData",
                data: "{classid :'" + $('select[id$=ddlclassty]').val() + "',division:'" + $("#txtdivi").val() + "',shortcds : '"+ $("#txtshortcode").val() +"'}",
                contentType: "application/JSON; charset=utf-8",
                datatype: "JSON",
                success: function (response) {
                    //window.location.reload();
                    $("#txtdivi").val(""); $("#txtshortcode").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: "division.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);
                    $("#txtdivi").val(data[0].division);
                    $('select[id$=ddlclassty]').val(data[0].classid).change();
                    $("#txtshortcode").val(data[0].shortcode);
                    id1 = id;

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

                }
            });
        }
        
        function datatable() {
            $.ajax({
                type: "POST",
                url: "division.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].classname, data[i].division,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() {

            $.ajax({

                url: "division.aspx/UpdateData",
                contentType: "application/JSON; charset=utf-8",
                method: "post",
                datatype: "JSON",
                data: "{id:'" + id1 + "',classid :'" + $('select[id$=ddlclassty]').val() + "',division:'" + $("#txtdivi").val() + "',shortcds : '" + $("#txtshortcode").val() + "'}",
                success: function (data) {


                    datatable();

                    $("#txtdivi").val("");
                    $('select[id$=ddlclassty]').val("0").change();
                    $("#txtshortcode").val("");
                    $("#btnadd").val("Update");
                    noty({ text: 'Update Successfully', layout: 'topRight', type: 'information' });
                    setTimeout(function () { $.noty.closeAll(); }, 3000);
                }
            });
        }
        function deletedata(id1) {

            $.ajax({

                url: "division.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>Division</h6>
						</div>
						<hr>
						
						
						<form class="row g-2">
							<div class="col-md-2"></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>
								<input class="form-control mb-3" type="text" id="txtdivi" placeholder="Enter Division"
								aria-label="default input example">
							</div>
                              <div class="col-md-3">
								<label>Shortcode</label>
							<input class="form-control mb-3" type="text" id="txtshortcode" placeholder="Enter Short Code"
								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>Division</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>

