@using Front.Services
@using Shared.DTOs
@inject Fixedvalues fv;
@inject HttpClientController hc;
<Preload LoadingText="در حال بارگذاری..." />


@* alert *@
<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-6">
        <Dropdown Color="DropdownColor.Secondary">
            <DropdownToggleButton>@orderSelectName</DropdownToggleButton>
            <DropdownMenu>
                @foreach (var i in orderstype)
                {
                    <DropdownItem @onclick="()=>OnChange(i.ID,i.Title)">@i.Title</DropdownItem>
                }
            </DropdownMenu>
        </Dropdown>
    </div>
</div>

<hr class="hr" />

<table class="table table-sm">
    <thead>
        <tr>
            <th style="width:40%" scope="col">سیستم</th>
            <th style="width:30%" scope="col">قیمت پایه</th>
            <th scope="col">تعداد</th>
        </tr>
    </thead>
    <tbody>
        @if (Selected!=null)
        {
            if (Selected.promotionDetails.Any(w => w.PermissionID == 3))
            {
                <tr>
                    <td>@Selected.promotionDetails.Where(w => w.PermissionID == 3).Select(s => s.PermissionTitle).FirstOrDefault()</td>
                    <td>@Selected.promotionDetails.Where(w => w.PermissionID == 3).Select(s => s.APrice).FirstOrDefault().ToString("N0") ريال</td>
                    <td>
                        @if (Selected.ID < 0)
                        {
                            <InputNumber @bind-Value="values[0]" @bind-Value:after="OnInput" type="text" class="form-control" id="inputfa" style="text-align:center;" placeholder="تعداد" />

                        }
                        else
                        {
                            <input value="@Selected.promotionDetails.Where(w => w.PermissionID == 3).Select(s => s.CreditAmount).FirstOrDefault()" style="text-align:center;" class="form-control" type="text" readonly>

                        }
                    </td>
                </tr>
            }

            if (Selected.promotionDetails.Any(w => w.PermissionID == 4))
            {
                <tr>
                    <td>@Selected.promotionDetails.Where(w => w.PermissionID == 4).Select(s => s.PermissionTitle).FirstOrDefault()</td>
                    <td>@Selected.promotionDetails.Where(w => w.PermissionID == 4).Select(s => s.APrice).FirstOrDefault().ToString("N0") ريال</td>
                    <td>
                        @if (Selected.ID < 0)
                        {
                            <InputNumber @bind-Value="values[1]" @bind-Value:after="OnInput" type="text" class="form-control" id="inputcod" style="text-align:center;" placeholder="تعداد" />

                        }
                        else
                        {
                            <input value="@Selected.promotionDetails.Where(w => w.PermissionID == 4).Select(s => s.CreditAmount).FirstOrDefault()" style="text-align:center;" class="form-control" type="text" readonly>

                        }
                    </td>
                </tr>
            }


            if (Selected.promotionDetails.Any(w => w.PermissionID == 5))
            {
                <tr>
                    <td>@Selected.promotionDetails.Where(w => w.PermissionID == 5).Select(s => s.PermissionTitle).FirstOrDefault()</td>
                    <td>@Selected.promotionDetails.Where(w => w.PermissionID == 5).Select(s => s.APrice).FirstOrDefault().ToString("N0") ريال</td>
                    <td>
                        @if (Selected.ID < 0)
                        {
                            <InputNumber @bind-Value="values[2]" @bind-Value:after="OnInput" type="text" class="form-control" id="inputcus" style="text-align:center;" placeholder="تعداد" />

                        }
                        else
                        {
                            <input value="@Selected.promotionDetails.Where(w => w.PermissionID == 5).Select(s => s.CreditAmount).FirstOrDefault()" style="text-align:center;" class="form-control" type="text" readonly>

                        }
                    </td>
                </tr>
            }
            if (Selected.promotionDetails.Any(w => w.PermissionID == 16))
            {
                <tr>
                    <td>@Selected.promotionDetails.Where(w => w.PermissionID == 16).Select(s => s.PermissionTitle).FirstOrDefault()</td>
                    <td>@Selected.promotionDetails.Where(w => w.PermissionID == 16).Select(s => s.APrice).FirstOrDefault().ToString("N0") ريال</td>
                    <td>
                        @if (Selected.ID < 0)
                        {
                            <InputNumber @bind-Value="values[3]" @bind-Value:after="OnInput" type="text" class="form-control" id="inputax" style="text-align:center;" placeholder="تعداد" />
                        }
                        else
                        {
                            <input value="@Selected.promotionDetails.Where(w => w.PermissionID == 16).Select(s => s.CreditAmount).FirstOrDefault()" style="text-align:center;" class="form-control" type="text" readonly>

                        }
                    </td>
                </tr>
            }
            if (Selected.promotionDetails.Any(w => w.PermissionID == 18))
            {
                <tr>
                    <td>@Selected.promotionDetails.Where(w => w.PermissionID == 18).Select(s => s.PermissionTitle).FirstOrDefault()</td>
                    <td>@Selected.promotionDetails.Where(w => w.PermissionID == 18).Select(s => s.APrice).FirstOrDefault().ToString("N0") ريال</td>
                    <td>
                        @if (Selected.ID < 0)
                        {
                            <InputNumber @bind-Value="values[4]" @bind-Value:after="OnInput" type="text" class="form-control" id="inputax" style="text-align:center;" placeholder="تعداد" />
                        }
                        else
                        {
                            <input value="@Selected.promotionDetails.Where(w => w.PermissionID == 18).Select(s => s.CreditAmount).FirstOrDefault()" style="text-align:center;" class="form-control" type="text" readonly>

                        }
                    </td>
                </tr>
            }

        }
    </tbody>
