using AntDesign;
using AntDesign.TableModels;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Json;
using System.Threading.Tasks;
using wispro.sp.share;
using wispro.sp.web.Services;

namespace wispro.sp.web.Pages
{
    public partial  class StaffGrade
    {
        [Inject] public StaffGradeService _sfService { get; set; }
        
        private List<wispro.sp.entity.StaffGrade> staffGrades;
        IEnumerable<wispro.sp.entity.StaffGrade> selectedRows;
        
        ITable table;

        int _pageIndex = 1;
        int _pageSize = 10;
        int _total = 0;
        bool _loading = false;

        wispro.sp.entity.StaffGrade EditingStaff = null;
        bool _visible = false;
        bool _isAdd = false;

        protected override async Task OnInitializedAsync()
        {

            _loading = true;


            List<wispro.sp.entity.StaffGrade> data = await _sfService.GetAll();// await Http.GetFromJsonAsync<List<wispro.sp.entity.StaffGrade>>($"http://localhost:39476/api/StaffGrade/GetAll");

            _loading = false;
            staffGrades = data;
            _total = data.Count;

        }

        private void HandlePageChange(PaginationEventArgs args)
        {
            if (_pageIndex != args.Page)
            {
                _pageIndex = args.Page;
            }

            if (_pageSize != args.PageSize)
            {
                _pageSize = args.PageSize;
            }
        }


        Dictionary<string, object> OnRow(RowData<wispro.sp.entity.StaffGrade> row)
        {
            Dictionary<string, object> ret = new Dictionary<string, object>();

            ret.Add("id", row.Data.Id);
            ret.Add("onclick", ((Action)delegate
            {
                //_message.Info($"row {row.Data.Grade} was clicked");
            }));


            return ret;
        }

        public void RemoveSelection(int id)
        {
            //var selected = selectedRows.Where(x => x.Id != id);
            //selectedRows = selected;
        }

        private void Delete(int id)
        {
            var rList = staffGrades.Where(x => x.Id == id).ToList();

            if (rList.Count() > 0)
            {
                EditingStaff = rList[0];
                _isAdd = false;
                _visible = true;
            }
            else
            {

            }
            _total = staffGrades.Count;
        }

        private void Edit(int id)
        {
            var rList = staffGrades.Where(x => x.Id == id).ToList();

            if (rList.Count() > 0)
            {
                EditingStaff = rList[0];
                _isAdd = false;
                _visible = true;
            }
            else
            {

            }
            _total = staffGrades.Count;
        }

        private void AddNew()
        {
            EditingStaff = new entity.StaffGrade();
            _isAdd = true;
            _visible = true;
        }

        private async Task HandleOk(MouseEventArgs e)
        {

            ApiSaveResponse result = await _sfService.Save(EditingStaff);
            
            if (result.Success)
            {
                await _message.Success("数据已保存!");
            }
            else
            {
                await _message.Error($"{result.ErrorMessage}");
            }

            _visible = false;
        }

        private void HandleCancel(MouseEventArgs e)
        {
            //Console.WriteLine(e);
            _visible = false;
        }
    }
}

