﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ParttimeistanbulProject.Models.ORM.Context;
using ParttimeistanbulProject.Models.ORM.Entity;
using System.Data.Entity;
using ParttimeistanbulProject.Areas.Yonetim.Models.DTO;
using System.IO;
using System.Net;

namespace ParttimeistanbulProject.Areas.Yonetim.Controllers
{
    public class IlanController : BaseController
    {
        // GET: Yonetim/Ilan
        public ActionResult Index()
        {
            var ilans = db.Ilans.Include(i => i.Category).Include(i => i.Company).Include(i => i.il).Include(i => i.ilce);
            return View(ilans.ToList().Where(x=> x.IsDeleted==false));
        }
        public ActionResult AddIlan()
        {
            ViewBag.KategoriID = new SelectList(db.Categorys, "ID", "Adi");
            ViewBag.FirmaID = new SelectList(db.Companys, "ID", "Unvan");
            ViewBag.IlID = new SelectList(db.ils, "ID", "Adi");
            ViewBag.IlceID = new SelectList(db.ilces, "ID", "Adi");
            return View();
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult AddIlan(IlanVM model)
        {
            IlanVM vmodel = new IlanVM();
            ViewBag.KategoriID = new SelectList(db.Categorys, "ID", "Adi");
            ViewBag.FirmaID = new SelectList(db.Companys, "ID", "Unvan");
            ViewBag.IlID = new SelectList(db.ils, "ID", "Adi");
            ViewBag.IlceID = new SelectList(db.ilces, "ID", "Adi");
            if (ModelState.IsValid)
            {
                string filename = string.Empty;
                foreach (string name in Request.Files)
                {
                    model.ImagePath = Request.Files[name];
                    string ext = Path.GetExtension(Request.Files[name].FileName);
                    if (ext == ".jpg" || ext == ".png" || ext == ".jpeg" || ext == ".gif")
                    {
                        string uniqnumber = Guid.NewGuid().ToString();
                        filename = uniqnumber + model.ImagePath.FileName;
                        model.ImagePath.SaveAs(Server.MapPath("~/Areas/Yonetim/Content/Site/images/Event/" + filename));
                    }
                }
                Ilan ilanevent = new Ilan();
                ilanevent.ImagePath = filename;
                ilanevent.Title = model.Title;
                ilanevent.ToplamKisi = model.ToplamKisi;
                ilanevent.FirmaID = model.FirmaID;
                ilanevent.KategoriID = model.KategoriID;
                ilanevent.IlID = model.IlID;
                ilanevent.IlceID = model.IlceID;
                ilanevent.Content = model.Content;
                db.Ilans.Add(ilanevent);
                db.SaveChanges();
                ViewBag.IslemDurum = 1;
                ViewBag.KategoriID = new SelectList(db.Categorys, "ID", "Adi");
                ViewBag.FirmaID = new SelectList(db.Companys, "ID", "Unvan");
                ViewBag.IlID = new SelectList(db.ils, "ID", "Adi");
                ViewBag.IlceID = new SelectList(db.ilces, "ID", "Adi");
                return View();
            }
            else
            {
                ViewBag.IslemDurum = 2;
                return View(vmodel);
            }
        }
        public ActionResult UpdateIlan(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Ilan ilan = db.Ilans.Find(id);
            if (ilan == null)
            {
                return HttpNotFound();
            }
            Ilan vilan = db.Ilans.FirstOrDefault(x => x.ID == id);
            ViewBag.ImagePath = vilan.ImagePath.ToString();
            ViewBag.KategoriID = new SelectList(db.Categorys, "ID", "Adi", ilan.KategoriID);
            ViewBag.FirmaID = new SelectList(db.Companys, "ID", "Unvan", ilan.FirmaID);
            ViewBag.IlID = new SelectList(db.ils, "ID", "Adi", ilan.IlID);
            ViewBag.IlceID = new SelectList(db.ilces, "ID", "Adi", ilan.IlceID);
            return View(ilan);
        }
        [HttpPost]
        [ValidateInput(false)]
        [ValidateAntiForgeryToken]
        public ActionResult UpdateIlan([Bind(Include = "ID,ImagePath,Title,ToplamKisi,ImagePath,FirmaID,IlID,IlceID,Content,KategoriID")] Ilan ilan)
        {
            if (ModelState.IsValid)
            {
                db.Entry(ilan).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.KategoriID = new SelectList(db.Categorys, "ID", "Adi", ilan.KategoriID);
            ViewBag.FirmaID = new SelectList(db.Companys, "ID", "Unvan", ilan.FirmaID);
            ViewBag.IlID = new SelectList(db.ils, "ID", "Adi", ilan.IlID);
            ViewBag.IlceID = new SelectList(db.ilces, "ID", "Adi", ilan.IlceID);
            return View(ilan);
        }
        public JsonResult DeleteIlan(int id)
        {
            Ilan ilan = db.Ilans.FirstOrDefault(x => x.ID == id);
            ilan.IsDeleted = true;
            ilan.DeleteDate = DateTime.Now;
            db.SaveChanges();
            return Json(new { success = true });
        }
    }
}