using BLL;
using Model;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading.Tasks;
namespace FineUIPro.Web.TestRun.BeforeTestRun
{
    public partial class IDPPreRunData : PageBase
    {
        /// 
        /// 项目id
        /// 
        public string ProjectId
        {
            get
            {
                return (string)ViewState["ProjectId"];
            }
            set
            {
                ViewState["ProjectId"] = value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.ProjectId = this.CurrUser.LoginProjectId;
                Funs.DropDownPageSize(this.ddlPageSize);
                Funs.DropDownPageSize(this.ddlPageSize2);
                Funs.DropDownPageSize(this.ddlPageSize3);
                ddlPageSize.SelectedValue = Grid1.PageSize.ToString();
                ddlPageSize2.SelectedValue = Grid2.PageSize.ToString();
                ddlPageSize3.SelectedValue = Grid3.PageSize.ToString();
                BindPipingGrid();
            }
        }
        /// 
        /// Tab切换
        /// 
        /// 
        /// 
        protected void TabStrip1_TabIndexChanged(object sender, EventArgs e)
        {
            if (TabStrip1.ActiveTabIndex == 1)
            {
                if (this.Grid2.Rows.Count == 0)
                {
                    BindEquipmentGrid();
                }
            }
            else if (TabStrip1.ActiveTabIndex == 2)
            {
                if (this.Grid3.Rows.Count == 0)
                {
                    BindInstrumentationGrid();
                }
            }
        }
        /// 
        /// 从IDP获取数据
        /// 
        protected void btnGetIDPData_Click(object sender, EventArgs e)
        {
            try
            {
                string msg = GetIDPPreRunData(1);
                if (!string.IsNullOrWhiteSpace(msg))
                {
                    if (msg.Contains("成功"))
                    {
                        ShowNotify(msg);
                        BindPipingGrid();
                    }
                    else
                    {
                        ShowNotify(msg, MessageBoxIcon.Error);
                    }
                }
            }
            catch (Exception ex)
            {
                ShowNotify(ex.Message, MessageBoxIcon.Error);
            }
        }
        /// 
        /// 从IDP获取数据
        /// 
        protected void btnGetIDPData2_Click(object sender, EventArgs e)
        {
            try
            {
                string msg = GetIDPPreRunData(2);
                if (!string.IsNullOrWhiteSpace(msg))
                {
                    if (msg.Contains("成功"))
                    {
                        ShowNotify(msg);
                        BindEquipmentGrid();
                    }
                    else
                    {
                        ShowNotify(msg, MessageBoxIcon.Error);
                    }
                }
            }
            catch (Exception ex)
            {
                ShowNotify(ex.Message, MessageBoxIcon.Error);
            }
        }
        #region 管道一览表
        /// 
        /// 行点击事件
        /// 
        protected void PipingGrid_RowCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "Detail")
            {
                string itemId = e.RowID;
                PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("IDPPreRunDataDetail.aspx?Type=1&ItemId={0}", itemId)));
            }
        }
        /// 
        /// 获取IDP试车数据
        /// 
        /// 
        /// 
        private string GetIDPPreRunData(int type)
        {
            string result = string.Empty;
            var session = IDPDataService.GetAuthenticationSession();
            if (type == 1)
            {
                string pipingMsg = IDPDataService.GetIDPProjectPreRunDataPiping(session, this.ProjectId);
                result = pipingMsg;
            }
            else if (type == 2)
            {
                string equipmentMsg = IDPDataService.GetIDPProjectPreRunDataEquipment(session, this.ProjectId);
                result = equipmentMsg;
            }
            else if (type == 3)
            {
                string instrumentationMsg = IDPDataService.GetIDPProjectPreRunDataInstrumentation(session, this.ProjectId);
                result = instrumentationMsg;
            }
            return result;
        }
        /// 
        /// IDP项目管道数据
        /// 
        private void BindPipingGrid()
        {
            string strSql = @"select * from IDP_PreRunData_Piping p where 1=1 ";
            List listStr = new List();
            string cpara = string.Empty;
            if (!string.IsNullOrWhiteSpace(this.ProjectId))
            {
                strSql += " AND p.ProjectId = @projectId";
                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 Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            BindPipingGrid();
        }
        /// 
        /// 排序
        /// 
        /// 
        /// 
        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            BindPipingGrid();
        }
        /// 
        /// 分页显示条数下拉框
        /// 
        /// 
        /// 
        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
            BindPipingGrid();
        }
        #endregion
        #endregion
        #region 设备一览表
        /// 
        /// 行点击事件
        /// 
        protected void EquipmentGrid_RowCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "Detail")
            {
                string itemId = e.RowID;
                PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("IDPPreRunDataDetail.aspx?Type=2&ItemId={0}", itemId)));
            }
        }
        /// 
        /// IDP项目管道数据
        /// 
        private void BindEquipmentGrid()
        {
            string strSql = @"select * from IDP_PreRunData_Equipment p where 1=1 ";
            List listStr = new List();
            string cpara = string.Empty;
            if (!string.IsNullOrWhiteSpace(this.ProjectId))
            {
                strSql += " AND p.ProjectId = @projectId";
                listStr.Add(new SqlParameter("@projectId", this.ProjectId));
            }
            SqlParameter[] parameter = listStr.ToArray();
            DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter);
            Grid2.RecordCount = tb.Rows.Count;
            var table = this.GetPagedDataTable(Grid2, tb);
            Grid2.DataSource = table;
            Grid2.DataBind();
        }
        #region 表排序、分页、关闭窗口
        /// 
        /// 分页
        /// 
        /// 
        /// 
        protected void Grid2_PageIndexChange(object sender, GridPageEventArgs e)
        {
            BindEquipmentGrid();
        }
        /// 
        /// 排序
        /// 
        /// 
        /// 
        protected void Grid2_Sort(object sender, GridSortEventArgs e)
        {
            BindEquipmentGrid();
        }
        /// 
        /// 分页显示条数下拉框
        /// 
        /// 
        /// 
        protected void ddlPageSize2_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid2.PageSize = Convert.ToInt32(ddlPageSize2.SelectedValue);
            BindEquipmentGrid();
        }
        #endregion
        #endregion
     
        #region 仪表索引
        /// 
        /// 行点击事件
        /// 
        protected void InstrumentationGrid_RowCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "Detail")
            {
                string itemId = e.RowID;
                PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("IDPPreRunDataDetail.aspx?Type=3&ItemId={0}", itemId)));
            }
        }
        /// 
        /// 从IDP获取数据
        /// 
        protected void btnGetIDPData3_Click(object sender, EventArgs e)
        {
            try
            {
                string msg = GetIDPPreRunData(3);
                if (!string.IsNullOrWhiteSpace(msg))
                {
                    if (msg.Contains("成功"))
                    {
                        ShowNotify(msg);
                        BindInstrumentationGrid();
                    }
                    else
                    {
                        ShowNotify(msg, MessageBoxIcon.Error);
                    }
                }
            }
            catch (Exception ex)
            {
                ShowNotify(ex.Message, MessageBoxIcon.Error);
            }
        }
        /// 
        /// IDP项目仪表索引数据
        /// 
        private void BindInstrumentationGrid()
        {
            string strSql = @"select * from IDP_PreRunData_Instrumentation p where 1=1 ";
            List listStr = new List();
            string cpara = string.Empty;
            if (!string.IsNullOrWhiteSpace(this.ProjectId))
            {
                strSql += " AND p.ProjectId = @projectId";
                listStr.Add(new SqlParameter("@projectId", this.ProjectId));
            }
            SqlParameter[] parameter = listStr.ToArray();
            DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter);
            Grid3.RecordCount = tb.Rows.Count;
            var table = this.GetPagedDataTable(Grid3, tb);
            Grid3.DataSource = table;
            Grid3.DataBind();
        }
        #region 表排序、分页、关闭窗口
        /// 
        /// 分页
        /// 
        /// 
        /// 
        protected void Grid3_PageIndexChange(object sender, GridPageEventArgs e)
        {
            BindInstrumentationGrid();
        }
        /// 
        /// 排序
        /// 
        /// 
        /// 
        protected void Grid3_Sort(object sender, GridSortEventArgs e)
        {
            BindInstrumentationGrid();
        }
        /// 
        /// 分页显示条数下拉框
        /// 
        /// 
        /// 
        protected void ddlPageSize3_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid3.PageSize = Convert.ToInt32(ddlPageSize3.SelectedValue);
            BindInstrumentationGrid();
        }
        #endregion
        #endregion
    }
}