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

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <script type="text/javascript">
        var id1 = "";
        $(document).ready(function () {
            getdatas(); 
            $("#btnadd").show();
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "semister.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$=ddlyears]').trigger("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: "semister.aspx/PopulateDropDownList5",
                    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='" + dd + "'></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);
                    }

                });
            });
            $('select[id$=ddlclassty]').change(function () {
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "semister.aspx/PopulateDropDownList6",
                    data: "{clids :'" + $('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='" + dd + "'></option>").val(value.DeptId).html(dd));
                            //console.log(value.DeptName);
                        });
                     //   $('select[id$=txtdivi]').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 saveddata() {
            var obj = {};
            obj.years = $("select[id$=ddlyears]").val();
            obj.cls = $("select[id$=ddlclassty]").val();
            obj.divs = $("select[id$=txtdivi]").val();
            obj.patten = $("select[id$=ddlpat]").val();
            obj.semis = $("#txtsemnm").val();
            $.ajax({
                type: "POST",
                url: "semister.aspx/SavaData",
                data: JSON.stringify(obj),
                contentType: "application/JSON; charset=utf-8",
                datatype: "JSON",
                success: function (response) {
                    //window.location.reload();
                    getdatas();
                  //  datatable();
                    Clear(); $("#btnadd").show(); getdatas();
                    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);

                }

            });
        }
        function Clear() {
          $("select[id$=ddlyears]").val("0").change();
          $("select[id$=ddlclassty]").val("0").change();
          $("select[id$=txtdivi]").val("0").change();
          $("select[id$=ddlpat]").val("").change();
          $("#txtsemnm").val(""); id1 = "";
        }
        function getdatas() {
         

            $.ajax({
                type: "POST",
                url: "semister.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, count = 1; i < data.length; i++, count++) {
                        var ddyear1 = data[i].fromdate;
                        var ddyear2 = data[i].todate;
                        var dtyear1 = new Date(ddyear1);
                        var dtyear2 = new Date(ddyear2);
                   
                        var stryr = dtyear1.getFullYear() + "-" + dtyear2.getFullYear();
                        table.row.add([
                             count, stryr, data[i].clsname, data[i].divsname, data[i].pattern,data[i].semnos,
                             "<button  style='height:35px; width: 35px; padding-left:7px;' type='button' title='Edit' onclick='editsem(\"" + data[i].semid + "\"," + data[i].yearid + "," + data[i].clsids + "," + data[i].divsids + ")' id='btnedit' class='btn btn-success' data-bs-toggle='modal' data-bs-target='#exampleLargeModal5' ><i class='lni lni-save'></i></button> <button  style='height:35px; width: 35px; padding-left:7px;' type='button' title='delete' onclick='deletedata(\"" + data[i].semid + "\")' id='btndelete' class='btn btn-danger'><i class='fadeIn animated bx bx-trash-alt'></i></button>",

                        ]).draw(true);

                    }
                }
            });
        }
        function editsem(semsid, yrsid, classid, divisionsid) {
            $("#Button1").show(); $("#btnadd").hide();
            $.ajax({

                url: "semister.aspx/EditData",
                contentType: "application/JSON; charset=utf-8",
                method: "post",
                datatype: "JSON",
                data: "{semisterid:'" + semsid + "'}",
                success: function (data) {
                    data = JSON.parse(data.d);
                    $("select[id$=ddlyears]").val(data[0].yearsid).change();
                    $("select[id$=ddlclassty]").val(data[0].classid).change();
                    $("select[id$=txtdivi]").val(data[0].divsid).change();
                    $("select[id$=ddlpat]").val(data[0].pattern).change();
                    $("#txtsemnm").val(data[0].semnos);
                    id1 = semsid;

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

                }
            });
        }
        function updateddata() {
            var obj1 = {};
            obj1.exids = id1;
            obj1.pkgname = $("#txtsemnm").val();
            obj1.yrs = $('select[id$=ddlyears]').val();
            obj1.clss = $("select[id$=ddlclassty]").val();
            obj1.divs = $("select[id$=txtdivi]").val();
            obj1.semisters = $("select[id$=ddlpat]").val();
         
            $.ajax({
                url: "semister.aspx/UpdateData",
                contentType: "application/JSON; charset=utf-8",
                method: "post",
                datatype: "JSON",
                data: JSON.stringify(obj1),
                success: function (data) {
                    getdatas();
                    Clear(); $("#Button1").hide();  $("#btnadd").show();
                    noty({ text: 'Update Successfully', layout: 'topRight', type: 'information' });
                    setTimeout(function () { $.noty.closeAll(); }, 3000);
                }
            });
        }
        function deletedata(sids) {
            
            $.ajax({
                url: "semister.aspx/DeleteData",
                contentType: "application/JSON; charset=utf-8",
                method: "post",
                datatype: "JSON",
                data: "{sid:'" + sids + "'}",
                success: function (data) {
                    getdatas();
                    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-12 mx-auto">
					
				<div class="card">
					<div class="card-body">
						<div class="card-title d-flex align-items-center">

							<h6>Semister</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>Pattern</label>
							<select id="ddlpat" class="form-select">
                                <option value="">Pattern</option>
								<option value="semister">Semister</option>
                                <option value="year">Year</option>
								</select>
							</div>
                             <div class="col-md-3">
								<label>Semister</label>
					
								<input class="form-control mb-3" type="text" id="txtsemnm" placeholder="Enter Division"
								aria-label="default input example" />
					
							</div>
                             
					<div class="col-md-2" style="margin-top: 4.7vh;">
			        <button type="button" id="btnadd" value="Submit" onclick="saveddata()" style="display:none" class="btn btn-primary"><i class='bx bx-plus-circle'></i>Add</button> <button type="button" id="Button1" value="Submit" style="display:none" onclick="updateddata()" 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>Sr.no</th>	
                                             <th>Year</th>	
                                            <th>Class</th>
										   <th>Division</th>
                                            <th>Pattern</th>
										   	 <th>Semister</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>