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 CustomerController : Controller
    {
        // GET: Admin/Customer/AddCustomer
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult AddCustomer(int id = 0)
        {
            Petrol_BunkEntities ctx = new Petrol_BunkEntities();
            ViewBag.CustId = 0;
            ViewBag.CustMSg = TempData["CustMSg"] == null ? "" : TempData["CustMSg"] as string;
            if (id > 0)
            {
                var CustEdit = ctx.TblCustomers.Where(x => x.CustId == id).FirstOrDefault();
                ViewBag.CustId = CustEdit.CustId;
                return View(CustEdit);
            }
            else
            {
                return View();
            }
        }
        public ActionResult AddCustomer_Post(TblCustomer obj)
        {
            Petrol_BunkEntities ctx = new Petrol_BunkEntities();
            if (obj.CustId > 0)
            {
                var CustUpdate = ctx.TblCustomers.Where(x => x.CustId == obj.CustId).FirstOrDefault();
                CustUpdate.ModifiedDate = DateTime.Now;
                CustUpdate.CustName = obj.CustName;
                CustUpdate.Mob = obj.Mob;
                CustUpdate.Address = obj.Address;
                ctx.Entry(CustUpdate).State = System.Data.Entity.EntityState.Modified;
                if (ctx.SaveChanges() > 0)
                {
                    TempData["CustMSg"] = "Updated Customer Details";
                }
                else
                {
                    TempData["CustMSg"] = "Try Again";
                }
            }
            else
            {
                obj.RowStatus = 0;
                obj.RemAmt = 0;
                obj.InsertedDate = DateTime.Now;
                ctx.TblCustomers.Add(obj);
                if (ctx.SaveChanges() > 0)
                {
                    TempData["CustMSg"] = "Added Customer Details";
                }
                else
                {
                    TempData["CustMSg"] = "Try Again";
                }
            }
            return RedirectToAction("AddCustomer");
        }
        public ActionResult CustomerDelete(int id = 0)
        {
            Petrol_BunkEntities ctx = new Petrol_BunkEntities();
            var CustUpdate = ctx.TblCustomers.Where(x => x.CustId == id).FirstOrDefault();
            CustUpdate.RowStatus = 1;
            ctx.Entry(CustUpdate).State = System.Data.Entity.EntityState.Modified;
            if (ctx.SaveChanges() > 0)
            {
                TempData["CustMSg"] = "Deleted Customer Details";
            }
            else
            {
                TempData["CustMSg"] = "Try Again";
            }
            return RedirectToAction("AddCustomer");
        }
    }
}