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 MachineReadController : Controller
    {
        // GET: Admin/MachineRead/getCardPaymentDetails
        public ActionResult Index()
        {
            Petrol_BunkEntities ctx = new Petrol_BunkEntities();

            ViewBag.MachineId = new SelectList(ctx.TblMachines.Where(x => x.RowStaus == 0).ToList(), "MachineId", "MachineName");
            ViewBag.MchMsg = TempData["MchMsg"] == null ? "" : TempData["MchMsg"] as string;

            return View();
        }

        public ActionResult MachineRead_Post(TblMachineReading obj, string ReadingDate = "")
        {
            Petrol_BunkEntities ctx = new Petrol_BunkEntities();
            string dt = Reverse_Date.ReverseYYYMMDD(ReadingDate);
            DateTime dt1 = Convert.ToDateTime(dt);
            TblMachineReadingDetail TblMachineReadingDetailobj = new TblMachineReadingDetail();
            var chkData = ctx.TblMachineReadings.Where(x => x.MachineId == obj.MachineId && x.ReadingDate == dt1).FirstOrDefault();
            if (chkData != null)
            {
                chkData.ReadingVal = obj.ReadingVal;
                chkData.Price = obj.Price;
                chkData.ModifidDate = DateTime.Now;
                ctx.Entry(chkData).State = System.Data.Entity.EntityState.Modified;
                if (ctx.SaveChanges() > 0)
                {
                    TempData["MchMsg"] = "Updated Machine Details";
                }
            }
            else
            {


                obj.ReadingDate = dt1;
                obj.InsertedDate = DateTime.Now;
                ctx.TblMachineReadings.Add(obj);
                if (ctx.SaveChanges() > 0)
                {
                    var chkOpenStock = ctx.TblMachineReadingDetails.Where(x => x.MachineId == obj.MachineId && x.ReadingDate == dt1 && x.StockType == "OpeningStock").FirstOrDefault();
                    if (chkOpenStock != null)
                    {
                        chkOpenStock.ReadingVal = obj.ReadingVal;
                        ctx.Entry(chkData).State = System.Data.Entity.EntityState.Modified;
                        if (ctx.SaveChanges() > 0)
                        {
                            TempData["MchMsg"] = "Updated Machine Details";
                        }
                    }
                    else
                    {
                        TblMachineReadingDetailobj.RowStatus = 0;
                        TblMachineReadingDetailobj.InsertedDate = DateTime.Now;
                        TblMachineReadingDetailobj.StockType = "OpeningStock";
                        TblMachineReadingDetailobj.CustomerId = 0;
                        TblMachineReadingDetailobj.MachineId = obj.MachineId;
                        TblMachineReadingDetailobj.ReadingDate = dt1;
                        TblMachineReadingDetailobj.ReadingVal = obj.ReadingVal;
                        TblMachineReadingDetailobj.Price = obj.Price;
                        ctx.TblMachineReadingDetails.Add(TblMachineReadingDetailobj);
                        if (ctx.SaveChanges() > 0)
                        {
                            TempData["MchMsg"] = "Added Machine Reading";
                        }
                        else
                        {
                            TempData["MchMsg"] = "Try Again";
                        }
                    }
                }
            }
            return new RedirectResult("/Admin/MachineRead");
        }

        public ActionResult CloseReadingByDate()
        {
            Petrol_BunkEntities ctx = new Petrol_BunkEntities();
            ViewBag.CloseStckMsg = TempData["CloseStckMsg"] == null ? "" : TempData["CloseStckMsg"] as string;

            ViewBag.MachineId = new SelectList(ctx.TblMachines.Where(x => x.RowStaus == 0).ToList(), "MachineId", "MachineName");

            return View();
        }

        public ActionResult CloseReadingByDate1()
        {
            Petrol_BunkEntities ctx = new Petrol_BunkEntities();
            ViewBag.CloseStckMsg = TempData["CloseStckMsg"] == null ? "" : TempData["CloseStckMsg"] as string;

            ViewBag.MachineId = new SelectList(ctx.TblMachines.Where(x => x.RowStaus == 0).ToList(), "MachineId", "MachineName");

            return View();
        }

        public JsonResult getReadings(string ReadingDate = "", int MachineId = 0)
        {
            string status = "";
            Petrol_BunkEntities ctx = new Petrol_BunkEntities();
            DateTime dt1 = Convert.ToDateTime(Reverse_Date.ReverseYYYMMDD(ReadingDate));
            var getdata = ctx.TblMachineReadings.Where(x => x.MachineId == MachineId && x.ReadingDate == dt1).FirstOrDefault();
            if (getdata != null)
            {
                status = getdata.ReadingVal + "," + getdata.Price;
            }
            else
            {
                status = "";
            }
            return Json(status, JsonRequestBehavior.AllowGet);
        }

        public JsonResult getMaxReading(string ReadingDate, int MachineId = 0)
        {
            string status = "";
            Petrol_BunkEntities ctx = new Petrol_BunkEntities();
            string dt = Reverse_Date.ReverseYYYMMDD(ReadingDate.ToString());
            DateTime dt1 = Convert.ToDateTime(dt);
            var getdata = ctx.TblMachineReadings.Where(x => x.MachineId == MachineId && x.CloseReadingDate == dt1).FirstOrDefault();
            if (getdata != null)
            {
                status = getdata.NextOpnReading + "," + getdata.Price;
            }
            else
            {
                status = "";
            }
            return Json(status, JsonRequestBehavior.AllowGet);
        }

        public ActionResult CloseReadingByDate_Post(TblMachineReading obj = null, string ReadingDate = "")
        {
            Petrol_BunkEntities ctx = new Petrol_BunkEntities();

            TblMachineReading TblMachineReadingobj = new TblMachineReading();
            DateTime dt1 = Convert.ToDateTime(Reverse_Date.ReverseYYYMMDD(ReadingDate));
            var getUpdate = ctx.TblMachineReadings.Where(x => x.MachineId == obj.MachineId && x.ReadingDate == dt1).FirstOrDefault();
            if (getUpdate != null)
            {

                getUpdate.CloseReadingVal = obj.CloseReadingVal;
                getUpdate.TotAmt = obj.TotAmt;
                getUpdate.Sales = obj.Sales;
                getUpdate.TestingLtr = obj.TestingLtr;
                getUpdate.FindSales = obj.FindSales;
                getUpdate.RowStatus = 0;
                getUpdate.CloseReadingDate = obj.ReadingDate.Value.AddDays(1);
                getUpdate.NextOpnReading = Convert.ToDecimal(obj.ReadingVal) + Convert.ToDecimal(obj.CloseReadingVal) - Convert.ToDecimal(obj.TestingLtr);
                ctx.Entry(getUpdate).State = System.Data.Entity.EntityState.Modified;
                if (ctx.SaveChanges() > 0)
                {
                    DateTime dd = obj.ReadingDate.Value.AddDays(1);

                    var getdata = ctx.TblMachineReadings.Where(x => x.ReadingDate == dd).FirstOrDefault();
                    if (getdata == null)
                    {
                        TblMachineReadingobj.ReadingDate = dt1.AddDays(1);
                        TblMachineReadingobj.ReadingVal = Convert.ToDecimal(obj.ReadingVal) + Convert.ToDecimal(obj.CloseReadingVal) - Convert.ToDecimal(obj.TestingLtr);
                        //TblMachineReadingobj.CloseReadingVal
                        TblMachineReadingobj.InsertedDate = DateTime.Now;
                        TblMachineReadingobj.MachineId = obj.MachineId;
                        TblMachineReadingobj.Price = obj.Price;
                        TblMachineReadingobj.RowStatus = 0;
                        TblMachineReadingobj.Sales = 0;
                        TblMachineReadingobj.TestingLtr = 0;
                        TblMachineReadingobj.TotAmt = 0;
                        ctx.TblMachineReadings.Add(TblMachineReadingobj);
                        if (ctx.SaveChanges() > 0)
                        {
                            TempData["CloseStckMsg"] = "Updated Closing Stock";

                        }
                        else
                        {

                        }
                    }
                    else
                    {
                        TempData["CloseStckMsg"] = "Updated Closing Stock";

                    }
                }
                else
                {
                    TempData["CloseStckMsg"] = "Try Again";
                }
            }
            else
            {
                TempData["CloseStckMsg"] = "Try Again";
            }
            return new RedirectResult("/Admin/MachineRead/CloseReadingByDate1");
        }

        public ActionResult StockReportByMachine()
        {
            Petrol_BunkEntities ctx = new Petrol_BunkEntities();
            ViewBag.MchMsg = TempData["MchMsg"] == null ? "" : TempData["MchMsg"] as string;

            ViewBag.MachineId = new SelectList(ctx.TblMachines.Where(x => x.RowStaus == 0).ToList(), "MachineId", "MachineName");

            return View();
        }

        public ActionResult getStockReportByMachine(string ReadingDate, int MachineId = 0, string All = "")
        {
            ViewBag.MachineId = MachineId;
            ViewBag.ReadingDate = ReadingDate;
            ViewBag.All = All;
            return View();
        }

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


        public ActionResult DigitalReport()
        {
            return View();
        }
        public ActionResult getDigitalReport(string date = "", string type = "")
        {
            ViewBag.date = Reverse_Date.ReverseYYYMMDD(date);
            ViewBag.type = type;
            return View();
        }
    }
}