using BLL;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using AspNet = System.Web.UI.WebControls;
namespace FineUIPro.Web.TestRun.DriverPrepare
{
    public partial class DutyEdit : PageBase
    {
        #region 加载
        /// 
        /// 页面加载
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string id = Request.Params["id"];
                if (!string.IsNullOrEmpty(id))
                {
                    BindGrid();
                }
                else
                {
                    var list = DriverPrepareDutyItemService.GetLists("Empty");
                    if (list.Count == 0)
                    {
                        DriverPrepareDutyItemService.CreateDutyItems();
                    }
                    BindGrid();
                }
                //Grid1.EnableCheckBoxSelect = true;
                //Grid1.CheckBoxSelectOnly = true;
            }
        }
        #endregion
        #region 数据绑定
        //加载列表
        public void BindGrid()
        {
            string strSql = @"select * from DriverPrepare_DutyItem chec"
                            + @" where chec.DutyId=@DutyId";
            List listStr = new List();
            string id = "Empty";
            if (!string.IsNullOrEmpty(Request.Params["id"]))
            {
                id = Request.Params["id"];
            }
            listStr.Add(new SqlParameter("@DutyId", id));
            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();
        }
        #endregion
        #region 排序
        /// 
        /// 排序
        /// 
        /// 
        /// 
        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            this.BindGrid();
        }
        #endregion
        #region 保存
        /// 
        /// 保存按钮
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string id = Request.Params["id"];
            Model.DriverPrepare_Duty newData = new Model.DriverPrepare_Duty();
            newData.CompileMan = this.CurrUser.UserId;
            newData.CompileDate = DateTime.Now;
            newData.ProjectId = this.CurrUser.LoginProjectId;
            if (!string.IsNullOrEmpty(id))
            {
                newData.DutyId = id;
                BLL.DriverPrepareDutyService.UpdateDriverPrepareDriverPlan(newData);
            }
            else
            {
                newData.DutyId = SQLHelper.GetNewID(typeof(Model.DriverPrepare_Duty));
                BLL.DriverPrepareDutyService.AddDriverPrepareDriverPlan(newData);
            }
            CheckBoxField Item1 = (CheckBoxField)Grid1.FindColumn("Item1");
            CheckBoxField Item2 = (CheckBoxField)Grid1.FindColumn("Item2");
            CheckBoxField Item3 = (CheckBoxField)Grid1.FindColumn("Item3");
            CheckBoxField Item4 = (CheckBoxField)Grid1.FindColumn("Item4");
            CheckBoxField Item5 = (CheckBoxField)Grid1.FindColumn("Item5");
            CheckBoxField Item6 = (CheckBoxField)Grid1.FindColumn("Item6");
            CheckBoxField Item7 = (CheckBoxField)Grid1.FindColumn("Item7");
            CheckBoxField Item8 = (CheckBoxField)Grid1.FindColumn("Item8");
            CheckBoxField Item9 = (CheckBoxField)Grid1.FindColumn("Item9");
            CheckBoxField Item10 = (CheckBoxField)Grid1.FindColumn("Item10");
            CheckBoxField Item11 = (CheckBoxField)Grid1.FindColumn("Item11");
            CheckBoxField Item12 = (CheckBoxField)Grid1.FindColumn("Item12");
            CheckBoxField Item13 = (CheckBoxField)Grid1.FindColumn("Item13");
            CheckBoxField Item14 = (CheckBoxField)Grid1.FindColumn("Item14");
            if (string.IsNullOrEmpty(id))
            {
                id = "Empty";
            }
            var list = DriverPrepareDutyItemService.GetLists(id);
            foreach (JObject mergedRow in Grid1.GetMergedData())
            {
                int i = mergedRow.Value("index");
                var item = list.FirstOrDefault(x => x.DutyItemId == Grid1.Rows[i].RowID);
                JObject values = mergedRow.Value("values");
                if (item != null)
                {
                    item.DutyId = newData.DutyId;
                    item.Item1 = Item1.GetCheckedState(i);
                    item.Item2 = Item2.GetCheckedState(i);
                    item.Item3 = Item3.GetCheckedState(i);
                    item.Item4 = Item4.GetCheckedState(i);
                    item.Item5 = Item5.GetCheckedState(i);
                    item.Item6 = Item6.GetCheckedState(i);
                    item.Item7 = Item7.GetCheckedState(i);
                    item.Item8 = Item8.GetCheckedState(i);
                    item.Item9 = Item9.GetCheckedState(i);
                    item.Item10 = Item10.GetCheckedState(i);
                    item.Item11 = Item11.GetCheckedState(i);
                    item.Item12 = Item12.GetCheckedState(i);
                    item.Item13 = Item13.GetCheckedState(i);
                    item.Item14 = Item14.GetCheckedState(i);
                    item.Remark = values.Value("Remark");
                    BLL.DriverPrepareDutyItemService.UpdateDutyItem(item);
                }
            }
            //int rowsCount = this.Grid1.Rows.Count;
            //foreach (JObject mergedRow in Grid1.GetMergedData())
            //{
            //    int i = mergedRow.Value("index");
            //    var item = list.FirstOrDefault(x => x.DutyItemId == Grid1.Rows[i].RowID);
            //    JObject values = mergedRow.Value("values");
            //    AspNet.CheckBox cbItem1 = (AspNet.CheckBox)(this.Grid1.Rows[i].FindControl("cbItem1"));
            //    AspNet.CheckBox cbItem2 = (AspNet.CheckBox)(this.Grid1.Rows[i].FindControl("cbItem2"));
            //    AspNet.CheckBox cbItem3 = (AspNet.CheckBox)(this.Grid1.Rows[i].FindControl("cbItem3"));
            //    AspNet.CheckBox cbItem4 = (AspNet.CheckBox)(this.Grid1.Rows[i].FindControl("cbItem4"));
            //    AspNet.CheckBox cbItem5 = (AspNet.CheckBox)(this.Grid1.Rows[i].FindControl("cbItem5"));
            //    AspNet.CheckBox cbItem6 = (AspNet.CheckBox)(this.Grid1.Rows[i].FindControl("cbItem6"));
            //    AspNet.CheckBox cbItem7 = (AspNet.CheckBox)(this.Grid1.Rows[i].FindControl("cbItem7"));
            //    AspNet.CheckBox cbItem8 = (AspNet.CheckBox)(this.Grid1.Rows[i].FindControl("cbItem8"));
            //    AspNet.CheckBox cbItem9 = (AspNet.CheckBox)(this.Grid1.Rows[i].FindControl("cbItem9"));
            //    AspNet.CheckBox cbItem10 = (AspNet.CheckBox)(this.Grid1.Rows[i].FindControl("cbItem10"));
            //    AspNet.CheckBox cbItem11 = (AspNet.CheckBox)(this.Grid1.Rows[i].FindControl("cbItem11"));
            //    AspNet.CheckBox cbItem12 = (AspNet.CheckBox)(this.Grid1.Rows[i].FindControl("cbItem12"));
            //    AspNet.CheckBox cbItem13 = (AspNet.CheckBox)(this.Grid1.Rows[i].FindControl("cbItem13"));
            //    AspNet.CheckBox cbItem14 = (AspNet.CheckBox)(this.Grid1.Rows[i].FindControl("cbItem14"));
            //    item.Item1 = cbItem1.Checked;
            //    item.Item2 = cbItem2.Checked;
            //    item.Item3 = cbItem3.Checked;
            //    item.Item4 = cbItem4.Checked;
            //    item.Item5 = cbItem5.Checked;
            //    item.Item6 = cbItem6.Checked;
            //    item.Item7 = cbItem7.Checked;
            //    item.Item8 = cbItem8.Checked;
            //    item.Item9 = cbItem9.Checked;
            //    item.Item10 = cbItem10.Checked;
            //    item.Item11 = cbItem11.Checked;
            //    item.Item12 = cbItem12.Checked;
            //    item.Item13 = cbItem13.Checked;
            //    item.Item14 = cbItem14.Checked;
            //    item.Remark = values.Value("Remark");
            //    BLL.DriverPrepareDutyItemService.UpdateDutyItem(item);
            //}
            ShowNotify("保存成功!", MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion
        protected void Button1_Click(object sender, EventArgs e)
        {
            CheckBoxField field1 = (CheckBoxField)Grid1.FindColumn("CheckBoxField1");
            int selectedCount = Grid1.SelectedRowIndexArray.Length;
            //if (selectedCount > 0)
            //{
            //    for (int i = 0; i < selectedCount; i++)
            //    {
            //        int rowIndex = Grid1.SelectedRowIndexArray[i];
            //        sb.Append("");
            //        sb.AppendFormat("| {0}", rowIndex + 1);
            //        sb.AppendFormat(" | {0}", field1.GetCheckedState(rowIndex));
            //        sb.Append(" | 
");
            //    }
            //    sb.Append("");
            //}
            //else
            //{
            //    sb.Append("没有选中任何一行!");
            //}
        }
    }
}