using System;
using System.Linq;
using System.Web.Mvc;
using DSWeb.Areas.Account.DAL.Chfee_Payapplication;
using DSWeb.Areas.Account.Models.Chfee_Payapplication;
using DSWeb.Areas.Account.Models.Chfee_do_detail;
using DSWeb.Areas.Account.Models.BillChfeeDetail;
using DSWeb.MvcShipping.Helper;
using DSWeb.TruckMng.Helper.Repository;
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;
//using DSWeb.TruckMng.Helper;
//using DSWeb.TruckMng.Helper;

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

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

        public ActionResult BLEdit()
        {
            return View();
        }

        //付费申请审核
        public ActionResult Audit()
        {
            return View();
        }

        //
        // GET:

        public ContentResult GetDataList(int start, int limit, string sort, string condition)
        {
            var dataList = ForeignPayDAL.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 GetAuditDataList(int start, int limit, string sort, string condition, string isaudit)
        {
            var dataList = ForeignPayDAL.GetAuditDataList(condition, Convert.ToString(Session["USERID"]), isaudit, 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)
        {
            ChPayapplication head = null;

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

            if (handle == "add")
            {
                head.APPLICANT = Convert.ToString(Session["USERID"]);
                head.APPLICANTNAME = Convert.ToString(Session["SHOWNAME"]);
                head.SETTLETYPE = 3;
            }



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



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


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


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

        public ContentResult GetBillSum(string condition)
        {
            var dataList = ForeignPayDAL.GetBodySumList(condition);


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

        public ContentResult GetFeeDataList(int start, int limit, string sort, string condition)
        {
            var dataList = ForeignPayDAL.GetFeeDataList(condition, Convert.ToString(Session["USERID"]), Convert.ToString(Session["SHOWNAME"]), 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 GetBillDataList(int start, int limit, string sort, string condition)
        {//下方的待添加列表
            var dataList = ForeignPayDAL.GetAddBillList(condition, Convert.ToString(Session["USERID"]), Convert.ToString(Session["SHOWNAME"]), 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 GetFeeDetailList(string sort, string condition)
        {
            var dataList = ForeignPayDAL.GetFeeDetailList(condition, Convert.ToString(Session["USERID"]), Convert.ToString(Session["SHOWNAME"]), Convert.ToString(Session["COMPANYID"]));
            var json = JsonConvert.Serialize(
             new { Success = true, Message = "查询成功", totalCount = dataList.Count, data = dataList.ToList() });
            return new ContentResult() { Content = json };
        }

        public ContentResult GetAddSum(string condition)
        {
            var dataList = ForeignPayDAL.GetAddSum(condition, Convert.ToString(Session["USERID"]), Convert.ToString(Session["SHOWNAME"]), Convert.ToString(Session["COMPANYID"]));


            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)
         {

            var headData = JsonConvert.Deserialize<ChPayapplication>(data);
            
            var DetailDate = JsonConvert.Deserialize<ForeignPayDetailmb>(data);

            if (opstatus == "add")
            {
                headData.GID = Guid.NewGuid().ToString();
                headData.BILLNO = PubSysDAL.GetBillNo("0303");
                //headData.COMPANYID = Convert.ToString(Session["COMPANYID"]);
                headData.APPLICANT = Convert.ToString(Session["USERID"]);
                headData.APPLYTIME = DateTime.Now;
                headData.ENTERTIME = DateTime.Now;
                headData.AMOUNTRMB = 0;
                headData.AMOUNTUSD = 0;
                headData.SETTLERMB = 0;
                headData.SETTLEUSD = 0;
                headData.DbOperationType = DbOperationType.DbotIns;
            }
            else if (opstatus == "edit")
            {
                headData.DbOperationType = DbOperationType.DbotUpd;
                headData.ModelUIStatus = "E";

                ForeignPayDAL.DeleteDetail(headData.BILLNO);
            }
            else
            {
                headData.DbOperationType = DbOperationType.DbotDel;
            }
            var BILLNO = headData.BILLNO;
            if (headData.INVDATE == "") headData.INVDATE = null;
          
            var modb = new ModelObjectDB();
            DBResult result = modb.Save(headData);

            var modb2 = new ModelObjectDB();
            DetailDate.DbOperationType = DbOperationType.DbotIns;
            DetailDate.ModelUIStatus = "I";
            DBResult result2 = modb2.Save(DetailDate);

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

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

        public ContentResult SaveList ( string data )
        {
            var headList = JsonConvert.Deserialize<List<ChPayApp_Edit>>(data);

            var result = new DBResult();
            var _L = ModelHeadConvert<ChPayApp_Edit>.ToModelObjectList(headList);

            var modb = new ModelObjectRepository();
            result = modb.SaveHeadList(_L);

            //刷新父窗口上的父节点
            var jsonRespose = new JsonResponse
            {
                Success = result.Success,
                Message = result.Message,
                //Data = XXHDAL.GetData("M.ContractNo='" + head.ContractNo + "'")
            };

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

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

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


             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)
         {
             var exrateList = JsonConvert.Deserialize<List<ChFeeExrate>>(storeCurrExrate);

             if ((exrateList == null) || (exrateList.Count == 0))
             {
                 if (billcurr!="")
                     exrateList = ForeignPayDAL.GetCurrExrateData(billcust, billcurr, feesql);
                 if ((exrateList == null) ||(exrateList.Count == 0))
                 {
                     var modb = new ModelObjectDB();
                     DBResult result = ForeignPayDAL.AddBill(billno, billcust, billcurr, feesql, exrateList, Convert.ToString(Session["COMPANYID"]), Convert.ToString(Session["USERID"]), Convert.ToString(Session["SHOWNAME"]));
                     if (result.Success) { 
                         ForeignPayDAL.p_update_Amount(billno);
                         ForeignPayDAL.Set_BS_CUSTOMERNAME(billno);
                     }

                     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 = ForeignPayDAL.AddBill(billno, billcust, billcurr, feesql, exrateList, Convert.ToString(Session["COMPANYID"]), Convert.ToString(Session["USERID"]), Convert.ToString(Session["SHOWNAME"]));
                 if (result.Success) { 
                     ForeignPayDAL.p_update_Amount(billno);
                     ForeignPayDAL.Set_BS_CUSTOMERNAME(billno);
                 }

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

         }


         public ContentResult DelBill(string data, string billno)
         {

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

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

        /*
         public DBResult Set_BS_CUSTOMERNAME ( string billno )
         {
             DBResult result = ForeignPayDAL.Set_BS_CUSTOMERNAME(billno);
             return result;
         }*/


         public ContentResult Delete(string data)
         {
             var headData = JsonConvert.Deserialize<ChPayapplication>(data);
             var dataList = ForeignPayDAL.GetBodyList("BILLNO='" + headData.BILLNO + "'");
             DBResult result = ForeignPayDAL.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 SubmitAudit(string bill)
         {

             DBResult result = ForeignPayDAL.SubmitAudit("FeeSettleAudit", Convert.ToString(Session["USERID"]), bill);

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

         public ContentResult SubmitAuditBack(string bill)
         {

             DBResult result = ForeignPayDAL.SubmitAuditBack("FeeSettleAudit", Convert.ToString(Session["USERID"]), bill);

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

         public ContentResult SubmitAuditList(string bills)
         {

             DBResult result = ForeignPayDAL.SubmitAuditList("FeeSettleAudit", Convert.ToString(Session["USERID"]), bills);

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

         public ContentResult SubmitAuditBackList(string bills)
         {
             DBResult result = ForeignPayDAL.SubmitAuditBackList("FeeSettleAudit", Convert.ToString(Session["USERID"]), bills);
             var json = JsonConvert.Serialize(result);
             return new ContentResult() { Content = json };
         }

         public ContentResult SubmitResetList ( string bills )
         {
             DBResult result = ForeignPayDAL.SubmitResetList("FeeSettleAudit", Convert.ToString(Session["USERID"]), Convert.ToString(Session["COMPANYID"]), bills);
             var json = JsonConvert.Serialize(result);
             return new ContentResult() { Content = json };
         }

         public ContentResult AuditList(string data)
         {

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

             DBResult result = ForeignPayDAL.AuditList(bodyList, Convert.ToString(Session["USERID"]));

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

         public ContentResult AuditBackList(string data)
         {

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

             DBResult result = ForeignPayDAL.AuditBackList(bodyList, Convert.ToString(Session["USERID"]));

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



         public ContentResult AuditPass ( string bill )
         {
             DBResult result = ForeignPayDAL.Audit("FeeSettleAudit", Convert.ToString(Session["USERID"]), bill);
             var json = JsonConvert.Serialize(result);
             return new ContentResult() { Content = json };
         }

        #endregion


        #region 参照部分

        #endregion

    }
}