using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using test.Models;



namespace test.Controllers
{
    public class UsersController : Controller
    {

        private testEntities db = new testEntities();

        // GET: Users
        public ActionResult Index()
        {
            return View(db.Users.ToList());
        }

        // GET: Users/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            User user = db.Users.Find(id);
            if (user == null)
            {
                return HttpNotFound();
            }
            return View(user);
        }

        // GET: Users/signUp
        public ActionResult Create()
        {
            return View();
        }

        // POST: Users/signUp
        // To protect from overposting attacks, enable the specific properties you want to bind to, for 
        // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "User_ID,User_Name,User_LastName,User_PhoneNumber,User_Password,User_Status")] User user)
        {
            if (ModelState.IsValid)
            {
                db.Users.Add(user);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(user);
        }


        public ActionResult Dashboard()
        {
            // اگر کاربر لاگین نکرده بود، منتقلش کن به لاگین
            if (Session["UserID"] == null)
                return RedirectToAction("Login");


            ViewBag.Categories = db.Categories.ToList();
            ViewBag.tag = db.Tags.ToList();

            return View();

        }

        public ActionResult Logout()
        {
            Session.Clear(); // یا Session.Abandon()
            return RedirectToAction("Login");
        }


        // GET: Users/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            User user = db.Users.Find(id);
            if (user == null)
            {
                return HttpNotFound();
            }
            return View(user);
        }



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

        // POST: Users/Login
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Login(string User_PhoneNumber, string User_Password)
        {
            var user = db.Users.FirstOrDefault(u => u.User_PhoneNumber == User_PhoneNumber && u.User_Password == User_Password);

            if (user != null)
            {
                // ذخیره اطلاعات کاربر در Session
                Session["UserID"] = user.User_ID;
                Session["UserName"] = user.User_Name;

                // انتقال به صفحه اصلی
                return RedirectToAction("Dashboard", "Users");

            }

            ViewBag.Message = "شماره یا رمز عبور اشتباه است.";
            return View();
        }

        // POST: Users/Edit/5
        // To protect from overposting attacks, enable the specific properties you want to bind to, for 
        // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "User_ID,User_Name,User_LastName,User_PhoneNumber,User_Password,User_Status")] User user)
        {
            if (ModelState.IsValid)
            {
                db.Entry(user).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(user);
        }




        // GET: Users/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            User user = db.Users.Find(id);
            if (user == null)
            {
                return HttpNotFound();
            }
            return View(user);
        }




        // POST: Users/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            User user = db.Users.Find(id);
            db.Users.Remove(user);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult AddCategoryFromDashboard(Category category)
        {
            if (ModelState.IsValid)
            {
                db.Categories.Add(category);
                db.SaveChanges();
                TempData["CategoryMessage"] = "دسته‌بندی با موفقیت اضافه شد.";
            }
            return RedirectToAction("Dashboard");
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult AddTagFromDashboard(Tag tag)
        {
            if (ModelState.IsValid)
            {
                db.Tags.Add(tag);
                db.SaveChanges();
                TempData["TagMessage"] = "تگ با موفقیت اضافه شد.";
            }
            return RedirectToAction("Dashboard");
        }







    }
}


































