@page "/Box/OrderImport"
@using MasaBlazorApp3.Pojo.Config
@using MasaBlazorApp3.Report
@using Microsoft.Extensions.Options
@using Radzen.Blazor.Rendering
@using log4net;

<style>
    .rz-grid-table {
        position: relative;
        border-collapse: separate;
        border-spacing: 0;
    }
</style>

<RadzenStack Orientation="Orientation.Horizontal">
    <div class="col-12">
        <form onsubmit="@(() => {grid.Reload();})">
            <RadzenFieldset Text="查询">
                <RadzenRow AlignItems="AlignItems.Center">
                    <RadzenColumn Size="6">

                        <RadzenLabel Text="处方号" Component="OrderNo" />
                        <RadzenTextBox @bind-Value="OrderNo" Style="width: 100%;" Name="OrderNo"></RadzenTextBox>

                    </RadzenColumn>
                    <RadzenColumn Size="6" Style="margin-top:0.5rem">
                        <RadzenLabel Text="处方时间" Component="OrderDate" />
                        <RadzenDatePicker DateFormat="yyyy-MM-dd" CurrentDateChanged="@OnCurrentDateChanged" @bind-Value="OrderDate" Style="width: 100%;" Name="OrderDate" />
                    </RadzenColumn>
                </RadzenRow>
                <RadzenRow AlignItems="AlignItems.Center">
                    <RadzenColumn Size="6">

                        <RadzenLabel Text="患者姓名" Component="PatientName" />
                        <RadzenTextBox @bind-Value="PatientName" Style="width: 100%;" Name="PatientName"></RadzenTextBox>

                    </RadzenColumn>
                    <RadzenColumn Size="6" Style="margin-top:4vh">
                        <RadzenButton Size="ButtonSize.Medium" ButtonType="ButtonType.Submit" IsBusy="isLoading" Icon="search" Text="查询" />
                        <RadzenButton Size="ButtonSize.Medium" Click="Confirm" ButtonType="ButtonType.Button" ButtonStyle="ButtonStyle.Warning" IsBusy="isLoading" Icon="input" Text="导入" />
                        <RadzenButton Click="@((args) => dialogService.Close(false))" Variant="Variant.Flat" ButtonStyle="ButtonStyle.Secondary" Text="取消" />
                    </RadzenColumn>
                </RadzenRow>
            </RadzenFieldset>
        </form>
        <div Style="width:75vw;height:50vh;overflow:auto">
            <RadzenDataGrid @ref="grid" AllowRowSelectOnRowClick="@allowRowSelectOnRowClick" AllowFiltering="true" LoadData="@LoadData" Style="width:75vw;overflow:auto" Groupable="true"
                            FilterPopupRenderMode="PopupRenderMode.OnDemand" FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive"
                            Data="@orderInfos" IsLoading="@isLoading" Count="@count" EmptyText="无数据"
                            SelectionMode="DataGridSelectionMode.Multiple"
                            @bind-Value="@(selectedOrderInfos)"
                            CellClick="@((DataGridCellMouseEventArgs<OrderInfo> args) => OnCellClick(args))"
                            AllowPaging="false">
                <Columns>
                    <RadzenDataGridColumn Sortable="false" Filterable="false" Width="4rem">
                        @*   <HeaderTemplate>
                            <RadzenCheckBox TabIndex="-1" TriState="false" TValue="bool?" InputAttributes="@(new Dictionary<string, object>() { { "aria-label", "Select all items" } })"
                            Value="@(selectedOrderInfos == null || selectedOrderInfos?.Any() != true ? false : !orderInfos.All(i => selectedOrderInfos.Contains(i)) ? null : orderInfos.Any(i => selectedOrderInfos.Contains(i)))"
                            Change="@(args => OnCheckSeleced(args))" />
                        </HeaderTemplate>
                        <Template Context="data">
                            <RadzenCheckBox TabIndex="-1" TriState="false" Value="@data.ItemIsChecked" InputAttributes="@(new Dictionary<string, object>() { { "aria-label", "Select item" } })"
                            TValue="bool" Change="@(args => { if (!allowRowSelectOnRowClick) { grid.SelectRow(data); } })" />
                        </Template> *@
                        <HeaderTemplate>
                            <RadzenCheckBox TabIndex="-1" TriState="false" TValue="bool?" InputAttributes="@(new Dictionary<string, object>() { { "aria-label", "Select all items" } })"
                                            Value="@(selectedOrderInfos == null || selectedOrderInfos?.Any() != true ? false : !orderInfos.All(i => selectedOrderInfos.Contains(i)) ? null : orderInfos.Any(i => selectedOrderInfos.Contains(i)))"
                                            Change="@(args => selectedOrderInfos = args == true ? orderInfos.ToList() : null)" />
                        </HeaderTemplate>
                        <Template Context="data">
                            <RadzenCheckBox TabIndex="-1" TriState="false"
                                            Value="@(selectedOrderInfos != null &&selectedOrderInfos.Contains(data))"
                                            InputAttributes="@(new Dictionary<string, object>() { { "aria-label", "Select item" } })"
                                            TValue="bool" />
                        </Template>
                    </RadzenDataGridColumn>
                    <RadzenDataGridColumn Property="anaesthetistName" Title="麻醉医师" Sortable="false" Filterable="false" Width="8vw" />
                    <RadzenDataGridColumn Property="PatientName" Title="姓名" Sortable="false" Filterable="false" Width="8vw" />
                    <RadzenDataGridColumn Property="Sex" Title="性别" Sortable="false" Filterable="false" Width="6vw" />
                    <RadzenDataGridColumn Property="OrderNo" Title="单号" Sortable="false" Filterable="false" Width="13vw" />
                    @* <RadzenDataGridColumn Property="ChargeDate" Title="处方时间" Sortable="false" Filterable="false" Width="10vw" /> *@
                    <RadzenDataGridColumn Property="DetailInfo.Drug.DrugName" Title="药品名称" FormatString="{0:d}" Sortable="false" Filterable="false" Width="20vw" />
                    @* <Template Context="DetailInfo">
                       @for (int i = 0; i < DetailList.DetailList.Count; i++)
                        {
                            OrderDetail orderDetail = DetailList.DetailList[i]; *@
                    @*     <RadzenText TextStyle="TextStyle.Subtitle2" class="rz-mb-0">@DetailInfo.Drug.DrugName</RadzenText>
                    }
                    </Template> 
                </RadzenDataGridColumn>*@
                    @*  <RadzenDataGridColumn Property="Detail.SetManuNo" Title="药品批次" Sortable="false" Filterable="false" Width="8rem">
                    <Template Context="DetailList">
                        @for (int i = 0; i < DetailList.DetailList.Count; i++)
                        {
                            OrderDetail orderDetail = DetailList.DetailList[i];
                            <RadzenText TextStyle="TextStyle.Subtitle2" class="rz-mb-0">@orderDetail.SetManuNo</RadzenText>
                        }
                    </Template>
                </RadzenDataGridColumn> *@
                    <RadzenDataGridColumn Title="批次" Property="DetailInfo.SetManuNo" Filterable="false" Width="18vw">
                        <Template Context="DetailInfo">
                            <RadzenText TextStyle="TextStyle.Subtitle2" class="mb-0">@DetailInfo.DetailInfo.SetManuNo</RadzenText>

                            @if (DetailInfo.DetailInfo.SetManuNo != null)
                            {
                                <RadzenText TextStyle="TextStyle.Caption">
                                    @DetailInfo.DetailInfo.SetManuNo
                                </RadzenText>
                            }
                        </Template>
                        @*      <EditTemplate Context="DetailInfo">
                            <RadzenDropDown TValue="DrugManuNo" Name="ManuNo" @bind-Value="DetailInfo.DetailInfo.drugManuNo" Data="DetailInfo.DetailInfo.Drug.Manus" Style="width:100%; display: block;">
                                <Template>
                                    <RadzenText TextStyle="TextStyle.Subtitle2" class="mb-0">@((context as DrugManuNo)?.ManuNo)</RadzenText>
                                    @if ((context as DrugManuNo).EffDate != null && (context as DrugManuNo).EffDate.ToString().Length > 10)
                                    {

                                        <RadzenText TextStyle="TextStyle.Caption">@((context as DrugManuNo).EffDate.ToString().Substring(0, 10))</RadzenText>
                                    }
                                    else
                                    {

                                        <RadzenText TextStyle="TextStyle.Caption">@((context as DrugManuNo)?.EffDate)</RadzenText>
                                    }
                                </Template>
                                <ValueTemplate>
                                    <RadzenStack Orientation="Orientation.Horizontal">
                                        <RadzenText TextStyle="TextStyle.Subtitle2" class="mb-0">@((context as DrugManuNo)?.ManuNo)</RadzenText>
                                        @if ((context as DrugManuNo).EffDate != null && (context as DrugManuNo).EffDate.ToString().Length > 10)
                                        {

                                            <RadzenText TextStyle="TextStyle.Caption">@((context as DrugManuNo).EffDate.ToString().Substring(0, 10))</RadzenText>
                                        }
                                        else
                                        {

                                            <RadzenText TextStyle="TextStyle.Caption">@((context as DrugManuNo)?.EffDate)</RadzenText>
                                        }
                                    </RadzenStack>
                                </ValueTemplate>
                            </RadzenDropDown>
                            <RadzenRequiredValidator Text="请选择批次" Component="ManuNo" Popup="true" />
                        </EditTemplate> *@
                    </RadzenDataGridColumn>
                    <RadzenDataGridColumn Property="DetailInfo.Quantity" Title="数量" Filterable="false" Width="6vw">
                        @* <EditTemplate Context="DetailInfo">
                            <RadzenNumeric Min="0" Style="display: block" Name="Quantity" @bind-Value=@DetailInfo.DetailInfo.Quantity />
                            <RadzenNumericRangeValidator Style="position: absolute;z-index: 9999;" Min="0" Text="请填写正确的添加数量" Component="Quantity" Popup="true" />
                        </EditTemplate> *@
                    </RadzenDataGridColumn>
                </Columns>
            </RadzenDataGrid>
        </div>
    </div>
