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

namespace FineUIPro.Web.Common.ProjectSet
{
    public partial class WorkArea : PageBase
    {
        #region 定义项
        /// <summary>
        /// 项目主键
        /// </summary>
        public string ProjectId
        {
            get
            {
                return (string)ViewState["ProjectId"];
            }
            set
            {
                ViewState["ProjectId"] = value;
            }
        }

        /// <summary>
        /// 装置主键
        /// </summary>
        public string InstallationId
        {
            get
            {
                return (string)ViewState["InstallationId"];
            }
            set
            {
                ViewState["InstallationId"] = value;
            }
        }
        #endregion

        #region 加载
        /// <summary>
        /// 加载页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {               
                this.ddlPageSize.SelectedValue = Grid1.PageSize.ToString();
                this.InstallationId = Request.Params["InstallationId"];
                Model.Project_Installation installation = BLL.Project_InstallationService.GetProject_InstallationByInstallationId(this.InstallationId);
                if (installation != null)
                {
                    this.ProjectId = installation.ProjectId;
                }
                BLL.Base_UnitService.InitProjectUnitDropDownList(this.drpUnit, true, this.ProjectId, BLL.Const.UnitType_5,Resources.Lan.PleaseSelect);
                BLL.Base_UnitService.InitProjectUnitDropDownList(this.drpSupervisorUnit, true, this.ProjectId, BLL.Const.UnitType_3,Resources.Lan.PleaseSelect);
                // 绑定表格
                this.BindGrid();
            }
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        private void BindGrid()
        {
            string strSql = @"SELECT WorkArea.ProjectId,Project.ProjectCode,Project.ProjectName,WorkArea.InstallationId,InstallationCode,InstallationName "
                        + @" ,WorkArea.UnitId,Unit.UnitName,WorkArea.SupervisorUnitId,SupervisorUnit.UnitName AS SupervisorUnitName,WorkAreaId,WorkAreaCode,WorkArea.Remark,WorkAreaName,EnWorkAreaName"
                        + @" FROM Project_WorkArea AS WorkArea "
                        + @" LEFT JOIN Base_Project AS Project ON WorkArea.ProjectId=Project.ProjectId "
                        + @" LEFT JOIN Project_Installation AS Installation ON Installation.InstallationId=WorkArea.InstallationId "
                        + @" LEFT JOIN Base_Unit AS Unit ON Unit.UnitId=WorkArea.UnitId "
                        + @" LEFT JOIN Base_Unit AS SupervisorUnit ON SupervisorUnit.UnitId=WorkArea.SupervisorUnitId "
                        + @" WHERE 1=1 ";
            List<SqlParameter> listStr = new List<SqlParameter>();           
            if (!string.IsNullOrEmpty(this.txtWorkAreaCode.Text.Trim()))
            {
                strSql += " AND WorkAreaCode LIKE @WorkAreaCode";
                listStr.Add(new SqlParameter("@WorkAreaCode", "%" + this.txtWorkAreaCode.Text.Trim() + "%"));
            }
            if (!string.IsNullOrEmpty(this.ProjectId))
            {
                strSql += " AND WorkArea.ProjectId = @ProjectId";
                listStr.Add(new SqlParameter("@ProjectId", this.ProjectId));
            }
            if (!string.IsNullOrEmpty(this.InstallationId))
            {
                strSql += " AND WorkArea.InstallationId = @InstallationId";
                listStr.Add(new SqlParameter("@InstallationId", this.InstallationId));
            }
            if (this.drpUnit.SelectedValue != BLL.Const._Null)
            {
                strSql += " AND WorkArea.UnitId = @UnitId";
                listStr.Add(new SqlParameter("@UnitId", this.drpUnit.SelectedValue));
            }
            if (this.drpSupervisorUnit.SelectedValue != BLL.Const._Null)
            {
                strSql += " AND WorkArea.SupervisorUnitId = @SupervisorUnitId";
                listStr.Add(new SqlParameter("@SupervisorUnitId", this.drpSupervisorUnit.SelectedValue));
            }
            SqlParameter[] parameter = listStr.ToArray();
            DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter);

