using CallCenter.Utility;
using CallCenter.Utility.Linq;
using CallCenterApi.DB;
using CallCenterApi.Interface.Controllers.Base;
using CallCenterApi.Interface.Models.Input;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CallCenterApi.Interface.Controllers
{
    //[Authority]
    public class RoleInfoController : BaseController
    {
        private BLL.T_Sys_RoleInfo roleInfoBLL = new BLL.T_Sys_RoleInfo();
        // 获取角色列表
        public ActionResult GetRoleList(int pageIndex = 1, int pageSize = 100, string code = "", string name = "")
        {
            ActionResult res = NoToken("未知错误，请重新登录");

            int deptid = CurrentUser.UserData.F_DeptId;         //部门id
            DataTable dt = new DataTable();
            var sql = "  ";
            #region 权限设置（角色权限）
            //部门信息
            var modelDep = new BLL.T_Sys_Department().GetModel(deptid);
            int depType = 0;
            if (modelDep != null)
                depType = modelDep.F_Type ?? 0;  //部门操作权限：1接待部，2办理人员，3区域客服，4监管

            int roleId = CurrentUser.UserData.F_RoleId;
            if (roleId != 17)
            {
                sql += $"  and F_RoleId != 17 ";
                //if (depType != 1)
                //{
                //    sql += $"  and F_Type != 1 ";
                //}
            }            
            #endregion

            if (!string.IsNullOrEmpty(name))
            {
                sql += "  and  F_RoleName like '%" + name + "%' and F_RoleCode like '%" + code + "%'  ";
            }
            int recordCount = 0;
            dt = BLL.PagerBLL.GetListPager(
                "T_Sys_RoleInfo",
                "F_RoleId",
                "*",
                sql,
                "",
                pageSize,
                pageIndex,
                true,
                out recordCount);
            var obj = new
            {
                rows = dt,
                total = recordCount
            };
            res = Content(obj.ToJson());
            return res;
        }

        //获取角色
        public ActionResult GetRole(int roleid = 0)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                Model.T_Sys_RoleInfo dModel = roleInfoBLL.GetModel(roleid);
                if (dModel != null)
                    res = Success("获取角色信息成功", dModel);
                else
                    res = Error("获取角色信息失败");
            }
            return res;
        }

        //添加角色
        public ActionResult AddRole(RoleInput input)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                Model.T_Sys_RoleInfo rModel = new Model.T_Sys_RoleInfo();
                rModel.F_RoleName = input.RoleName;
                rModel.F_Remark = input.RoleRemark;
                if (string.IsNullOrEmpty(input.Code))
                {
                    rModel.F_RoleCode = ChineseToPinYin.GetSpellCode2(input.RoleName).ToUpper();
                }
                else
                {
                    rModel.F_RoleCode = input.Code;
                }
                if (roleInfoBLL.Add(rModel) > 0)
                    res = Success("添加成功");
                else
                    res = Success("添加失败");
            }
            return res;
        }

        //编辑角色
        public ActionResult EditRole(RoleInput input)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                if (input.RoleId <= 0)
                    return Error("请选择要编辑的角色");
                Model.T_Sys_RoleInfo rModel = roleInfoBLL.GetModel(input.RoleId);
                rModel.F_Remark = input.RoleRemark;
                if (!string.IsNullOrEmpty(input.Code))
                    rModel.F_RoleCode = input.Code;
                rModel.F_RoleName = input.RoleName;
                if (rModel == null)
                    return Error("获取信息失败");
                if (roleInfoBLL.Update(rModel))
                    res = Success("编辑成功");
                else
                    res = Success("编辑失败");
            }
            return res;
        }

        //删除角色
        public ActionResult DelRole(int id = 0)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                if (id <= 0)
                    return Error("请选择角色");

                if (roleInfoBLL.Delete(id))
                    res = Success("删除成功");
                else
                    res = Error("删除失败");
            }
            return res;
        }
    }
}