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 StaffSelect
    {
        private Staff _SelectedItem;
        private List<Staff> _ShowStaffs;

        [Parameter]
        public int? StaffId { get; set; }

        [Parameter]
        public EventCallback<int?> StaffIdChanged { get; set; }

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

        [Parameter]
        public bool AutoSelectedFirstItem { get; set; } = true;

        [Inject] IUserService _UserService { get; set; }


        protected override void OnInitialized()
        {
            _ShowStaffs = StaffLists;
            if(_ShowStaffs !=null && _ShowStaffs.Count > 0 && AutoSelectedFirstItem && StaffId ==null)
            {
                StaffId = _ShowStaffs[0].Id;
            }
            base.OnInitialized();
        }

        protected override Task OnParametersSetAsync()
        {
            _ShowStaffs = StaffLists;
            if (_ShowStaffs != null && _ShowStaffs.Count > 0 && AutoSelectedFirstItem && StaffId == null)
            {
                StaffId = _ShowStaffs[0].Id;
            }
            return base.OnParametersSetAsync();
        }

        private void OnSelectedItemChangedHandler(Staff value)
        {
            _SelectedItem = value;
            StaffIdChanged.InvokeAsync(_SelectedItem.Id);
        }

        private void OnSearch(string value)
        {
            //_ShowStaffs = StaffLists.Where<Staff>(p => p.Name.Contains(value)).ToList();
            //StateHasChanged();
        }
    }
}
