using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Data;
using wispro.sp.entity;
using wispro.sp.share;
using System.Linq;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;

namespace wispro.sp.api.Controllers
{
    //[Authorize]
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class StaffController : ControllerBase
    {
        private static List<Staff> CacheList = new List<Staff>();
        spDbContext Context;

        public StaffController(spDbContext context)
        {
            Context = context;
        }
        

        public List<Staff> GetAll()
        {
            return Context.Staffs.ToList<Staff>();
        }
        
        
        public ListApiResponse<Staff> Query(int pageIndex,int pageSize)
        {
            ListApiResponse<Staff> ret = new ListApiResponse<Staff>();
            ret.TotalCount = Context.Staffs.Count<Staff>();

            List<Staff> retList = Context.Staffs.Skip<Staff>((pageIndex - 1) * pageSize).Take(pageSize).ToList<Staff>();

            //for(int i = (pageIndex-1) * pageSize; i < pageIndex  * pageSize; i++)
            //{
            //    if (i < CacheList.Count)
            //    {
            //        retList.Add(CacheList[i]);
            //    }
            //}
            ret.Results = retList;

            return ret;
        }

        [HttpPost]
        public ApiSaveResponse Save(Staff staff)
        {
            ApiSaveResponse ret = new ApiSaveResponse();
            try
            {
                if (staff.Id == 0)
                {
                    
                    Staff temStaff = Context.Staffs.Where<Staff>(x => x.Name == staff.Name).FirstOrDefault();

                    if (temStaff != null)
                    {
                        temStaff.Account = staff.Account;
                        temStaff.Department = staff.Department;
                        temStaff.EntyDate = staff.EntyDate;
                        temStaff.IsCalPerformsnce = staff.IsCalPerformsnce;
                        temStaff.IsOnJob = staff.IsOnJob;
                        temStaff.Mail = staff.Mail;
                        temStaff.Memo = staff.Memo;
                        temStaff.Mobile = staff.Mobile;
                        temStaff.Name = staff.Name;
                        temStaff.Password = staff.Password;
                        temStaff.Sex = staff.Sex;
                        temStaff.StaffGradeId = staff.StaffGradeId;
                        temStaff.Status = staff.Status;
                        temStaff.Tel = staff.Tel;
                        temStaff.WorkPlace = staff.WorkPlace;

                        Context.SaveChanges();

                        ret.Success = false;
                        ret.ErrorMessage = $"用户【{staff.Name}】已存在！";
                    }
                    else
                    {
                        Context.Staffs.Add(staff);
                    }
                }
                else
                {
                    Staff editObject = Context.Staffs.Where<Staff>(x => x.Id == staff.Id).FirstOrDefault();

                    if (editObject != null)
                    {
                        editObject.Account = staff.Account;
                        editObject.Department = staff.Department;
                        editObject.EntyDate = staff.EntyDate;
                        editObject.IsCalPerformsnce = staff.IsCalPerformsnce;
                        editObject.IsOnJob = staff.IsOnJob;
                        editObject.Mail = staff.Mail;
                        editObject.Memo = staff.Memo;
                        editObject.Mobile = staff.Mobile;
                        editObject.Name = staff.Name;
                        editObject.Password = staff.Password;
                        editObject.Sex = staff.Sex;
                        editObject.StaffGradeId = staff.StaffGradeId;
                        editObject.Status = staff.Status;
                        editObject.Tel = staff.Tel;
                        editObject.WorkPlace = staff.WorkPlace;
                    }
                    else
                    {
                        ret.Success = false;
                        ret.ErrorMessage = $"编号为【{staff.Id}】的用户不存在！";
                    }
                    
                }

                Context.SaveChanges();

                ret.Success = true;
            }
            catch (Exception ex)
            {
                ret.Success = false;
                ret.ErrorMessage = ex.Message;
            }

            return ret;
        }
    }
}
