using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
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 TagController : BaseController
    {
        BLL.T_Com_Tag busTag = new BLL.T_Com_Tag();
       

        #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

        [ActionName("GetListData")]
        public string GetListData(string key = "", int page = 0, int limit = 20)
        {
            string strWhere = " 1=1 ";
            if (!string.IsNullOrEmpty(key))
            {
                strWhere += string.Format(" and F_TagName like '%{0}%'", key);
            }
            AddAction("T_Com_Tag", "page", "获取标签列表","key="+ key);
            DataTable dt = busTag.GetListByPage(strWhere, " F_Sort ", (page - 1) * limit, limit).Tables[0];
            int count = busTag.GetRecordCount(strWhere);
            return Success("成功", dt, count);
        }

        [ActionName("GetData")]
        public string GettData(int id)
        {
            if (id==0)
                return Error("请输入ID");
            AddAction("T_Com_Tag", id.ToMyString(), "获取单个标签", "标签id=" + id);
            Model.T_Com_Tag model = busTag.GetModel(id);
            return Success("成功", model, 1);
        }

        [ActionName("deletedata")]
        public string DeleteData(int id)
        {
            if (busTag.Delete(id))
            {
                AddAction("T_Com_Tag", id.ToMyString(), "删除单个标签", "标签id=" + id);
                return Success("删除成功");
            }              
            else
                return Error("失败");
        }



        [ActionName("adddata")]
        public string AddData(T_Com_Tag modelinput)
        {
            T_Com_Tag model = new T_Com_Tag();
            model.F_Table = "T_Wo_WorkOrderBase";
            model.F_AddUser = F_UserID;
            model.F_AddUserName = HttpUtility.UrlDecode(F_UserName);
            model.F_TagName = modelinput.F_TagName;
            model.F_Content = modelinput.F_Content;
            model.F_About = modelinput.F_About;
            model.F_Sort = modelinput.F_Sort;
            if (string.IsNullOrEmpty(modelinput.F_TagName))
            {
                return Error("请输入值tagname");
            }

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


            int id = busTag.Add(model);

            if (id > 0)
            {
                AddAction("T_Com_Tag", id.ToMyString(), "添加标签", "标签id=" + id);
                return Success("成功", modelinput, 1);
            }
            else
                return Error("失败");
        }

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

            if (string.IsNullOrEmpty(modelinput.F_TagName))
            {
                return Error("请输入值tagname");
            }
            if (modelinput.F_Id == 0)
                return Error("请输入值id");
            modelinput.F_Table = "T_Wo_WorkOrderBase";
            string strWhere = string.Format("  F_TagName='{0}' and F_Table='{1}' and F_Id!={2} ", modelinput.F_TagName, modelinput.F_Table, modelinput.F_Id);
            int count = busTag.GetRecordCount(strWhere);
            if (count > 0)
            {
                return Error("已存在");
            }


            Model.T_Com_Tag model = busTag.GetModel(modelinput.F_Id);
            model.F_TagName = modelinput.F_TagName;
            model.F_Content = modelinput.F_Content;
            model.F_About = modelinput.F_About;
            model.F_Sort = modelinput.F_Sort;
            if (busTag.Update(model))
            {
                AddAction("T_Com_Tag", modelinput.F_Id.ToMyString(), "修改标签", "标签id=" + modelinput.F_Id);
                return Success("成功", model, 1);
            }
            else
                return Error("失败");
        }

       
        public bool SetTopping(int noticeid)
        {
          return  busTag.SetTopping(noticeid);
        }

    }
}
