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

namespace CallCenterApi.Interface.Controllers
{
    public class AnnotationController : BaseController
    {
        // GET: Annotation
        public ActionResult GetList()
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            string sql = " ";
            DataTable dt = new DataTable();

            string strpageindex = RequestString.GetQueryString("page");
            int pageindex = 1;
            string strpagesize = RequestString.GetQueryString("pagesize");
            int pagesize = 10;
            if (strpageindex.Trim() != "")
            {
                pageindex = Convert.ToInt32(strpageindex);
            }

            if (strpagesize.Trim() != "")
            {
                pagesize = Convert.ToInt32(strpagesize);
            }
            int recordCount = 0;
            dt = BLL.PagerBLL.GetListPager(
                "T_Wo_AnnotationTab",
                "F_Id",
                "*",
                sql,
                "ORDER BY F_Id desc",
                pagesize,
                pageindex,
                true,
                out recordCount);

            var obj = new
            {
                state = "success",
                message = "成功",
                rows = dt,
                total = recordCount
            };

            res = Content(obj.ToJson());

            return res;
        }
        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult GetModel(int id)
        {
            if (id <= 0)
                return Error("参数错误");
            var model = new BLL.T_Wo_AnnotationTab().GetModel(id);
            return Content(model.ToJson());
        }
        /// <summary>
        ///修改
        /// </summary>
        /// <returns></returns>
        public ActionResult AddAnnotation()
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                int userId = CurrentUser.UserData.F_UserId;
                if (userId != 0)
                {
                    Model.T_Sys_UserAccount ua = new BLL.T_Sys_UserAccount().GetModel(userId);
                    if (ua != null)
                    {
                        int id = RequestString.GetInt("id", 0);
                        string contents = RequestString.GetFormString("contents");
                        string wocode = RequestString.GetFormString("wocodes");



                        Model.T_Wo_AnnotationTab model = new Model.T_Wo_AnnotationTab();

                        if (id == 0)
                        {
                            model.F_Contents = contents;
                            model.F_AnnotationPerson = ua.F_UserCode;
                            model.F_CreateTime = DateTime.Now;
                            model.F_Wocode = wocode;
                            if (new BLL.T_Wo_AnnotationTab().Add(model) > 0)
                            {
                                res = Success("注解成功！");
                            }
                            else
                            {
                                res = Error("注解失败！");
                            }
                        }
                        else
                        {
                            model = new BLL.T_Wo_AnnotationTab().GetModel(id);
                            if (model != null)
                            {
                                model.F_Contents = contents;
                                model.F_ModifyBy = ua.F_UserCode;
                                model.F_ModifyTime = DateTime.Now;                           
                                if (new BLL.T_Wo_AnnotationTab().Update(model))
                                {
                                    res = Success("修改成功！");
                                }
                                else
                                {
                                    res = Error("修改失败！");
                                }
                            }
                            else
                            {
                                res = Error("修改失败！");
                            }
                        }
                    }
                }

            }
            return res;
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public ActionResult Delete(string[] ids)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {

                int userId = CurrentUser.UserData.F_UserId;
                if (userId != 0)
                {
                    Model.T_Sys_UserAccount ua = new BLL.T_Sys_UserAccount().GetModel(userId);
                    if (ua != null)
                    {
                        if (ids != null && ids.Length > 0)
                        {
                            string idd = " ";
                            foreach (string str in ids)
                            {
                                idd += str + ",";
                            }                          
                            if (!string.IsNullOrEmpty(idd.Trim()))
                            {
                                new BLL.T_Wo_AnnotationTab().DeleteList(idd.TrimEnd(','));
                            }
                            else
                            {
                                res = Error("请选择记录");
                            }
                        }
                        else
                        {
                            res = Error("获取参数失败");
                        }
                    }
                }
            }
            return res;
        }
    }
}