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 ManageEmployeeController : Controller
    {
        //Admin/ManageEmployee/getAttendanceReport
        string status = "";
        public ActionResult AddEmployee(int id = 0)
        {
            Petrol_BunkEntities ctx = new Petrol_BunkEntities();
            ViewBag.EmpMsg = TempData["EmpMsg"] == null ? "" : TempData["EmpMsg"] as string;
            if (id == 0)
            {
                ViewBag.EmployeeId = id;
                return View();
            }
            else
            {
                var getdata = ctx.TblEmployees.Where(x => x.EmployeeId == id).FirstOrDefault();
                ViewBag.EmployeeId = getdata.EmployeeId;
                return View(getdata);
            }
        }
        public ActionResult AddEmployee_Post(TblEmployee obj)
        {
            Petrol_BunkEntities ctx = new Petrol_BunkEntities();
            string imagefilename = string.Empty;
            if (obj.EmployeeId == 0)
            {
                obj.RowStatus = 0;
                obj.InsertedDate = DateTime.Now;

                if (Request.Files["EmpImg"] != null)
                {
                    var img = Request.Files["EmpImg"];
                    imagefilename = DateTime.Now.ToString("ddmm_hhss") + System.IO.Path.GetExtension(img.FileName);
                    if (img.ContentLength != 0)
                    {
                        obj.EmpImg = imagefilename;
                        img.SaveAs(Server.MapPath("~/Images/" + imagefilename));
                    }
                }
                ctx.TblEmployees.Add(obj);
                if (ctx.SaveChanges() > 0)
                {
                    TempData["EmpMsg"] = "Added Employee Details";
                }
                else
                {
                    TempData["EmpMsg"] = "Try Again";
                }
            }
            else
            {
                var getdata = ctx.TblEmployees.Where(x => x.EmployeeId == obj.EmployeeId).FirstOrDefault();
                getdata.EmpName = obj.EmpName;
                getdata.Address = obj.Address;
                getdata.Mob = obj.Mob;
                getdata.ShiftType = obj.ShiftType;
                getdata.OtAmt = obj.OtAmt;
                getdata.AadharCardNo = obj.AadharCardNo;
                getdata.SalAmt = obj.SalAmt;
                if (Request.Files["EmpImg"] != null)
                {
                    var img = Request.Files["EmpImg"];
                    imagefilename = DateTime.Now.ToString("ddmm_hhss") + System.IO.Path.GetExtension(img.FileName);
                    if (img.ContentLength != 0)
                    {
                        getdata.EmpImg = imagefilename;
                        img.SaveAs(Server.MapPath("~/Images/" + imagefilename));
                    }
                }
                ctx.Entry(getdata).State = System.Data.Entity.EntityState.Modified;
                if (ctx.SaveChanges() > 0)
                {
                    TempData["EmpMsg"] = "Updated Employee Details";
                }
                else
                {
                    TempData["EmpMsg"] = "Try Again";
                }
            }
            return new RedirectResult("/Admin/ManageEmployee/AddEmployee");
        }
        public ActionResult DeleteEmployee_Post(int id = 0)
        {
            Petrol_BunkEntities ctx = new Petrol_BunkEntities();
            var getdata = ctx.TblEmployees.Where(x => x.EmployeeId == id).FirstOrDefault();
            getdata.RowStatus = 1;
            ctx.Entry(getdata).State = System.Data.Entity.EntityState.Modified;
            if (ctx.SaveChanges() > 0)
            {
                TempData["EmpMsg"] = "Deleted Employee Details";
            }
            else
            {
                TempData["EmpMsg"] = "Try Again";
            }
            return new RedirectResult("/Admin/ManageEmployee/AddEmployee");
        }

        public ActionResult AddAttendance()
        {
            return View();
        }
        public ActionResult getEmployees(string date1 = "")
        {
            ViewBag.date1 = Reverse_Date.ReverseYYYMMDD(date1);
            return View();
        }

        public ActionResult AttendanceReport()
        {
            ViewBag.MnthName = new SelectList(ConstantList.MnthList(), null, null);
            ViewBag.Year = new SelectList(ConstantList.YearList(), null, null);

            return View();
        }
        public ActionResult getAttendanceReport(string MnthName = "", int Year = 0)
        {
            ViewBag.MnthName = MnthName;
            ViewBag.Year = Year;
            return View();
        }

        public JsonResult AddAttendance_Post(
            DateTime dt, string date1 = "",
            int[] TotalChk = null,
            int[] TotalUnCk = null)
        {
            TblAttedance obj = new TblAttedance();
            Petrol_BunkEntities ctx = new Petrol_BunkEntities();
            if (TotalUnCk != null && TotalUnCk.Length > 0)
            {
                for (int i = 0; i < TotalUnCk.Length; i++)
                {
                    int Id = TotalUnCk[i];
                    var getdata = ctx.TblAttedances.Where(x => x.AttDate == dt && x.EmpId == Id).FirstOrDefault();
                    if (getdata != null)
                    {
                        getdata.AttStatus = "A";
                        ctx.Entry(getdata).State = System.Data.Entity.EntityState.Modified;
                        if (ctx.SaveChanges() > 0)
                        {

                        }
                    }
                    else
                    {
                        obj.AttDate = dt;
                        obj.EmpId = Id;
                        obj.AttStatus = "A";
                        obj.InsertedDate = DateTime.Now;
                        ctx.TblAttedances.Add(obj);
                        if (ctx.SaveChanges() > 0)
                        {

                        }
                    }
                }
            }


            if (TotalChk != null && TotalChk.Length > 0)
            {
                for (int i = 0; i < TotalChk.Length; i++)
                {
                    int Id = TotalChk[i];
                    var getdata = ctx.TblAttedances.Where(x => x.AttDate == dt && x.EmpId == Id).FirstOrDefault();
                    if (getdata != null)
                    {
                        getdata.AttStatus = "P";
                        ctx.Entry(getdata).State = System.Data.Entity.EntityState.Modified;
                        if (ctx.SaveChanges() > 0)
                        {

                        }
                    }
                    else
                    {
                        obj.AttDate = dt;
                        obj.EmpId = Id;
                        obj.AttStatus = "P";
                        obj.InsertedDate = DateTime.Now;
                        ctx.TblAttedances.Add(obj);
                        if (ctx.SaveChanges() > 0)
                        {

                        }
                    }
                }
            }
            status = "Attendance Added";
            return Json(status, JsonRequestBehavior.AllowGet);
        }

    }
}