@using Front.Services
@using Shared.DTOs
@inject HttpClientController hc;
<div class="row">
    <Alert hidden="@Hidealert" Color="@alertColor" Dismissable="false">
        <Icon Name="@alertIconName" class="me-2"></Icon>
        @alertMessage
    </Alert>

</div>
<div dir="ltr" class="chat">
    <div class="messages" id="chat">
       
        @for (int i = 0; i < subTickets?.Count; i++)
        {
            if (i == 0 || subTickets[i - 1].Date != @subTickets[i].Date)
            {
                <div class="time"> @subTickets[i].Date </div>
            }
            
            string type = subTickets[i].Type == SideType.Customer ? "parker" : "stark";

                <div class="message @type">
                @subTickets[i].Text
                </div>
        }


    </div>
    @if (Ticket.Status == StatusTicket.Awaitingreview || Ticket.Status == StatusTicket.Read_Checking
    || Ticket.Status == StatusTicket.hasbeenanswered)
    {
       
        <div class="input">
            <InputText @bind-Value="inputsub" placeholder="Type your message here!" type="text"/>
            <Button Color="ButtonColor.Info" @onclick="sendsubTicket">ارسال</Button>
        </div>
    }
   
</div>

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

    [Parameter]
    public TicketDTO Ticket { get; set; }
    public string? inputsub { get; set; }
    public List<SubTicketDTO>? subTickets { get; set; } = new List<SubTicketDTO>();

    protected override async Task OnParametersSetAsync()
    {
       await Inital();
        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;
    }
    private async Task Inital()
    {
        var rsp = await hc.Get($"Ticket/GetSubTicket/{Ticket.TicketID}");
        if (rsp.IsSuccessStatusCode)
        {
            var res = await rsp.Content.ReadFromJsonAsync<ICollection<SubTicketDTO>>();
            subTickets = res?.ToList();
        }
        else
        {
            ShowDangerAlert("خطایی در اجرای عملیات رخ داده");
        }
    }
    private async Task sendsubTicket()
    {
        if (!string.IsNullOrEmpty(inputsub))
        {
            var rsp = await hc.Post<AddSubTicket>($"Ticket/AddSubicket", new AddSubTicket
                {
                    TicketID = Ticket.TicketID,
                    Text = inputsub
                });
            if (rsp.IsSuccessStatusCode)
            {
                inputsub = "";
                await Inital();
            }
            else
            {
                var errors = await rsp.Content.ReadFromJsonAsync<List<string>>();
                ShowDangerAlert(errors[0]);
            }

        }
       


    }
}