using BLL;
using Model;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace FineUIPro.Web.TestRun.BeforeTestRun
{
    public partial class SelectIDPInstrumentation : PageBase
    {
        /// 
        /// ProjectId
        /// 
        public string ProjectId
        {
            get { return (string)ViewState["ProjectId"]; }
            set { ViewState["ProjectId"] = value; }
        }
        ///// 
        ///// 子系统id
        ///// 
        //public string SubSystemId
        //{
        //    get { return (string)ViewState["SubSystemId"]; }
        //    set { ViewState["SubSystemId"] = value; }
        //}
        /// 
        /// 系统id
        /// 
        public string SystemId
        {
            get { return (string)ViewState["SystemId"]; }
            set { ViewState["SystemId"] = value; }
        }
        ///// 
        ///// 选中的id
        ///// 
        //public string SelectPropertyIds
        //{
        //    get { return (string)ViewState["SelectPropertyIds"]; }
        //    set { ViewState["SelectPropertyIds"] = value; }
        //}
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.ProjectId = this.CurrUser.LoginProjectId;
                //this.SubSystemId = Request["SubSystemId"];
                this.SystemId = Request["SystemId"];
                //this.SelectPropertyIds = Request["SelectPropertyIds"];
                // 绑定表格
                BindGrid();
            }
        }
        /// 
        /// 绑定数据
        /// 
        private void BindGrid()
        {
            string strSql = @"select * from IDP_PreRunData_Instrumentation where ProjectId=@ProjectId and ItemId not in (select InstrumentId from PreRun_InstrumentSysPiping) ";
            List listStr = new List();
            listStr.Add(new SqlParameter("@ProjectId", this.ProjectId));
            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();
        }
        #region 确认
        /// 
        /// 确认
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Grid1.SelectedRowIndexArray.Length == 0)
            {
                Alert.ShowInTop("请至少选择一条记录!", MessageBoxIcon.Warning);
                return;
            }
            var lastSysPiping = Funs.DB.PreRun_InstrumentSysPiping.Where(x => x.ProjectId == this.ProjectId && x.SystemId == this.SystemId).OrderByDescending(x => x.Sort).FirstOrDefault();
            int sortIndex = lastSysPiping != null ? (int)lastSysPiping.Sort + 1 : 1;
            foreach (int rowIndex in Grid1.SelectedRowIndexArray)
            {
                this.SaveData(Grid1.DataKeys[rowIndex][0].ToString(),  sortIndex);
                sortIndex++;
            }
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        /// 
        /// 保存数据
        /// 
        /// 被选中的仪表索引Id
        /// 周信息
        /// 计划排序
        private void SaveData(string ItemId, int sortIndex)
        {
            Model.SGGLDB db = Funs.DB;
            var item = db.IDP_PreRunData_Instrumentation.FirstOrDefault(x => x.ItemId == ItemId);
            if (item != null)
            {
                Model.PreRun_InstrumentSysPiping model = new Model.PreRun_InstrumentSysPiping();
                model.ProjectId = this.ProjectId;
                model.SystemId = this.SystemId;
                model.InstrumentId = item.ItemId;
                model.InstrumentTag = item.InstrumentTag;
                model.UseTo = item.UseTo;
                model.InstrumentName = item.InstrumentName;
                model.Supplier = item.Supplier;
                model.InstallationPosition = item.InstallationPosition;
                model.SignalType = item.SignalType;
                model.AssociatedSystem = item.AssociatedSystem;
                model.Cp25 = item.Cp25;
                model.In11 = item.In11;
                model.In40 = item.In40;
                model.Remark = item.Remark;
                model.AddUser = this.CurrUser.UserId;
                model.AddTime = DateTime.Now;
                model.Sort = sortIndex;
                db.PreRun_InstrumentSysPiping.InsertOnSubmit(model);
                db.SubmitChanges();
            }
        }
        #endregion
        #region 表排序、分页、关闭窗口
        /// 
        /// 分页
        /// 
        /// 
        /// 
        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }
        /// 
        /// 排序
        /// 
        /// 
        /// 
        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            BindGrid();
        }
        /// 
        /// 分页显示条数下拉框
        /// 
        /// 
        /// 
        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindGrid();
        }
        /// 
        /// 关闭弹出窗
        /// 
        /// 
        /// 
        protected void Window1_Close(object sender, WindowCloseEventArgs e)
        {
            BindGrid();
        }
        /// 
        /// 分页下拉框
        /// 
        protected void ddlPageSize_SelectedIndexChanged1(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
            BindGrid();
        }
        #endregion
    }
}