using System;
using System.Linq;
using System.Web.Mvc;
using DSWeb.MvcShipping.DAL.MsCodeFeeTemplateAuto;
using DSWeb.MvcShipping.Models.MsCodeFeeTemplateAuto;
using DSWeb.MvcShipping.Helper;
using DSWeb.MvcShipping.Comm.Cookie;
using System.Collections.Generic;
using HcUtility.Comm;
using HcUtility.Core;

namespace DSWeb.MvcShipping.Controllers
{
    [JsonRequestBehavior]
    public class MsCodeFeeTemplateAutoController : Controller
    {
        //
        // GET: 
        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /
        public ActionResult Edit()
        {
            return View();
        }

        //
        // GET:

        public ContentResult GetDataList(int start, int limit, string sort, string condition)
        {
            var dataList = MsCodeFeeTempldateAutoDAL.GetDataList(condition, Convert.ToString(Session["USERID"]), CookieConfig.GetCookie_UserCode(Request), Convert.ToString(Session["COMPANYID"]), sort);

            var json = JsonConvert.Serialize(new { Success = true, Message = "查询成功", totalCount = dataList.Count, data = dataList.ToList() });
            return new ContentResult() { Content = json };
        }

        public ContentResult GetDataListStr(string sort, string condition)
        {
            var dataListStr = MsCodeFeeTempldateAutoDAL.GetDataListStr(condition, Convert.ToString(Session["USERID"]), CookieConfig.GetCookie_UserCode(Request), Convert.ToString(Session["COMPANYID"]), sort);

            var json = JsonConvert.Serialize(new { Success = true, Message = "查询成功", data = dataListStr });
            return new ContentResult() { Content = json };
        }

        //
        // GET:/TruckMng/MsWlTyreAcc/GetData/

        public ContentResult GetData(string handle, string condition)
        {
            CodeFeeTemplateAuto head = null;

            if (handle == "edit")
            {
                var list = MsCodeFeeTempldateAutoDAL.GetDataList(condition, Convert.ToString(Session["USERID"]), CookieConfig.GetCookie_UserCode(Request), Convert.ToString(Session["COMPANYID"]));
                if (list.Count > 0)
                    head = list[0];
            }

            if (head == null)
            {
                head = new CodeFeeTemplateAuto();
            }

            var json = JsonConvert.Serialize(
               new { Success = true, Message = "查询成功", data = head });
            return new ContentResult() { Content = json };
        }


        public ContentResult GetBodyList( string sort, string condition)
        {
            var dataList = MsCodeFeeTempldateAutoDAL.GetBodyList(condition, sort);


            var json = JsonConvert.Serialize(new { Success = true, Message = "查询成功", totalCount = dataList.Count, data = dataList.ToList() });
            return new ContentResult() { Content = json };
        }
        public ContentResult GetConditList(string sort, string condition)
        {
            var dataList = MsCodeFeeTempldateAutoDAL.GetConditList(condition, sort);


            var json = JsonConvert.Serialize(new { Success = true, Message = "查询成功", totalCount = dataList.Count, data = dataList.ToList() });
            return new ContentResult() { Content = json };
        }

        public ContentResult Save(string opstatus, string data, string body, string condit)
        {
            var headData = JsonConvert.Deserialize<CodeFeeTemplateAuto>(data);
            var bodyList = JsonConvert.Deserialize<List<CodeFeeTemplateDetailAuto>>(body);
            var conditList = JsonConvert.Deserialize<List<CodeFeeTemplateCondition>>(condit);

            if (opstatus == "add")
            {
                headData.DbOperationType = DbOperationType.DbotIns;
                headData.CREATEUSER = Convert.ToString(Session["USERID"]);
                headData.CORPID = Convert.ToString(Session["COMPANYID"]);
                headData.CREATETIME = DateTime.Now;

            }
            else if (opstatus == "edit")
            {
                headData.DbOperationType = DbOperationType.DbotUpd;
                headData.ModelUIStatus = "E";
            }
            else
            {
                headData.DbOperationType = DbOperationType.DbotDel;
            }

            if (headData.BGNDATE == "") headData.BGNDATE = null;
            if (headData.ENDDATE == "") headData.ENDDATE = null;

            var modb = new ModelObjectDB();
            modb.Save(headData);
            var GID = headData.GID;
            DBResult result = MsCodeFeeTempldateAutoDAL.SaveDetail(headData, bodyList, conditList, Convert.ToString(Session["USERID"]));



            var jsonRespose = new JsonResponse
            {
                Success = result.Success,
                Message = result.Message,
                Data = MsCodeFeeTempldateAutoDAL.GetData("GID='" + GID + "'", Convert.ToString(Session["USERID"]), CookieConfig.GetCookie_UserCode(Request), Convert.ToString(Session["COMPANYID"]))
            };

            return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
        }

    
       

        public ContentResult Delete(string data)
        {
            var headData = JsonConvert.Deserialize<CodeFeeTemplateAuto>(data);

            DBResult result =MsCodeFeeTempldateAutoDAL.DeleteDetail(headData);
       

            var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message };
            return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
        }

        public ContentResult GetFeeUnit(string condition)
        {
            var dataList = MsCodeFeeTempldateAutoDAL.GetFeeUnit(condition);

            var json = JsonConvert.Serialize(new { Success = true, Message = "查询成功", totalCount = dataList.Count, data = dataList.ToList() });
            return new ContentResult() { Content = json };
        }


        #region 参照部分

        #endregion

    }
}