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;
            
            //DataTable dt = wispro.sp.utility.NPOIExcel.ExcelToDataTable(@"D:\users\luowen\Documents\工作文档\深圳威世博人力绩效\210730-威世博人员清单v1r01-xm.xls", true);

            //CacheList = new List<Staff>();

            //if (dt != null)
            //{
            //    foreach (DataRow row in dt.Rows)
            //    {
            //        CacheList.Add(new Staff()
            //        {
            //            Name = row["姓名"].ToString(),
            //            Status = row["岗位状态"].ToString(),
            //            IsCalPerformsnce = (row["是否核算绩效"].ToString() == "是"),
            //            StaffGrade = new StaffGrade() { Grade = row["工程师等级"].ToString(), Coefficient = double.Parse(row["等级系数"].ToString()) },
            //            Department = row["部门"].ToString(),
            //            WorkPlace = row["工作地"].ToString(),
            //            EntyDate = DateTime.Parse(row["入职时间"].ToString()),
            //            Memo = row["备注"].ToString()

            //        });
            //    }
            //}
        }
        

        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)
                {
                    
                    List<Staff> grades = Context.Staffs.Where<Staff>(x => x.Name == staff.Name).ToList();

                    if (grades.Count > 0)
                    {
                        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.Name = staff.Name;
                        editObject.StaffGradeId = staff.StaffGradeId;
                        editObject.Account = staff.Account;
                        editObject.Department = staff.Department;
                        editObject.EntyDate = staff.EntyDate;
                        editObject.IsCalPerformsnce = staff.IsCalPerformsnce;
                        editObject.Memo = staff.Memo;
                        editObject.Password = editObject.Password;
                        editObject.Status = staff.Status;
                        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;
        }
    }
}
