using RMYY_CallCenter_Api.Controllers;
using RMYY_CallCenter_Api.DB;
using RMYY_CallCenter_Api.Models;
using RMYY_CallCenter_Api.Models.Enum;
using RMYY_CallCenter_Api.Models.Input;
using RMYY_CallCenter_Api.Utility;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;

namespace RMYY_CallCenter_Api.Controllers
{
    public class ClassController : BaseController
    {
        // GET: Class

        /// <summary>
        /// 获取班别列表
        /// </summary>
        /// <returns></returns>
        
        public ActionResult GetList(int isdc = 0)
        {
            DataTable dt = new DataTable();
            string sql = " and F_IsDelete=0 and F_deptid in(select F_DeptId from[dbo].[GetDeptid] ('" + User.F_DeptId + "'))";
            string strcode = HttpUtility.UrlDecode(RequestString.GetQueryString("code"));
            string strname = HttpUtility.UrlDecode(RequestString.GetQueryString("name"));

            string strpageindex = RequestString.GetQueryString("page");
            int pageindex = 1;
            string strpagesize = RequestString.GetQueryString("pagesize");
            int pagesize = 10;


            if (strcode.Trim() != "" && strcode != "undefined")
            {
                sql += " and F_Code = '" + strcode.Trim() + "' ";
            }
            if (strname.Trim() != "" && strname != "undefined")
            {
                sql += " and F_Name like '%" + strname + "%' ";
            }
            

            if (strpageindex.Trim() != "")
            {
                pageindex = Convert.ToInt32(strpageindex);
            }

            if (strpagesize.Trim() != "")
            {
                pagesize = Convert.ToInt32(strpagesize);
            }

            string cols = "*,dbo.GetUserName(F_CreateUser) as UserName";

            if (isdc > 0)
            {
                var dtdc = DbHelperSQL.Query(" select " + cols + " from T_Sys_Class where 1=1 " + sql).Tables[0];
                var msg = new NPOIHelper().ExportToExcel("班别列表", dtdc);
                if (msg == "")
                {
                    return Success("导出成功");
                }
                else
                {
                    return Error("导出失败");
                }
            }

            int recordCount = 0;
            dt = Bll.PagerBll.GetListPager(
                "T_Sys_Class",
                "F_Id",
                cols,
                sql,
                "ORDER BY F_CreateTime DESC",
                pagesize,
                pageindex,
                true,
                out recordCount);

            var obj = new
            {
                state = "success",
                message = "成功",
                rows = dt,
                total = recordCount
            };
            return Content(obj.ToJson());

        }

        /// <summary>
        /// 获取班别列表
        /// </summary>
        /// <returns></returns>
        
        public ActionResult GetClassList(int deptid=0)
        {
            if (deptid==0)
            {
                deptid = User.F_DeptId;
            }
            DataTable dt = new Bll.T_Sys_Class().GetList(" F_State=0 and F_IsDelete=0 and F_deptid='"+deptid+"' ").Tables[0];
            return Success("加载成功", dt);
        }

        /// <summary>
        /// 获取班别
        /// </summary>
        /// <param name="ClassId"></param>
        /// <returns></returns>
        
        public ActionResult GetClass(int id = 0)
        {
            if (id != 0)
            {
                return Success("成功", new Bll.T_Sys_Class().GetModel(id));
            }
            else
            {
                return Error("参数错误");
            }
        }

        /// <summary>
        /// 获取班别
        /// </summary>
        /// <param name="ClassId"></param>
        /// <returns></returns>
        
        //public ActionResult GetClassByCode(string code)
        //{
        //    if (!string.IsNullOrEmpty(code))
        //    {
        //        return Success("成功", new Bll.T_Sys_Class().GetModelList(" F_Code='" + code + "' ").FirstOrDefault());
        //    }
        //    else
        //    {
        //        return Error("参数错误");
        //    }
        //}

        /// <summary>
        /// 添加班别
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        
        public ActionResult AddClass()
        {
            int deptid = RequestString.GetInt("deptid", 0);

            int id = RequestString.GetInt("id", 0);
            string name = RequestString.GetFormString("name");
            string code = RequestString.GetFormString("code");
            string intime = RequestString.GetFormString("intime");
            string outtime = RequestString.GetFormString("outtime");
            string remark = RequestString.GetFormString("remark");
            int  isAutomatic = RequestString.GetInt("isAutomatic",0);

            if (deptid == 0)
            {
                deptid = User.F_DeptId;
            }

            Model.T_Sys_Class dModel = new Model.T_Sys_Class();
            if (id == 0)
            {
                var list = new Bll.T_Sys_Class().GetModelList(" F_Name='" + name + "' and F_IsDelete=0 and F_deptid='"+ deptid + "' ");
                if (list.Count > 0)
                {
                    return Error("已经存在此班别");
                }
                else
                {
                    dModel.F_Code = code;
                    dModel.F_Name = name;
                    dModel.F_InTime = intime;
                    dModel.F_OutTime = outtime;
                    dModel.F_Remark = remark;
                    dModel.F_State = 0;
                    dModel.F_IsDelete = 0;
                    dModel.F_CreateUser = User.F_UserCode;
                    dModel.F_CreateTime = DateTime.Now;
                    dModel.F_DeptId = deptid;
                    dModel.F_IsAutomatic = isAutomatic;
                    int n = new Bll.T_Sys_Class().Add(dModel);
                    if (n > 0)
                        return Success("添加成功", n);
                    else
                        return Error("添加失败");
                }
            }
            else
            {
                dModel = new Bll.T_Sys_Class().GetModel(id);
                if (dModel != null)
                {
                    var list = new Bll.T_Sys_Class().GetModelList(" F_Name='" + name + "' and F_IsDelete=0  and F_deptid='" + deptid + "' and F_Id!='" + id + "' ");
                    if (list.Count > 0)
                    {
                        return Error("已经存在此班别");
                    }
                    else
                    {
                        dModel.F_Code = code;
                        dModel.F_Name = name;
                        dModel.F_InTime = intime;
                        dModel.F_OutTime = outtime;
                        dModel.F_Remark = remark;
                        dModel.F_IsAutomatic = isAutomatic;
                        if (new Bll.T_Sys_Class().Update(dModel))
                            return Success("修改成功");
                        else
                            return Error("修改失败");
                    }
                }
                else
                {
                    return Error("修改失败");
                }
            }
        }

        /// <summary>
        /// 删除班别
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        
        public ActionResult DelClass(string[] ids)
        {
           

            if (ids == null || ids.Length <= 0)
            {
                return Error("请选择要删除的班别");
            }

            var idStr = string.Join(",", ids);
            if (string.IsNullOrEmpty(idStr.Trim()))
            {
                return Error("请选择要删除的班别");
            }
            int n = DbHelperSQL.ExecuteSql(" update T_Sys_Class set F_IsDelete=1,F_DeleteUser='" + User.F_UserCode + "',F_DeleteTime=getdate() where F_Id in (" + idStr + ")");
            if (n > 0)
            {
                return Success("删除成功");
            }
            return Error("删除失败");
        }
    }
}