@page "/Cod"
@using Front.Services
@using Shared.DTOs
@using Front.CUSComponent
@using Shared.DTOs.Serch
@inject HttpClientController hc;
@inject Fixedvalues fv;
<PageTitle>کالا</PageTitle>
<Modal Size="ModalSize.ExtraLarge" @ref="modal" />
<Preload LoadingText="در حال بارگذاری..." />
@* search *@
<div class="row">
    <h4 class="fw-bold py-3 mb-4">
        <span class="text-muted fw-light">سرویس ها /</span> کالا
    </h4>
    <div class="col-md-12">
        <div class="card mb-2">
            <div class="row">
                <div class="col-md-12">
                    <ul class="list-group fa-padding" style="border: 2px solid #0d6efd">
                        <li class="list-group-item" data-toggle="modal" data-target="#issue">
                            <div class="row g-3">
                                <div class="col-md-1">
                                    <input @bind-value="itemsearch.CODID" placeholder="شناسه" style="text-align:center;" class="form-control" type="text"  >
                                </div>
                                <div class="col-md-2">
                                    <input @bind-value="itemsearch.Title" placeholder="نام کالا" style="text-align:center;" class="form-control" type="text">
                                </div>
                                <div class="col-md-2">
                                    <select @bind="itemsearch.UnitID" class="form-control" aria-label="Default select example">
                                        <option value="0" style="color: #b5b5b5" selected>واحد اندازه گیری ...</option>
                                        @if (Unitrequest!=null)
                                        {
                                            foreach (var item in Unitrequest)
                                            {
                                                <option value="@item.ID">@item.Title</option>
                                            }
                                        }
                                    </select>
                                </div>
                                <div class="col-md-2">
                                    <input @bind-value="itemsearch.ItemTaxID" placeholder="شناسه مالیاتی" style="text-align:center;" class="form-control" type="text">
                                </div>
                            
                                <div class="col-auto">
                                    <button @onclick="() => LoadCod(1)" type="submit" class="btn btn-primary">جستجو</button>
                                </div>
                            </div>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
</div>

@* action *@
<div class="row">
    <div class="col-md-12">
        <ul class="list-group fa-padding" >
            <li class="list-group-item" data-toggle="modal" data-target="#issue">
                <div class="row g-3">
                    <div class="col-auto">

                        <button type="submit" @onclick="()=>CodItem(0)" class="btn btn-primary">جدید</button>


                    </div>
                    <div class="col-auto">

                        <Button Color="ButtonColor.Info" @onclick="Showstuff" Type="ButtonType.Button">
                            دریافت شناسه کالا
                        </Button>

                    </div>
                </div>

            </li>
        </ul>
       
    </div>
</div>

@* alert *@
<div class="row">
    <Alert hidden="@Hidealert" Color="@alertColor" Dismissable="false">
        <Icon Name="@alertIconName" class="me-2"></Icon>
        @alertMessage
    </Alert>

</div>

@* data *@
@if (request != null)
{
    <LTable ModelinComponent="request?.list" OnMultipleOfThree="EventCallback.Factory.Create<int>(this,CodItem)" />
    @* pagination *@
    <p style="color:red">@request?.RowCount آیتم یافت شد</p>
    <nav aria-label="Page navigation">
        <ul class="pagination justify-content-center">
            @for (int page = 1; page <= request?.PageCount; page++)
            {
                if (page == PageIndex)
                {
                    <li class="page-item active">
                        <a class="page-link" href="@hc._nav.GetUriWithQueryParameter("PageIndex",page)">@(page)</a>
                    </li>
                }
                else
                {
                    <li class="page-item">
                        <a class="page-link" href="@hc._nav.GetUriWithQueryParameter("PageIndex",page)">@(page)</a>
                    </li>
                }
            }
           
           
        </ul>
    </nav>
}

@layout PanelLayout
@code {
    //private Modal stuffmodal = default!;
    [Inject] protected PreloadService PreloadService { get; set; } = default!;
    ItemSerchGetCOD itemsearch = new ItemSerchGetCOD();
    [Parameter, SupplyParameterFromQuery]
    public int? PageIndex { get; set; }
    public List<IdName<int>>? Unitrequest { get; set; }
    public Shared.DTOs.PagingDto<RCOD>? request { get; set; }
    private Modal modal = default!;
    // alert
    AlertColor alertColor = AlertColor.Primary;
    IconName alertIconName = IconName.CheckCircleFill;
    bool Hidealert = true;
    string alertMessage = "";

    protected override async Task OnInitializedAsync()
    {

        Unitrequest =await fv.GetUnits();

        await base.OnInitializedAsync();
    }
    protected override async Task OnParametersSetAsync()
    {
        if (PageIndex == null) PageIndex = 1;
        await LoadCod(PageIndex.Value);
        await base.OnParametersSetAsync();
    }  
}
@functions {
    private void ShowSuccessAlert(string msg)
    {
        Hidealert = false;
        alertColor = AlertColor.Success;
        alertIconName = IconName.CheckCircleFill;
        alertMessage = msg;
    }
    private void ShowDangerAlert(string msg)
    {
        Hidealert = false;
        alertColor = AlertColor.Danger;
        alertIconName = IconName.ExclamationTriangleFill;
        alertMessage = msg;
    }
    public async Task LoadCod(int pi)
    {

        itemsearch.PageSize = 10;
        itemsearch.PageIndex = pi;
        PreloadService.Show(SpinnerColor.Dark);
        var rsp = await hc.Post<ItemSerchGetCOD>("COD/GetAll", itemsearch);
        if (rsp.IsSuccessStatusCode)
        {
            request = await rsp.Content.ReadFromJsonAsync<PagingDto<RCOD>>();
        }
        else if(rsp.StatusCode==System.Net.HttpStatusCode.Forbidden)
        {
            ShowDangerAlert("شما دسترسی به خواندن اطلاعات کالا را نداربد");
        }
        else
        {
            ShowDangerAlert("خطایی در اجرای عملیات رخ داده");
        }
        PreloadService.Hide();
    }
    public async Task CallBackCodItem(ActionInResultComponent result)
    {

        if (result.Action == ComponentAction.add)
        {
            if (result.Status==ComponentStatus.success)
                ShowSuccessAlert("کالا جدید با موفقیت اضافه شد");

        }
        else if (result.Action == ComponentAction.update)
        {
            if (result.Status == ComponentStatus.success)
                ShowSuccessAlert("اطلاعات کالا با موفقیت ویرایش شد");
        }
        else if (result.Action == ComponentAction.delete)
        {
            if (result.Status == ComponentStatus.success)
                ShowSuccessAlert("کالا با موفقیت حذف شد");
        }

        if (result.Status == ComponentStatus.success)
            await LoadCod(1);

        await modal.HideAsync();
    }
    public async Task CodItem(int ID)
    {
        var parameters = new Dictionary<string, object>();

        if(ID == 0) parameters.Add("Cod", new RCOD(){ID=0});
        else parameters.Add("Cod", request.list.Where(w=>w.ID==ID).First().Clone());
        parameters.Add("Unitrequest", Unitrequest);
        parameters.Add("OnMultipleOfThree", EventCallback.Factory.Create<ActionInResultComponent>(this, CallBackCodItem));
        await modal.ShowAsync<CodItem>(title: ID == 0 ? "کالا جدید" : "ویرایش اطلاعات", parameters: parameters);


    }
    public async Task Showstuff()
    {
        var parameters = new Dictionary<string, object>();
        await modal.ShowAsync<Front.CUSComponent.Taxstuff>(title: "دریافت شناسه کالا", parameters: parameters);


    }
}