using AntDesign;
using AntDesign.TableModels;
using Blazor.ECharts.Options.Enum;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
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.Pages
{
    public partial class CustomerList
    {
        private List<entity.Customer> _Customers =new List<entity.Customer>();
        private List<entity.Staff> _Staffs = new List<entity.Staff>();

        IEnumerable<entity.Customer> selectedRows;
        ITable table;

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

        entity.Customer Editing = null;
        bool _visible = false;
        bool _isAdd = false;

        [Inject]OrganizationService  orgService { get; set; }
        [Inject] protected IAuthService _authService { get; set; }

        [Inject] IUserService _UserService { get; set; }

        protected override async System.Threading.Tasks.Task OnInitializedAsync()
        {
            await _authService.CanVisitResource();

            _loading = true;
            _Customers = await  orgService.GetAllCustomer();
            
            _Staffs = await _UserService.GetAll();
            //await System.Threading.Thread.Sleep();
            //List<wispro.sp.entity.StaffGrade> data = await Http.GetFromJsonAsync<List<wispro.sp.entity.StaffGrade>>($"http://localhost:39476/api/StaffGrade/GetAll");

            _loading = false;
            _total = _Customers.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<entity.Customer> 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 DelCustumer = _Customers.Where(x => x.Id == id).FirstOrDefault<entity.Customer>();

            if (DelCustumer != null)
            {
                Editing = DelCustumer;
                _isAdd = false;
                _visible = true;
            }
            else
            {

            }
            _total = _Customers.Count;
        }

        private void Edit(int id)
        {
            Editing = _Customers.Where(x => x.Id == id).FirstOrDefault<entity.Customer>();

            if (Editing != null)
            {
               
                //Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(Editing));
                _isAdd = false;
                _visible = true;
            }
            else
            {

            }
            _total = _Customers.Count;
        }

        private void AddNew()
        {
            Editing = new entity.Customer();
            //_isAdd = true;
            _visible = true;
        }

        private async Task HandleOk(MouseEventArgs e)
{
            var data = await orgService.SaveCustomer(Editing); // Http.PostAsJsonAsync<wispro.sp.entity.Staff>($"http://localhost:39476/api/Staff/Save", EditingStaff);

            if (data.Success)
            {
                if (_isAdd)
                {
                    _Customers = await orgService.GetAllCustomer();
                    //table.ReloadData();
                    StateHasChanged();
                }
                await _message.Success("数据已保存!");
            }
            else
            {
                await _message.Error($"{data.ErrorMessage}");
            }

            _visible = false;
        }

        private void HandleCancel(MouseEventArgs e)
        {
            _visible = false;
        }
    }
}
