using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.SqlClient;
using System.Data;
using System.Web.UI.WebControls;
using BLL;

namespace FineUIPro.Web.common.ProjectSet
{
    public partial class SelectWelder : PageBase
    {
        #region 加载
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitTreeMenu();//加载树
            }
        }

        private void BindGrid()
        {
            string projectId = Request.Params["projectId"];
            string unitid = this.tvUnit.SelectedNodeID;

            string strSql = @"select w.WelderId,w.WelderCode,w.WelderName,w.ProjectId,w.UnitId,u.UnitName
                                FROM dbo.Welder_Welder w
                                LEFT JOIN dbo.Base_Unit u ON u.UnitId = w.UnitId 
                                where w.UnitId=@UnitId";

            List<SqlParameter> listStr = new List<SqlParameter>();
            listStr.Add(new SqlParameter("@UnitId", unitid));

            strSql += " order by WelderCode";
            SqlParameter[] parameter = listStr.ToArray();
            DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter);

            tb = GetFilteredTable(Grid1.FilteredData, tb);
            Grid1.DataSource = tb;
            Grid1.DataBind();

            var welders = BLL.WelderService.GetWelderByProjectIdAndUnitId(projectId, unitid);
            if (welders != null)
            {
                string[] arr = new string[welders.Count()];
                int i = 0;
                foreach (var q in welders)
                {
                    arr[i] = q.WelderId;
                    i++;
                }
                Grid1.SelectedRowIDArray = arr;
            }
        }
        #endregion

        #region
        /// <summary>
        /// 加载树
        /// </summary>
        private void InitTreeMenu()
        {
            this.tvUnit.Nodes.Clear();
            var projectUnits = from x in Funs.DB.Project_Unit
                               where x.ProjectId == Request.Params["projectId"]
                               && x.UnitType == BLL.Const.UnitType_5
                               orderby x.UnitType descending
                               select x;
            if (projectUnits.Count() > 0)
            {
                foreach (var item in projectUnits)
                {
                    var baseUnit = Funs.DB.Base_Unit.FirstOrDefault(x => x.UnitId == item.UnitId);

                    TreeNode rootNode = new TreeNode();//定义根节点
                    rootNode.Text = baseUnit.UnitName;
                    rootNode.NodeID = item.UnitId;
                    rootNode.EnableClickEvent = true;
                    this.tvUnit.Nodes.Add(rootNode);
                }
            }
        }
        #endregion

        #region 点击TreeView
        /// <summary>
        /// 点击TreeView
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void tvUnit_NodeCommand(object sender, TreeCommandEventArgs e)
        {
            if (!string.IsNullOrEmpty(this.tvUnit.SelectedNode.NodeID))
            {
                BindGrid();
            }
        }
        #endregion

        #region 编辑
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            if (CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.HJGL_ProjectWelderMenuId, Const.BtnSave))
            {
                string projectId = Request.Params["projectId"];
                string coutValue = IsSave();
                if (!string.IsNullOrEmpty(coutValue))
                {
                    ShowNotify(coutValue + Resources.Lan.PleaseSelectTeamGroup);
                }
                else
                {
                    BLL.Welder_ProjectWelderService.DeleteProjectWelderByProjectId(projectId, this.tvUnit.SelectedNode.NodeID);

                    string[] selectRowId = Grid1.SelectedRowIDArray;
                    int n = 0;
                    int j = 0;
                    int[] selections = new int[selectRowId.Count()];
                    foreach (GridRow row in Grid1.Rows)
                    {
                        if (selectRowId.Contains(row.DataKeys[0]))
                        {
                            selections[n] = j;
                            n++;
                        }
                        j++;
                    }
                    var select = selections.Distinct();
                    foreach (int i in select)
                    {
                        //System.Web.UI.WebControls.DropDownList drpUnitId = (System.Web.UI.WebControls.DropDownList)Grid1.Rows[i].FindControl("drpUnitId");
                        System.Web.UI.WebControls.DropDownList drpTeamGroupId = (System.Web.UI.WebControls.DropDownList)Grid1.Rows[i].FindControl("drpTeamGroupId");
                        Model.Welder_ProjectWelder newProjectWelder = new Model.Welder_ProjectWelder();
                        newProjectWelder.ProjectId = projectId;
                        newProjectWelder.WelderId = Grid1.DataKeys[i][0].ToString();
                        //if (drpUnitId.SelectedValue != "0" && !string.IsNullOrEmpty(drpUnitId.SelectedValue))
                        //{
                        //    newProjectWelder.UnitId = drpUnitId.SelectedValue;
                        //}
                        newProjectWelder.UnitId = this.tvUnit.SelectedNodeID;
                        if (drpTeamGroupId.SelectedValue != "0" && !string.IsNullOrEmpty(drpTeamGroupId.SelectedValue))
                        {
                            newProjectWelder.TeamGroupId = drpTeamGroupId.SelectedValue;
                        }
                        newProjectWelder.ProjectWelderId = SQLHelper.GetNewID(typeof(Model.Welder_ProjectWelder));
                        BLL.Welder_ProjectWelderService.AddProjectWelder(newProjectWelder);
                    }

                    BLL.Sys_LogService.AddLog(Const.System_1, this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.HJGL_ProjectWelderMenuId, Const.BtnSave, "");
                    PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
                }
            }
            else
            {
                ShowNotify(Resources.Lan.NoPrivilegePrompt, MessageBoxIcon.Warning);
            }
        }
        #endregion

        #region 排序
        /// <summary>
        /// 排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            Grid1.SortDirection = e.SortDirection;
            Grid1.SortField = e.SortField;
            BindGrid();
        }
        #endregion

        #region Grid行绑定事件
        protected void Grid1_RowDataBound(object sender, GridRowEventArgs e)
        {
            string projectId = Request.Params["projectId"];
            System.Web.UI.WebControls.DropDownList drpTeamGroupId = (System.Web.UI.WebControls.DropDownList)Grid1.Rows[e.RowIndex].FindControl("drpTeamGroupId");
           
            Funs.PleaseSelect(drpTeamGroupId, Resources.Lan.PleaseSelect);
            drpTeamGroupId.Items.AddRange(BLL.Welder_TeamGroupService.GetAllTeamGroupList(projectId, tvUnit.SelectedNodeID));

            var projectWelder = BLL.Welder_ProjectWelderService.GetProjectWelderByProjectIdAndWelderId(projectId, e.RowID);
            if (projectWelder != null)
            {
                if (!string.IsNullOrEmpty(projectWelder.TeamGroupId))
                {
                    drpTeamGroupId.SelectedValue = projectWelder.TeamGroupId;
                }
            }
        }
        #endregion
        
        #region 验证是否选择角色
        /// <summary>
        /// 验证是否选择角色
        /// </summary>
        /// <returns></returns>
        protected string IsSave()
        {
            string coutValue = string.Empty;
            string[] selectRowId = Grid1.SelectedRowIDArray;
            int n = 0;
            int j = 0;
            int[] selections = new int[selectRowId.Count()];
            foreach (GridRow row in Grid1.Rows)
            {
                if (selectRowId.Contains(row.DataKeys[0]))
                {
                    selections[n] = j;
                    n++;
                }
                j++;
            }
            foreach (int i in selections)
            {
                System.Web.UI.WebControls.DropDownList drpTeamGroupId = (System.Web.UI.WebControls.DropDownList)Grid1.Rows[i].FindControl("drpTeamGroupId");
                if (drpTeamGroupId.SelectedValue == BLL.Const._Null || string.IsNullOrEmpty(drpTeamGroupId.SelectedValue))
                {
                    coutValue += Resources.Lan.Line + (i + 1).ToString() + ",";
                }
            }
            return coutValue;
        }
        #endregion
    }
}