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

namespace FineUIPro.Web.common.ProjectSet
{
    public partial class SelectUnit : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
            }
        }

        private void BindGrid()
        {
            string strSql = @"select u.UnitId,u.UnitCode,u.UnitName
                              from dbo.Base_Unit u  
                              order by u.UnitCode";

            
            DataTable tb = SQLHelper.GetDataTableRunText(strSql, null);
            //Grid1.Items.Clear();
            Grid1.RecordCount = tb.Rows.Count;
            var table = this.GetPagedDataTable(Grid1, tb);

            Grid1.DataSource = tb;
            Grid1.DataBind();

            string projectId = Request.Params["projectId"];
            var pro = BLL.Project_UnitService.GetProjectUnitList(projectId);
            if (pro.Count() > 0)
            {
                string[] arr = new string[pro.Count()];
                int i = 0;
                foreach (var q in pro)
                {
                    arr[i] = q.UnitId;
                    i++;
                }
                Grid1.SelectedRowIDArray = arr;
            }
        }


        protected void btnEdit_Click(object sender, EventArgs e)
        {
            if (CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.ProjectUnitMenuId, Const.BtnSave))
            {
                string type = string.Empty;
                string coutValue = IsSave();

                if (!string.IsNullOrEmpty(coutValue))
                {
                    ShowNotify(coutValue + Resources.Lan.SelectCompanyType);
                }
                else
                {
                    string projectId = Request.Params["projectId"];
                    BLL.Project_UnitService.DeleteProject_UnitByProjectId(projectId);
                    //int[] selections = Grid1.SelectedRowIndexArray;
                    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)
                    {
                        string unitType = string.Empty;
                        //System.Web.UI.WebControls.DropDownList ddlUnitType = (System.Web.UI.WebControls.DropDownList)Grid1.Rows[i].FindControl("ddlUnitType");
                        System.Web.UI.WebControls.CheckBoxList cblUnitType = (System.Web.UI.WebControls.CheckBoxList)Grid1.Rows[i].FindControl("cblUnitType");
                        Model.Project_Unit newUnit = new Model.Project_Unit();
                        newUnit.ProjectId = projectId;
                        newUnit.UnitId = Grid1.DataKeys[i][0].ToString();
                        if (!string.IsNullOrEmpty(cblUnitType.SelectedValue))
                        {
                            foreach (System.Web.UI.WebControls.ListItem item in cblUnitType.Items)
                            {
                                if (item.Selected)
                                {
                                    unitType += item.Value + ",";
                                }
                            }
                            if (!string.IsNullOrEmpty(unitType))
                            {
                                unitType = unitType.Substring(0, unitType.LastIndexOf(","));
                                newUnit.UnitType = unitType;
                                newUnit.InTime = System.DateTime.Now;
                                BLL.Project_UnitService.AddProject_Unit(newUnit);
                            }
                            
                        }
                    }
                    BLL.Sys_LogService.AddLog(Const.System_1, this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.ProjectUnitMenuId, Const.BtnSave, "");
                    PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
                }
            }
            else
            {
                ShowNotify(Resources.Lan.NoPrivilegePrompt,MessageBoxIcon.Warning);
            }
        }
        #region 页索引改变事件
        /// <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 Grid1_Sort(object sender, GridSortEventArgs e)
        {
            Grid1.SortDirection = e.SortDirection;
            Grid1.SortField = e.SortField;
            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

        protected void Grid1_RowDataBound(object sender, GridRowEventArgs e)
        {
            System.Web.UI.WebControls.CheckBoxList cblUnitType = (System.Web.UI.WebControls.CheckBoxList)Grid1.Rows[e.RowIndex].FindControl("cblUnitType");
            string projectId = Request.Params["projectId"];

            cblUnitType.DataTextField = "UnitTypeName";
            cblUnitType.DataValueField = "UnitTypeId";
            cblUnitType.DataSource = (from x in Funs.DB.Base_UnitType select x).ToList();
            cblUnitType.DataBind();

            var pUnit = BLL.Project_UnitService.GetProject_UnitByProjectIdUnitId(projectId, e.RowID);
            if (pUnit != null)
            {
                // 单位类型下拉框
                if (!string.IsNullOrEmpty(pUnit.UnitType))
                {
                    List<string> unitTypeList = pUnit.UnitType.Split(',').ToList();
                    foreach (var type in unitTypeList)
                    {
                        for (int i = 0; i < cblUnitType.Items.Count; i++)
                        {
                            if (cblUnitType.Items[i].Value == type)
                            {
                                cblUnitType.Items[i].Selected = true;
                            }
                        }
                    }
                }
            }
        }

        #region 验证是否选择单位类型
        /// <summary>
        /// 验证是否选择单位类型
        /// </summary>
        /// <returns></returns>
        protected string IsSave()
        {
            string coutValue = string.Empty;
            //int[] selections = Grid1.SelectedRowIndexArray;//注意超过20行属性用不了
            string[] selectRowId = Grid1.SelectedRowIDArray;
            int i = 0;
            int j = 0;
            int[] selections = new int[selectRowId.Count()];
            foreach (GridRow row in Grid1.Rows)
            {
                if (selectRowId.Contains(row.DataKeys[0]))
                {
                    selections[i] = j;
                    i++;
                }
                j++;
            }

            foreach (int n in selections)
            {
                //System.Web.UI.WebControls.DropDownList drpUnitType = (System.Web.UI.WebControls.DropDownList)Grid1.Rows[n].FindControl("ddlUnitType");

                System.Web.UI.WebControls.CheckBoxList cblUnitType = (System.Web.UI.WebControls.CheckBoxList)Grid1.Rows[n].FindControl("cblUnitType");
                if (string.IsNullOrEmpty(cblUnitType.SelectedValue))
                {
                    coutValue += "第" + (n + 1).ToString() + "行;";
                }
            }
            return coutValue;
        }
        #endregion       

    }
}