using BLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using AspNet = System.Web.UI.WebControls;
namespace FineUIPro.Web.HSSE.Environmental
{
    public partial class EnvironmentalMonitoring : 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;
                if (!string.IsNullOrEmpty(Request.Params["projectId"]) && Request.Params["projectId"] != this.CurrUser.LoginProjectId)
                {
                    this.ProjectId = Request.Params["projectId"];
                }
                this.ucTree.UnitId = this.CurrUser.UnitId;
                this.ucTree.ProjectId = this.ProjectId;
                this.ddlPageSize.SelectedValue = this.Grid1.PageSize.ToString();
                txtStartTime.SelectedDate = DateTime.Now;
                txtLogTime.SelectedDate = DateTime.Now;
                // 绑定表格
                this.BindGrid();
                if (!string.IsNullOrEmpty(this.CurrUser.LoginProjectId))
                {
                    this.panelLeftRegion.Hidden = true;
                }
            }
        }
        protected void changeTree(object sender, EventArgs e)
        {
            this.ProjectId = this.ucTree.ProjectId;
            this.BindGrid();
        }
        #region 绑定数据
        /// 
        /// 绑定数据
        /// 
        private void BindGrid()
        {
            if (!string.IsNullOrEmpty(ProjectId))
            {
                Model.EnvironmentalCheck table = new Model.EnvironmentalCheck();
                var q = (from x in Funs.DB.EnvironmentalCheck
                         where x.Time.Value.Date >= txtStartTime.SelectedDate.Value && x.Time.Value.Date <= txtLogTime.SelectedDate.Value && x.ProjectId == this.ProjectId
                         select
                         x).ToList();
                Grid1.RecordCount = q.Count;
                q = q.OrderByDescending(x => x.Time).Skip(Grid1.PageSize * (Grid1.PageIndex)).Take(Grid1.PageSize).ToList();
                Grid1.DataSource = q;
                Grid1.DataBind();
            }
        }
        #endregion
        #region GV 数据操作
        /// 
        /// 分页
        /// 
        /// 
        /// 
        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            this.Grid1.PageIndex = e.NewPageIndex;
            this.BindGrid();
        }
        /// 
        /// 排序
        /// 
        /// 
        /// 
        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            this.Grid1.SortDirection = e.SortDirection;
            this.Grid1.SortField = e.SortField;
            this.BindGrid();
        }
        /// 
        /// 分页显示条数下拉框
        /// 
        /// 
        /// 
        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.Grid1.PageSize = Convert.ToInt32(this.ddlPageSize.SelectedValue);
            this.BindGrid();
        }
        protected void txtLogTime_TextChanged(object sender, EventArgs e)
        {
            BindGrid();
        }
        #endregion
        #region 数据编辑事件
        /// 
        /// 批量删除
        /// 
        /// 
        /// 
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (Grid1.SelectedRowIndexArray.Length > 0)
            {
                foreach (int rowIndex in Grid1.SelectedRowIndexArray)
                {
                    string rowID = Grid1.DataKeys[rowIndex][0].ToString();
                    Model.EnvironmentalCheck table = Funs.DB.EnvironmentalCheck.FirstOrDefault(x => x.Id == rowID);
                    if (table != null)
                    {
                        Funs.DB.EnvironmentalCheck.DeleteOnSubmit(table);
                        Funs.DB.SubmitChanges();
                    }
                }
            }
            BindGrid();
            ShowNotify("删除数据成功!", MessageBoxIcon.Success);
        }
        #endregion
        #region 导出按钮
        /// 导出按钮
        /// 
        /// 
        /// 
        protected void btnOut_Click(object sender, EventArgs e)
        {
            Response.ClearContent();
            string filename = Funs.GetNewFileName();
            Response.AddHeader("content-disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode("环境监测" + filename, System.Text.Encoding.UTF8) + ".xls");
            Response.ContentType = "application/excel";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            this.Grid1.PageSize = this.Grid1.RecordCount;
            this.BindGrid();
            Response.Write(GetGridTableHtml(Grid1));
            Response.End();
        }
        #endregion
        protected void btnApiView_Click(object sender, EventArgs e)
        { 
            PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("EnvironmentalMonitoringApiView.aspx?Projectid={0}", this.ProjectId, "查看 - ")));
        }
    }
}