            Grid1.RecordCount = tb.Rows.Count;
           // tb = GetFilteredTable(Grid1.FilteredData, tb);
            var table = this.GetPagedDataTable(Grid1, tb);
            Grid1.DataSource = table;
            Grid1.DataBind();
        }

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

        /// <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();
        }

        /// <summary>
        /// 排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Grid1_Sort(object sender, FineUIPro.GridSortEventArgs e)
        {
            BindGrid();
        }

        /// <summary>
        /// 关闭弹出窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Window1_Close(object sender, EventArgs e)
        {
            BindGrid();
        }
        #endregion

        #region 增加按钮事件
        /// <summary>
        /// 增加按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnNew_Click(object sender, EventArgs e)
        {
            if (CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.HJGL_InstallationMenuId, Const.BtnAdd))
            {
                PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("WorkAreaEdit.aspx?InstallationId={0}",this.InstallationId, "新增 - ")));
            }
            else
            {
                Alert.ShowInTop(Resources.Lan.NoPrivilegePrompt, MessageBoxIcon.Warning);
                return;
            }
        }
        #endregion

        #region 编辑
        /// <summary>
        /// 双击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Grid1_RowDoubleClick(object sender, GridRowClickEventArgs e)
        {
            this.EditData();
        }

        /// <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(Resources.Lan.SelectLeastOneRecord, MessageBoxIcon.Warning);
                return;
            }

            ////双击事件 编辑权限有:编辑页面,无:查看页面 或者状态是完成时查看页面
            if (CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.HJGL_InstallationMenuId, Const.BtnModify))
            {   
                PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("WorkAreaEdit.aspx?WorkAreaId={0}", Grid1.SelectedRowID, "编辑 - ")));                
            }
            else
            {
                PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("WorkAreaView.aspx?WorkAreaId={0}", Grid1.SelectedRowID, "查看 - ")));
            }
        }
        #endregion

        #region 删除
        /// <summary>
        /// 右键删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnMenuDelete_Click(object sender, EventArgs e)
        {
            if (CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.HJGL_InstallationMenuId, Const.BtnDelete))
            {
                if (Grid1.SelectedRowIndexArray.Length > 0)
                {
                    string strShowNotify = string.Empty;
                    foreach (int rowIndex in Grid1.SelectedRowIndexArray)
                    {
                        string rowID = Grid1.DataKeys[rowIndex][0].ToString();
                        var getWorkArea = BLL.Project_WorkAreaService.GetProject_WorkAreaByWorkAreaId(rowID);
                        if (getWorkArea != null)
                        {
                            string cont = judgementDelete(rowID);
                            if (string.IsNullOrEmpty(cont))
                            {
                                BLL.Project_WorkAreaService.DeleteProject_WorkAreaByWorkAreaId(rowID);
                                BLL.Sys_LogService.AddLog(Const.System_1, this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.HJGL_InstallationMenuId, Const.BtnDelete, rowID);
                            }
                            else
                            {
                                strShowNotify += Resources.Lan.ConstructionArea+":" + getWorkArea.WorkAreaCode + cont;
                            }
                        }
                    }

                    
                    if (!string.IsNullOrEmpty(strShowNotify))
                    {
                        Alert.ShowInTop(strShowNotify, MessageBoxIcon.Warning);
                    }
                    else
                    {
                        BindGrid();
                        ShowNotify(Resources.Lan.DeletedSuccessfully, MessageBoxIcon.Success);
                    }
                }
            }
            else
            {
                Alert.ShowInTop(Resources.Lan.NoPrivilegePrompt, MessageBoxIcon.Warning);
                return;
            }
        }

        #region 判断是否可删除
        /// <summary>
        /// 判断是否可以删除
        /// </summary>
        /// <returns></returns>
        private string judgementDelete(string id)
        {
            string content = string.Empty;
            if (Funs.DB.Pipeline_Pipeline.FirstOrDefault(x => x.WorkAreaId == id) != null)
            {
                content += "已在【管线信息】中使用,不能删除!";
            }

            return content;
        }
        #endregion
        #endregion

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            this.BindGrid();
        }
        
        /// <summary>
        /// 查看按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnView_Click(object sender, EventArgs e)
        {
            PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("WorkAreaView.aspx?WorkAreaId={0}", Grid1.SelectedRowID, "查看 - ")));
        }
    }
}