shoc-backend/SeaHavenIndustries/Components/Pages/Quotes/Index.razor
2024-09-28 14:58:36 -04:00

280 lines
12 KiB
Text

@page "/quotes"
<Header pagename="Quotes " icon='<i class="material-icons-round opacity-10">article</i>'></Header>
<div class="row mt-3">
<div class="col-12">
<div class="card cardbg">
<!-- Card header -->
<div class="card-header pb-0 cardbg">
<div class="d-lg-flex">
<div>
<h5 class="mb-0 ps-3">Quotes</h5>
</div>
<div class="ms-auto my-auto mt-lg-0 mt-4">
<div class="ms-auto my-auto d-flex flex-wrap justify-content-end text-end">
<div class="ms-auto my-auto d-flex flex-wrap justify-content-end text-end">
@* <RadzenButton Text="Export XLS" Icon="grid_on" Click="@(args => Export("excel"))" class="mb-0 me-2" /><RadzenButton Text="Export CSV" Icon="wrap_text" Click="@(args => Export("csv"))" class="mb-0 me-2" />
*@
<a class="btn bg-gradient-info btn-sm mb-0" href="createquotes/0">+&nbsp; New Quote</a>
</div>
</div>
</div>
</div>
</div>
<div class="card-body ">
<RadzenDataGrid style="height: auto" @ref="grid" Count="@count" PagerAlwaysVisible="true" ShowPagingSummary="true" IsLoading=@isLoading Data="@model" LoadData="@LoadData" AllowSorting="true" AllowFiltering="true" AllowPaging="true" PageSize="10" PagerHorizontalAlign="HorizontalAlign.Right" TItem="QuotesViewModel" ColumnWidth="200px">
<Columns>
<RadzenDataGridColumn TItem="QuotesViewModel" Property="QouteId" Title="Quote ID" Width="100px" />
<RadzenDataGridColumn TItem="QuotesViewModel" Property="WOID" Title="WO ID" Width="100px" />
<RadzenDataGridColumn TItem="QuotesViewModel" Property="SiteName" Title="Site Name" Width="120px" />
<RadzenDataGridColumn TItem="QuotesViewModel" Property="ProjectName" Title="Project Name" Width="120px" />
<RadzenDataGridColumn TItem="QuotesViewModel" Property="TotalAmount" Title="Total Quotes" Width="100px" />
<RadzenDataGridColumn TItem="QuotesViewModel" Property="Status" Title="Status" Width="120px">
<Template Context="itm">
@if (itm.Status == "Open")
{
<span class="badge badge-success">Open</span>
}
else if (itm.Status == "On Hold")
{
<span class="badge badge-warning">On Hold</span>
}
else if (itm.Status == "In Progress")
{
<span class="badge badge-danger">In Progress</span>
}
else if (itm.Status == "Done")
{
<span class="badge badge-info">Done</span>
}
</Template>
</RadzenDataGridColumn>
<RadzenDataGridColumn TItem="QuotesViewModel" Title="Action" Context="employee" Filterable="false" Sortable="false" TextAlign="TextAlign.Left" Width="186px">
<Template Context="model">
<RadzenButton Icon="visibility" ButtonStyle="ButtonStyle.Info" Variant="Variant.Flat" Size="ButtonSize.Medium"
Click="@(args => view(model.Id))" @onclick:stopPropagation="true">
</RadzenButton>
<RadzenButton Icon="download" ButtonStyle="ButtonStyle.Success" Variant="Variant.Flat" Size="ButtonSize.Medium"
Click="@(args => GeneratePdf(model.Id))" @onclick:stopPropagation="true">
</RadzenButton>
<RadzenButton Icon="email" ButtonStyle="ButtonStyle.Warning" Variant="Variant.Flat" Size="ButtonSize.Medium"
Click="@(args => SendPdf(model.Id))" @onclick:stopPropagation="true">
</RadzenButton>
<RadzenButton Icon="edit" ButtonStyle="ButtonStyle.Success" Variant="Variant.Flat" Size="ButtonSize.Medium"
Click="@(args => Edit(model.Id))" @onclick:stopPropagation="true">
</RadzenButton>
<RadzenButton Icon="delete" ButtonStyle="ButtonStyle.Danger" Variant="Variant.Flat" Size="ButtonSize.Medium"
Click="@(args => DeleteSweetAlert(model.Id))" @onclick:stopPropagation="true">
</RadzenButton>
</Template>
</RadzenDataGridColumn>
</Columns>
</RadzenDataGrid>
</div>
</div>
</div>
</div>
<div class="d-none" @ref="StepService.htmlContentReference">
<QuotesPDF id="@eid" ></QuotesPDF>
</div>
@* @if (isloading)
{
<div class="loading">Loading&#8230;</div>
} *@
@using Microsoft.JSInterop
@inject IJSRuntime JSRuntime
@inject NavigationManager Navigation;
@using SeaHavenIndustries.Data.IRepository;
@inject IQuotesRepository _services;
@using System.Text.Json
@using SeaHavenIndustries.Data.Services
@using SeaHavenIndustries.Helper.ViewModels
@using global::Data.SeaHavenIndustries;
@inject NorthwindService service
@using System.Linq.Dynamic.Core
@inject ChangeStepService StepService
@inject PdfService pdfService
@code {
[Parameter]
public int? id { get; set; }
private int step = 0;
RadzenDataGrid<QuotesViewModel> grid;
int count;
int eid;
private bool ispdf = false;
IEnumerable<QuotesViewModel> model;
bool isLoading = false;
bool isloading = false;
protected void Edit(int id)
{
Navigation.NavigateTo("createquotes/" + id);
}
protected void view(int id)
{
Navigation.NavigateTo("quotesdetails/" + id);
}
private async Task SelectAllQuotes()
{
await JSRuntime.InvokeVoidAsync("selectAllQuotes");
}
private async Task DeleteSweetAlert(int val)
{
var result = await JSRuntime.InvokeAsync<JsonElement>("Swal.fire", new
{
html = "<div style='text-align: center;'><img src='./assets/img/delete icon.png' style='width: 100px;' /></div>" +
"<h4>Are you sure you want to delete</h4>" +
"<p style='font-size: 18px;'>\"Nicolas Alvarado\"</p>",
type = "warning",
showCancelButton = true,
customClass = new
{
confirmButton = "btn btn-success m-1",
cancelButton = "btn btn-danger m-1"
},
buttonsStyling = false,
confirmButtonText = "Yes",
cancelButtonText = "No"
});
if (result.TryGetProperty("isConfirmed", out var isConfirmedProperty) && isConfirmedProperty.GetBoolean())
{
// Call your delete API here
var response = await _services.DeleteQuotes(val);
if (response == "success")
{
await JSRuntime.InvokeVoidAsync("Swal.fire", new
{
icon = "success",
title = "Deleted!",
text = "Data has been deleted.",
type = "primary",
showCancelButton = false,
customClass = new
{
confirmButton = "btn btn-success",
cancelButton = "btn btn-danger"
},
buttonsStyling = false,
confirmButtonText = "Ok"
});
model = await _services.GetQuotesqueryable();
// await JSRuntime.InvokeVoidAsync("location.reload");
}
else
{
await JSRuntime.InvokeVoidAsync("Swal.fire", new
{
title = "Not Deleted!",
text = "Unsuccessful! Cannot delete this.",
type = "danger"
});
}
}
}
async Task LoadData(LoadDataArgs args)
{
isLoading = true;
await Task.Yield();
// This demo is using https://dynamic-linq.net
var query = await _services.GetQuotesqueryable();
if (!string.IsNullOrEmpty(args.Filter))
{
// Filter via the Where method
query = query.Where(args.Filter);
}
if (!string.IsNullOrEmpty(args.OrderBy))
{
// Sort via the OrderBy method
query = query.OrderBy(args.OrderBy);
}
// Important!!! Make sure the Count property of RadzenDataGrid is set.
count = query.Count();
// Perform paging via Skip and Take.
model = query.Skip(args.Skip.Value).Take(args.Top.Value).ToList();
isLoading = false;
ispdf = true;
}
public void Export(string type)
{
service.Export("Quotes", type, new Query()
{
OrderBy = grid.Query.OrderBy,
Filter = grid.Query.Filter,
Select = string.Join(",", grid.ColumnsCollection.Where(c => c.GetVisible() && !string.IsNullOrEmpty(c.Property))
.Select(c => c.Property.Contains(".") ? $"{c.Property} as {c.Property.Replace(".", "_")}" : c.Property))
});
}
private async Task GeneratePdf(int qid)
{
eid = qid;
ispdf = true;
isloading = true;
await Task.Delay(TimeSpan.FromSeconds(4));
var htmlContent = await JSRuntime.InvokeAsync<string>("getHtmlContent", StepService.htmlContentReference);
var pdfBytes = await pdfService.GeneratePdf(htmlContent);
ispdf = false;
isloading = false;
var pdfBase64 = Convert.ToBase64String(pdfBytes);
await JSRuntime.InvokeVoidAsync("downloadFile", pdfBase64, "Quote.pdf");
}
private async Task SendPdf(int qid)
{
eid = qid;
ispdf = true;
isloading = true;
await Task.Delay(TimeSpan.FromSeconds(4));
var htmlContent = await JSRuntime.InvokeAsync<string>("getHtmlContent", StepService.htmlContentReference);
var pdfBytes = await pdfService.GeneratePdf(htmlContent);
var result = await _services.SendEmailQuote(pdfBytes, qid);
isloading = false;
if (result == "success")
{
await ShowSweetAlert("Email send successfully", "success");
}
else
{
await ShowSweetAlert(result, "error");
}
ispdf = false;
}
private async Task ShowSweetAlert(string title, string icon)
{
// Display a SweetAlert dialog
await JSRuntime.InvokeVoidAsync("Swal.fire", new
{
toast = true,
showConfirmButton = false,
timer = 3000,
title = title,
icon = icon,
position = "top-end"
});
await JSRuntime.InvokeVoidAsync("closemodal");
}
}