@page "/Setting"
@* @page "/Profile/{from}" *@
@using Front.Services
@using Shared.DTOs
@layout PanelLayout
@inject UserAuthenticationDTO userinfo
@inject HttpClientController _hc
@inject ILocalStorageService Storage;
<PageTitle>تنظیمات</PageTitle>
<div class="container-xxl flex-grow-1 container-p-y">
    <h4 class="fw-bold py-3 mb-4">
        <span class="text-muted fw-light">متفرقه /</span> تنظیمات
    </h4>
    <div class="row">
        <Alert hidden="@Hidealert" Color="@alertColor" Dismissable="false">
            <Icon Name="@alertIconName" class="me-2"></Icon>
            @alertMessage
        </Alert>


    </div>
    <div class="row">
        <div class="col-md-12">
            <div class="card mb-4">
                <h5 class="card-header">مالیات</h5>
                <div class="card-body">
                    <EditForm Model="InfoModel" OnSubmit="Submit" FormName="TaxPayer">
                        <div class="row">
                            <div class="mb-3 col-md-6">
                                <label for="defaultFormControlInput" class="form-label">کد اقتصادی</label>
                                <InputText @bind-Value="@InfoModel.EconomicCode" style="text-align:center;" class="form-control" type="text" id="EconomicCode" />
                            </div>
                            <div class="mb-3 col-md-6">
                                <label for="defaultFormControlInput" class="form-label">حافظه مالیاتی</label>
                                <InputText style="text-align:center;" @bind-Value="@InfoModel.UniqeMemory" class="form-control" type="text" id="UniqeMemory" />
                            </div>
                            <div class="mb-3 col-md-6">
                                <label for="defaultFormControlInput" class="form-label">کلید خصوصی</label>
                                <InputText @bind-Value="@InfoModel.PrivateKey" style="text-align:center;" class="form-control" type="text" id="PrivateKey" />
                            </div>
                            <div class="mb-3 col-md-6">
                                <label for="defaultFormControlInput" class="form-label">کد شعبه</label>
                                <InputText @bind-Value="@InfoModel.BranchID" style="text-align:center;" class="form-control" type="text" id="BaranchID" />
                            </div>
                        </div>

                        <div class="mt-2">
                            <button type="submit" class="btn btn-primary">ارسال</button>
                        </div>
                      
        
                    </EditForm>
          
                </div>
            </div>
        </div>
    </div>

</div>



@code {

    [SupplyParameterFromForm]
    public TaxPayerInfoDto? InfoModel { get; set; }

    // alert
    AlertColor alertColor = AlertColor.Primary;
    IconName alertIconName = IconName.CheckCircleFill;
    bool Hidealert = true;
    string alertMessage = "";

    protected override async Task OnInitializedAsync()
    {
        InfoModel ??= new();
        var request = await _hc.Get($"Company/TaxPayerInfo");
        if (request.IsSuccessStatusCode)
        {
            InfoModel = await request.Content.ReadFromJsonAsync<TaxPayerInfoDto>();
        }
        else
            _hc._nav.NavigateTo("Panel");

      
        await base.OnInitializedAsync();
    }

}
@functions{
    private void ShowDangerAlert(string msg)
    {
        Hidealert = false;
        alertColor = AlertColor.Danger;
        alertIconName = IconName.ExclamationTriangleFill;
        alertMessage = msg;
    }
    private void ShowSuccessAlert(string msg)
    {
        Hidealert = false;
        alertColor = AlertColor.Success;
        alertIconName = IconName.CheckCircleFill;
        alertMessage = msg;
    }
   
   
    private async Task Submit()
    {

        var request = await _hc.Put($"Company/ChangeTaxPayerInfo", InfoModel);
            if (request.IsSuccessStatusCode)
            {
                if (await request.Content.ReadFromJsonAsync<bool>())
                {
                    ShowSuccessAlert("تغییر نام با موفقیت انجام شد");

                }
               else
                {
                    ShowDangerAlert("خطایی در اجرای عملیات رخ داده");
                }


            }
            else
            {
                var errors = await request.Content.ReadFromJsonAsync<List<string>>();
                ShowDangerAlert(errors[0]);
            }
        

    }
   

}