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

namespace EnvigoHiring.Models.User
{
    public class UserModel : BaseClass
    {
        public int ID { get; set; }
        public int UserType { get; set; }
        public string PageHeading { get; set; }
        public string UserName { get; set; }
        public string Email { get; set; }
        public bool chkActive { get; set; }
        public int Status { get; set; }
        [Required(ErrorMessage = "Alert! Select Roles")]
        public string ddlRoles { get; set; }
        public string backUrl { get; set; }
        public DataTable dtUser { get; set; }
        DataLayer obj = DataLayer.Instance;
        public DataTable ViewUser()
        {
            string query = "Select t.*,t1.RoleName from tblUserLogin t left outer join tblMasterRole t1 on (t.role=t1.RoleID) where status!=0 and (Role is not null and Role!=0)";
            if (UserType == 0)
            {
                query = "Select t.*,t1.RoleName from tblUserLogin t left outer join tblMasterRole t1 on (t.role=t1.RoleID) where status=0 or Role=0 or role is null";
            }
            return obj.SelectDatatable(query);
        }
        public bool FillData()
        {
            bool result = false;
            string query = "Select UserName,UserEmail,Status,Role from tblUserLogin where UserID=" + ID;
            DataTable dt = obj.SelectDatatable(query);
            if (dt != null && dt.Rows.Count > 0)
            {
                result = true;
                DataRow row = dt.Rows[0];
                Email = row.SetParamValue("UserEmail");
                UserName = row.SetParamValue("UserName");
                ddlRoles = row.SetParamValue("Role");
                chkActive = (row.SetParamValue("status") == "1") ? true : false;

            }
            return result;
        }
        public string RedirectUrl()
        {
            var url = HttpContext.Current.Request.UrlReferrer;
            return (url != null && url.ToString() != "") ? url.ToString() : "/user/pendingusers";
        }
        public void SaveData()
        {
            SaveData sv = new SaveData();
            Status = (chkActive == true) ? 1 : 0;
            SqlParameter[] perm1 = new SqlParameter[3];
            perm1[0] = new SqlParameter("@UserId", ID);
            perm1[1] = new SqlParameter("@Role", ddlRoles);
            perm1[2] = new SqlParameter("@Status", Status);
            DataTable success1 = obj.ExecuteStoreProcedure("proc_tblUserLogin_Approve", perm1);
            if (success1 != null)
            {
                sv.isSuccess = true;
                sv.Message = "<b>Success :</b> User activated successfully.";

            }
            else
            {
                sv.isSuccess = false;
                sv.Message = "<b>Error :</b> Please try again later.";
            }
            alert(sv.Message, sv.isSuccess);
        }

        public List<SelectListItem> RolesList()
        {
            string query = "Select RoleID, RoleName from tblMasterRole";
            return Common.FillSelectList(query, "RoleName", "RoleID");
        }
    }
}