</table>



<div class="row">
    <div class="col-md-6">
    <span class="text-muted fw-light">قیمت نهایی :</span> @TotalPrice.ToString("N0") ريال
    </div>
    <div class="col-md-4">
        <Button Disabled="SpinnerVisible" Color="ButtonColor.Primary" @onclick="NewOrder">ثبت</Button>
    </div>
    <div class="col-md-2">
        <Spinner Visible="SpinnerVisible" Color="SpinnerColor.Primary" />
    </div>
</div>
@code {
    public bool SpinnerVisible { get; set; } = false;
    #region Alert
    // alert
    AlertColor alertColor = AlertColor.Primary;
    IconName alertIconName = IconName.CheckCircleFill;
    bool Hidealert = true;
    string alertMessage = "";
    #endregion

    string orderSelectName = "نوع سفارش";
    int orderSelectID = 0;
    [Inject] protected PreloadService PreloadService { get; set; } = default!;
    [Parameter] public EventCallback<OrderDto> OnMultipleOfThree { get; set; }
    List<IdName<int>> orderstype = new List<IdName<int>>();
    public List<PromotionDto> Promotions { get; set; } = new();   
    public PromotionDto? Selected { get; set; } = null;
    // invoice cod cus tax
    int[] values = { 0,0,0,0,0};
    decimal TotalPrice = 0;
}
@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;
    }
    protected override async Task OnParametersSetAsync()
    {
        orderSelectName = "نوع سفارش";
        Selected  = null;
        values =new int[] { 0,0,0,0,0};
        TotalPrice = 0;
        Promotions = await fv.GetPromotion();
        orderstype = new List<IdName<int>>();
        orderstype.AddRange(Promotions.Select(s => new IdName<int>
            { ID = s.ID, Title = s.Name }).ToList());

        await base.OnParametersSetAsync();

    }
    public async Task OnChange(int ID, string Title)
    {
        orderSelectID = ID;
        orderSelectName = Title;


        Selected = Promotions.Where(w => w.ID == ID).First();


        if (ID > 0)
            TotalPrice = Selected.TotalPrice;


    }
    private async Task OnInput()
    {
        if (Selected != null)
        {
            var itemFac= Selected.promotionDetails.Where(w => w.PermissionID == 3).FirstOrDefault();
            if (itemFac!=null)
                itemFac.CreditAmount = values[0];

            var itemcod = Selected.promotionDetails.Where(w => w.PermissionID == 4).FirstOrDefault();
            if (itemcod != null)
                itemcod.CreditAmount = values[1];

            var itemcus = Selected.promotionDetails.Where(w => w.PermissionID == 5).FirstOrDefault();
            if (itemcus != null)
                itemcus.CreditAmount = values[2];

            var itemtax = Selected.promotionDetails.Where(w => w.PermissionID == 16).FirstOrDefault();
            if (itemtax != null)
                itemtax.CreditAmount = values[3];

            var itemwarehouse = Selected.promotionDetails.Where(w => w.PermissionID == 18).FirstOrDefault();
            if (itemwarehouse != null)
                itemwarehouse.CreditAmount = values[4];

            TotalPrice = Selected.TotalPrice;
        }


    }
    public async Task NewOrder()
    {
        SpinnerVisible = true;
        if (Selected != null)
        {
            if (Selected.ID < 0 && (values[0] < 0 || values[1] < 0 || values[2] < 0 || values[3] < 0 || values[4] < 0)) return;
            if (Selected.ID < 0 && values[0] == 0 && values[1] == 0 && values[2] == 0 && values[3] == 0 && values[4] == 0) return;
            
            //---------
                var itemFac = Selected.promotionDetails.Where(w => w.PermissionID == 3).FirstOrDefault();
                if (itemFac != null)
                    itemFac.CreditAmount = values[0];

                var itemcod = Selected.promotionDetails.Where(w => w.PermissionID == 4).FirstOrDefault();
                if (itemcod != null)
                    itemcod.CreditAmount = values[1];

                var itemcus = Selected.promotionDetails.Where(w => w.PermissionID == 5).FirstOrDefault();
                if (itemcus != null)
                    itemcus.CreditAmount = values[2];

                var itemtax = Selected.promotionDetails.Where(w => w.PermissionID == 16).FirstOrDefault();
                if (itemtax != null)
                    itemtax.CreditAmount = values[3];

            var itemwarehouse = Selected.promotionDetails.Where(w => w.PermissionID == 18).FirstOrDefault();
            if (itemwarehouse != null)
                itemwarehouse.CreditAmount = values[4];
                //---------
                var rsp = await hc.Post<PromotionDto>($"Orders/AddOrder", Selected);
                if (rsp.IsSuccessStatusCode)
                {
                    var newidorder = await rsp.Content.ReadFromJsonAsync<OrderDto>();
                    await OnMultipleOfThree.InvokeAsync(newidorder);
                }
                else if (rsp.StatusCode == System.Net.HttpStatusCode.NotFound)
                {
                    ShowDangerAlert("مرجع یافت نشد یافت نشد");
                }
                else
                {
                    var request = await rsp.Content.ReadFromJsonAsync<List<string>>();
                    ShowDangerAlert(request[0]);
                }
            
        }
        SpinnerVisible = false;
       
    }
}
