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

namespace CallCenterApi.Interface.Controllers
{
    public class MaterialController : BaseController
    {
        // GET: Material

        /// <summary>
        /// 获取材料列表
        /// </summary>
        /// <returns></returns>
        [Authority]
        public ActionResult GetList()
        {
            int type = RequestString.GetInt("type", 0);
            string key = RequestString.GetQueryString("key");
            string strpageindex = RequestString.GetQueryString("page");
            int pageindex = 1;
            string strpagesize = RequestString.GetQueryString("pagesize");
            int pagesize = 10;

            var sql = " and 1=1 ";
            if (type != 0)
            {
                sql += " and F_Type = '" + type + "'";
            }
            if (!string.IsNullOrEmpty(key))
            {
                sql += " and (F_Title like '%" + key + "%' or F_Content like '%" + key + "%')";
            }
            if (strpageindex.Trim() != "")
            {
                pageindex = Convert.ToInt32(strpageindex);
            }
            if (strpagesize.Trim() != "")
            {
                pagesize = Convert.ToInt32(strpagesize);
            }

            var recordCount = 0;
            var dt = BLL.PagerBLL.GetListPager(
              "T_Material_Info",
              "F_Id",
              "*,dbo.GetDeptName(F_DeptId) as DeptName",
              sql,
              "ORDER BY F_CreateTime desc ",
              pagesize,
              pageindex,
              true,
              out recordCount);

            var obj = new
            {
                rows = dt,
                total = recordCount
            };
            return Content(obj.ToJson());

        }

        /// <summary>
        /// 获取材料
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        [Authority]
        public ActionResult GetMaterial()
        {
            int id = RequestString.GetInt("id", 0);

            Model.T_Material_Info baseModel = new BLL.T_Material_Info().GetModel(id);
            if (baseModel != null)
            {
                string prefix = "";
                var configfj = new BLL.T_Sys_SystemConfig().GetModelList(" F_ParamCode='FileUrlPath' ").FirstOrDefault();
                if (configfj != null)
                {
                    prefix = configfj.F_ParamValue;
                }

                DataTable dt = new DataTable();
                if (!string.IsNullOrEmpty(baseModel.F_File))
                {
                    dt = new BLL.T_Sys_Accessories().GetList("F_FileId in (" + baseModel.F_File + ")").Tables[0];

                    foreach (DataRow dr in dt.Rows)
                    {
                        dr["F_FileUrl"] = prefix + dr["F_FileUrl"].ToString();
                    }
                }

                var deptinfo = new BLL.T_Sys_Department().GetModel(baseModel.F_DeptId.Value);

                var obj = new
                {
                    baseModel.F_Id,
                    baseModel.F_Title,
                    baseModel.F_Type,
                    baseModel.F_Content,
                    baseModel.F_File,
                    baseModel.F_DeptId,
                    DeptName = deptinfo.F_DeptName,
                    Files = dt,
                    baseModel.F_CreateUser,
                    baseModel.F_CreateTime
                };
                return Success("加载成功", obj);
            }
            else
            {
                return Error("加载失败");
            }

        }

        /// <summary>
        /// 添加/编辑材料
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [Authority]
        public ActionResult AddMaterial()
        {
            int userId = Utils.StrToInt(User.UserData["F_UserID"], 0);
            Model.T_Sys_UserAccount userModel = new BLL.T_Sys_UserAccount().GetModel(userId);
            int id = RequestString.GetInt("id", 0);
            int type = RequestString.GetInt("type", 0);
            string title = RequestString.GetFormString("title");
            string cont = WebHelper.UrlDecode(RequestString.GetFormString("cont"));
            string file = RequestString.GetFormString("file");

            Model.T_Material_Info orderModel = new Model.T_Material_Info();
            if (id == 0)
            {
                orderModel.F_Type = type;
                orderModel.F_Title = title;
                orderModel.F_Content = cont;
                orderModel.F_File = file;
                orderModel.F_CreateTime = DateTime.Now;
                orderModel.F_CreateUser = userModel.F_UserCode;
                orderModel.F_DeptId = userModel.F_DeptId;
                if (new BLL.T_Material_Info().Add(orderModel) > 0)
                {
                    return Success("材料添加成功");
                }
                else
                    return Error("材料添加失败");

            }
            else
            {
                orderModel = new BLL.T_Material_Info().GetModel(id);
                if (orderModel != null)
                {
                    orderModel.F_Type = type;
                    orderModel.F_Title = title;
                    orderModel.F_Content = cont;
                    orderModel.F_File = file;
                    if (new BLL.T_Material_Info().Update(orderModel))
                    {
                        return Success("材料修改成功");
                    }
                    else
                        return Error("材料修改失败");

                }
                else
                {
                    return Error("材料修改失败");
                }
            }
        }

        /// <summary>
        /// 删除材料
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [Authority]
        public ActionResult DelMaterial(string[] ids)
        {
            if (ids == null || ids.Length <= 0)
                return Error("获取参数失败");
            StringBuilder sb = new StringBuilder();
            foreach (var item in ids)
            {
                sb.Append("'" + item + "',");
            }
            if (new BLL.T_Material_Info().DeleteList(sb.ToString().Trim(',')))
            {
                return Success("删除成功");
            }
            else
                return Error("删除失败");

        }
    }
}