using System;
using System.Linq;
using System.Web.Mvc;
using DSWeb.Areas.Account.DAL.Chfee_Recvapplication;
using DSWeb.Areas.Account.DAL.Chfee_Recvprocess;
using DSWeb.Areas.Account.Models.Chfee_Recvapplication;
using DSWeb.Areas.Account.Models.Chfee_Settlement;
using DSWeb.Areas.Account.Models.Chfee_do_detail;
using DSWeb.Areas.Account.Models.BillChfeeDetail;
using DSWeb.MvcShipping.Helper;
using DSWeb.MvcShipping.Comm.Cookie;
using DSWeb.Areas.CommMng.DAL;
using System.Collections.Generic;
using HcUtility.Comm;
using HcUtility.Core;
using DSWeb.EntityDA;
using DSWeb.Areas.Account.Models.Chfee_Exrate;


namespace DSWeb.Areas.Account.Controllers
{
    [JsonRequestBehavior]
    public class Chfee_recvprocessController : Controller
    {
        //
        // GET: 
        public ActionResult EditIndex()
        {
            return View();
        }

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

        public ActionResult EntryIndex()
        {
            return View();
        }
        public ActionResult Entry()
        {
            return View();
        }
        //
        public ActionResult Audit()
        {
            return View();
        }

        //
        // GET:

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

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



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

            if (handle == "edit")
            {
                var list = ChrecvprocessDAL.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 ChSettlement();
            }

            if (handle == "add")
            {
                head.CREATEUSER = Convert.ToString(Session["USERID"]);
                head.CREATEUSERREF = Convert.ToString(Session["SHOWNAME"]);
                head.CREATETIME = DateTime.Now.ToString("yyyy-MM-dd");
                head.SETTLETIME = DateTime.Now.ToString("yyyy-MM-dd");

                head.BILLSTATUS = 7;
                head.BILLSTATUSREF = "收费登记";

            }



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



        public ContentResult Save(string opstatus, string data, string accountsdata, string prepaydata, string ahsrdata, string financialdata, string advancedata, int billtype, string prepaychange)
        {


            var headData = JsonConvert.Deserialize<ChSettlement>(data.Replace("}", ",") + accountsdata.Replace("{", "").Replace("}", ",") + prepaydata.Replace("{", "").Replace("}", ",")
                + financialdata.Replace("{", "").Replace("}", ",") + advancedata.Replace("{", ""));

            if (opstatus == "add")
            {
                headData.GID = Guid.NewGuid().ToString();
                if (billtype == 2)
                    headData.BILLNO = PubSysDAL.GetBillNo("0304");
                else if (billtype == 1)
                    headData.BILLNO = PubSysDAL.GetBillNo("0305");
                headData.COMPANYID = Convert.ToString(Session["COMPANYID"]);
                headData.BILLDATE = DateTime.Now;
                headData.DbOperationType = DbOperationType.DbotIns;
            }
            else if (opstatus == "edit")
            {
                headData.DbOperationType = DbOperationType.DbotUpd;
                headData.ModelUIStatus = "E";
            }
            else
            {
                headData.DbOperationType = DbOperationType.DbotDel;
            }
            var BILLNO = headData.BILLNO;
            if (headData.CURR == "RMB")
            {
                headData.AMOUNTRMB = headData.AMOUNT;
                headData.ACCOUNTRMB = headData.ACCOUNT;
                headData.AMOUNTUSD = 0;
                headData.ACCOUNTUSD = "";

            }
            else
            {

                headData.AMOUNTUSD = headData.AMOUNT;
                headData.ACCOUNTUSD = headData.ACCOUNT;
                headData.ACCOUNTRMB = "";
                headData.AMOUNTRMB = 0;
            }
            headData.AHSR_CURRENCY = headData.CURR;
            if (headData.SETTLETIME == "") headData.SETTLETIME = null;
            var modb = new ModelObjectDB();
            DBResult result = modb.Save(headData);

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

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

        }

        public ContentResult SaveInfo ( string BILLNO, string CUSTOMERNAME, string SETTLEUSER )
        {//用于给收费核销模块选择往来单位和核销人

            DBResult result = ChrecvprocessDAL.SaveInfo(BILLNO, CUSTOMERNAME, SETTLEUSER);
            var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message };
            return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };

        }

        public ContentResult AddDetail(string bill, string data, string curr, int billtype)
        {
            var bodyList = JsonConvert.Deserialize<List<BillChfeeDetail>>(data);

            var modb = new ModelObjectDB();
            DBResult result = ChrecvprocessDAL.AddDetail(bill, bodyList, curr, Convert.ToString(Session["COMPANYID"]));
            if (result.Success) { ChrecvprocessDAL.p_update_Amount(bill, billtype); }


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

        public ContentResult AddBill(string billno, string billcust, string billcurr, string feesql, string storeCurrExrate, int billtype)
        {
            var exrateList = JsonConvert.Deserialize<List<ChFeeExrate>>(storeCurrExrate);

            if ((exrateList == null) || (exrateList.Count == 0))
            {

                exrateList = ChrecvprocessDAL.GetCurrExrateData(billcust, billcurr, feesql);
                if (exrateList.Count == 0)
                {
                    var modb = new ModelObjectDB();
                    DBResult result = ChrecvprocessDAL.AddBill(billno, billcust, billcurr, feesql, exrateList, Convert.ToString(Session["COMPANYID"]));
                    if (result.Success) { ChrecvprocessDAL.p_update_Amount(billno, billtype); }

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


                }
                else
                {
                    var jsonRespose = new JsonResponse { Success = true, Message = "查询成功", Data = exrateList.ToList() };
                    return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };


                }

            }
            else
            {

                var modb = new ModelObjectDB();
                DBResult result = ChrecvprocessDAL.AddBill(billno, billcust, billcurr, feesql, exrateList, Convert.ToString(Session["COMPANYID"]));
                if (result.Success) { ChrecvprocessDAL.p_update_Amount(billno, billtype); }

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

        }

        public ContentResult DelBill(string data, string billno, int billtype)
        {

            var bodyList = JsonConvert.Deserialize<List<Chfee_do_detail>>(data);

            DBResult result = ChrecvprocessDAL.DelFeeDo(bodyList);
            if (result.Success) { ChrecvprocessDAL.p_update_Amount(billno, billtype); }
            var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message };
            return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
        }

        public ContentResult Delete(string data)
        {
            var headData = JsonConvert.Deserialize<ChSettlement>(data);
            DBResult result;
            var dataList = ChrecvprocessDAL.GetBodyList("BILLNO='" + headData.BILLNO + "'");
            result = ChrecvprocessDAL.DelFeeDo(dataList);

            if (result.Success == true)
            {
                var modb = new ModelObjectDB();
                modb.Delete(headData);
            }

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


         //



        #region 状态更新

        public ContentResult UpdateStatus ( string bill,string status )
         {

             DBResult result = ChrecvprocessDAL.UpdateStatus(bill,status);

             var json = JsonConvert.Serialize(result);
             return new ContentResult() { Content = json };
         }


        #endregion


        #region 参照部分

        #endregion

    }
}