using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using MySystem.Models;
using Library;

namespace MySystem.Areas.Admin.Controllers
{
    [Area("Admin")]
    [Route("Admin/[controller]/[action]")]
    public class MerchantManageController : Controller
    {
        public WebCMSEntities db = new WebCMSEntities();
        public BsModels.WebCMSEntities bsdb = new BsModels.WebCMSEntities();
        public IHttpContextAccessor _accessor;
        public MerchantManageController(IHttpContextAccessor accessor)
        {
            _accessor = accessor;
        }

        /// <summary>
        /// 主界面
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            if (function.GetSession(_accessor.HttpContext, "IsLogin") != "1")
            {
                return Redirect("/Admin/Merchants/Login");
            }
            ViewBag.SysUserName = function.GetCookie(_accessor.HttpContext, "SysUserName");

            List<BsModels.RightDic> RightList = bsdb.RightDic.OrderBy(m => m.Id).ToList();
            ViewBag.RightList = RightList;

            ViewBag.SysRealName = function.GetCookie(_accessor.HttpContext, "SysRealName");
            ViewBag.RightInfo = function.GetSession(_accessor.HttpContext, "RightInfo");
            return View();
        }

        /// <summary>
        /// 统计界面
        /// </summary>
        /// <returns></returns>
        public IActionResult Main()
        {
            int SysId = int.Parse(function.CheckInt(function.GetCookie(_accessor.HttpContext, "SysId")));
            int productCount = db.Products.Count(m => m.MerchantId == SysId);
            int orderCount = db.Orders.Count(m => m.MerchantId == SysId);
            ViewBag.productCount = productCount.ToString();
            ViewBag.orderCount = orderCount.ToString();
            return View();
        }

        #region 后台管理员登录
        public IActionResult Login()
        {
            return View();
        }

        /// <summary>
        /// 后台管理员登录
        /// </summary>
        /// <param name="UserName">用户名</param>
        /// <param name="Pwd">密码</param>
        /// <returns></returns>
        [HttpPost]
        public string Login(string UserName, string Pwd, string CheckCode)
        {
            string result = "";
            if (function.GetCookie(_accessor.HttpContext, "checkcode") != CheckCode)
            {
                result = "验证码错误!!";
            }
            else
            {
                Pwd = function.MD532(Pwd);
                var user = db.Merchants.FirstOrDefault(m => m.UserName == UserName && m.LoginPwd == Pwd);
                if (user != null)
                {
                    function.WriteCookie(_accessor.HttpContext, "SysUserName", user.UserName);
                    function.WriteCookie(_accessor.HttpContext, "SysRealName", user.Name);
                    function.WriteCookie(_accessor.HttpContext, "SysRealRole", "3");
                    BsModels.WebCMSEntities db = new BsModels.WebCMSEntities();
                    BsModels.SysAdminRole Role = db.SysAdminRole.FirstOrDefault(m => m.Id == 3) ?? new BsModels.SysAdminRole();
                    string Rights = Role.RightInfo;
                    Rights = Rights.Replace(",3_2,", ",");
                    function.WriteSession(_accessor.HttpContext, "RightInfo", Rights);
                    string UserId = user.Id.ToString();
                    function.WriteCookie(_accessor.HttpContext, "SysId", UserId);
                    function.WriteSession(_accessor.HttpContext, "IsLogin", "1");
                    db.Dispose();
                    result = "success";
                }
                else
                {
                    result = "用户名或密码错误";
                }
            }
            return result;
        }
        #endregion

        #region 退出登录
        /// <summary>
        /// 退出登录
        /// </summary>
        public void Quit()
        {
            function.WriteCookie(_accessor.HttpContext, "SysUserName", "", -1);
            function.WriteCookie(_accessor.HttpContext, "SysRealName", "", -1);
            function.WriteCookie(_accessor.HttpContext, "SysId", "", -1);
            function.WriteCookie(_accessor.HttpContext, "Role", "", -1);
            function.WriteCookie(_accessor.HttpContext, "IsLogin", "", -1);
            _accessor.HttpContext.Response.Redirect("/Admin/Merchants/Login");
        }

        #endregion

        #region 修改登录密码
        /// <summary>
        /// 修改登录密码
        /// </summary>
        /// <returns></returns>
        public IActionResult LoginPassword()
        {
            return View();
        }
        public string LoginPasswordPost(string OldPassword, string NewPassword, string NewPassword2)
        {
            if (string.IsNullOrEmpty(OldPassword))
            {
                return "请输入旧密码";
            }
            if (string.IsNullOrEmpty(NewPassword))
            {
                return "请输入新密码";
            }
            if (string.IsNullOrEmpty(NewPassword2))
            {
                return "请确认密码";
            }
            if (NewPassword != NewPassword2)
            {
                return "两次密码输入不一致";
            }
            int SysId = int.Parse(function.CheckInt(function.GetCookie(_accessor.HttpContext, "SysId")));
            Merchants sys = db.Merchants.FirstOrDefault(m => m.Id == SysId);
            if (sys != null)
            {
                if (function.MD5_32(OldPassword) != sys.LoginPwd)
                {
                    return "旧密码不正确";
                }
                sys.LoginPwd = function.MD5_32(NewPassword);
                db.SaveChanges();
                return "success";
            }
            return "修改失败";
        }

        #endregion

        #region 个人资料
        /// <summary>
        /// 修改登录密码
        /// </summary>
        /// <returns></returns>
        public IActionResult AdminInfo()
        {
            int SysId = int.Parse(function.CheckInt(function.GetCookie(_accessor.HttpContext, "SysId")));
            Merchants data = db.Merchants.FirstOrDefault(m => m.Id == SysId) ?? new Merchants();
            ViewBag.data = data;
            return View();
        }
        public string AdminInfoPost(Merchants data)
        {
            int SysId = int.Parse(function.CheckInt(function.GetCookie(_accessor.HttpContext, "SysId")));
            Merchants sys = db.Merchants.FirstOrDefault(m => m.Id == SysId);
            if (sys != null)
            {
                sys.Name = data.Name;
                sys.Phone = data.Phone;
                sys.Areas = data.Areas;
                sys.Address = data.Address;
                sys.BusinessLicense = data.BusinessLicense;
                sys.Logo = data.Logo;
                sys.Principal = data.Principal;
                db.SaveChanges();
                return "success";
            }
            return "修改失败";
        }

        #endregion
    }
}
