// GET: Internship/Create
        public ActionResult Create()
        {
            if (Session["user_type"].Equals("GST") || Session["user_type"].Equals("STD"))
            {
                return(RedirectToAction("../Home/Index"));
            }

            ViewBag.Categories = db.CategoriesLists;
            ViewBag.Locations  = db.LocationsLists;
            ViewBag.Perks      = db.PerksLists;
            ViewBag.Skills     = db.SkillsLists;
            InternshipRegErr err = new InternshipRegErr();

            return(View(err));
        }
        public async Task <ActionResult> Create(FormCollection collection)
        {
            if (Session["user_type"].Equals("GST") || Session["user_type"].Equals("STD"))
            {
                return(RedirectToAction("../Home/Index"));
            }

            bool             flag = false;
            InternshipRegErr err  = new InternshipRegErr();

            ViewBag.Categories = db.CategoriesLists;
            ViewBag.Locations  = db.LocationsLists;
            ViewBag.Perks      = db.PerksLists;
            ViewBag.Skills     = db.SkillsLists;

            Internship i = new Internship();

            i.About          = Request.Form["about"];
            i.ContactEmail   = Request.Form["contactemail"];
            i.ContactMobile  = Request.Form["contactmobile"];
            i.C_Id           = Session["user_id"].ToString();
            i.InternshipType = Request.Form["type"];

            DateTime result;

            if (string.IsNullOrWhiteSpace(i.About))
            {
                flag      = true;
                err.About = "You must write details about the internship";
            }

            if (!string.IsNullOrWhiteSpace(Request.Form["startdate"]))
            {
                if (DateTime.TryParse(Request.Form["startdate"], out result))
                {
                    i.StartDate = Convert.ToDateTime(Request.Form["startdate"]);
                }
                else
                {
                    flag          = true;
                    err.StartDate = "Start date not in proper format";
                }
            }

            int value;

            if (string.IsNullOrWhiteSpace(Request.Form["minduration"]) || !Int32.TryParse(Request.Form["minduration"], out value))
            {
                flag            = true;
                err.MinDuration = "Minimum Duration cannot be Empty";
            }
            else
            {
                i.MinDuration = Convert.ToInt32(Request.Form["minduration"]);
                if (i.MinDuration <= 0)
                {
                    flag            = true;
                    err.MinDuration = "Minimum Duration should be atleast 1 month";
                }
            }

            if (string.IsNullOrWhiteSpace(Request.Form["maxduration"]) || !Int32.TryParse(Request.Form["maxduration"], out value))
            {
                flag            = true;
                err.MaxDuration = "Maximum Dauration cannot be Empty";
            }
            else
            {
                i.MaxDuration = Convert.ToInt32(Request.Form["maxduration"]);
                if (i.MaxDuration <= 0)
                {
                    flag            = true;
                    err.MaxDuration = "Maximum Duration should be atleast 1 month";
                }
            }

            if (string.IsNullOrWhiteSpace(Request.Form["seats"]) || !Int32.TryParse(Request.Form["seats"], out value))
            {
                flag = true;
                err.AvailableSeats = "Cannot be empty";
            }
            else
            {
                i.AvailableSeats = Convert.ToInt32(Request.Form["seats"]);
                if (i.AvailableSeats <= 0)
                {
                    flag = true;
                    err.AvailableSeats = "Value should be atleast 1 seat";
                }
            }

            if (string.IsNullOrWhiteSpace(Request.Form["applybefore"]) || !DateTime.TryParse(Request.Form["applybefore"], out result))
            {
                flag            = true;
                err.ApplyBefore = "Apply Before Date cannot be Empty";
            }
            else
            {
                i.ApplyBefore = Convert.ToDateTime(Request.Form["applybefore"]);
            }

            if (string.IsNullOrWhiteSpace(i.ContactEmail) || !Regex.IsMatch(i.ContactEmail, @"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z", RegexOptions.IgnoreCase))
            {
                flag             = true;
                err.ContactEmail = "Email Cannot be empty. It should be a valid email address.";
            }

            if (!string.IsNullOrWhiteSpace(Request.Form["contactmobile"]))
            {
                i.ContactMobile = Request.Form["contactmobile"];
            }

            if (!string.IsNullOrWhiteSpace(Request.Form["locations"]))
            {
                foreach (string s in Request.Form["locations"].Split(','))
                {
                    InternshipLocation templ = new InternshipLocation();
                    templ.L_Id = Convert.ToInt32(s);
                    i.InternshipLocations.Add(templ);
                }
            }
            else
            {
                flag          = true;
                err.Locations = "You should select atleast one location";
            }

            if (!string.IsNullOrWhiteSpace(Request.Form["skills"]))
            {
                foreach (string s in Request.Form["skills"].Split(','))
                {
                    InternshipSkill temps = new InternshipSkill();
                    temps.S_Id = Convert.ToInt32(s);
                    i.InternshipSkills.Add(temps);
                }
            }

            if (!string.IsNullOrWhiteSpace(Request.Form["perks"]))
            {
                foreach (string s in Request.Form["perks"].Split(','))
                {
                    InternshipPerk tempp = new InternshipPerk();
                    tempp.P_Id = Convert.ToInt32(s);
                    i.InternshipPerks.Add(tempp);
                }
            }

            if (!string.IsNullOrWhiteSpace(Request.Form["category"]))
            {
                i.InternshipIn = Convert.ToInt32(Request.Form["category"]);
            }
            else
            {
                flag         = true;
                err.Category = "You need to select A Category";
            }

            if (flag)
            {
                err.i = i;
                return(View(err));
            }

            try
            {
                string response = "";

                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri(Baseurl);

                    client.DefaultRequestHeaders.Clear();

                    //Define request data format
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    //Sending request to find web api REST service resource GetAllEmployees using HttpClient

                    HttpResponseMessage Res = await client.PostAsJsonAsync("api/internshipsAPI/", i);

                    //Checking the response is successful or not which is sent using HttpClient
                    if (Res.IsSuccessStatusCode)
                    {
                        //Storing the response details recieved from web api
                        var StudentResponse = Res.Content.ReadAsStringAsync().Result;

                        //Deserializing the response recieved from web api and storing into the Employee list
                        response = JsonConvert.DeserializeObject <string>(StudentResponse);
                    }
                }

                if (response.Equals("exists"))
                {
                    err.Error = "Such an Internship already exists";
                    err.i     = i;
                    return(View(err));
                }
                else
                {
                    return(RedirectToAction("../Company/Internships"));
                }
            }
            catch
            {
                err.i = i;
                return(View(err));
            }
        }