@page "/Panel"
@page "/"
@using Front.Services
@using Shared
@using Shared.DTOs
@inject localService localserv;
@inject NavigationManager nav
@layout PanelLayout
@inject HttpClientController hc;
@inject Fixedvalues fv;

<PageTitle>مودیران / کاربری</PageTitle>
<Toasts class="p-3" Messages="messages" AutoHide="true" Delay="delaymessage" Placement="ToastsPlacement.BottomLeft" />

<div class="row">

    <div class="col-lg-6 mb-3 order-0">
        <div class="card">
            <div class="d-flex align-items-end row">
                <div class="col-sm-7">
                    <div class="card-body">
                        <h5 class="card-title text-primary">خوش آمدید! 🎉</h5>
                        <p class="mb-4">یک دید کلی از سیستم برای شما تهیه شده<br /> بهتر است روزانه نگاهی به آن داشته باشید</p>

                    </div>
                </div>
                <div class="col-sm-5 text-center text-sm-left">
                    <div class="card-body pb-0 px-0 px-md-4">
                        <img src="assets/img/illustrations/man-with-laptop-light.png" height="165" alt="View Badge User" data-app-dark-img="illustrations/man-with-laptop-dark.png" data-app-light-img="illustrations/man-with-laptop-light.png">
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="col-lg-2 mb-1 order-0">
        @if (fv.dashBoard != null)
    {
        <div class="col-12 mb-4">
            <div class="card">
                <div class="card-body">
                    <div class="d-flex justify-content-between flex-sm-row flex-column gap-3">
                        <div class="d-flex flex-sm-column flex-row align-items-start justify-content-between">
                           
                            <div class="card-title">
                                <h5 class="text-nowrap mb-2">گزارش فروش</h5>
                                <span class="badge bg-label-warning rounded-pill">این ماه</span>
                            </div>
                                @if (fv.dashBoard != null && fv.dashBoard.SaleInMonth.HasValue)
                            {
                                <div class="mt-sm-auto">
                                        @if (fv.dashBoard.Salepercent.HasValue && fv.dashBoard.Salepercent.Value > 0)
                                    {
                                            if (fv.dashBoard.Salepercent.Value >= 100)
                                        {
                                                <small class="text-success text-nowrap fw-semibold"><i class='bx bx-chevron-up'></i> @fv.dashBoard.Salepercent %</small>

                                        }
                                        else
                                        {
                                                <small class="text-danger text-nowrap fw-semibold"><i class='bx bx-chevron-down'></i> @fv.dashBoard.Salepercent %</small>

                                        }
                                    }

                                        <h6 class="mb-0">
                                            @fv.dashBoard?.SaleInMonth.Value.ToString("N0")
                                        <span style="font-size: 10px"> ریال</span>
                                        </h6>
                                </div>
                            }
                          
                        </div>
                      
                    </div>
                </div>
            </div>
        </div>
        }
    </div>
    <div class="col-lg-4 col-md-4 order-1">
        <div class="row">
            @if (fv.dashBoard != null && fv.dashBoard.AlistofServices.Any(w => w.PermissionID == 3))
            {

                <div class="col-lg-6 col-md-12 col-6 mb-4">
                    <div class="card">
                        <div class="card-body">
                            <div class="card-title d-flex align-items-start justify-content-between">
                                <div class="avatar flex-shrink-0">
                                    <img src="assets/img/icons/unicons/cc-success.png" alt="User" class="rounded">
                                </div>
                                <div class="dropdown">
                                    <button class="btn p-0" type="button" id="cardOpt3" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                        <i class="bx bx-dots-vertical-rounded"></i>
                                    </button>
                                    <div class="dropdown-menu dropdown-menu-end customStyle new-style-15" aria-labelledby="cardOpt3">
                                        <NavLink class="dropdown-item" href="Orders">تمدید</NavLink>
                                        <NavLink class="dropdown-item" href="Invoice">صورتحساب ها</NavLink>
                                    </div>
                                </div>
                            </div>
                            <span class="fw-semibold d-block">صورتحساب     <br /> (کل @fv.dashBoard.AlistofServices.Where(w => w.PermissionID == 3 && w.CalTypeID == 1).Select(s => s.Total).First())</span>
                            <h3 class="card-title mb-1"><span style="font-size: 14px">مانده</span></h3>
                            @if (fv.dashBoard.AlistofServices.Any(w => w.PermissionID == 3 && w.CalTypeID == 1))
                            {
                                @if (fv.dashBoard.AlistofServices.Where(w => w.PermissionID == 3 && w.CalTypeID == 1).Select(s => s.Remaining).First() > 0)
                                {

                                    <small class="text-success fw-semibold">
                                        <i class='bx'></i>
                                        @fv.dashBoard.AlistofServices.Where(w => w.PermissionID == 3 && w.CalTypeID == 1).Select(s => s.Remaining).First() آیتم
                                    </small>
                                }
                                else
                                {
                                    <small class="text-danger fw-semibold">
                                        <i class='bx'></i>
                                        @fv.dashBoard.AlistofServices.Where(w => w.PermissionID == 3 && w.CalTypeID == 1).Select(s => s.Remaining).First() آیتم
                                    </small>
                                }

                            }
                            else if (fv.dashBoard.AlistofServices.Any(w => w.PermissionID == 3 && w.CalTypeID == 2))
                            {
                                <small class="text-success fw-semibold">
                                    <i class='bx'></i>
                                    نامحدود
                                </small>

                            }
                            else
                            {
                                @if (Convert.ToInt32(fv.dashBoard.Datenow) <= fv.dashBoard.AlistofServices.Where(w => w.PermissionID == 3 && w.CalTypeID == 1).Select(s => s.Remaining).First())
                                {
                                    <small class="text-success fw-semibold">
                                        <i class='bx'></i>
                                        تا تاریخ  @fv.dashBoard.AlistofServices.Where(w => w.PermissionID == 3 && w.CalTypeID == 1).Select(s => s.Remaining).First().ToString().ShamciToFormatShamciinFront()
                                    </small>
                                }
                                else
                                {
                                    <small class="text-danger fw-semibold">
                                        <i class='bx'></i>
                                        تا تاریخ  @fv.dashBoard.AlistofServices.Where(w => w.PermissionID == 3 && w.CalTypeID == 1).Select(s => s.Remaining).First().ToString().ShamciToFormatShamciinFront()
                                    </small>
                                }
                            }
                        </div>
                    </div>
                </div>
            }
            @if (fv.dashBoard != null && fv.dashBoard.AlistofServices.Any(w => w.PermissionID == 4))
            {
                <div class="col-lg-6 col-md-12 col-6 mb-4">
                    <div class="card">
                        <div class="card-body">
                            <div class="card-title d-flex align-items-start justify-content-between">
                                <div class="avatar flex-shrink-0">
                                    <img src="assets/img/icons/unicons/wallet-info.png" alt="chart success" class="rounded">
                                </div>
                                <div class="dropdown">
                                    <button class="btn p-0" type="button" id="cardOpt3" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                        <i class="bx bx-dots-vertical-rounded"></i>
                                    </button>
                                    <div class="dropdown-menu dropdown-menu-end customStyle new-style-15" aria-labelledby="cardOpt3">
                                        <NavLink class="dropdown-item" href="Orders">تمدید</NavLink>
                                        <NavLink class="dropdown-item" href="Cod">کالا ها</NavLink>
                                    </div>
                                </div>
                            </div>
                            <span class="fw-semibold d-block">کالا     <br /> (کل @fv.dashBoard.AlistofServices.Where(w => w.PermissionID == 4 && w.CalTypeID == 1).Select(s => s.Total).First())</span>
                            <h3 class="card-title mb-1"><span style="font-size: 14px">مانده</span></h3>
                            @if (fv.dashBoard.AlistofServices.Any(w => w.PermissionID == 4 && w.CalTypeID == 1))
                            {
                                @if (fv.dashBoard.AlistofServices.Where(w => w.PermissionID == 4 && w.CalTypeID == 1).Select(s => s.Remaining).First() > 0)
                                {
                                    <small class="text-success fw-semibold">
                                        <i class='bx'></i>
                                        @fv.dashBoard.AlistofServices.Where(w => w.PermissionID == 4 && w.CalTypeID == 1).Select(s => s.Remaining).First() آیتم
                                    </small>
                                }
                                else
                                {
                                    <small class="text-danger fw-semibold">
                                        <i class='bx'></i>
                                        @fv.dashBoard.AlistofServices.Where(w => w.PermissionID == 4 && w.CalTypeID == 1).Select(s => s.Remaining).First() آیتم
                                    </small>
                                }

                            }
                            else if (fv.dashBoard.AlistofServices.Any(w => w.PermissionID == 4 && w.CalTypeID == 2))
                            {
                                <small class="text-success fw-semibold">
                                    <i class='bx'></i>
                                    نامحدود
                                </small>

                            }
                            else
                            {
                                @if (Convert.ToInt32(fv.dashBoard.Datenow) <= fv.dashBoard.AlistofServices.Where(w => w.PermissionID == 4 && w.CalTypeID == 1).Select(s => s.Remaining).First())
                                {
                                    <small class="text-success fw-semibold">
                                        <i class='bx'></i>
                                        تا تاریخ  @fv.dashBoard.AlistofServices.Where(w => w.PermissionID == 4 && w.CalTypeID == 1).Select(s => s.Remaining).First().ToString().ShamciToFormatShamciinFront()
                                    </small>
                                }
                                else
                                {
                                    <small class="text-danger fw-semibold">
                                        <i class='bx'></i>
                                        تا تاریخ  @fv.dashBoard.AlistofServices.Where(w => w.PermissionID == 4 && w.CalTypeID == 1).Select(s => s.Remaining).First().ToString().ShamciToFormatShamciinFront()
                                    </small>
                                }
                            }
                        </div>
                    </div>
                </div>
            }
        </div>
    </div>
    <!-- Total Revenue -->
    <div class="col-12 col-lg-8 order-2 order-md-3 order-lg-2 mb-4">
        <div class="card col-12">
            <div class="row row-bordered g-0">
                @if (fv.dashBoard != null)
                {
                    <div class="col-md-12">
                        <SaleChart items="fv.dashBoard?.ForSaleChart" />
                </div>}
            </div>
        </div>
    </div>
    <!--/ Total Revenue -->
    <div class="col-12 col-md-8 col-lg-4 order-3 order-md-2">
        <div class="row">
            @if (fv.dashBoard != null && fv.dashBoard.AlistofServices.Any(w => w.PermissionID == 5))
            {
                <div class="col-6 mb-4">
                    <div class="card">
                        <div class="card-body">
                            <div class="card-title d-flex align-items-start justify-content-between">
                                <div class="avatar flex-shrink-0">
                                    <img src="assets/img/icons/unicons/cc-primary.png" alt="chart success" class="rounded">
                                </div>
                                <div class="dropdown">
                                    <button class="btn p-0" type="button" id="cardOpt3" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                        <i class="bx bx-dots-vertical-rounded"></i>
                                    </button>
                                    <div class="dropdown-menu dropdown-menu-end customStyle new-style-15" aria-labelledby="cardOpt3">
                                        <NavLink class="dropdown-item" href="Orders">تمدید</NavLink>
                                        <NavLink class="dropdown-item" href="Customer">مشتری ها</NavLink>
                                    </div>
                                </div>
                            </div>
                            <span class="fw-semibold d-block">مشتری     <br /> (کل @fv.dashBoard.AlistofServices.Where(w => w.PermissionID == 5 && w.CalTypeID == 1).Select(s => s.Total).First())</span>
                            <h3 class="card-title mb-1"><span style="font-size: 14px">مانده</span></h3>
                            @if (fv.dashBoard.AlistofServices.Any(w => w.PermissionID == 5 && w.CalTypeID == 1))
                            {
                                @if (fv.dashBoard.AlistofServices.Where(w => w.PermissionID == 5 && w.CalTypeID == 1).Select(s => s.Remaining).First() > 0)
                                {

                                    <small class="text-success fw-semibold">
                                        <i class='bx'></i>
                                        @fv.dashBoard.AlistofServices.Where(w => w.PermissionID == 5 && w.CalTypeID == 1).Select(s => s.Remaining).First() آیتم
                                    </small>
                                }
                                else
                                {
                                    <small class="text-danger fw-semibold">
                                        <i class='bx'></i>
                                        @fv.dashBoard.AlistofServices.Where(w => w.PermissionID == 5 && w.CalTypeID == 1).Select(s => s.Remaining).First() آیتم
                                    </small>
                                }

                            }
                            else if (fv.dashBoard.AlistofServices.Any(w => w.PermissionID == 5 && w.CalTypeID == 2))
                            {
                                <small class="text-success fw-semibold">
                                    <i class='bx'></i>
                                    نامحدود
                                </small>

                            }
                            else
                            {
                                @if (Convert.ToInt32(fv.dashBoard.Datenow) <= fv.dashBoard.AlistofServices.Where(w => w.PermissionID == 5 && w.CalTypeID == 1).Select(s => s.Remaining).First())
                                {
                                    <small class="text-success fw-semibold">
                                        <i class='bx'></i>
                                        تا تاریخ  @fv.dashBoard.AlistofServices.Where(w => w.PermissionID == 5 && w.CalTypeID == 1).Select(s => s.Remaining).First().ToString().ShamciToFormatShamciinFront()
                                    </small>
                                }
                                else
                                {
                                    <small class="text-danger fw-semibold">
                                        <i class='bx'></i>
                                        تا تاریخ  @fv.dashBoard.AlistofServices.Where(w => w.PermissionID == 5 && w.CalTypeID == 1).Select(s => s.Remaining).First().ToString().ShamciToFormatShamciinFront()
                                    </small>
                                }
                            }
                        </div>
                    </div>
                </div>
            }
            @if (fv.dashBoard != null && fv.dashBoard.AlistofServices.Any(w => w.PermissionID == 16))
            {
                <div class="col-6 mb-4">
                    <div class="card">
                        <div class="card-body">
                            <div class="card-title d-flex align-items-start justify-content-between">
                                <div class="avatar flex-shrink-0">
                                    <img src="assets/img/icons/unicons/paypal.png" alt="chart success" class="rounded">
                                </div>
                                <div class="dropdown">
                                    <button class="btn p-0" type="button" id="cardOpt3" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                        <i class="bx bx-dots-vertical-rounded"></i>
                                    </button>
                                    <div class="dropdown-menu dropdown-menu-end customStyle new-style-15" aria-labelledby="cardOpt3">
                                        <NavLink class="dropdown-item" href="Orders">تمدید</NavLink>
                                        <NavLink class="dropdown-item" href="TaxPayer">سامانه مودیان</NavLink>
                                    </div>
                                </div>
                            </div>
                            <span class="fw-semibold d-block">سامانه مودیان     <br /> (کل @fv.dashBoard.AlistofServices.Where(w => w.PermissionID == 16 && w.CalTypeID == 1).Select(s => s.Total).First())</span>
                            <h3 class="card-title mb-1"><span style="font-size: 14px">مانده</span></h3>
                            @if (fv.dashBoard.AlistofServices.Any(w => w.PermissionID == 16 && w.CalTypeID == 1))
                            {
                                @if (fv.dashBoard.AlistofServices.Where(w => w.PermissionID == 16 && w.CalTypeID == 1).Select(s => s.Remaining).First() > 0)
                                {
                                    <small class="text-success fw-semibold">
                                        <i class='bx'></i>
                                        @fv.dashBoard.AlistofServices.Where(w => w.PermissionID == 16 && w.CalTypeID == 1).Select(s => s.Remaining).First() آیتم
                                    </small>
                                }
                                else
                                {
                                    <small class="text-danger fw-semibold">
                                        <i class='bx'></i>
                                        @fv.dashBoard.AlistofServices.Where(w => w.PermissionID == 16 && w.CalTypeID == 1).Select(s => s.Remaining).First() آیتم
                                    </small>
                                }

                            }
                            else if (fv.dashBoard.AlistofServices.Any(w => w.PermissionID == 16 && w.CalTypeID == 2))
                            {
                                <small class="text-success fw-semibold">
                                    <i class='bx'></i>
                                    نامحدود
                                </small>

                            }
                            else
                            {
                                @if (Convert.ToInt32(fv.dashBoard.Datenow) <= fv.dashBoard.AlistofServices.Where(w => w.PermissionID == 16 && w.CalTypeID == 1).Select(s => s.Remaining).First())
                                {
                                    <small class="text-success fw-semibold">
                                        <i class='bx'></i>
                                        تا تاریخ  @fv.dashBoard.AlistofServices.Where(w => w.PermissionID == 16 && w.CalTypeID == 1).Select(s => s.Remaining).First().ToString().ShamciToFormatShamciinFront()
                                    </small>
                                }
                                else
                                {
                                    <small class="text-danger fw-semibold">
                                        <i class='bx'></i>
                                        تا تاریخ  @fv.dashBoard.AlistofServices.Where(w => w.PermissionID == 16 && w.CalTypeID == 1).Select(s => s.Remaining).First().ToString().ShamciToFormatShamciinFront()
                                    </small>
                                }
                            }
                        </div>
                    </div>
                </div>
            }
            @if (fv.dashBoard != null && fv.dashBoard.AlistofServices.Any(w => w.PermissionID == 18))
            {
                <div class="col-6 mb-4">
                    <div class="card">
                        <div class="card-body">
                            <div class="card-title d-flex align-items-start justify-content-between">
                                <div class="avatar flex-shrink-0">
                                    <img src="assets/img/icons/unicons/cc-warning.png" alt="chart success" class="rounded">
                                </div>
                                <div class="dropdown">
                                    <button class="btn p-0" type="button" id="cardOpt3" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                        <i class="bx bx-dots-vertical-rounded"></i>
                                    </button>
                                    <div class="dropdown-menu dropdown-menu-end customStyle new-style-15" aria-labelledby="cardOpt3">
                                        <NavLink class="dropdown-item" href="Orders">تمدید</NavLink>
                                        <NavLink class="dropdown-item" href="Warehouse">انبارداری</NavLink>
                                    </div>
                                </div>
                            </div>
                            <span class="fw-semibold d-block">انبارداری     <br /> (کل @fv.dashBoard.AlistofServices.Where(w => w.PermissionID == 18 && w.CalTypeID == 1).Select(s => s.Total).First())</span>
                            <h3 class="card-title mb-1"><span style="font-size: 14px">مانده</span></h3>
                            @if (fv.dashBoard.AlistofServices.Any(w => w.PermissionID == 18 && w.CalTypeID == 1))
                            {
                                @if (fv.dashBoard.AlistofServices.Where(w => w.PermissionID == 18 && w.CalTypeID == 1).Select(s => s.Remaining).First() > 0)
                                {
                                    <small class="text-success fw-semibold">
                                        <i class='bx'></i>
                                        @fv.dashBoard.AlistofServices.Where(w => w.PermissionID == 18 && w.CalTypeID == 1).Select(s => s.Remaining).First() آیتم
                                    </small>
                                }
                                else
                                {
                                    <small class="text-danger fw-semibold">
                                        <i class='bx'></i>
                                        @fv.dashBoard.AlistofServices.Where(w => w.PermissionID == 18 && w.CalTypeID == 1).Select(s => s.Remaining).First() آیتم
                                    </small>
                                }

                            }
                            else if (fv.dashBoard.AlistofServices.Any(w => w.PermissionID == 18 && w.CalTypeID == 2))
                            {
                                <small class="text-success fw-semibold">
                                    <i class='bx'></i>
                                    نامحدود
                                </small>

                            }
                            else
                            {
                                @if (Convert.ToInt32(fv.dashBoard.Datenow) <= fv.dashBoard.AlistofServices.Where(w => w.PermissionID == 18 && w.CalTypeID == 1).Select(s => s.Remaining).First())
                                {
                                    <small class="text-success fw-semibold">
                                        <i class='bx'></i>
                                        تا تاریخ  @fv.dashBoard.AlistofServices.Where(w => w.PermissionID == 18 && w.CalTypeID == 1).Select(s => s.Remaining).First().ToString().ShamciToFormatShamciinFront()
                                    </small>
                                }
                                else
                                {
                                    <small class="text-danger fw-semibold">
                                        <i class='bx'></i>
                                        تا تاریخ  @fv.dashBoard.AlistofServices.Where(w => w.PermissionID == 18 && w.CalTypeID == 1).Select(s => s.Remaining).First().ToString().ShamciToFormatShamciinFront()
                                    </small>
                                }
                            }
                        </div>
                    </div>
                </div>
            }
            <!-- </div>
            <div class="row"> -->
           
        </div>
    </div>
