using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLayer;
using DTO;
using Telerik.Web.UI;

namespace NikaiDSR.Admin
{
    public partial class Users : PageBase
    {
        #region declarartion
        CommonManager CM = new CommonManager();
        UserManager UM = new UserManager();
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PageTitle = "NIKAI- ADMIN - USERS";
                FillCountryComboBox();
                FillRegionComboBox();
                FillLocationComboBox();
                FillRoleComboBox();

                btnAdd.OnClientClick = "openRadWin('../Controls/AddEditUser.aspx?UId=','AddUser'); return false;";
            }
        }

        void FillCountryComboBox()
        {
            IList<CountryDTO> CountryList = CM.GetCountry(null, null);
            Helper.Validation.FillTelerikDropdown(cmbCountry, CountryList, "Country_Name", "Country_Id", "Select Country", "0");
        }

        void FillRegionComboBox()
        {
            IList<RegionDTO> RegionList = CM.GetRegion(null, null, null);
            Helper.Validation.FillTelerikDropdown(cmbRegion, RegionList, "Region_Name", "Region_Id", "Select Region", "0");
        }

        void FillLocationComboBox()
        {
            IList<LocationDTO> LocationList = CM.GetLocation(null, null, null);
            Helper.Validation.FillTelerikDropdown(cmbLocation, LocationList, "Location_Name", "Location_Id", "Select Location", "0");
        }

        void FillRoleComboBox()
        {
            IList<RoleDTO> RoleList = CM.GetRole(null, null);
            Helper.Validation.FillTelerikDropdown(cmbRole, RoleList, "Role_Name", "Role_Id", "Select Role", "0");
        }

        protected void btnGo_Click(object sender, EventArgs e)
        {
            Grid.Rebind();
        }

        protected void Grid_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            IList<UserRoleLocationRegionCountryDTO> User = UM.GetUserRoleLocationRegionCountry(null, null, null, null,
                Convert.ToInt32(cmbRole.SelectedValue), null,
                Convert.ToInt32(cmbLocation.SelectedValue), null,
                Convert.ToInt32(cmbRegion.SelectedValue), null,
                Convert.ToInt32(cmbCountry.SelectedValue), null);

            Grid.DataSource = User;
        }

        protected void Grid_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem item = (GridDataItem)e.Item;

                if (item["Status"].Text == "True")
                {
                    item["Status"].Text = "Active";
                }
                else
                {
                    item["Status"].Text = "InActive";
                }
                
                Button btnEdit = (Button)item["Edit"].Controls[1];
                btnEdit.OnClientClick = "openRadWin('../Controls/AddEditUser.aspx?UserId=" + item["Site_User_Id"].Text + "','EditUser'); return false;";
            }
        }
    }
}