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.System
{
    public class WoStatusOptController : BaseController
    {

        private Bll.T_Wo_StatusOpt optbll = new Bll.T_Wo_StatusOpt();
        private Bll.T_Sys_GongDan gdbll = new Bll.T_Sys_GongDan();
        private Bll.T_Sys_Menu menubll = new Bll.T_Sys_Menu();
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="type"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public ActionResult GetList(int type=0, int pageindex = 1, int pagesize = 10)
        {
            if (User != null)
            {
                #region 查询添加
                string sql = "";
                DataTable data = new DataTable();                    
                if (type != 0)
                    sql += $"and F_Type={type}";
                #endregion
                int recordCount = 0;
                data = Bll.PagerBll.GetListPager
                      (
                      "T_Wo_StatusOpt aa",
                      "F_Id",
                      "*,(select F_Name from T_Sys_GongDan where F_GDId=aa.F_Type)as F_Name",
                      sql,
                      "ORDER BY F_Id desc",
                      pagesize,
                      pageindex,
                      true,
                      out recordCount
                      );
                data.Columns.Add("F_StateName", typeof(string));//工单状态名称
                data.Columns.Add("F_WoTypeName", typeof(string));//工单类型名称
                data.Columns.Add("F_MenuName", typeof(string));//功能模块名称
                foreach (DataRow dr in data.Rows)
                {
                    string state = dr["F_State"] != null ? dr["F_State"].ToString() : "";
                    int wotype = dr["F_Type"] != null ? Convert.ToInt32(dr["F_Type"]) : 0;
                    if (state != "" && wotype>0)
                    {
                        dr["F_StateName"] = WorkOrderBase.GetTypeState(wotype).Where(t=>t.StateId==state).FirstOrDefault().StateRemark;
                        dr["F_WoTypeName"] = gdbll.GetModel(Convert.ToInt32(dr["F_Type"])).F_Name;
                        dr["F_MenuName"] = menubll.GetModel(Convert.ToInt32(dr["F_MenuId"])).F_MenuName;
                    }              
                }

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

            }
            return Error("无操作权限！");
        }

        /// <summary>
        /// 获取详情
        /// </summary>
        /// <returns></returns>
        public ActionResult GetModel(int id)
        {
            if (id < 0)
                return Error("参数错误！");
            var model = optbll.GetModel(id);
            if (model != null)
            {
                model.F_StateName = WorkOrderBase.GetTypeState(Convert.ToInt32(model.F_Type)).Where(t => t.StateId == model.F_State.ToString()).FirstOrDefault().StateRemark;
                model.F_WoTypeName= gdbll.GetModel(Convert.ToInt32(model.F_Type)).F_Name;
                model.F_MenuName = menubll.GetModel(Convert.ToInt32(model.F_MenuId)).F_MenuName;
                return Content(model.ToJson());

            }
            else
                return Error("查询失败！");
        }
        /// <summary>
        /// 添加修改
        /// </summary>
        /// <param name="state"></param>
        /// <param name="type"></param>
        /// <param name="menuid"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult AddModel(int state,int type,int menuid,string opt="",int id=0)
        {
            if (state<0)
                return Error("请选择工单状态！");
            if(type==0)
                return Error("请选择工单类型！");
            if (menuid == 0)
                return Error("请选择操作按钮！");
            if (id > 0)
            {
                var optmodel = optbll.GetModel(id);
                optmodel.F_State = state;
                optmodel.F_Type = type;
                optmodel.F_MenuId = menuid;
                optmodel.F_OptMethod = opt;
                optbll.Update(optmodel);
                return Success("修改成功！");
            }
            else
            {
                Model.T_Wo_StatusOpt optmodel = new Model.T_Wo_StatusOpt();
                optmodel.F_State = state;
                optmodel.F_Type = type;
                optmodel.F_MenuId = menuid;
                optmodel.F_OptMethod = opt;
                optbll.Add(optmodel);
                return Success("添加成功！");
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public ActionResult Delete(string[] ids)
        {
            if (ids != null && ids.Length > 0)
            {
                string idstr = string.Join(",", ids);
                if (optbll.DeleteList(idstr))
                    return Success("删除成功！");
                else return Error("删除失败！");
            }
            else
            {
                return Error("请选择要删除的记录");
            }
        }
    }
}