</RadzenStack>
@code {

    private readonly ILog logger = LogManager.GetLogger(typeof(CheckOrder));
    @inject IOrderInfoDao orderInfoDao;
    @inject DialogService dialogService;
    @inject Microsoft.Extensions.Options.IOptions<Pojo.Config.SettingConfig> setting;
    @inject NotificationService _message;
    @inject IReportDataDao reportDataDao;
    @inject IOptions<SettingConfig> setting;



    [Parameter] public BoxModel boxModel { get; set; }

    bool allowRowSelectOnRowClick = true;
    IEnumerable<OrderInfo> orderInfos;
    IList<OrderInfo> selectedOrderInfos = new List<OrderInfo>();
    RadzenDataGrid<OrderInfo> grid;
    //麻醉师集合
    List<Anaesthetist> NamesList = new List<Anaesthetist>();
    Anaesthetist NameInfo;
    Anaesthetist BoxColor;
    //药盒集合
    List<BoxModel> BoxList = new List<BoxModel>();
    //药盒颜色集合
    List<Anaesthetist> BoxColorList = new List<Anaesthetist>();
    BoxModel BoxNum;
    bool isLoading;
    int count;
    int[] DrawerNos = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 };
    int status = 0;
    string OrderNo;
    string PatientName;
    DateTime OrderDate = DateTime.Now;
    DateTime PortOrderDate = DateTime.Now;
    Popup popup;
    RadzenButton button;
    //第一次加载不执行LoadData方法
    // int firstLod = 1;
    void OnCheckSeleced(object dete)
    {
        if (orderInfos != null)
        {
            orderInfos.ToList().ForEach(it => it.ItemIsChecked = true);
        }
    }
    void OnCurrentDateChanged(DateTime args)
    {
        OrderDate = new DateTime(args.Year, args.Month, args.Day);
    }
    void OnCellClick(DataGridCellMouseEventArgs<OrderInfo> args)
    {
        if (args.Data.DetailInfo.SetManuNo != null)
        {
            args.Data.DetailInfo.drugManuNo = args.Data.DetailInfo.Drug.Manus.Where(m => m.ManuNo == args.Data.DetailInfo.SetManuNo).FirstOrDefault();
        }
        if (args.Data.DetailInfo.drugManuNo == null)
        {
            args.Data.DetailInfo.drugManuNo = args.Data.DetailInfo.Drug.Manus.FirstOrDefault();
        }
        args.Data.ItemIsChecked = !args.Data.ItemIsChecked;
        grid.EditRow(args.Data);
    }
    //确认
    async Task Confirm()
    {
        if (selectedOrderInfos == null || selectedOrderInfos.Count<=0)
        {
            _message.Notify(
                   new NotificationMessage { Severity = NotificationSeverity.Error, Summary = "提示", Detail = $"请选择处方", Duration = 4000 }
               );
            return;
        }
        // 关闭弹窗selectedOrderInfos
        dialogService.Close(selectedOrderInfos);

    }
    PageData<OrderInfo> result;
    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
    }
    async Task LoadData(LoadDataArgs args)
    {
        // if (firstLod == 1)
        if (boxModel != null && boxModel.BoxName > 0)
        {
            //查询除本药箱外的处方信息
            result = await orderInfoDao.GetAllOrderInfoForImport(PatientName, OrderNo, OrderDate, boxModel, args.Top, args.Skip);
            orderInfos = result.Desserts;
            selectedOrderInfos = orderInfos.ToList();
            count = result.TotalDesserts;
            isLoading = true;
        }
        isLoading = false;
    }
}