@using Shared.DTOs
@page "/Verification/{ID:int}"
@inject HttpClient _hc
@inject NavigationManager nav
@inject UserAuthenticationDTO userinfo
<PageTitle>احراز هویت</PageTitle>
<main>

    <!-- tp-banner-area-start -->
    <div class="signin-banner-area signin-banner-main-wrap d-flex align-items-center">
        <div class="signin-banner-left-box signin-banner-bg p-relative">
            <div class="signin-banner-bottom-shape">
                <img src="img/login/login-shape-1.png" alt="">
            </div>
            <div class="signin-banner-left-wrap">
                <div class="signin-banner-title-box mb-100">
                    <br />
                    <h4 class="signin-banner-title" style="color:black">
                        لطفا کد ارسالی را وارد نمایید
                    </h4>
                </div>
                <div class="signin-banner-img-box position-relative">
                    <div class="signin-banner-img signin-img-1 d-none d-md-block z-index-3">
                        <img src="img/login/login-2.png" alt="">
                    </div>
                    <div class="signin-banner-img signin-img-2 d-none d-md-block">
                        <img src="img/login/login-1.png" alt="">
                    </div>
                    <div class="signin-banner-img signin-img-3 d-none d-md-block z-index-5">
                        <img src="img/login/login-3.png" alt="">
                    </div>
                    <div class="signin-banner-img signin-img-4 d-none d-sm-block">
                        <img src="img/login/login-4.png" alt="">
                    </div>
                </div>
            </div>
        </div>
        <div class="signin-banner-from d-flex justify-content-center align-items-center">
            <div class="signin-banner-from-wrap">
                <div class="signin-banner-title-box">
                    <h4 class="signin-banner-from-title">احراز هویت</h4>
                </div>

                <div class="signin-banner-from-box">


                    <EditForm Model="VerificationCodeModel" OnSubmit="SubmitVerificationCode" FormName="VerificationCodeDto">


                        <div class="row">
                            <div class="col-12">
                                <div class="postbox__comment-input mb-30">
                                    <InputNumber @bind-Value="VerificationCodeModel.Code" style="text-align: center" id="Code" type="text" class="inputText" required="" />
                                    <span class="floating-label">کد تائید</span>
                                </div>
                            </div>
                            <div class="signin-banner-from-btn mb-20">
                                <button type="submit" class="signin-btn">احراز</button>
                            </div>
                        </div>
                   
                    </EditForm>
                    <div class="row">
                        <Alert hidden="@Hidealert" Color="@alertColor" Dismissable="false"> 
                            <Icon Name="@alertIconName" class="me-2"></Icon>
                            @alertMessage
                            <Button Color="ButtonColor.Primary" @onclick="EndForm">اتمام عملیات</Button>
                        </Alert>


                    </div>
                    <div class="signin-banner-from-register">
                        <NavLink style="cursor:pointer" @onclick="ReSend">کد ارسال نشد؟ <span>ارسال مجدد</span></NavLink>

                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- tp-banner-area-end -->

</main>

@code {
    bool successfull = false;
    // alert
    AlertColor alertColor = AlertColor.Primary;
    IconName alertIconName = IconName.CheckCircleFill;
    bool Hidealert = true;
    string alertMessage = "";

    
    [Parameter]
    public int ID { get; set; }

    [SupplyParameterFromForm]
    public VerificationCodeDto? VerificationCodeModel { get; set; } = new VerificationCodeDto();

    protected override async Task OnParametersSetAsync()
    {
        var request = await _hc.GetAsync($"Verification/GetVerification/{ID}");
        if (request.IsSuccessStatusCode)
        {

            VerificationCodeModel = await request.Content.ReadFromJsonAsync<VerificationCodeDto>();
        }
        else
        {
            nav.NavigateTo($"/");
        }

        await base.OnParametersSetAsync();
    }
}
    @functions {

    private async Task ReSend()
    {

        var request = await _hc.GetAsync($"Verification/ReSend/{VerificationCodeModel?.ID}");
        if (request.IsSuccessStatusCode)
        {
            ShowSuccessAlert("کد مجدد ارسال شد");
        }
        else
        {
            var listerror = await request.Content.ReadFromJsonAsync<List<string>>();
            ShowDangerAlert(listerror[0]);
        }


    }
    private async Task SubmitVerificationCode()
    {
        if (VerificationCodeModel.Code < 1000 || VerificationCodeModel.Code > 9000)
        {
            ShowDangerAlert("کد احراز صحیح نمی باشد");
        }
        else
        {
            var request = await _hc.PostAsJsonAsync($"Verification/Submit", VerificationCodeModel);
            if (request.IsSuccessStatusCode)
            {
                var status = await request.Content.ReadFromJsonAsync<bool>();
                if (status)
                {
                    if (VerificationCodeModel.Type == "CompanyRegistration")
                        nav.NavigateTo("/Sign-in/VerificationRegister");
                    else if (VerificationCodeModel.Type == "ForgetPassword")
                        nav.NavigateTo("/Sign-in/VerificationFrogetPass");
                    else if (VerificationCodeModel.Type == "ChangeUserName")
                    {
                        userinfo.UserName = VerificationCodeModel.val;

                        nav.NavigateTo("/Profile/Verification");
                    }
                
                    else
                        nav.NavigateTo("/");

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

            }
            else
            {
                var listerror = await request.Content.ReadFromJsonAsync<List<string>>();
                ShowDangerAlert(listerror[0]);
            }
        }
        
    }
    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;
    }
    private async Task EndForm() 
    {
        if (!successfull)
            await _hc.DeleteAsync($"Verification/Remove/{VerificationCodeModel?.ID}");
        if (VerificationCodeModel.Type == "ChangeUserName")
            nav.NavigateTo("/Profile");
        nav.NavigateTo("/");
    }
}
