using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using CommonFunc;
using System.ComponentModel.DataAnnotations;
using System.Data.SqlClient;
using System.Web.Mvc;

namespace EnvigoHiring.Models.Position
{
    public class JDModel : BaseClass
    {
        [AllowHtml]
        //[Required(ErrorMessage = "Alert! Job Description should not be blank")]
        public string JobDescription { get; set; }
        public string DepartmentName { get; set; }
        public string DesignationName { get; set; }
        public int JDStatus { get; set; }
        public int PositionID { get; set; }
        DataLayer obj = DataLayer.Instance;
        public void FillData()
        {
            string query = "Select JobDescription,DepartmentName,DesignationName,ApproveStatus from tblJobDescription t right outer join vwPosition t1 on (t.PositionID=t1.PositionID) where t1.PositionID=" + PositionID;
            DataTable dt = obj.SelectDatatable(query);
            if (dt != null && dt.Rows.Count > 0)
            {
                DataRow row = dt.Rows[0];
                JobDescription = row.SetParamValue("JobDescription");
                DepartmentName = row.SetParamValue("DepartmentName");
                DesignationName = row.SetParamValue("DesignationName");
                JDStatus= row.SetParamValue("ApproveStatus").ToInt32();
            }
            
        }
        public void reset()
        {
            JobDescription = "";
            DepartmentName = "";
            DesignationName = "";
        }
        public void SaveData()
        {
            SaveData sv = new SaveData();
            if (JobDescription == null || JobDescription == "")
            {
                alert("Alert! Job Description should not be blank", false);
                return;
            }
            if (!SpecialCharacterFunc.TextAreaString(JobDescription))
            {
                alert("Alert! Job Description special character not allowed", false);
                return;
            }
            //var vl = ValidationFunc.ValidateModelData<JDModel>(this, "JobDescription");
            //if (vl.isValid == false)
            //{
            //    alert(vl.Message, vl.isValid);
            //    return;
            //}
            sv = Save();
            alert(sv.Message, sv.isSuccess);
            return ;
        }
        public SaveData Save()
        {
            SaveData sv = new SaveData();
            string query = "Select ApproveStatus from tblJobDescription where positionID=" + PositionID;
            int jdstatus = obj.GetDatafromQuery(query)[0].ToInt32();
            jdstatus = (jdstatus > 1) ? jdstatus : 1;
            SqlParameter[] perm1 = new SqlParameter[5];
            perm1[0] = new SqlParameter("@PositionID", PositionID);
            perm1[1] = new SqlParameter("@JobDescription", JobDescription.HtmlEncode());
            perm1[2] = new SqlParameter("@UserID", UserID);
            perm1[3] = new SqlParameter("@nstatus", DbType.Int32);
            perm1[3].Direction = ParameterDirection.Output;
            perm1[4] = new SqlParameter("@ApproveStatus", jdstatus);
            DataTable success1 = obj.ExecuteStoreProcedure("proc_tblJobDescription", perm1);
            if (success1 != null)
            {
                int nstatus = perm1[3].Value.ToString().ToInt32();
                if (nstatus == 1)
                {
                    sv.isSuccess = true;
                    sv.Message = "<b>Success :</b> Job Description added successfully.";

                }
                else if (nstatus == 2)
                {
                    sv.isSuccess = true;
                    sv.Message = "<b>Success :</b> Job Description updated successfully.";

                }
            }
            else
            {
                sv.isSuccess = false;
                sv.Message = "<b>Error :</b> Please try again later.";
            }
            return sv;
        }
        public void Approve()
        {
            SaveData sv = new SaveData();
            SqlParameter[] perm1 = new SqlParameter[3];
            perm1[0] = new SqlParameter("@PositionID", PositionID);
            perm1[1] = new SqlParameter("@ApproveStatus", 2);
            perm1[2] = new SqlParameter("@UserID", UserID);
            DataTable success1 = obj.ExecuteStoreProcedure("proc_tblJobDescription_Approve", perm1);
            if (success1 != null)
            {
                //sv.isSuccess = true;
                //sv.Message = "<b>Success :</b> Job Description approved successfully.";
                HttpContext.Current.Response.Redirect("/position/viewposition");
            }
            else
            {
                sv.isSuccess = false;
                sv.Message = "<b>Error :</b> Please try again later.";
            }
            alert(sv.Message, sv.isSuccess);
        }
        public void Reject()
        {
            SaveData sv = new SaveData();
            SqlParameter[] perm1 = new SqlParameter[3];
            perm1[0] = new SqlParameter("@PositionID", PositionID);
            perm1[1] = new SqlParameter("@ApproveStatus", 3);
            perm1[2] = new SqlParameter("@UserID", UserID);
            DataTable success1 = obj.ExecuteStoreProcedure("proc_tblJobDescription_Approve", perm1);
            if (success1 != null)
            {
                //sv.isSuccess = true;
                //sv.Message = "<b>Success :</b> Job Description rejected successfully.";
                HttpContext.Current.Response.Redirect("/position/viewposition");
            }
            else
            {
                sv.isSuccess = false;
                sv.Message = "<b>Error :</b> Please try again later.";
            }
            alert(sv.Message, sv.isSuccess);
        }
    }
}