using RMYY_CallCenter_Api.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RMYY_CallCenter_Api.Controllers.CarManage
{
    public class T_Car_MessageController :BaseController
    {
             
        /// <summary>
        /// 获取列表数据
        /// </summary>
        /// <param name="keywork"></param>
        /// <param name="pagesize"></param>
        /// <param name="pageindex"></param>
        /// <returns></returns>
        public ActionResult GetList(string keyword, string starttime = "", string endtime = "",int pagesize=10,int pageindex=1)
        {
            DataTable date = new DataTable();
            string sql = "";
            string where = "";
            if (starttime.Trim() != "" && starttime != "undefined")
                sql += $"and datediff(day,F_CreateTime,'" + starttime + "')<=0";
            if (endtime.Trim() != "" && endtime != "undefined")
                sql += $"and datediff(day,F_CreateTime,'" + endtime + "')>=0";
            if (!string.IsNullOrEmpty(keyword))
                sql += $" and (F_CarName like '%"+ keyword + "%'or F_Remark like '%" + keyword + "%' )";
            int recordCount = 0;
            if (!string.IsNullOrEmpty(sql))
            {
                where += sql;
            }
            date = Bll.PagerBll.GetListPager
                                ("T_Car_Message aa",
                                  "F_Id",
                                  "F_Id,dbo.GetDictionaryName(F_CarType) F_CarTypeName,F_CarName,F_CarType,F_CarDeptId,F_Responser,F_Phon,F_Remark,F_CreateTime,F_State,(select F_DeptName from T_Sys_Department where F_DeptId=aa.F_CarDeptId )F_DeptName,(select top 1 F_UserName from T_Sys_UserAccount where F_UserCode=aa.F_Responser)F_ResponserName",
                                 where,
                                  "order by F_Id desc",
                                  pagesize,
                                  pageindex,
                                  true,
                                  out recordCount

                                );
            List<Model.T_Car_Message> carlist = new Bll.T_Car_Message().DataTableToList(date);
            int i = 0;
            foreach (var item in carlist)
            {
                item.F_CarTypeName = date.Rows[i]["F_CarTypeName"].ToString();
                i++;
            }
        
                var obj = new
                {
                    state = "success",
                    message = "成功",
                    rows = carlist,
                    total = recordCount
                };
                return Content(obj.ToJson());
           
        }

        /// <summary>
        /// 获取所有车辆信息
        /// </summary>
        /// <returns></returns>
        public ActionResult GetAllCar(int state=-1)
        {
            string sql = "";
            if (state > -1)
                sql += $"F_State={state} ";
            var modelist = new Bll.T_Car_Message().GetModelList(sql);
            var obj = new
            {
                rows = modelist.Select( p => new{ carid= p.F_Id, carname = p.F_CarName })
            };

            return Content(obj.ToJson());
        }

        /// <summary>
        /// 获取车辆详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult GetModel(int id)
        {
            if (id <= 0)
            {
                return Error("请选择要查询车辆！");
            }
            var model = new Bll.T_Car_Message().GetModel(id);
            if (model != null)
            {
                model.F_DeptName = new Bll.T_Sys_Department().GetModel(Convert.ToInt32(model.F_CarDeptId))?.F_DeptName ?? "";
                model.F_ResponserName = new Bll.T_Sys_UserAccount().GetModel(model.F_Responser)?.F_UserName ?? "";
                model.F_CarTypeName = new Bll.T_Sys_DictionaryValue ().GetModel(model.F_CarType.Value  )?.F_Name  ?? "";
                return Success("获取成功", model);
            }
            else return Error("获取失败！");
        }

        /// <summary>
        ///添加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ActionResult AddMessage(Model.T_Car_Message input)
        {
            //var userId = User.F_UserCode;
            //if(userId!=null)
            //{
                if (string.IsNullOrEmpty(input.F_CarName))
                    return Error("请填写车辆名称！");
                if (input.F_CarType == 0)
                    return Error("请填写车辆类型！");
                if (input.F_CarDeptId == 0)
                    return Error("请选择所属科室！");
                if (string.IsNullOrEmpty(input.F_Responser))
                    return Error("请填写负责人编号！");
                if (string.IsNullOrEmpty(input.F_Phon))
                    return Error("请填写联系电话！");
                Model.T_Car_Message car = input;
                car.F_State = 0;//空闲
                car.F_CreateTime = DateTime.Now;
                if (new Bll.T_Car_Message().Add(car) > 0)
                    return Success("添加成功！");
                else return Error("添加失败！");
            //}
            //return Error("没有添加权限！");
        }


        /// <summary>
        ///修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ActionResult UpdateMessage(Model.T_Car_Message input)
        {
            //var userId = User.F_UserCode;
            //if (userId != null)
            //{
                if (input.F_Id <= 0)
                    return Error("请选择要修改的车辆！");
                Model.T_Car_Message car = new Bll.T_Car_Message().GetModel(input.F_Id);
                if (car == null)
                    return Error("无此车辆信息！");
                if (string.IsNullOrEmpty(input.F_CarName))
                    return Error("请填写车辆名称！");
                if (input.F_CarType == 0)
                    return Error("请填写车辆类型！");
                if (input.F_CarDeptId == 0)
                    return Error("请选择所属科室！");
                if (string.IsNullOrEmpty(input.F_Responser))
                    return Error("请填写负责人编号！");
                if (string.IsNullOrEmpty(input.F_Phon))
                    return Error("请填写联系电话！");
                car.F_CarName = input.F_CarName;
                car.F_CarType = input.F_CarType;
                car.F_CarDeptId = input.F_CarDeptId;
                car.F_DeptName = input.F_DeptName;
                car.F_Responser = input.F_Responser;
                car.F_ResponserName = input.F_ResponserName;
                car.F_Phon = input.F_Phon;
                car.F_Remark = input.F_Remark;
                if (new Bll.T_Car_Message().Update(car))
                    return Success("修改成功！");
                else return Error("修改失败！");
            //}
            //return Error("没有权限！");
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Delete(int id)
        {
            if (id <= 0)
             return Error("请选择要删除的车辆");
             var car = new Bll.T_Car_Message().GetModel(id);
            if (car == null)
                return Error("查询不到此车辆信息！");
            if (new Bll.T_Car_Message().Delete(id))
                return Success("删除成功！");
            else
                return Error("删除失败！");
        }
    }
}