using AntDesign;
using Microsoft.AspNetCore.Components;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using wispro.sp.entity;
using wispro.sp.web.Services;

namespace wispro.sp.web.Components
{
    public partial class StaffAutoComplete
    {
        
        private List<Staff> ShowStaffs = new List<Staff>();

        [Parameter]
        public string SelectName { get; set; }

        [Parameter]
        public EventCallback<string> SelectNameChanged { get; set; }

        [Parameter]
        public List<Staff> Staffs { get; set; }

        [Parameter]
        public EventCallback<Staff> OnSelected { get; set; }

        [Inject] IUserService _UserService { get; set; }

        protected override async System.Threading.Tasks.Task OnInitializedAsync()
        {
            base.OnInitialized();
            if (Staffs == null)
            {
                Staffs = await _UserService.GetAll();
                ShowStaffs = Staffs;
            }
        }

        
        void OnSelectionChange(AutoCompleteOption item)
        {
            SelectNameChanged.InvokeAsync(item.Value.ToString());

            if (OnSelected.HasDelegate)
            {
                Staff obj = ShowStaffs.FirstOrDefault<Staff>(p => p.Name == item.Value.ToString());
                OnSelected.InvokeAsync(obj);
            }
        }

        void OnInput(ChangeEventArgs e)
        {
            var v = e.Value.ToString();
            
            this.ShowStaffs = Staffs.Where<Staff>(s => s.Name.Contains(v)).ToList();
            
        }

    }
}
