using EnvigoHiring.Models.CandidatePool;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace EnvigoHiring.Controllers
{
    [CustomAuthorize(Roles = "Admin, Recruiter")]
    public class candidateController : Controller
    {
        // GET: candidate
        public ActionResult addcandidate(string id)
        {
            CandidateModel cm = new CandidateModel();
            if (id != null && id!="")
            {
                cm.CandidateID = Int32.Parse(id);
                bool result = cm.Filldata();
                if (result == false)
                {
                    return RedirectToAction("viewcandidate", "candidate");
                }
            }
            return View(cm);
        }
        [HttpPost]
        public ActionResult addcandidate(CandidateModel cm, string btnsubmit, string id)
        {
            ModelState.Clear();
            if (id != null && id != "")
            {
                cm.CandidateID = Int32.Parse(id);
            }
            else
            {
                cm.CandidateID = 0;
            }
            if (btnsubmit == "Submit")
            {
                cm.SaveData();
                //if error on saving data
                if (cm.MessageType == "success")
                {
                    return RedirectToAction("viewcandidate", "candidate");
                }
            }
            if (cm.CandidateID >0)
            {
                //Load Data
                cm.Filldata();
            }
            return View(cm);
        }

        public ActionResult viewcandidate()
        {
            CandidateModel cm = new CandidateModel();
            cm.dtCandidate = cm.viewcandidate();
            return View(cm);
        }
        [HttpPost]
        public ActionResult viewcandidate(CandidateModel cm, string btnDownload,string btnSearch,string btnReset)
        {
            cm.dtCandidate = cm.viewcandidate();
            if (btnDownload == "Download")
            {
                cm.DownloadCandidate();
                cm.dtCandidate = cm.SearchCandidate();
            }
            if(btnSearch=="Search") {
                cm.dtCandidate = cm.SearchCandidate();
            }
            if (btnReset == "Reset") {
                cm.dtCandidate = cm.resetSearchCandidate();
            }

            return View(cm);
        }
    }
}