using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Petrol_Bunk.Models;
namespace Petrol_Bunk.Areas.Admin.Controllers
{
    public class CreaditTransController : Controller
    {
        // GET: Admin/CreaditTrans/MakeFullPayment_Post
        public ActionResult SearchCustomers()
        {
            return View();
        }
        public ActionResult getCustomerList(string SearchKey = "")
        {
            ViewBag.SearchKey = SearchKey;
            return View();
        }
        public ActionResult PaidHistory(int id = 0)
        {
            ViewBag.PaidMsg = TempData["PaidMsg"] == null ? "" : TempData["PaidMsg"] as string;

            ViewBag.Id = id;
            return View();
        }
        public ActionResult PaidHistory_Post(int id = 0, TblCreadit obj = null)
        {
            ViewBag.Id = id;
            Petrol_BunkEntities ctx = new Petrol_BunkEntities();
            decimal rem1 = Convert.ToDecimal(obj.Amt) - Convert.ToDecimal(obj.PaidAmt);


            obj.RemAmt = rem1;
            obj.RowStatus = 0;
            obj.InsertedDate = DateTime.Now;
            obj.PaidDate = DateTime.Now;
            ctx.TblCreadits.Add(obj);
            if (ctx.SaveChanges() > 0)
            {
                if (obj.PaidAmt != obj.Amt)
                {

                    decimal rem = Convert.ToDecimal(obj.Amt) - Convert.ToDecimal(obj.PaidAmt);

                    var getdata = ctx.TblCustomers.Where(x => x.CustId == obj.CustomerId).FirstOrDefault();
                    if (getdata != null)
                    {
                        decimal tot = Convert.ToDecimal(getdata.RemAmt);
                        tot += Convert.ToDecimal(rem);
                        getdata.RemAmt = tot;
                        ctx.Entry(getdata).State = System.Data.Entity.EntityState.Modified;
                        if (ctx.SaveChanges() > 0)
                        {

                        }
                    }
                }
                TempData["PaidMsg"] = "Updated Customer Ledger";
            }
            return new RedirectResult("/Admin/CreaditTrans/PaidHistory/" + obj.CustomerId);
        }

        public ActionResult CustomerWiseReport()
        {
            Petrol_BunkEntities ctx = new Petrol_BunkEntities();
            ViewBag.CustId = new SelectList(ctx.TblCustomers.Where(x => x.RowStatus == 0).ToList(), "CustId", "CustName");
            return View();
        }
        public ActionResult getCustomerWiseReport(int id = 0)
        {
            ViewBag.Id = id;
            return View();
        }
        public ActionResult MakeFullPayment(int id = 0)
        {
            ViewBag.Id = id;
            return View();
        }
        public ActionResult MakeFullPayment_Post(int id = 0,
            decimal RemAmt = 0, string PaymentType = "", string Remarks = "")
        {
            Petrol_BunkEntities ctx = new Petrol_BunkEntities();
            var getdata = ctx.TblCustomers.Where(x => x.CustId == id).FirstOrDefault();
            TblCreadit obj = new TblCreadit();
            if (getdata != null)
            {
                getdata.RemAmt = 0;
                obj.Amt = 0;
                obj.CustomerId = id;
                obj.InsertedDate = DateTime.Now;
                obj.Litrs = 0;
                obj.MachineId = 0;
                obj.RemAmt = 0;
                obj.PaidAmt = RemAmt;
                obj.PaidDate = DateTime.Now;
                obj.PaymentType = PaymentType;
                obj.PetrolType = "---";
                obj.RefNo = "---";
                obj.Remarks = Remarks;
                obj.RowStatus = 0;
                ctx.TblCreadits.Add(obj);
                if (ctx.SaveChanges() > 0)
                {
                    TempData["PaidMsg"] = "Full Amount Paid";
                }
                else
                {
                    TempData["PaidMsg"] = "Try Again";
                }
            }
            else
            {
                TempData["PaidMsg"] = "Try Again";
            }
            return new RedirectResult("/Admin/CreaditTrans/PaidHistory/" + id);
        }
    }
}