using BLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
namespace FineUIPro.Web.TestRun.DriverPrepare
{
    public partial class SchemePlanItemSelect : PageBase
    {
        #region 加载页面
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.hdType.Text = this.rblType2.SelectedValue;
                this.BindGrid();
            }
        }
        #endregion
        protected void TextBox_TextChanged(object sender, EventArgs e)
        {
            if (this.rblType.SelectedValue == "合成氨尿素项目")
            {
                this.Toolbar2.Hidden = false;
            }
            else
            {
                this.Toolbar2.Hidden = true;
            }
            this.BindGrid();
            this.hdType.Text = this.rblType2.SelectedValue;
        }
        #region 数据绑定
        /// 
        /// 数据绑定
        /// 
        private void BindGrid()
        {
            string[] selectRowId = Grid1.SelectedRowIDArray;
            foreach (GridRow row in Grid1.Rows)
            {
                if (selectRowId.Contains(row.RowID))
                {
                    if (this.rblType.SelectedValue == "合成氨尿素项目")
                    {
                        hdNames.Text += row.DataKeys[1].ToString() + "|" + this.hdType.Text + ",";
                    }
                    else
                    {
                        hdNames.Text += row.DataKeys[1].ToString() + ",";
                    }
                }
            }
            string strSql = @"SELECT * 
                               FROM dbo.DriverPrepare_SchemePlanItem 
                               WHERE SchemePlanId=@SchemePlanId";
            List listStr = new List();
            if (this.rblType.SelectedValue == "合成氨尿素项目")
            {
                listStr.Add(new SqlParameter("@SchemePlanId", this.rblType2.SelectedValue));
            }
            else
            {
                listStr.Add(new SqlParameter("@SchemePlanId", this.rblType.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();
            Model.DriverPrepare_SchemePlan plan = BLL.DriverPrepareSchemePlanService.GetDriverPlanByProjectId(this.CurrUser.LoginProjectId);
            if (plan != null)
            {
                var items = BLL.DriverPrepareSchemePlanItemService.GetSchemePlanItemByschemePlanId(plan.SchemePlanId);
                List nameLists = items.Select(x => x.SolutionName).ToList();
                List ids = new List();
                foreach (GridRow row in Grid1.Rows)
                {
                    if (nameLists.Contains(row.DataKeys[1].ToString()))
                    {
                        ids.Add(row.RowID);
                    }
                }
                this.Grid1.SelectedRowIDArray = ids.ToArray();
            }
        }
        #endregion
        #region 排序
        /// 
        /// 排序
        /// 
        /// 
        /// 
        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            this.BindGrid();
        }
        #endregion
        #region 确定按钮
        /// 
        /// 确定按钮
        /// 
        /// 
        /// 
        protected void btnAccept_Click(object sender, EventArgs e)
        {
            string schemePlanId = Request.Params["SchemePlanId"];
            Model.DriverPrepare_SchemePlan newData = new Model.DriverPrepare_SchemePlan();
            newData.SchemePlanCode = "001";
            newData.SchemePlanName = "开车方案编制计划";
            newData.CompileMan = this.CurrUser.UserId;
            newData.CompileDate = DateTime.Now;
            newData.ProjectId = this.CurrUser.LoginProjectId;
            if (!string.IsNullOrEmpty(schemePlanId))
            {
                newData.SchemePlanId = schemePlanId;
                BLL.DriverPrepareSchemePlanService.UpdateDriverPrepareDriverPlan(newData);
            }
            else
            {
                newData.SchemePlanId = SQLHelper.GetNewID(typeof(Model.DriverPrepare_SchemePlan));
                BLL.DriverPrepareSchemePlanService.AddDriverPrepareDriverPlan(newData);
            }
            var items = BLL.DriverPrepareSchemePlanItemService.GetSchemePlanItemByschemePlanId(newData.SchemePlanId);
            List nameLists = items.Select(x => x.SolutionName).ToList();
            string[] selectRowId = Grid1.SelectedRowIDArray;
            foreach (GridRow row in Grid1.Rows)
            {
                if (selectRowId.Contains(row.RowID) && !nameLists.Contains(row.DataKeys[1].ToString()) && !hdNames.Text.Contains(row.DataKeys[1].ToString()))
                {
                    Model.DriverPrepare_SchemePlanItem newItem = new Model.DriverPrepare_SchemePlanItem();
                    newItem.SchemePlanItemId = SQLHelper.GetNewID();
                    newItem.SchemePlanId = newData.SchemePlanId;
                    newItem.SolutionName = row.DataKeys[1].ToString();
                    newItem.SortIndex = row.RowIndex + 1;
                    if (this.rblType.SelectedValue == "合成氨尿素项目")
                    {
                        newItem.SolutionType = this.rblType2.SelectedValue;
                    }
                    BLL.DriverPrepareSchemePlanItemService.AddSchemePlanItem(newItem);
                }
            }
            if (!string.IsNullOrEmpty(hdNames.Text))
            {
                string[] names = hdNames.Text.Split(',');
                foreach (var item in names)
                {
                    if (!string.IsNullOrEmpty(item) && !nameLists.Contains(item))
                    {
                        Model.DriverPrepare_SchemePlanItem newItem = new Model.DriverPrepare_SchemePlanItem();
                        newItem.SchemePlanItemId = SQLHelper.GetNewID();
                        newItem.SchemePlanId = newData.SchemePlanId;
                        if (this.rblType.SelectedValue == "合成氨尿素项目")
                        {
                            newItem.SolutionName = item.Split('|')[0];
                            newItem.SolutionType = item.Split('|')[1];
                        }
                        else
                        {
                            newItem.SolutionName = item;
                        }
                        Random ra = new Random();
                        newItem.SortIndex = ra.Next(20, 200);
                        BLL.DriverPrepareSchemePlanItemService.AddSchemePlanItem(newItem);
                    }
                }
            }
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion
    }
}