using BLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;

namespace FineUIPro.Web.BaseInfo
{
    public partial class DisciplinesWBS : PageBase
    {
        #region 加载
        /// <summary>
        /// 加载页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetButtonPower();//按钮权限
                ddlPageSize.SelectedValue = Grid1.PageSize.ToString();

                //部门(查询)
                this.drpDepartmentIdS.DataTextField = "DepartName";
                this.drpDepartmentIdS.DataValueField = "DepartId";
                this.drpDepartmentIdS.DataSource = BLL.DepartService.GetDepartList();
                this.drpDepartmentIdS.DataBind();
                Funs.FineUIPleaseSelect(this.drpDepartmentIdS);

                //部门
                this.drpDepartId.DataTextField = "DepartName";
                this.drpDepartId.DataValueField = "DepartId";
                this.drpDepartId.DataSource = BLL.DepartService.GetDepartList();
                this.drpDepartId.DataBind();
                Funs.FineUIPleaseSelect(this.drpDepartId);
                this.drpDepartId.SelectedIndex = 0;

                //类型
                this.drpType.DataTextField = "ConstText";
                this.drpType.DataValueField = "ConstValue";
                this.drpType.DataSource = BLL.ConstValue.drpConstItemList(BLL.ConstValue.Group_PlanType);
                this.drpType.DataBind();
                Funs.FineUIPleaseSelect(this.drpType);

                this.drpTypeS.DataTextField = "ConstText";
                this.drpTypeS.DataValueField = "ConstValue";
                this.drpTypeS.DataSource = BLL.ConstValue.drpConstItemList(BLL.ConstValue.Group_PlanType);
                this.drpTypeS.DataBind();
                this.drpTypeS.SelectedIndex = 0;

