using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using BLL;
namespace FineUIPro.Web.ZHGL.RealName
{
    public partial class BasicData : PageBase
    {
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetButtonPower();
                SynchroSetService.InitCountryDropDownList(this.drpCountry, false);
                this.drpCountry.SelectedValue ="101";
                SynchroSetService.InitProjectDropDownList(this.drpProject, false);
                if (!string.IsNullOrEmpty(this.CurrUser.LoginProjectId))
                {
                    var getproject = ProjectService.GetProjectByProjectId(this.CurrUser.LoginProjectId);
                    if (getproject != null)
                    {
                        this.drpProject.SelectedValue = getproject.JTProjectCode;
                        this.drpProject.Readonly = true;
                    }
                }
                // 绑定表格
                this.BindGrid();
                this.BindGrid2();
                this.BindGrid3();
                this.BindGrid4();
                this.BindGrid5();
            }
        }
        #region 获取按钮权限
        /// 
        /// 获取按钮权限
        /// 
        /// 
        /// 
        private void GetButtonPower()
        {
            string menuId = !string.IsNullOrEmpty(this.CurrUser.LoginProjectId) ? Const.ProjectRealNameBasicDataMenuId : Const.ServerRealNameBasicDataMenuId;
            var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, menuId);
            if (buttonList.Count() > 0)
            {
                if (buttonList.Contains(BLL.Const.BtnSave))
                {
                    //this.btnSave.Hidden = false;
                    this.btnDatabaseGo.Hidden = false;
                    this.btnDatabaseGo2.Hidden = false;
                    this.btnDatabaseGo3.Hidden = false;
                    this.btnDatabaseGo31.Hidden = false;
                    this.btnDatabaseGo4.Hidden = false;
                    this.btnDatabaseGo5.Hidden = false;
                    this.btnDatabaseGo51.Hidden = false;
                }
            }
        }
        #endregion
        #region TAB 1 基础字典数据
        /// 
        /// 绑定数据
        /// 
        private void BindGrid()
        {
            string strSql = @"SELECT BasicDataId,dictTypeCode,dictCode,dictName FROM dbo.RealName_BasicData"
                                + @" WHERE 1=1";
            List listStr = new List();
            if (!string.IsNullOrEmpty(this.drpBaseType.SelectedValue))
            {
                strSql += " AND dictTypeCode = @dictTypeCode";
                listStr.Add(new SqlParameter("@dictTypeCode", this.drpBaseType.SelectedValue));
            }
            SqlParameter[] parameter = listStr.ToArray();
            DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter);
            Grid1.RecordCount = tb.Rows.Count;
            var table = this.GetPagedDataTable(Grid1, tb);
            Grid1.DataSource = table;
            Grid1.DataBind();
        }
        protected void drpBaseType_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.BindGrid();
        }
        protected void btnDatabaseGo_Click(object sender, EventArgs e)
        {
            ShowNotify(BLL.SynchroSetService.getBasicData(this.drpBaseType.SelectedValue), MessageBoxIcon.Information);
            this.BindGrid();
        }
        #endregion
        #region TAB 2 国家数据
        /// 
        /// 获取数据
        /// 
        /// 
        /// 
        protected void btnDatabaseGo2_Click(object sender, EventArgs e)
        {
            ShowNotify(BLL.SynchroSetService.getCountry(), MessageBoxIcon.Information);
            this.BindGrid2();
        }
             
        /// 
        /// 绑定数据
        /// 
        private void BindGrid2()
        {
            string strSql = @"SELECT ID,CountryId,countryCode,cname,name FROM dbo.RealName_Country"
                                + @" WHERE 1=1";
            List listStr = new List();
            //if (!string.IsNullOrEmpty(this.drpBaseType.SelectedValue))
            //{
            //    strSql += " AND dictTypeCode = @dictTypeCode";
            //    listStr.Add(new SqlParameter("@dictTypeCode", this.drpBaseType.SelectedValue));
            //}
            SqlParameter[] parameter = listStr.ToArray();
            DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter);
            Grid2.RecordCount = tb.Rows.Count;
            var table = this.GetPagedDataTable(Grid2, tb);
            Grid2.DataSource = table;
            Grid2.DataBind();
        }
        #endregion
        #region TAB 3 省份数据
        /// 
        /// 国家下拉框
        /// 
        /// 
        /// 
        protected void drpCountry_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.BindGrid3();
        }
        /// 
        /// 绑定数据
        /// 
        private void BindGrid3()
        {
            string strSql = @"SELECT ID,provinceCode,cityCode,cname,cnShortName,name,countryId FROM dbo.RealName_City"
                                + @" WHERE 1=1";
            List listStr = new List();
            if (!string.IsNullOrEmpty(this.drpCountry.SelectedValue))
            {
                strSql += " AND countryId = @countryId";
                listStr.Add(new SqlParameter("@countryId", this.drpCountry.SelectedValue));
            }
            SqlParameter[] parameter = listStr.ToArray();
            DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter);
            Grid3.RecordCount = tb.Rows.Count;
            var table = this.GetPagedDataTable(Grid3, tb);
            Grid3.DataSource = table;
            Grid3.DataBind();
        }
        protected void btnDatabaseGo3_Click(object sender, EventArgs e)
        {
            ShowNotify(BLL.SynchroSetService.getCity(this.drpCountry.SelectedValue), MessageBoxIcon.Information);
            this.BindGrid3();
        }
        protected void btnDatabaseGo31_Click(object sender, EventArgs e)
        {
            ShowNotify(BLL.SynchroSetService.getCity(null), MessageBoxIcon.Information);
            this.BindGrid3();
        }
        #endregion
        #region TAB 4 项目数据
        /// 
        /// 获取项目数据
        /// 
        /// 
        /// 
        protected void btnDatabaseGo4_Click(object sender, EventArgs e)
        {
            List getCodeList = new List();
            if (!string.IsNullOrEmpty(this.CurrUser.LoginProjectId))
            {
                var getProject = ProjectService.GetProjectByProjectId(this.CurrUser.LoginProjectId);
                if (getProject != null)
                {
                    getCodeList.Add(getProject.JTProjectCode);
                }
            }
            else
            {
                getCodeList = Funs.DB.RealName_SynchroSet.Select(x => x.ProCode).ToList();
            }
            if (getCodeList != null && getCodeList.Count() > 0)
            {
                string info = string.Empty;
                foreach (var item in getCodeList)
                {
                    info += "项目[" + item + "]" + SynchroSetService.getProject(item);
                }
                ShowNotify(info, MessageBoxIcon.Information);
            }
            this.BindGrid4();
        }
        /// 
        /// 绑定项目数据
        /// 
        private void BindGrid4()
        {
            string strSql = @"SELECT ID,proCode,proName,proShortName FROM dbo.RealName_Project"
                                + @" WHERE 1=1";
            List listStr = new List();
            //if (!string.IsNullOrEmpty(this.drpBaseType.SelectedValue))
            //{
            //    strSql += " AND dictTypeCode = @dictTypeCode";
            //    listStr.Add(new SqlParameter("@dictTypeCode", this.drpBaseType.SelectedValue));
            //}
            SqlParameter[] parameter = listStr.ToArray();
            DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter);
            Grid4.RecordCount = tb.Rows.Count;
            var table = this.GetPagedDataTable(Grid4, tb);
            Grid4.DataSource = table;
            Grid4.DataBind();
        }
        #endregion
        #region TAB 5 施工队数据
        /// 
        /// 国家下拉框
        /// 
        /// 
        /// 
        protected void drpProject_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.BindGrid5();
        }
        /// 
        /// 绑定数据
        /// 
        private void BindGrid5()
        {
            string strSql = @"SELECT ID,teamId,proCode,teamName,teamLeaderName,teamLeaderMobile FROM dbo.RealName_CollTeam"
                                + @" WHERE 1=1";
            List listStr = new List();
            if (!string.IsNullOrEmpty(this.drpProject.SelectedValue))
            {
                strSql += " AND proCode = @proCode";
                listStr.Add(new SqlParameter("@proCode", this.drpProject.SelectedValue));
            }
            SqlParameter[] parameter = listStr.ToArray();
            DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter);
            Grid5.RecordCount = tb.Rows.Count;
            var table = this.GetPagedDataTable(Grid5, tb);
            Grid5.DataSource = table;
            Grid5.DataBind();
        }
        protected void btnDatabaseGo5_Click(object sender, EventArgs e)
        {
            ShowNotify(BLL.SynchroSetService.getCollTeam(this.drpProject.SelectedValue), MessageBoxIcon.Information);
            this.BindGrid5();
        }
        protected void btnDatabaseGo51_Click(object sender, EventArgs e)
        {
            ShowNotify(BLL.SynchroSetService.getCollTeam(null), MessageBoxIcon.Information);
            this.BindGrid5();
        }
        #endregion
    }
}