using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using ZXDT.CallCenter.Model;
using ZXDT.CallCenter.MVCWeb.Models;

/// <summary>
/// 标签快捷管理
/// </summary>
namespace ZXDT.CallCenter.MVCWeb.Controllers
{
    public class ClassController : BaseController
    {
        BLL.T_Com_Class busClass = new BLL.T_Com_Class();
       

        #region 纯视图
        /// <summary>
        /// 回复列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetList()
        {
            return View();
        }
        /// <summary>
        /// 添加 视图
        /// </summary>
        /// <returns></returns>
        public ActionResult Add()
        {
            return View();
        }
        /// <summary>
        /// 修改 视图
        /// </summary>
        /// <returns></returns>
        public ActionResult Edit()
        {
            return View();
        }

        #endregion


        #region 增删改查
        [ActionName("GetListData")]
        public string GetListData(int parentid = 0, int page = 0, int limit = 20)
        {
            string strWhere = " 1=1 ";
            if (parentid>-1)
            {
                strWhere += string.Format(" and Parentid ={0}", parentid);
            }

            DataTable dt = busClass.GetListByPage(strWhere, " Sort asc ", (page - 1) * limit, limit).Tables[0];
            int count = busClass.GetRecordCount(strWhere);
            return Success("成功", dt, count);
        }

        [ActionName("GetData")]
        public string GettData(int id)
        {
            if (id==0)
                return Error("请输入ID");

            Model.T_Com_Class model = busClass.GetModel(id);
            return Success("成功", model, 1);
        }

        [ActionName("deletedata")]
        public string DeleteData(int id)
        {
            if (busClass.Delete(id))
            {
                AddAction("t_com_class", id.ToMyString(), "删除分类名称");
                return Success("删除成功");
            }              
            else
                return Error("失败");
        }



        [ActionName("adddata")]
        public string AddData(T_Com_Class modelinput)
        {
            T_Com_Class model = new T_Com_Class();
            model.Classcode = modelinput.Classcode;
            model.Classname = modelinput.Classname;
            //model.Parentcode = modelinput.Parentcode;
            model.Parentid = modelinput.Parentid;
            model.Remark = modelinput.Remark ?? "";
            model.Sort = modelinput.Sort;
            if (modelinput.Parentid != 0)
            {
                T_Com_Class modeltemp = busClass.GetModel(modelinput.Parentid);
                if (modeltemp != null)
                {
                    if (modeltemp.Parentid == 0)
                        model.typeid = modeltemp.Classid;
                    else
                        model.typeid = modeltemp.typeid;
                }
                else
                {
                    return Error("父节点不存在");
                }
            }
            if (string.IsNullOrEmpty(modelinput.Classname))
            {
                return Error("请输入值Classname");
            }

            string strWhere = string.Format("  Classname='{0}' and Parentid='{1}' ", model.Classname, model.Parentid);
            int count = busClass.GetRecordCount(strWhere);
            if (count > 0)
            {
                return Error("已存在");
            }



            int id = busClass.Add(model);
            if (id > 0)
            {
                AddAction("t_com_class", id.ToMyString(), "添加分类名称", model.Classname);
                return Success("成功", modelinput, 1);
            }
            else
                return Error("失败");
        }

        [ActionName("editdata")]
        public string EditData(T_Com_Class modelinput)
        {

            if (string.IsNullOrEmpty(modelinput.Classname))
            {
                return Error("请输入值Classname");
            }
            if (modelinput.Classid == 0)
                return Error("请输入值Classid");

            string strWhere = string.Format("  Classname='{0}' and Parentid='{1}' and Classid!={2} ", modelinput.Classname, modelinput.Parentid, modelinput.Classid);
            int count = busClass.GetRecordCount(strWhere);
            if (count > 0)
            {
                return Error("已存在");
            }


            Model.T_Com_Class model = busClass.GetModel(modelinput.Classid);
            model.Classname = modelinput.Classname;
            model.Parentid = modelinput.Parentid;
            model.Remark = modelinput.Remark ?? "";
            model.Sort = modelinput.Sort;
            model.Classcode = modelinput.Classcode;

            if (modelinput.Parentid != 0)
            {
                T_Com_Class modeltemp = busClass.GetModel(modelinput.Parentid);
                if (modeltemp.Parentid == 0)
                    model.typeid = modeltemp.Classid;
                else
                    model.typeid = modeltemp.typeid;
            }

            if (busClass.Update(model))
            {
                AddAction("t_com_class", model.Classid.ToMyString(), "更新分类名称", model.Classname);
                return Success("成功", model, 1);
            }
            else
                return Error("失败");
        }
        #endregion


        #region 获取树结构

        /// <summary>
        ///  获得当前节点下的所有子集以tree的格式
        /// </summary>
        public string GetTreeJsonModel(int parentId, int type = 0)
        {
              return Newtonsoft.Json.JsonConvert.SerializeObject(busClass.GetTreeJsonModel(parentId, type));
        }


        /// <summary>
        /// 常用人员名单
        /// </summary>
        public string GetTreeJsonModelTurn()
        {
            return Newtonsoft.Json.JsonConvert.SerializeObject(busClass.GetTreeJsonModelTurn(F_UserCode));
        }


        #endregion
    }
}