                // 绑定表格
                BindGrid();
            }
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        private void BindGrid()
        {
            string strSql = @"SELECT DisciplinesWBSId,DisciplinesWBSCode,DisciplinesWBSName,WBS,DepartId,DepartName,Designer,Checker,Approver,Leader,Roles,Remark,Type,NetworkOper,NetworkOper1,WorkorderOper"
                            + @" FROM Base_DisciplinesWBS WHERE Type=@Type";
            List<SqlParameter> listStr = new List<SqlParameter>();
            listStr.Add(new SqlParameter("@Type", this.drpTypeS.SelectedValue));
            if (!string.IsNullOrEmpty(this.txtDisciplinesWBSNameS.Text.Trim()))
            {
                strSql += " AND DisciplinesWBSName LIKE @DisciplinesWBSName ";
                listStr.Add(new SqlParameter("@DisciplinesWBSName", "%" + this.txtDisciplinesWBSNameS.Text.Trim() + "%"));
            }
            if (this.drpDepartmentIdS.SelectedValue != BLL.Const._Null)
            {
                strSql += " AND DepartId=@DepartId ";
                listStr.Add(new SqlParameter("@DepartId", this.drpDepartmentIdS.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();

            if (this.drpTypeS.SelectedValue != "1")
            {

                Grid1.Columns[8].Hidden = true;
                Grid1.Columns[9].Hidden = true;
                Grid1.Columns[10].Hidden = true;
                Grid1.Columns[11].Hidden = true;
                Grid1.Columns[12].Hidden = false;

                Grid1.Width = 940;
            }
            else
            {
                Grid1.Columns[8].Hidden = false;
                Grid1.Columns[9].Hidden = false;
                Grid1.Columns[10].Hidden = false;
                Grid1.Columns[11].Hidden = false;
                Grid1.Columns[12].Hidden = true;

                Grid1.Width = 1200;
            }
           
        }     

        /// <summary>
        /// 改变索引事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }
        #endregion

        #region 分页下拉选择
        /// <summary>
        /// 分页下拉选择
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
            BindGrid();
        }
        #endregion

        #region 增加
        /// <summary>
        /// 增加按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            EmptyText();
        }
        #endregion

        #region 清空文本框
        /// <summary>
        /// 清空文本框
        /// </summary>
        private void EmptyText()
        {
            this.hfFormID.Text = string.Empty;
            this.txtDisciplinesWBSCode.Text = string.Empty;
            this.txtDisciplinesWBSName.Text = string.Empty;
            this.txtWBS.Text = string.Empty;
            this.drpDepartId.SelectedValue = Const._Null;
            this.drpType.SelectedValue = Const._Null;
            this.txtNetworkOper.Text = string.Empty;
            this.txtNetworkOper1.Text = string.Empty;
            this.txtWorkorderOper.Text = string.Empty;
            this.txtDesigner.Text = string.Empty;
            this.txtChecker.Text = string.Empty;
            this.txtApprover.Text = string.Empty;
            this.txtLeader.Text = string.Empty;
            this.txtRoles.Text = string.Empty;
            this.txtRemark.Text = string.Empty;
            this.btnDelete.Enabled = false;
        }
        #endregion

        #region 选择行事件
        /// <summary>
        /// 选择行事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Grid1_RowSelect(object sender, GridRowSelectEventArgs e)
        {
            EditData();
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            BLL.DisciplinesWBSService.DeleteDisciplinesWBSById(hfFormID.Text);
            BLL.Sys_LogService.AddLog(this.CurrUser.UserId, "Delete the DisciplinesWBS");
            // 重新绑定表格,并模拟点击[新增按钮]
            BindGrid();
            //PageContext.RegisterStartupScript("onNewButtonClick();");

        }
        /// <summary>
        /// 右键删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnMenuDelete_Click(object sender, EventArgs e)
        {
            this.DeleteData();
        }

        /// <summary>
        /// 删除方法
        /// </summary>
        private void DeleteData()
        {
            if (Grid1.SelectedRowIndexArray.Length > 0)
            {
                foreach (int rowIndex in Grid1.SelectedRowIndexArray)
                {
                    string rowID = Grid1.DataKeys[rowIndex][0].ToString();

                    BLL.DisciplinesWBSService.DeleteDisciplinesWBSById(rowID);
                    BLL.Sys_LogService.AddLog(this.CurrUser.UserId, "Delete the DisciplinesWBS");
                }

                BindGrid();
                EmptyText();
                //PageContext.RegisterStartupScript("onNewButtonClick();");
            }
        }
        #endregion

        #region 编辑
        /// <summary>
        /// 右键编辑事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnMenuEdit_Click(object sender, EventArgs e)
        {
            this.EditData();
        }

        /// <summary>
        /// 编辑数据方法
        /// </summary>
        private void EditData()
        {
            if (Grid1.SelectedRowIndexArray.Length == 0)
            {
                Alert.ShowInTop("Please select at least one record!", MessageBoxIcon.Warning);
                return;
            }
            string Id = Grid1.SelectedRowID;
            var cons = BLL.DisciplinesWBSService.GetDisciplinesWBSById(Id);
            if (cons != null)
            {
                this.txtDisciplinesWBSCode.Text = cons.DisciplinesWBSCode;
                this.txtDisciplinesWBSName.Text = cons.DisciplinesWBSName;
                this.txtWBS.Text = cons.WBS;
                if (!string.IsNullOrEmpty(cons.DepartId))
                {
                    this.drpDepartId.SelectedValue = cons.DepartId;
                }
                this.txtNetworkOper.Text = cons.NetworkOper;
                this.txtNetworkOper1.Text = cons.NetworkOper1;
                this.txtWorkorderOper.Text = cons.WorkorderOper;
                this.txtDesigner.Text = cons.Designer;
                this.txtChecker.Text = cons.Checker;
                this.txtApprover.Text = cons.Approver;
                this.txtLeader.Text = cons.Leader;
                this.txtRoles.Text = cons.Roles;
                this.txtRemark.Text = cons.Remark;
                this.drpType.SelectedValue = cons.Type;
                hfFormID.Text = Id;
                this.btnDelete.Enabled = true;
            }
        }
        #endregion

        #region 保存
        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string strRowID = hfFormID.Text;
            //if (this.drpDepartId.SelectedValue == BLL.Const._Null)
            //{
            //    ShowNotify("Please select Department!", MessageBoxIcon.Warning);
            //    return;
            //}
            if (this.drpType.SelectedValue == BLL.Const._Null)
            {
                ShowNotify("Please select Type!", MessageBoxIcon.Warning);
                return;
            }
            if (!BLL.DisciplinesWBSService.IsExitDisciplinesWBSName(this.txtDisciplinesWBSName.Text.Trim(),this.drpType.SelectedValue, strRowID))
            {
                Model.Base_DisciplinesWBS cons = new Model.Base_DisciplinesWBS
                {
                    DisciplinesWBSCode = this.txtDisciplinesWBSCode.Text.Trim(),
                    DisciplinesWBSName = this.txtDisciplinesWBSName.Text.Trim(),
                    WBS = this.txtWBS.Text.Trim(),
                    NetworkOper = this.txtNetworkOper.Text.Trim(),
                    NetworkOper1 = this.txtNetworkOper1.Text.Trim(),
                    WorkorderOper = this.txtWorkorderOper.Text.Trim(),
                    Designer = this.txtDesigner.Text.Trim(),
                    Checker = this.txtChecker.Text.Trim(),
                    Approver = this.txtApprover.Text.Trim(),
                    Leader = this.txtLeader.Text.Trim(),
                    Roles = this.txtRoles.Text.Trim(),
                    Remark = this.txtRemark.Text.Trim(),
                };
                if (this.drpDepartId.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpDepartId.SelectedValue))
                {
                    cons.DepartId = this.drpDepartId.SelectedValue;
                    cons.DepartName = this.drpDepartId.SelectedItem.Text;
                }
                if (this.drpType.SelectedValue!=BLL.Const._Null)
                {
                    cons.Type = this.drpType.SelectedValue;
                }
                if (string.IsNullOrEmpty(strRowID))
                {
                    if (this.drpType.SelectedValue == "4")
                    {
                        var projectmanger = BLL.DisciplinesWBSService.GetDisciplinesWBSListByType("4");
                        if (projectmanger.Count == 0)
                        {
                            cons.DisciplinesWBSId = SQLHelper.GetNewID(typeof(Model.Base_DisciplinesWBS));
                            BLL.DisciplinesWBSService.AddDisciplinesWBS(cons);
                            BLL.Sys_LogService.AddLog(this.CurrUser.UserId, "Add the Disciplines/WBS");
                            ShowNotify("Save Successfully!", MessageBoxIcon.Success);
                        }
                        else
                        {
                            Alert.ShowInTop("The Project Manager already exists!");
                            return;
                        }
                    }
                    else
                    {
                        cons.DisciplinesWBSId = SQLHelper.GetNewID(typeof(Model.Base_DisciplinesWBS));
                        BLL.DisciplinesWBSService.AddDisciplinesWBS(cons);
                        BLL.Sys_LogService.AddLog(this.CurrUser.UserId, "Add the Disciplines/WBS");
                        ShowNotify("Save Successfully!", MessageBoxIcon.Success);
                    }
                }
                else
                {
                    cons.DisciplinesWBSId = strRowID;
                    BLL.DisciplinesWBSService.UpdateDisciplinesWBS(cons);
                    BLL.Sys_LogService.AddLog(this.CurrUser.UserId, "Modify the Disciplines/WBS");
                    ShowNotify("Save Successfully!", MessageBoxIcon.Success);
                }
                this.SimpleForm1.Reset();
                // 重新绑定表格,并点击当前编辑或者新增的行 
                BindGrid();
                //PageContext.RegisterStartupScript(String.Format("F('{0}').selectRow('{1}');", Grid1.ClientID, cons.DisciplinesWBSId));
                PageContext.RegisterStartupScript("onNewButtonClick();");
            }
            else
            {
                Alert.ShowInTop("The Number or DisciplinesWBS already exists!", MessageBoxIcon.Warning);
            }
        }
        #endregion

        #region 查询
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Text_TextChanged(object sender, EventArgs e)
        {
            BindGrid();
            if (this.drpTypeS.SelectedValue != "1")
            {
                for (int i = 0; i < Grid1.Rows.Count; i++)
                {
                    txtDesigner.Hidden = true;
                    txtApprover.Hidden = true;
                    txtLeader.Hidden = true;
                    txtChecker.Hidden = true;
                    txtRoles.Hidden = false;
                }
            }
            else
            {
                txtDesigner.Hidden = false;
                txtApprover.Hidden = false;
                txtChecker.Hidden = false;
                txtLeader.Hidden = false;
                txtRoles.Hidden = true;
            }

        }
        #endregion

        #region 权限设置
        /// <summary>
        /// 菜单按钮权限
        /// </summary>
        private void GetButtonPower()
        {
            var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.UserId, BLL.Const.DisciplinesWBSMenuId);
            if (buttonList.Count() > 0)
            {
                if (buttonList.Contains(BLL.Const.BtnAdd))
                {
                    this.btnAdd.Hidden = false;
                }
                if (buttonList.Contains(BLL.Const.BtnModify))
                {
                    this.btnMenuEdit.Hidden = false;
                }
                if (buttonList.Contains(BLL.Const.BtnSave))
                {
                    this.btnSave.Hidden = false;
                }
                if (buttonList.Contains(BLL.Const.BtnDelete))
                {
                    this.btnDelete.Hidden = false;
                }
            }
        }
        #endregion        
    }
}