using BLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using static FineUIPro.Web.CQMS.WBS.Control.DivisionSubProjects;
namespace FineUIPro.Web.CQMS.WBS.Control
{
    public partial class DivisionSubProjects : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataBrid();
            }
        }
        #region 数据绑定和事件
        /// 
        /// 数据绑定
        /// 
        public void DataBrid()
        {
            string strSql = @"select * from(select a.DivisionId,a.ProjectId,a.ParentId,a.DivisionLevel,a.BranchEngineeringCode,a.BranchEngineeringName,a.SubBranchEngineeringName,a.ProEngineeringCode,a.ProEngineeringName,a.ProEngineeringNum,a.Remark,a.AddUser,a.OperateTime,a.Sort,a.Status from Division_SubProjects as a where isnull(a.ProjectId,'')='' ";
            string strSql1 = @"select a.DivisionId,a.ProjectId,a.ParentId,a.DivisionLevel,a.BranchEngineeringCode,a.BranchEngineeringName,a.SubBranchEngineeringName,a.ProEngineeringCode,a.ProEngineeringName,a.ProEngineeringNum,a.Remark,a.AddUser,a.OperateTime,a.Sort,a.Status from Division_SubProjects as a inner join Base_Project as b on a.ProjectId=b.ProjectId where a.ProjectId=@ProjectId ";
            List listStr = new List();
            listStr.Add(new SqlParameter("@ProjectId", this.CurrUser.LoginProjectId));            
            SqlParameter[] parameter = listStr.ToArray();
            var zxsql = strSql + " union all " + strSql1 + " ) as t order by t.Sort,t.DivisionLevel asc";
            DataTable tb = SQLHelper.GetDataTableRunText(zxsql, parameter);
            Grid1.DataSource = tb;
            Grid1.DataBind();
        }
        /// 
        /// 行点击 事件
        /// 
        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                string rowID = e.RowID;
                DeleteRowByIDInternal(rowID);
                DataBrid();
                ShowNotify("删除成功!");
            }
        }
        /// 
        /// 行加载事件
        /// 
        protected void Grid1_RowDataBound(object sender, GridRowEventArgs e)
        {
            var states = int.Parse(Grid1.DataKeys[e.RowIndex][3].ToString());
            GridColumn isDelete = Grid1.FindColumn("Delete");
            if (states == 0)
            {
                e.CellAttributes[isDelete.ColumnIndex]["data-yc"] = "yc";
            }
        }
        #endregion
        #region 按钮
        /// 
        /// 搜索
        /// 
        //protected void btnSearch_Click(object sender, EventArgs e)
        //{
        //    DataBrid();
        //}
        /// 
        /// 删除
        /// 
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (Grid1.SelectedRowIndexArray.Length == 0)
            {
                ShowNotify("至少选择一条数据!");
                return;
            }
            foreach (int rowIndex in Grid1.SelectedRowIndexArray)
            {
                string rowID = Grid1.DataKeys[rowIndex][0].ToString();
                DeleteRowByIDInternal(rowID);
            }
            DataBrid();
            ShowNotify("删除成功!");
        }
        /// 
        /// 新增分部工程
        /// 
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            PageContext.RegisterStartupScript(Window1.GetShowReference($"DivisionSubProjectsEdit.aspx?DivisionId=&ParentId=", "新增"));
        }
        /// 
        /// 添加子级
        /// 
        protected void btnParentAdd_Click(object sender, EventArgs e)
        {
            if (Grid1.SelectedRowIndexArray.Length == 0)
            {
                ShowNotify("请选择一条数据!", MessageBoxIcon.Warning);
                return;
            }
            var rowIndex = Grid1.SelectedRowIndex;
            var rowId = Grid1.DataKeys[rowIndex][0].ToString();
            var runLevel = Grid1.DataKeys[rowIndex][2] != null ? Convert.ToInt32(Grid1.DataKeys[rowIndex][2]) : 1;
            if (runLevel > 2)
            {
                ShowNotify("最小节点无子节点!", MessageBoxIcon.Warning);
                return;
            }
            PageContext.RegisterStartupScript(Window1.GetShowReference($"DivisionSubProjectsEdit.aspx?DivisionId=&ParentId={rowId}", "添加子级"));
        }
        /// 
        /// 编辑
        /// 
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            if (Grid1.SelectedRowIndexArray.Length == 0)
            {
                ShowNotify("请选择一条数据!", MessageBoxIcon.Warning);
                return;
            }
            var rowIndex = Grid1.SelectedRowIndex;
            var rowId = Grid1.DataKeys[rowIndex][0].ToString();
            var parentId = Grid1.DataKeys[rowIndex][1] != null ? Grid1.DataKeys[rowIndex][1].ToString() : string.Empty;
            PageContext.RegisterStartupScript(Window1.GetShowReference($"DivisionSubProjectsEdit.aspx?DivisionId={rowId}&ParentId={parentId}", "编辑"));
        }
        /// 
        /// 关闭
        /// 
        protected void Window1_Close(object sender, WindowCloseEventArgs e)
        {
            DataBrid();
        }
        /// 
        /// 右击修改
        /// 
        protected void btnMenuModify_Click(object sender, EventArgs e)
        {
            btnEdit_Click(sender, e);
        }
        /// 
        /// 右击新增子级别
        /// 
        protected void btnMenuParentAdd_Click(object sender, EventArgs e)
        {
            btnParentAdd_Click(sender, e);
        }
        #endregion
        #region 私有方法
        /// 
        /// 根据行ID来删除行数据
        /// 
        private void DeleteRowByIDInternal(string rowID)
        {
            var model = Funs.DB.Division_SubProjects.FirstOrDefault(p => p.DivisionId == rowID);
            if (model != null)
            {
                Funs.DB.Division_SubProjects.DeleteOnSubmit(model);
                GetDevice(rowID);
            }
            Funs.DB.SubmitChanges();
        }
        /// 
        /// 删除
        /// 
        private void GetDevice(string id)
        {
            var model = Funs.DB.Division_SubProjects.FirstOrDefault(p => p.ParentId == id);
            if (model != null)
            {
                Funs.DB.Division_SubProjects.DeleteOnSubmit(model);
                GetDevice(model.DivisionId);
            }
        }
        /// 
        /// 判断是否为空
        /// 
        /// 
        public bool StrIsNumm(object value)
        {
            bool result = false;
            if (value == null) result = true;
            if (string.IsNullOrWhiteSpace(value.ToString())) result = true;
            return result;
        }
        #endregion
    }
}