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.workorder
{
    public class MaterialItemsController : BaseController
    {
        // GET: MaterialItems
        public ActionResult GetList()
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            string sql = " ";
            DataTable dt = new DataTable();

            string code = HttpUtility.UrlDecode(RequestString.GetQueryString("code"));
            string strpageindex = RequestString.GetQueryString("page");
            int pageindex = 1;
            string strpagesize = RequestString.GetQueryString("pagesize");
            int pagesize = 10;
            if (code.Trim() != "")
            {
                sql += " and F_MaterialCode="+ code;
            }
            if (strpageindex.Trim() != "")
            {
                pageindex = Convert.ToInt32(strpageindex);
            }

            if (strpagesize.Trim() != "")
            {
                pagesize = Convert.ToInt32(strpagesize);
            }
            int recordCount = 0;
            dt = BLL.PagerBLL.GetListPager(
                "T_Wo_MaterialItems",
                "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>
        /// <returns></returns>
        public ActionResult AddMaterialItem()
        {
            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 code = RequestString.GetFormString("code");
                        string name = RequestString.GetFormString("name");
                        string formula = RequestString.GetFormString("formula");
                        int number = RequestString.GetInt("number", 0);
                        int woid = RequestString.GetInt("woid", 0);
                      

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

                        if (id == 0)
                        {
                            model.F_MaterialCode = code;
                            model.F_MaterialName = name;
                            model.F_Formula = formula;
                            model.F_ReturnQuantity = number;
                            model.F_WoID = woid;                      
                            model.F_CreateBy = ua.F_UserCode;
                            model.F_CreateOn = DateTime.Now;

                            if (new BLL.T_Wo_MaterialItems().Add(model) > 0)
                            {
                                res = Success("新增成功！");
                            }
                            else
                            {
                                res = Error("新增失败！");
                            }
                        }
                        else
                        {
                            model = new BLL.T_Wo_MaterialItems().GetModel(id);
                            if (model != null)
                            {
                                model.F_MaterialCode = code;
                                model.F_MaterialName = name;
                                model.F_Formula = formula;
                                model.F_ReturnQuantity = number;
                                model.F_WoID = woid;
                                if (new BLL.T_Wo_MaterialItems().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 + ",";
                            }
                            string sql = "delete from  T_Wo_MaterialItems  where F_Id in(" + idd.TrimEnd(',') + ")";
                            if (!string.IsNullOrEmpty(idd.Trim()))
                            {
                                if (DbHelperSQL.ExecuteSql(sql) > 0)
                                {
                                    res = Success("删除成功");
                                }
                                else
                                {
                                    res = Error("删除失败");
                                }
                            }
                            else
                            {
                                res = Error("请选择记录");
                            }
                        }
                        else
                        {
                            res = Error("获取参数失败");
                        }
                    }
                }
            }
            return res;
        }

    }
}