@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">
        <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>
    <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 Disabled="SpinnerVisible" Type="ButtonType.Submit" Color="ButtonColor.Primary">ارسال</Button>
                        </div>
                        <div class="mt-2">
                        <Button Disabled="SpinnerVisible"  Color="ButtonColor.Info" @onclick="OnClickCheckEconomicCode" Type="ButtonType.Button">
                            بررسی کد اقتصادی
                        </Button>
                        </div>
                        <div class="mt-2">
                        <Button Disabled="SpinnerVisible"  Color="ButtonColor.Info" @onclick="OnClickCheckUniqeMemory" Type="ButtonType.Button">
                            بررسی حافظه مالیاتی
                        </Button>
                        </div>
                    </EditForm>
          
                </div>
            </div>
        </div>
    </div>

</div>



@code {
    public bool SpinnerVisible { get; set; } = false;
    [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{
    public async Task OnClickCheckUniqeMemory()
    {
        SpinnerVisible = !SpinnerVisible;
        if (!string.IsNullOrEmpty(InfoModel.UniqeMemory))
        {

            var rsp = await _hc.Get($"TaxPayer/FiscalInformation?item={InfoModel.UniqeMemory}");
            if (rsp.IsSuccessStatusCode)
            {
                var request = await rsp.Content.ReadFromJsonAsync<FiscalInformationModelDto>();
                if (request.FiscalStatus !=0)
                {
                    ShowDangerAlert("این حافظه مالیاتی غیرفعال است");
                }
                else
                    ShowInfoAlert($"حافظه مالیاتی فعال و برای کد اقتصادی '{request.EconomicCode}' در دسترس می باشد");

            }
            else if (rsp.StatusCode == System.Net.HttpStatusCode.NotFound)
            {
                ShowDangerAlert("حافظه مالیاتی یافت نشد");
            }
            else
            {
                ShowDangerAlert("خطا...");

            }
        }
        else ShowDangerAlert("حافظه مالیاتی را مشخص کنید");
        SpinnerVisible = !SpinnerVisible;
    } 
    public async Task OnClickCheckEconomicCode()
    {
        SpinnerVisible = !SpinnerVisible;
        if (!string.IsNullOrEmpty(InfoModel.EconomicCode))
        {

            var rsp = await _hc.Get($"TaxPayer/EconomicCodeInformation?item={InfoModel.EconomicCode}");
            if (rsp.IsSuccessStatusCode)
            {
                var request = await rsp.Content.ReadFromJsonAsync<EconomicCodeModelDto>();
              
                if (request.TaxpayerStatus!="ACTIVE" )
                {
                    ShowDangerAlert("کد اقتصادی فعال نیست");
                }
                else
                    ShowInfoAlert($"کد اقتصادی فعال و به نام '{request.NameTrade}' می باشد");
            }
            else if (rsp.StatusCode == System.Net.HttpStatusCode.NotFound)
            {
                ShowDangerAlert("کد اقتصادی یافت نشد");
            }
            else
            {
                ShowDangerAlert("خطا...");
            }
        }
        else ShowDangerAlert("کد اقعصادی را مشخص کنید");
        SpinnerVisible = !SpinnerVisible;
    }
    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 void ShowInfoAlert(string msg)
    {
        Hidealert = false;
        alertColor = AlertColor.Info;
        alertIconName = IconName.CheckCircleFill;
        alertMessage = msg;
    }

    private async Task Submit()
    {
        SpinnerVisible = true;
        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]);
            }
        
        SpinnerVisible = false;
    }
   

}