<PageTitle>سامانه مودیان</PageTitle>
@inject HttpClientController hc;
@layout PanelLayout
@page "/TaxPayer"
@page "/TaxPayer/{routed:int}"
@using Front.Services
@using Shared
@using Shared.DTOs
@using Shared.DTOs.Serch
<Modal @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.ID" placeholder="شناسه" style="text-align:center;" class="form-control" type="text">
                                </div>
                                <div class="col-md-2">
                                    <input @bind-value="itemsearch.InvoiceID" placeholder="شناسه صورتحساب" style="text-align:center;" class="form-control" type="text">
                                </div>
                                <div class="col-md-2">
                                    <select @bind="itemsearch.invoiceType" class="form-control" aria-label="Default select example">
                                        <option value="100" style="color: #b5b5b5" selected>نوع صورتحساب...</option>
                                        @foreach (InvoiceType i in Enum.GetValues(typeof(InvoiceType)))
                                        {

                                            <option value="@i">@i.GetEnumDisplayName()</option>


                                        }

                                    </select>
                                </div>
                                <div class="col-md-2">
                                    <select @bind="itemsearch.SentStatus" class="form-control" aria-label="Default select example">
                                        <option value="100" style="color: #b5b5b5" selected>وضعیت...</option>
                                        @foreach (SentStatus i in Enum.GetValues(typeof(SentStatus)))
                                        {

                                            <option value="@i">@i.GetEnumDisplayName()</option>


                                        }

                                    </select>
                                </div>
                                <div class="col-auto">
                                    <Button Disabled="SpinnerVisible" @onclick="() => Load(1)" Color="ButtonColor.Primary">جستجو</Button>
                                </div>
                            </div>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
</div>
@* alert *@
<div class="row">
    <div class="col-md-10">
    <Alert hidden="@Hidealert" Color="@alertColor" Dismissable="false">
        <Icon Name="@alertIconName" class="me-2"></Icon>
        @alertMessage
    </Alert>
    </div>
    <div class="col-md-2">
        <Spinner Visible="SpinnerVisible" Color="SpinnerColor.Primary" />
    </div>
</div>
@* data *@
@if (request != null)
{
    <LTable ModelinComponent="request?.list" OnMultipleOfThree="EventCallback.Factory.Create<int>(this,SentTaxItem)" />
    @* 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>
}

@code {
    public bool SpinnerVisible { get; set; } = false;
    private Modal modal = default!;
    [Inject] protected PreloadService PreloadService { get; set; } = default!;
    ItemSerchGetSentTax itemsearch = new ItemSerchGetSentTax();
    [Parameter]
    public int? routed { get; set; } = 0;
    // alert
    AlertColor alertColor = AlertColor.Primary;
    IconName alertIconName = IconName.CheckCircleFill;
    bool Hidealert = true;
    string alertMessage = "";
    [Parameter, SupplyParameterFromQuery]
    public int? PageIndex { get; set; }
    public Shared.DTOs.PagingDto<SentTaxDto>? request { get; set; }

    protected override Task OnInitializedAsync()
    {
        SpinnerVisible = false;
        return base.OnInitializedAsync();
    }
    protected override async Task OnParametersSetAsync()
    {
        SpinnerVisible = false;
        if (routed.HasValue && routed > 0)
        {
            ShowSuccessAlert($"صورتحساب شماره {routed} با موفقیت ارسال شد " +'\n'+
            "برای برای 'نمایش وضعیت' آن را تعیین وضعیت کنید");
            itemsearch.InvoiceID = routed.Value;
        }

        if (PageIndex == null) PageIndex = 1;
        await Load(PageIndex.Value);
        await base.OnParametersSetAsync();
    }
}
@functions{
    public async Task SentTaxItem(int ID)
    {
        PreloadService.Show(SpinnerColor.Dark);
        SpinnerVisible = true;
        var rsp = await hc.Get($"TaxPayer/GetResult/{ID}");
        if (rsp.IsSuccessStatusCode)
        {
            var requestsendTax = await rsp.Content.ReadFromJsonAsync<DataInSendTaxDto>();
            //عویض کردن اطلاعات در گرید
            var ingriddata= request.list.Where(w => w.ID == ID).First();
            ingriddata.SentStatus = requestsendTax.SentStatus;
            ingriddata.msgSentStatus = requestsendTax.msgSentStatus;
            SpinnerVisible = false;
            PreloadService.Hide();
            //-----------------
            var parameters = new Dictionary<string, object>();
            parameters.Add("inData", requestsendTax);
            await modal.ShowAsync<CUSComponent.SendTaxItem>(title: "وضعیت صورتحساب ارسالی", parameters: parameters);
        }
        else{
            var request = await rsp.Content.ReadFromJsonAsync<List<string>>();
            SpinnerVisible = false; PreloadService.Hide();
            ShowDangerAlert(request[0]);
        }

    }
    public async Task Load(int pi)
    {
        SpinnerVisible = true;
        itemsearch.PageSize = 10;
        itemsearch.PageIndex = pi;
        PreloadService.Show(SpinnerColor.Dark);
        var rsp = await hc.Post<ItemSerchGetSentTax>("TaxPayer/GetAllSentTax", itemsearch);
        if (rsp.IsSuccessStatusCode)
        {
            request = await rsp.Content.ReadFromJsonAsync<PagingDto<SentTaxDto>>();
        }
        else if (rsp.StatusCode == System.Net.HttpStatusCode.Forbidden)
        {
            ShowDangerAlert("شما دسترسی به خواندن اطلاعات صورتحساب را نداربد");
        }
        else
        {
            ShowDangerAlert("خطایی در اجرای عملیات رخ داده");
        }
        PreloadService.Hide();
        SpinnerVisible = false;
    }
    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;
    }
}