</div>
<div class="row">
    @if (fv.dashBoard == null)
    {
        <div class="col-auto" >
        <Spinner Visible="fv.dashBoard == null" Color="SpinnerColor.Primary" />
        </div>   <div class="col-auto">
            <p>در حال بارگذاری...</p>
        </div>
    }
    else
    {
        <LastActivityServices dashBoard="fv.dashBoard" />
        <MostInvoicedCustomer model="fv.dashBoard?.MostInvoicedCustomer" />
        <MostInvoicedProduct model="fv.dashBoard?.MostInvoicedProduct" />
    }
  
    <!--/ MostInvoicedProduct -->
</div>


@code {
    int delaymessage = 0;
    List<ToastMessage> messages = new List<ToastMessage>();
    private Modal modal = default!;
    private void ShowMessage(ToastType toastType, string Message) => messages.Add(CreateToastMessage(toastType, Message));
    // public DashBoardDTO? dashBoard { get; set; }
    protected override async Task OnInitializedAsync()
    {

        if (await localserv.OnlineUser())
        {
            await fv.LoadDashBoard();
            if (fv.dashBoard != null)
                foreach (var war in fv.dashBoard.Warning)
                    ShowMessage((ToastType)Enum.Parse(typeof(ToastType), war.Status.ToString()), war.Message);

            delaymessage = messages.Count * 5000;


        }
        else
        {
            nav.NavigateTo("/Sign-in");
        }
        await base.OnInitializedAsync();
    }
}
@functions {
 
    private ToastMessage CreateToastMessage(ToastType toastType, string Message)
      => new ToastMessage
      {
              Type = toastType,
              Message = Message,
      };
}