using System.Collections.Generic;
using System.Web.Mvc;
using Learun.Application.Base.AuthorizeModule;
using Learun.Application.Organization;
using Learun.Util;

namespace Learun.Application.Web.Areas.LR_OrganizationModule.Controllers
{
    /// <summary>
    /// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
    /// Copyright (c) 2013-2018 Hexagon PPM
    /// 创建人：研发部
    /// 日 期：2017.03.09
    /// 描 述：岗位管理
    /// </summary>
    public class PostController : MvcControllerBase
    {
        private PostIBLL postIBLL = new PostBLL();
        private UserRelationIBLL userRelationIBLL = new UserRelationBLL();
        private UserIBLL userIBLL = new UserBLL();
        private CompanyIBLL companyIBLL = new CompanyBLL();

        #region 获取视图
        /// <summary>
        /// 主页
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 表单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Form()
        {
            return View();
        }
        /// <summary>
        /// 岗位选择页面
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult SelectForm()
        {
            return View();
        }
        /// <summary>
        /// 表单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult PlatIndex()
        {
            return View();
        }
        /// <summary>
        /// 岗位页
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult PostForm()
        {
            return View();
        }
        #endregion

        #region 获取数据
        /// <summary>
        /// 获取岗位列表信息
        /// </summary>
        /// <param name="keyWord">查询关键字</param>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetPageList(string pagination, string queryJson)
        {
            Pagination paginationobj = pagination.ToObject<Pagination>();
            var data = postIBLL.GetPostList(paginationobj, queryJson);

            return Success(data);
        }

        /// <summary>
        /// 获取表单数据
        /// <summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetFormData(string keyValue)
        {
            var PostEntitydata = postIBLL.GetEntity(keyValue);
            var jsonData = new
            {
                PostEntity = PostEntitydata,
            };
            return Success(jsonData);
        }

        /// <summary>
        /// 获取岗位树形数据
        /// </summary>
        /// <param name="ProjectId">项目id</param>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetOrganizationTree(string ProjectId)
        {
            var list = postIBLL.GetOrganizationTree(ProjectId);
            List<TreeModel> treeList = new List<TreeModel>();
            foreach (var item in list)
            {
                TreeModel node = new TreeModel();
                node.id = item.F_PostId;
                node.text = item.F_Name;
                node.value = item.F_PostId;
                node.showcheck = false;
                node.checkstate = 0;
                node.isexpand = false;
                node.parentId = item.F_ParentId;
                treeList.Add(node);
            }
            return Success(treeList.ToTree());
        }

        /// <summary>
        /// 获取树形数据
        /// </summary>
        /// <param name="companyId">公司主键</param>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetSelectTree(string companyId)
        {
            if (string.IsNullOrEmpty(companyId))
            {
                return Success(new List<TreeModel>());
            }

            var list = postIBLL.GetTree(companyId);
            List<TreeModel> treeList = new List<TreeModel>();
            foreach (var item in list)
            {
                TreeModel node = new TreeModel();
                node.id = item.F_PostId;
                node.text = item.F_Name;

                node.value = item.F_PostId;
                node.showcheck = false;
                node.checkstate = 0;
                node.isexpand = false;
                node.parentId = item.F_ParentId;
                treeList.Add(node);
            }
            return Success(treeList.ToTree());
        }

        /// <summary>
        /// 获取单位级的树形数据
        /// </summary>
        /// <param name="companyId">公司主键</param>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetCompanyTree()
        {
            var list = postIBLL.GetCompanyTree();
            List<TreeModel> treeList = new List<TreeModel>();
            foreach (var item in list)
            {
                TreeModel node = new TreeModel();
                node.id = item.F_PostId;
                node.text = item.F_Name;
                node.value = item.F_PostId;
                node.showcheck = false;
                node.checkstate = 0;
                node.isexpand = false;
                node.parentId = item.F_ParentId;
                treeList.Add(node);
            }
            return Success(treeList.ToTree());
        }
        /// <summary>
        /// 获取岗位名称
        /// </summary>
        /// <param name="keyValue">岗位主键</param>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetEntityName(string keyValue)
        {
            if (keyValue == "0")
            {
                return SuccessString("");
            }
            var data = postIBLL.GetEntity(keyValue);
            return SuccessString(data.F_Name);
        }
        /// <summary>
        /// 获取岗位实体数据
        /// </summary>
        /// <param name="keyValue">岗位主键</param>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetEntity(string keyValue)
        {
            var data = postIBLL.GetEntity(keyValue);
            return Success(data);
        }
        #endregion

        #region 提交数据
        /// <summary>
        /// 保存表单数据
        /// </summary>
        /// <param name="keyValue">主键</param>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        [AjaxOnly]
        public ActionResult SaveForm(string keyValue, PostEntity entity)
        {
            int code = 0;
            int.TryParse(entity.F_EnCode, out code);
            if (code > 0 && code < 10)
            {
                entity.F_EnCode = entity.F_EnCode.PadLeft(2, '0');
            }
            postIBLL.SaveEntity(keyValue, entity);
            return Success("保存成功！");
        }

        /// <summary>
        /// 删除岗位数据
        /// </summary>
        /// <param name="keyValue">主键</param>
        /// <returns></returns>
        [HttpPost]
        [AjaxOnly]
        public ActionResult DeletePostForm(string keyValue)
        {
            postIBLL.VirtualDelete(keyValue);
            return Success("删除成功！");
        }
        #endregion
    }
}