Promotion NET MVC
************************************
Repositories.cs
************************************
public static List<PromotionDTO> GetPromotion(int startIndex = 0, int pageRow = 10, string search = "", string searchBtn = "")
{
using (var dc = new ShowtimeEntities())
{
var query = promotionQuery(dc,search,searchBtn);
return query.OrderByDescending(x=>x.Name).Skip(startIndex*pageRow).Take(pageRow).ToList();
}
}
public static int CountPromotion(string search = "", string searchBtn = "")
{
using (var dc = new ShowtimeEntities())
{
var query = promotionQuery(dc,search,searchBtn);
return query.Count();
}
}
public static IQueryable<PromotionDTO> promotionQuery(ShowtimeEntities dc, string search = "", string searchBtn = "")
{
var query = from pc in dc.PromotionContents
join ct in dc.Contents on pc.ContentId equals ct.Id
join cg in dc.ContentCategories on ct.ContentCategoryId equals cg.Id
join cc in dc.ContentClassifications on ct.ContentClassificationId equals cc.Id
select new PromotionDTO
{
Id = pc.Id,
Name = pc.Name,
Title = ct.Title,
IsCP = ct.IsCP,
CategoryName = cg.Name,
ClassificationName = cc.Name,
PaymentCarrier = pc.PaymentCarrier
};
if (search != null) {
switch (searchBtn) {
case "IsCP":
query = query.Where(w => w.IsCP.ToString().Contains(search));
break;
case "Category":
query = query.Where(w => w.CategoryName.Contains(search));
break;
case "Classification":
query = query.Where(w => w.ClassificationName.Contains(search));
break;
case "ContentName":
query = query.Where(w => w.Title.Contains(search));
break;
case "PaymentCarrier":
query = query.Where(w => w.PaymentCarrier.Contains(search));
break;
case "PromotionName":
query = query.Where(w => w.Name.Contains(search));
break;
case "All":
query = query.Where(w => w.IsCP.ToString().Contains(search) || w.CategoryName.Contains(search) || w.ClassificationName.Contains(search) ||
w.Title.Contains(search) || w.PaymentCarrier.Contains(search) || w.Name.Contains(search)
);
break;
}
}
return query;
}
public static List<PromotionDTO> GetContentModal(string search,int startIndex=0,int pageRow=10, string keySearchBtn = "")
{
using (var dc = new ShowtimeEntities())
{
var query = QueryContentModel(dc, search, keySearchBtn);
return query.OrderByDescending(x=>x.ContentId).Skip(startIndex*pageRow).Take(pageRow).ToList();
}
}
public static int CountContentModel(string search, string keySearchBtn = "")
{
using (var dc = new ShowtimeEntities())
{
var query = QueryContentModel(dc,search,keySearchBtn);
return query.Count();
}
}
public static IQueryable<PromotionDTO> QueryContentModel(ShowtimeEntities dc,string search, string keySearchBtn = "")
{
var query = from ct in dc.Contents
join cg in dc.ContentCategories on ct.ContentCategoryId equals cg.Id
join cc in dc.ContentClassifications on ct.ContentClassificationId equals cc.Id
where ct.Title.Contains(search) || ct.Id.ToString().Contains(search) || cg.Name.Contains(search) || cc.Name.Contains(search)
select new PromotionDTO
{
ContentId = ct.Id,
Title = ct.Title,
IsCP = ct.IsCP,
CategoryName = cg.Name,
ClassificationName = cc.Name
};
if (search != null){
switch (keySearchBtn) {
case "ContentId":
query = query.Where(s => s.ContentId.ToString().Contains(search));
break;
case "ContentName":
query = query.Where(s => s.Title.Contains(search));
break;
case "IsCP":
query = query.Where(s => s.IsCP.ToString().Contains(search));
break;
case "Category":
query = query.Where(s => s.CategoryName.Contains(search));
break;
case "Classification":
query = query.Where(s => s.ClassificationName.Contains(search));
break;
case "All":
query = query.Where(s => s.ContentId.ToString().Contains(search) || s.Title.Contains(search) ||
s.IsCP.ToString().Contains(search) || s.CategoryName.Contains(search) || s.ClassificationName.Contains(search));
break;
}
}
return query;
}
public static List<PromotionContent> SavePromotion(UpdatePromotionDTO dataPromo)
{
using (var dc = new ShowtimeEntities())
{
var dataDelete = (from objData in dc.PromotionContents where objData.Name == dataPromo.promotionName select objData).ToList();
dc.PromotionContents.RemoveRange(dataDelete);
var datas = new List<PromotionContent>();
for (var i = 0; i < dataPromo.contents.Length; i++)
{
datas.Add(new PromotionContent() {Name = dataPromo.promotionName,PaymentCarrier = dataPromo.payment, ContentId = dataPromo.contents[i],TokenDiscountRate = dataPromo.discountRate,
TokenFixValueDiscount = dataPromo.discountValue});
}
dc.PromotionContents.AddRange(datas);
dc.SaveChanges();
return datas;
}
}
public static EditPromotionDTO EditPromotion(string dataPromo)
{
var dc = new ShowtimeEntities();
var getDataContent = (from obj1 in dc.PromotionContents where obj1.Name == dataPromo select obj1).ToList();
var getPromotionName = getDataContent.Select(x => x.Name).FirstOrDefault();
var getDiscountRate = getDataContent.Select(x => x.TokenDiscountRate).FirstOrDefault();
var getDiscountValue = getDataContent.Select(x => x.TokenDiscountRate).FirstOrDefault();
var getPayment = getDataContent.Select(x=>x.PaymentCarrier).FirstOrDefault();
var getContentId = getDataContent.Select(x => x.ContentId).ToList();
var query = (from ct in dc.Contents
join cg in dc.ContentCategories on ct.ContentCategoryId equals cg.Id
join cc in dc.ContentClassifications on ct.ContentClassificationId equals cc.Id
where getContentId.Contains(ct.Id)
select new PromotionDTO
{
ContentId = ct.Id,
Title = ct.Title,
IsCP = ct.IsCP,
CategoryName = cg.Name,
ClassificationName = cc.Name
}).ToList();
var arrContents = new List<PromotionDTO>();
foreach (var i in query)
{
arrContents.Add(new PromotionDTO() {ContentId = i.ContentId,Title = i.Title,IsCP = i.IsCP, CategoryName = i.CategoryName,ClassificationName = i.ClassificationName});
}
var data = new EditPromotionDTO();
data.contents = arrContents;
data.promotionName = getPromotionName;
data.discountRate = getDiscountRate;
data.discountValue = getDiscountValue;
data.payment = getPayment;
return data;
}
public static bool DeletePromotion(string promoName)
{
var dc = new ShowtimeEntities();
List<PromotionContent> cekData = dc.PromotionContents.Where(x => x.Name == promoName).ToList();
var success = false;
if (cekData != null)
{
dc.PromotionContents.RemoveRange(cekData);
dc.SaveChanges();
success = true;
}
return success;
}
public static bool SingleDeletePromotion(int promoId)
{
var dc = new ShowtimeEntities();
PromotionContent cekData = dc.PromotionContents.Where(x => x.Id == promoId).FirstOrDefault();
if (cekData == null) return false;
dc.PromotionContents.Remove(cekData);
dc.SaveChanges();
return true;
}
************************************
RestController.cs
************************************
public ActionResult GetPromotion(int startIndex=0,int pageRow = 10,string search="",string searchBtn ="")
{
var data = Repositories.GetPromotion(startIndex,pageRow,search,searchBtn);
var countData = Repositories.CountPromotion(search, searchBtn);
var getData = new { data, countData };
return Content(JsonConvert.SerializeObject(getData),"application/json");
}
public ActionResult GetContentModal(string search,int startIndex=0,int pageRow=0,string keySearchBtn="")
{
var data = Repositories.GetContentModal(search,startIndex,pageRow, keySearchBtn);
var countData = Repositories.CountContentModel(search, keySearchBtn);
var getData = new { data,countData};
return Content(JsonConvert.SerializeObject(getData),"application/json");
}
public ActionResult SavePromotion(UpdatePromotionDTO promo)
{
var dataPromo = new UpdatePromotionDTO();
dataPromo.contents = promo.contents;
dataPromo.promotionName = promo.promotionName;
dataPromo.discountRate = promo.discountRate;
dataPromo.discountValue = promo.discountValue;
dataPromo.payment = promo.payment;
var cekData = Repositories.SavePromotion(dataPromo);
var success = false;
if (cekData != null) success = true;
var getData = new { cekData, success };
return Content(JsonConvert.SerializeObject(getData));
}
public ActionResult EditPromotion(string dataPromo)
{
var data = Repositories.EditPromotion(dataPromo);
var getData = new {data};
return Content(JsonConvert.SerializeObject(getData), "application/json");
}
public ActionResult DeletePromotion(string promoName)
{
var data = Repositories.DeletePromotion(promoName);
var success = false;
if (data) success = true;
var getData = new {data, success};
return Content(JsonConvert.SerializeObject(getData), "application/json");
}
public ActionResult SingleDeletePromotion(int promoId)
{
var success = false;
var data = Repositories.SingleDeletePromotion(promoId);
if (data) success = true;
var getData = new {data,success};
return Content(JsonConvert.SerializeObject(getData),"application/json");
}
************************************
View/Promotion
************************************
@{
if (Session["userID"] == null)
{
Response.Redirect("~/Login/index");
}
ViewBag.Title = "Promotion";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<style>
#customers {
font-family: Arial, Helvetica, sans-serif;
border-collapse: collapse;
width: 100%;
}
#customers td, #customers th {
border: 1px solid #ddd;
padding: 8px;
}
#customers tr:nth-child(even) {
background-color: #f2f2f2;
}
#customers tr:hover {
background-color: #ddd;
}
#customers th {
padding-top: 12px;
padding-bottom: 12px;
text-align: left;
background-color: #ffc6db;
color: white;
}
.btn-primary {
color: black;
background-color: #ffc6db;
border-color: #ffc6db;
}
button.btn.btn-primary.btn-sm.ml-3:hover {
background: white;
color: black;
}
.btn-primary:focus, .btn-primary.focus {
color: black;
background-color: #ffc6db;
border-color: #ffc6db;
box-shadow: white;
}
</style>
<div class=" page-fBot2">
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold ">@ViewBag.Title </h6>
</div>
<div class="card-body">
<div class="row" style="margin-top: 10px;margin-left:-16px;">
<div id="2_left" style="width:20%;height:110%;background:;float:left;" class="mr-2 ml-3">
<input type="text" id="keySearchFirst" placeholder="Search By" class="form-control" style="width:100%;height:107%;">
</div>
<div id="3_left" style="width:15%;height:110%;background:;float:left;">
<select class="form-control form-select-sm text-center" id="keySearchBtnFirst" onchange="getData(true)" style="background: #ffc6db; color:black;height:107%;font-size:14px;">
<option value="All">Select All</option>
<option value="IsCP">IsCP</option>
<option value="Category">Category</option>
<option value="Classification">Classification</option>
<option value="ContentName">Content Name</option>
<option value="PaymentCarrier">Payment Carrier</option>
<option value="PromotionName">Promotion Name</option>
</select>
</div>
<button type="button" class="btn btn-primary btn-sm ml-3" data-toggle="modal" onclick="clearModal()" data-target="#exampleModal">
<i class="fas fa-plus"></i> Add Promotion
</button>
</div>
</div>
</div>
<div class="card shadow mb-4">
<div class="card-header py-3">
</div>
<div class="card-body">
<div class="headTable" style="background-color:;width:100%;height:10%;">
<div class="form-group" style="float:right;margin-right:1%;">
<select class="form-control" id="txtPageSize" onchange="getData(true)">
<option value="10">10</option>
<option value="25">25</option>
<option value="50">50</option>
<option value="75">75</option>
<option value="100">100</option>
</select>
</div>
<label style="float:right;margin-right:2%;">Show Result </label>
</div>
<div class="table-responsive">
<table class="table" width="100%" cellspacing="0">
<thead>
<tr>
<td scope="col">Promotion Name</td>
<td scope="col">Content Name</td>
<td scope="col">IsCP</td>
<td scope="col">Category</td>
<td scope="col">Classification</td>
<td scope="col">Payment Carrier</td>
<td scope="col" align="center">Action</td>
</tr>
</thead>
<tbody id="listUser">
</tbody>
</table>
</div>
<nav aria-label="...">
<ul class="pagination" id="paging">
</ul>
</nav>
</div>
</div>
</div>
<div class="modal fade " id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-xl">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Promotion Management</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body ml-3">
<label>Promotion Name : </label>
<input type="text" id="txtPromotion" />
<label>Discount Rate : </label>
<input type="number" id="txtDiscountRate" min="1" maxlength = "2" oninput="javascript: if (this.value.length > this.maxLength) this.value = this.value.slice(0, this.maxLength);" /><br>
<label>Discount Value : </label>
<input type="number" id="txtDiscountValue" min="1" maxlength = "2" oninput="javascript: if (this.value.length > this.maxLength) this.value = this.value.slice(0, this.maxLength);" />
<label>Payman Carrier: </label>
<select id="txtPayment">
<option value="Digi">Digi</option>
<option value="Smartfren">Smartfren</option>
</select>
<table id="customers">
<tr>
<th>content Id</th>
<th>Content Name</th>
<th>IsCP</th>
<th>Category</th>
<th>Classification</th>
<th>Action</th>
</tr>
<tbody id="txtTableHeaderModal">
</tbody>
</table>
<br />
<div id="txtPagTableHeaderModal"></div>
<br />
@*
<label>Telko : </label>
<select id="txtTelko">
<option value="value">text</option>
</select>
<label>Filter : </label>
<select id="filter">
<option value="value">A</option>
<option value="value">B</option>
</select>*@
<br />
@*<button class="btn btn-primary btn-sm" onclick="search(true)">Search</button>*@
<input type="text" id="txtSearch" value="" style="float:left;margin-right:10px;" placeholder="Search By"/>
<div id="3_left" style="width:15%;height: 89%;background: ;float:left;margin-bottom:10px;">
<select class="form-control form-select-sm text-center" id="keySearchBtn" onchange="search(true)" style="background: #ffc6db; color:black;height:50%;font-size:14px;">
<option value="All">Select All</option>
<option value="ContentId">Content Id</option>
<option value="ContentName">Content Name</option>
<option value="IsCP">IsCP</option>
<option value="Category">Category</option>
<option value="Classification">Classification</option>
</select>
</div>
<table id="customers">
<tr>
<th>Content Id</th>
<th>Content Name</th>
<th>IsCP</th>
<th>Category</th>
<th>Classification</th>
<th>Action</th>
</tr>
<tbody id="txtTableModal">
</tbody>
</table>
<nav aria-label="...">
<ul class="pagination mt-3" id="txtPaginationModal">
</ul>
</nav>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" onclick="closeModal()" >Close</button>
@* <button type="button" class="btn btn-danger" onclick="deleteData()" id="txtDelete">Delete</button>*@
<button type="button" class="btn btn-primary" onclick="saveModal()" id="txtSaveData">Save changes</button>
</div>
</div>
</div>
</div>
@section scripts
{
<script>
var arrBodyTableHeader = [];//All Data Change in Header Table the data same with variable 'dataTableHeader'
var page = 0;
$(document).ready(function () {
getData();
search(true);
})
function getData(isResetPage = false) {
if (isResetPage) page = 0;
var pageRow = $("#txtPageSize").val();
var keySearchFirst = $("#keySearchFirst").val();
var keySearchBtnFirst = $("#keySearchBtnFirst").val();
$.ajax({
url: '/rest/GetPromotion?startIndex=' + page + '&pageRow=' + pageRow + '&search=' + keySearchFirst + '&searchBtn=' + keySearchBtnFirst,
success: function (respon) {
var data = respon.data;
tablePromotion(data);
bodyPagination(respon.countData, pageRow);
if (data.length == 0) {
notFound();
$("#paging").hide();
} else {
$("#paging").show();
}
},
error: function() { alert("Error")}
});
}
function tablePromotion(Data) {
var content = "";
for (var i = 0; i < Data.length; i++) {
var getData = Data[i];
content += `<tr><td>${getData.Name} </td>`;
content += `<td>${getData.Title} </td>`;
content += `<td>${getData.IsCP} </td>`;
content += `<td>${getData.CategoryName} </td>`;
content += `<td>${getData.ClassificationName} </td>`;
content += `<td>${getData.PaymentCarrier} </td>`;
content += `<td> <button class="btn-approve" onclick="getEditModal('${getData.Name}')"><i class="fas fa-eye"></i> See Details</button>
<button class="btn-approve" onclick="confirmsingleDeletePromotion('${getData.Id}')"><i class="fa fa-trash"></i> Delete</button>
</td></tr>`;
}
$("#listUser").html(content);
}
function bodyPagination(countData, pageRow) {
var content = "";
var beforePage = 2;
var nextPage = 3;
var totalPage = countData > pageRow ? countData / pageRow : 1;
var firstPage = page - beforePage >= 0 ? page - beforePage : 0;
var limitPage = page + nextPage >= totalPage ? totalPage : page + nextPage;
content += `
<li class="page-item ${page <= 0 ? "disabled" : ""}">
<a class="page-link" onclick = "movePagination(${page - 1})" href="#">Previous</a>
</li>`;
for (var i = firstPage; i < limitPage; i++) {
content += `<li class="page-item ${page == i ? "active" : ""}"><a class="page-link" onclick = "movePagination(${i})" href="#">${i + 1}</a></li>`;
}
content += `<li class="page-item ${page >= totalPage - 1 ? "disabled" : ""}">
<a class="page-link" onclick = "movePagination(${page + 1})" href="#">Next</a>
</li>`;
$("#paging").html(content);
if (countData == 0) notFound();
}
function movePagination(i) {
page = i;
getData();
}
var startIndex = 0;
function search(resetStartIndex=false) {
if (resetStartIndex) startIndex = 0;
var txtSearch = $("#txtSearch").val();
var keySearchBtn = $("#keySearchBtn").val();
var pageRowModal = 10;
$.ajax({
url: '/rest/GetContentModal?search=' + txtSearch + '&startIndex=' + startIndex + '&pageRow=' + pageRowModal + '&keySearchBtn=' + keySearchBtn,
success: function (respon) {
var content = '';
var getData = respon.data;
for (var i = 0; i < getData.length; i++) {
var data = getData[i];
content += `<tr><td>${data.ContentId}</td>`;
content += `<td>${data.Title}</td>`;
content += `<td>${data.IsCP}</td>`;
content += `<td>${data.CategoryName}</td>`;
content += `<td>${data.ClassificationName}</td>`;
content += `<td><a href="#" onclick="addTableHeaderModal(this)"><i class="fas fa-plus"></i> Add</a></td></tr>`;
}
bodyPaginationModal(respon.countData, pageRowModal);
if (getData.length == 0) {
notFound();
$("#txtPaginationModal").hide();
} else {
$("#txtPaginationModal").show();
}
$("#txtTableModal").html(content);
},
error: function () { alert("Error");}
});
}
//var noPage = 1;
function drawTableHeaderModal(dataTableHeader, noPage = 1) {
//just for convert the data 'dataTableHeader' same with 'arrBodyTableHeader'
var pageSizeModal = 5;
var content = "";
var pagData = dataTableHeader.slice((noPage - 1) * pageSizeModal, noPage * pageSizeModal);
for (var i = 0; i < pagData.length; i++) {
content += `<tr>`;
content += `<td>${pagData[i].ContentId}</td>`;
content += `<td>${pagData[i].Title}</td>`;
content += `<td>${pagData[i].IsCP}</td>`;
content += `<td>${pagData[i].CategoryName}</td>`;
content += `<td>${pagData[i].ClassificationName}</td>`;
content += `<td><a href="#" onclick="deleteBodyHeaderArr(${i})"><i class="fa fa-trash"></i> Remove</a></td>`;
content += `</tr>`;
}
$("#txtTableHeaderModal").html(content);
pagTableHeaderModal(arrBodyTableHeader, noPage, pageSizeModal);
}
function pagTableHeaderModal(arrBodyTableHeaderObj, noPageObj, pageSizeModalObj) {
var beforePag = 2;
var afterPag = 3;
var totalPag = arrBodyTableHeaderObj.length > pageSizeModalObj ? arrBodyTableHeaderObj.length / pageSizeModalObj : 1;
var limitPag = noPageObj + afterPag >= totalPag ? totalPag : noPageObj + afterPag;
var firstPag = noPageObj - beforePag >= 0 ? noPageObj - beforePag : 0;
var content1 = "";
content1 += `<nav aria-label="..."><ul class="pagination">`;
content1 += `<li class="page-item ${noPageObj <= 1 ? "disabled" : ""}"><a class="page-link" onclick="changePagModalHeader(${noPageObj-1})" href="#">Previous</a></li>`;
for (var x = firstPag; x < limitPag; x++) {
content1 += `<li class="page-item ${noPageObj == x+1 ? "active" : ""}"><a onclick="changePagModalHeader(${x+1})" class="page-link" changePagModalHeader(${x}) href="#">${x + 1}</a></li>`;
}
content1 += `<li class="page-item ${noPageObj >= totalPag ? "disabled": ""}"><a class="page-link" onclick="changePagModalHeader(${noPageObj + 1})" href="#">Next</a></li>`;
content1 += `</ul></nav>`;
$("#txtPagTableHeaderModal").html(content1);
}
function changePagModalHeader(no) {
drawTableHeaderModal(arrBodyTableHeader,no);
}
function addTableHeaderModal(obj) {
var content = "";
var getBodyTableHeader = $(obj).parent().parent().find("td");
var objBodyTableHeader = { ContentId: getBodyTableHeader[0].textContent, Title: getBodyTableHeader[1].textContent, IsCP: getBodyTableHeader[2].textContent, CategoryName: getBodyTableHeader[3].textContent, ClassificationName:getBodyTableHeader[4].textContent}
var searchArrBodyTableHeader = arrBodyTableHeader.find(x => x.ContentId == objBodyTableHeader.ContentId);
if (!searchArrBodyTableHeader) arrBodyTableHeader.push(objBodyTableHeader);
drawTableHeaderModal(arrBodyTableHeader);
}
function deleteBodyHeaderArr(indexArr) {
arrBodyTableHeader.splice(indexArr, 1);
drawTableHeaderModal(arrBodyTableHeader);
}
function bodyPaginationModal(countData, pageRowModal) {
var content = "";
var beforePage = 2;
var afterPage = 3;
var totalPage = countData > pageRowModal ? countData / pageRowModal : 1;
var firstPage = startIndex - beforePage >= 0 ? startIndex - beforePage : 0;
var limitPage = startIndex + afterPage >= totalPage ? totalPage : startIndex + afterPage;
content += `<li class="page-item ${startIndex <= 0 ? "disabled" : ""}"><a class="page-link" onclick="movePaginationModal(${startIndex-1})" href="#">Previous</a></li>`;
for (var i = firstPage; i < limitPage;i++) {
content += `<li class="page-item ${startIndex == i ? "active":""}"><a class="page-link" onclick="movePaginationModal(${i})" href="#">${i+1}</a></li>`;
}
content += `<li class="page-item ${startIndex >= totalPage - 1 ? "disabled" : ""}"><a class="page-link" href="#" onclick="movePaginationModal(${startIndex+1})">Next</a></li>`;
$("#txtPaginationModal").html(content);
}
function movePaginationModal(i) {
startIndex = i;
search();
}
var input = document.getElementById("txtSearch");
input.addEventListener("keyup", function (event) {
if (event.keyCode === 13) {
search(true);
}
});
var input = document.getElementById("keySearchFirst");
input.addEventListener("keyup", function (event) {
if (event.keyCode === 13) {
getData(true);
}
});
function notFound() {
swal("Failed", "Data Not Found", "error");
}
function failedBackHome() {
swal("Failed", "Process Data Failed", "error");
setTimeout(function () { window.location.href = "/Home/Promotion"; }, 1000);
}
function success() {
swal("success", "Data Saved Successfully", "success");
}
function getEditModal(dataPromo) {
$("#txtDelete").show();
$.ajax({
url: '/rest/EditPromotion?dataPromo=' + dataPromo,
success: function (respon) {
var getDataContent = respon.data.contents;
var getData = respon.data;
$('#exampleModal').modal('show');
$("#txtPromotion").val(getData.promotionName);
$("#txtDiscountValue").val(getData.discountRate);
$("#txtDiscountRate").val(getData.discountValue);
$("#txtPayment").val(getData.payment);
arrBodyTableHeader = getDataContent;
drawTableHeaderModal(arrBodyTableHeader, noPage = 1);
},
error: function () { alert("Error") }
});
}
function clearArrayHeaderTable() {
for (var i = 0; arrBodyTableHeader.length; i++) {
arrBodyTableHeader.splice(i, arrBodyTableHeader.length + 1);
}
}
function clearModal() {
$("#txtTableHeaderModal").empty();
$("#txtPromotion").val("");
$("#txtDiscountValue").val("");
$("#txtDiscountRate").val("");
$("#txtPayment").val("");
$("#txtPagTableHeaderModal").empty();
$("#txtDelete").hide();
clearArrayHeaderTable();
}
function closeModal() {
clearModal();
clearArrayHeaderTable();
$('#exampleModal').modal('hide');
}
function deleteData() {
var promoName = $("#txtPromotion").val();
$.ajax({
url: '/Rest/DeletePromotion?promoName=' + promoName,
success: function (respon) {
if (respon.success) {
swal("success", "Data Success Deleted", "success");
setTimeout(function () { window.location.href = "/Home/Promotion"; }, 1000);
} else {
failedBackHome();
}
},
error: function () { alert("Error")}
});
}
function confirmsingleDeletePromotion(Id) {
swal({
title: "Are you sure?",
text: "You won't be able to revert this!",
icon: "warning",
buttons: true,
dangerMode: true,
buttons: ["Cancel", "Yes,Delete It"],
})
.then((willDelete) => {
if (willDelete) {
singleDeletePromotion(Id);
}
});
}
function singleDeletePromotion(Id) {
$.ajax({
url: '/Rest/SingleDeletePromotion?promoId=' + Id,
success: function (respon) {
if (respon.success) {
console.log(respon.success);
swal("success", "Data Success Deleted", "success");
setTimeout(function () { window.location.href = "/Home/Promotion"; }, 1000);
} else {
failedBackHome();
}
}
});
}
function saveModal() {
var promotionName = $("#txtPromotion").val();
var discountRate = $("#txtDiscountRate").val();
var discountValue = $("#txtDiscountValue").val();
var Payment = $("#txtPayment").val();
var cekTableHeaderModal = $("#txtTableHeaderModal").find("td");
let pattern = /[0-9]/g;
var message = "";
let resultRate = discountRate.match(pattern);
let resultValue = discountValue.match(pattern);
if (promotionName == '') message += "Promotion Name Can't Be Empty \n";
if (discountRate == '') message += "Discount Rate Can't Be Empty \n";
if (discountValue == '') message += "Discount Value Can't Be Empty \n";
if (Payment == '' || Payment == null) message += "Payment Can't Be Empty \n";
if (!resultRate) message += "Discount Rate Must Be Number \n";
if (!resultValue) message += "Discount Value Must Be Number \n";
if (cekTableHeaderModal.length <= 0) message += "Table Can't Be Empty \n";
if (message != "") {
swal("Failed", message, "error");
}
else
{
document.getElementById("txtSaveData").disabled = true;
var objBodyTableHeader = [];
for (var i = 0; i < arrBodyTableHeader.length; i++) {
var data = arrBodyTableHeader[i];
objBodyTableHeader.push(parseInt(data.ContentId));
}
var dataBodyTableHeader = { contents: objBodyTableHeader, promotionName: promotionName, discountRate: discountRate, discountValue: discountValue, payment: Payment }
console.log(dataBodyTableHeader);
$.ajax({
async: true,
type: 'POST',
url: '/Rest/SavePromotion',
data: JSON.stringify(dataBodyTableHeader),
dataType: 'JSON',
contentType: 'application/json;charset=utf-8',
success: function (respon) {
if (respon.success) {
document.getElementById("txtSaveData").disabled = false;
success();
setTimeout(function () { window.location.href = "/Home/Promotion"; }, 1000);
} else {
alert("Failed");
document.getElementById("txtSaveData").disabled = false;
}
},
error: function (response) {
alert("Error");
document.getElementById("txtSaveData").disabled = false;
}
});
}
}
</script>
}
Tidak ada komentar