using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ViewModels;
using WebAPIServer.Extensions;
using WebAPIServer.Models;

namespace WebAPIServer.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UsersController : ControllerBase
    {
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="LLL"></param>
        /// <returns></returns>
        [HttpPost()]
        [Authorize()]
        public ReturnInfo DelUser([FromBody] TblUtsManageUser LLL)
        {
            ReturnInfo returnInfo = new ReturnInfo();

            try
            {
                using (var q = new UtsManageContext())
                {
                    var FFF = q.TblUtsManageUsers.FirstOrDefault(A => A.Id == LLL.Id);
                    if (FFF != null)
                    {
                        FFF.IsValid = false;
                        q.TblUtsManageUsers.Update(FFF);
                        q.SaveChanges();
                        returnInfo.isok = true;
                    }
                }
            }
            catch (Exception ex)
            {
                returnInfo.isok = false;
                returnInfo.message = ex.Message;
            }
            return returnInfo;
        }

        /// <summary>
        /// 修改用户
        /// </summary>
        /// <param name="LLL"></param>
        /// <returns></returns>
        [HttpPost()]
        [Authorize()]
        public ReturnInfo EditUser([FromBody] ReturnUser LLL)
        {
            ReturnInfo returnInfo = new ReturnInfo();

            try
            {
                using (var q = new UtsManageContext())
                {
                    var lll = q.TblUtsManageUsers.SingleOrDefault(A => A.Id == LLL.Id);
                    if (lll != null)
                    {

                        string username = LLL.UserName;
                        int companyId = LLL.CompanyId;
                        string mobile = LLL.Mobile;
                        string weiXin = LLL.WeiXin;
                        string email = LLL.Email;

                        lll.UserName = username;
                        lll.CompanyId = companyId;
                        lll.Mobile = mobile;
                        lll.WeiXin = weiXin;
                        lll.Email = email;
                        lll.UpdateTime = DateTime.Now;

                        q.TblUtsManageUsers.Update(lll);
                        q.SaveChanges();
                        returnInfo.isok = true;
                    }
                }
            }
            catch (Exception ex)
            {
                returnInfo.isok = false;
                returnInfo.message = ex.Message;
            }
            return returnInfo;
        }

        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="LLL"></param>
        /// <returns></returns>
        [HttpPost()]
        [Authorize()]
        public ReturnInfo ResetPassWord([FromBody] PWD_Reset LLL)
        {
            ReturnInfo returnInfo = new ReturnInfo();
            try
            {
                using (var q = new UtsManageContext())
                {
                    var QQQ = q.TblUtsManageUsers.SingleOrDefault(A => A.Id == LLL.Id);
                    if (QQQ != null)
                    {
                        QQQ.Password = LLL.PlaintextPwd.ToMD5().ToMD5();
                        QQQ.PlaintextPwd = LLL.PlaintextPwd;
                        q.TblUtsManageUsers.Update(QQQ);
                        q.SaveChanges();
                        returnInfo.isok = true;
                    }
                }
            }
            catch (Exception ex)
            {
                returnInfo.isok = false;
                returnInfo.message = ex.Message;
            }
            return returnInfo;
        }
        /// <summary>
        /// 新增密码为123456
        /// </summary>
        /// <param name="LLL"></param>
        /// <returns></returns>
        [HttpPost()]
        [Authorize()]
        public ReturnInfo NewPassWord([FromBody] PWD_Reset LLL)
        {
            ReturnInfo returnInfo = new ReturnInfo();
            try
            {
                using (var q = new UtsManageContext())
                {
                    var Q = q.TblUtsManageUsers.SingleOrDefault(A => A.Id == LLL.Id);
                    if (Q != null)
                    {
                        Q.Password = "123456".ToMD5().ToMD5();
                        Q.PlaintextPwd = "123456";
                        q.TblUtsManageUsers.Update(Q);
                        q.SaveChanges();
                        returnInfo.isok = true;
                    }
                }
            }
            catch (Exception ex)
            {
                returnInfo.isok = false;
                returnInfo.message = ex.Message;
            }
            return returnInfo;
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="S"></param>
        /// <returns></returns>
        [HttpPost()]
        [Authorize()]
        public ReturnInfo GetUserInfo([FromBody] QueryAll_Or_Single S)
        {
            ReturnInfo returnInfo = new ReturnInfo();

            try
            {
                using (var q = new UtsManageContext())
                {
                    if (S.IsAll)
                    {
                        returnInfo.isok = true;
                        returnInfo.response = q.TblUtsManageUsers.Where(A => A.IsValid).Select(F => new ReturnUser
                        {
                            Id = F.Id,
                            UserName = F.UserName,
                            CompanyId = F.CompanyId,
                            Mobile = F.Mobile,
                            WeiXin = F.WeiXin,
                            Email = F.Email,
                        }).ToList();
                    }
                    else
                    {
                        returnInfo.isok = true;
                        var a = q.TblUtsManageUsers.SingleOrDefault(A => A.Id == S.ID);
                        if (a != null)
                        {
                            ReturnUser u = new ReturnUser();
                            u.Id = a.Id;
                            u.UserName = a.UserName;
                            u.CompanyId = a.CompanyId;
                            u.Mobile = a.Mobile;
                            u.WeiXin = a.WeiXin;
                            u.Email = a.Email;
                            returnInfo.response = u;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                returnInfo.isok = false;
                returnInfo.message = ex.Message;
            }
            return returnInfo;
        }

        /// <summary>
        /// 新增用户
        /// </summary>
        /// <param name="LLL"></param>
        /// <returns></returns>
        [HttpPost()]
        [Authorize()]
        public ReturnInfo AddUser([FromBody] ReturnUser LLL)
        {
            ReturnInfo returnInfo = new ReturnInfo();

            try
            {

                //            {
                //"id": 0,
                //"userName": "test6",
                //"companyId": "",
                //"companyName": "",
                //"mobile": "",
                //"weiXin": "",
                //"email": "",
                //"resetpsw": true,
                //"permissions": [],
                //"company": ""
                //}
                string username = LLL.UserName;
                int companyId = LLL.CompanyId;
                string mobile = LLL.Mobile;
                string weiXin = LLL.WeiXin;
                string email = LLL.Email;

                TblUtsManageUser lll = new TblUtsManageUser();
                lll.UserName = username;
                lll.CompanyId = companyId;
                lll.Mobile = mobile;
                lll.WeiXin = weiXin;
                lll.Email = email;
                lll.IsValid = true;
                lll.CreateTime = DateTime.Now;
                lll.UpdateTime = DateTime.Now;

                using (var q = new UtsManageContext())
                {
                    var Q = q.TblUtsManageUsers.Where(A => A.UserName.Equals(username));
                    if (Q.Count() > 0)
                    {
                        returnInfo.isok = false;
                        returnInfo.message = "此用户名已经存在";
                    }
                    else
                    {
                        lll.PlaintextPwd = "123456";
                        lll.Password = "123456".ToMD5().ToMD5(); ;
                        q.TblUtsManageUsers.Add(lll);
                        returnInfo.isok = true;
                    }
                    q.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                returnInfo.isok = false;
                returnInfo.message = ex.Message;
            }
            return returnInfo;
        }
    }
    /// <summary>
    /// 返回用户信息
    /// </summary>
    public class ReturnUser
    {
        public int Id { get; set; }
        public string UserName { get; set; }
        public int CompanyId { get; set; }
        public string Mobile { get; set; }
        public string WeiXin { get; set; }
        public string Email { get; set; }
    }
    public class PWD_Reset
    {
        public int Id { get; set; }
        public string PlaintextPwd { get; set; }
    }

}
