using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CommonFunc;
using System.Data;
using System.Data.SqlClient;

namespace EnvigoHiring.Models.CandidatePool
{
    public class ValidateCandidate : CandidateModel
    {
        DataLayer obj = DataLayer.Instance;
        public validate ValidateData()
        {
            validate vm = new validate();
            /* Check Duplicate Page */
            string qryChkExist1 = "select EmailID from tblMasterCandidatePool where EmailID = '" + HttpUtility.HtmlEncode(EmailID.ToString().Trim()) + "'";

            string ChkRecord1 = obj.GetDatafromQuery(qryChkExist1)[0];
            if (ChkRecord1 != null)
            {
                if (ChkRecord1 != "")
                {
                    vm.isValid = false;
                    vm.Message = "Email  already exist! Please choose diffrent";
                    return vm;
                }
            }
            
            /* Upload Banner Image */
            if (flResumeName != null)
            {
                ResumeName = (ResumeName != null && ResumeName != "") ? ResumeName : UploadFunc.CustomFileName(flResumeName);
                vm = UploadFunc.SaveFile(flResumeName, FilePath, ResumeName,  ".png, .jpg, .jpeg, .gif");
                if (!vm.isValid)
                {
                    return vm;
                }
            }
            return vm;
        }
        public SaveData Save()
        {
            SaveData sv = new SaveData();
            var vl = ValidateData();
            sv.isSuccess = vl.isValid;
            if (vl.isValid == false)
            {
                sv.Message = vl.Message;
                return sv;
            }
            SqlParameter[] perm1 = new SqlParameter[12];
            perm1[0] = new SqlParameter("@CandidateID", CandidateID);
            perm1[1] = new SqlParameter("@Name", HttpUtility.HtmlEncode(CandidateName));
            perm1[2] = new SqlParameter("@gender", HttpUtility.HtmlEncode(Gender));
            perm1[3] = new SqlParameter("@Experience", Experience);
            perm1[4] = new SqlParameter("@EmailID", HttpUtility.HtmlEncode(EmailID));
            perm1[5] = new SqlParameter("@Education", HttpUtility.HtmlEncode(Education)); 
            perm1[6] = new SqlParameter("@Year_of_Passing", HttpUtility.HtmlEncode(PassingYear));
            perm1[7] = new SqlParameter("@InterestedDepartmentID", Int32.Parse(ddlDepartment));
            perm1[8] = new SqlParameter("@ProfessionalSummary", HttpUtility.HtmlEncode(ProfessionalSummary));
            perm1[9] = new SqlParameter("@PrimaryIndustry", HttpUtility.HtmlEncode(PrimaryIndustry));
            perm1[10] = new SqlParameter("@Phone", HttpUtility.HtmlEncode(ContactNo));
            perm1[11] = new SqlParameter("@PreviousNotableOrganisations", HttpUtility.HtmlEncode(PreviousNotableOrganisations));
            perm1[12] = new SqlParameter("@CandidateStatus", HttpUtility.HtmlEncode(ddlCandidateStatus));
            perm1[13] = new SqlParameter("@Source", HttpUtility.HtmlEncode(Source));
            perm1[14] = new SqlParameter("@SourcingDate", HttpUtility.HtmlEncode(SourcingDate));
            perm1[15] = new SqlParameter("@LastContactedDate", HttpUtility.HtmlEncode(LastContactedDate));
            perm1[16] = new SqlParameter("@ResumeGoogleLink", HttpUtility.HtmlEncode(ResumeGoogleLink));
            perm1[17] = new SqlParameter("@ResumeName", HttpUtility.HtmlEncode(ResumeName));
            perm1[18] = new SqlParameter("@addedby", Int32.Parse(UserID));
            perm1[19] = new SqlParameter("@nid", DbType.Int32);
            perm1[19].Direction = ParameterDirection.Output;
            DataTable success1 = obj.ExecuteStoreProcedure("proc_tblBlog", perm1);
            //CandidateID = Int32.Parse(perm1[19].Value.ToString().Trim());
            if (success1 != null)
            {
                if (CandidateID == 0)
                {
                    sv.isSuccess = true;
                    sv.Message = "<b>Success :</b> Page added successfully.";
                    return sv;
                }
                else
                {
                    sv.isSuccess = true;
                    sv.Message = "<b>Success :</b> Page updated successfully.";

                    return sv;
                }
            }
            return sv;
        }
        public DataTable LoadCandidate()
        {
            string query = string.Format("select * from tblMasterCandidatePool where CandidateID={0}", CandidateID);
            DataTable dt = obj.SelectDatatable(query);
            return dt;
        }
        public DataTable viewcandidateData()
        {
            string query = string.Format(@"select CandidateID,Name,Experience,EmailID,CandidateStatus,Source,LastContactedDate,Education,DepartmentName from tblMasterCandidatePool t inner join 
                                        tblMasterDepartment t1 on(t.InterestedDepartmentID = t1.DepartmentID)");
            return obj.SelectDatatable(query);
        }
    }
}