using BLL;
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 SelectInstrument : PageBase
    {
        /// 
        /// 子系统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 SelectInstrumentIds
        {
            get { return (string)ViewState["SelectInstrumentIds"]; }
            set { ViewState["SelectInstrumentIds"] = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.SubSystemId = Request["SubSystemId"];
                this.SystemId = Request["SystemId"];
                this.SelectInstrumentIds = Request["SelectInstrumentIds"];
                // 绑定表格
                BindGrid();
            }
        }
        /// 
        /// 绑定数据
        /// 
        private void BindGrid()
        {
            //string strSql = @"select * from PreRun_InstrumentSysPiping where ProjectId=@ProjectId and SystemId=@SystemId ";
            string strSql = @"select * from PreRun_InstrumentSysPiping where ProjectId=@ProjectId and SystemId=@SystemId and InstrumentId not in (select InstrumentId from PreRun_SubInstrumentSelect) ";
            List listStr = new List();
            listStr.Add(new SqlParameter("@ProjectId", this.CurrUser.LoginProjectId));
            listStr.Add(new SqlParameter("@SystemId", this.SystemId));
            //if (!string.IsNullOrWhiteSpace(SelectInstrumentIds))
            //{
            //    var ids = SelectInstrumentIds.Split(',').ToList();
            //    strSql += $" and InstrumentId not in ('{string.Join("','", ids)}')";
            //}
            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)
        {
            var requestIds = SelectInstrumentIds.Split(',').ToList();
            string ids = SelectInstrumentIds;
            foreach (var item in this.Grid1.SelectedRowIDArray)
            {
                if (requestIds.Contains(item)) continue;
                if (string.IsNullOrWhiteSpace(ids))
                {
                    ids = item;
                }
                else
                {
                    ids += "," + item;
                }
            }
            PageContext.RegisterStartupScript(ActiveWindow.GetWriteBackValueReference(ids) + ActiveWindow.GetHidePostBackReference());
        }
        #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
    }
}