using Aspose.Words;
using BLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
namespace FineUIPro.Web.HSSE.EduTrain
{
    public partial class TestRecord : PageBase
    {
        #region 加载页面
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Funs.DropDownPageSize(this.ddlPageSize);
                ddlPageSize.SelectedValue = Grid1.PageSize.ToString();
                GetButtonPower();
                var sysTestRule = Funs.DB.Sys_TestRule.FirstOrDefault();
                if (sysTestRule != null)
                {
                    if (sysTestRule.PassingScore > 0)
                    {
                        this.lbPassScore.Text = "红色表示分数低于" + sysTestRule.PassingScore.ToString() + "分。";
                    }
                    else
                    {
                        this.lbPassScore.Text = "红色表示分数低于80分。";
                    }
                }
                else
                {
                    this.lbPassScore.Text = "红色表示分数低于80分。";
                    
                }
                ///更新没有结束时间且超时的考试记录
                GetDataService.UpdateTestPlanStates();
                // 绑定表格
                BindGrid();
            }
            else
            {
                if (GetRequestEventArgument() == "reloadGrid")
                {
                    BindGrid();
                }
            }
        }
        #endregion
        #region 绑定数据
        /// 
        /// 绑定数据
        /// 
        private void BindGrid()
        {
            string strSql = @"SELECT TestRecord.TestRecordId,TestRecord.TestPlanId, TestRecord.TestManId,TestRecord.TestStartTime,TestRecord.TestEndTime, TestRecord.TestScores,
                            (CASE WHEN TestPlan.PlanName IS NULL THEN Training.TrainingName ELSE TestPlan.PlanName END) AS PlanName, 
                            ISNULL(TestPlan.Duration,90) AS Duration,ISNULL(TestPlan.TotalScore,100) AS TotalScore,TestPlan.TestPalce,ISNULL(TestPlan.QuestionCount,95) AS QuestionCount,TestRecord.TemporaryUser,Person.PersonName AS TestManName
                            ,Unit.UnitName"
                         + @" FROM dbo.Training_TestRecord AS TestRecord"
                         + @" LEFT JOIN dbo.Training_TestPlan AS TestPlan ON TestPlan.TestPlanId=TestRecord.TestPlanId"
                         + @" LEFT JOIN dbo.Training_TestTraining AS Training ON Training.TrainingId = TestRecord.TestType"
                         + @" LEFT JOIN dbo.SitePerson_Person AS Person ON Person.PersonId = TestRecord.TestManId "
                         + @" LEFT JOIN dbo.Base_Unit AS Unit ON Person.UnitId=Unit.UnitId"
                         + @" WHERE (isFiled IS NULL OR isFiled = 0) and TestRecord.ProjectId = '" + this.CurrUser.LoginProjectId+"'";
            List listStr = new List();
            if (!string.IsNullOrEmpty(this.txtName.Text.Trim()))
            {
                strSql += " AND (Person.PersonName LIKE @name OR TestPlan.PlanName LIKE @name OR Training.TrainingName LIKE @name)";
                listStr.Add(new SqlParameter("@name", "%" + this.txtName.Text.Trim() + "%"));
            }
            if (!string.IsNullOrEmpty(this.txtMinScores.Text.Trim()))
            {
                strSql += " AND TestRecord.TestScores >= @MinScores";
                listStr.Add(new SqlParameter("@MinScores", Funs.GetNewDecimalOrZero(this.txtMinScores.Text.Trim())));
            }
            if (!string.IsNullOrEmpty(this.txtMaxScores.Text.Trim()))
            {
                strSql += " AND TestRecord.TestScores <= @MaxScores";
                listStr.Add(new SqlParameter("@MaxScores", Funs.GetNewDecimalOrZero(this.txtMaxScores.Text.Trim())));
            }
            //if (this.IsTemp.Checked)
            //{
            //    strSql += " AND Users.IsTemp = 1 ";
            //}
            //else
            //{
            //    strSql += " AND (Users.IsTemp = 0 OR Users.IsTemp IS NULL)";
            //}
            if (this.ckIsNULL.Checked)
            {
                strSql += " AND (TestRecord.TestStartTime IS NULL OR TestRecord.TestEndTime IS NULL) ";
            }
            if (!string.IsNullOrEmpty(this.txtStartDate.Text))
            {
                strSql += " AND TestRecord.TestStartTime >= @StartDate";
                listStr.Add(new SqlParameter("@StartDate", Funs.GetNewDateTime(this.txtStartDate.Text)));
            }
            if (!string.IsNullOrEmpty(this.txtEndDate.Text))
            {
                strSql += " AND TestRecord.TestEndTime <= @EndDate ";
                listStr.Add(new SqlParameter("@EndDate", Funs.GetNewDateTime(this.txtEndDate.Text)));
            }
            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();
            for (int i = 0; i < Grid1.Rows.Count; i++)
            {
                string testRecordId = Grid1.Rows[i].DataKeys[0].ToString();
                var testRecord = BLL.TestRecordService.GetTestRecordById(testRecordId);
                if (testRecord != null)
                {                   
                    if (testRecord.TestScores < SysConstSetService.getPassScore())
                    {
                        Grid1.Rows[i].RowCssClass = "Red";
                    }
                }
            }
        }
        #endregion
        #region 分页、关闭窗口
        /// 
        /// 分页
        /// 
        /// 
        /// 
        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }
        /// 
        /// 分页显示条数下拉框
        /// 
        /// 
        /// 
        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
            BindGrid();
        }
        /// 
        /// 排序
        /// 
        /// 
        /// 
        protected void Grid1_Sort(object sender, FineUIPro.GridSortEventArgs e)
        {
            BindGrid();
        }
        #endregion
        #region 编辑
        /// 
        /// Grid行双击事件
        /// 
        /// 
        /// 
        protected void Grid1_RowDoubleClick(object sender, GridRowClickEventArgs e)
        {
            this.EditData();
        }
        /// 
        /// 右键编辑事件
        /// 
        /// 
        /// 
        protected void btnMenuView_Click(object sender, EventArgs e)
        {
            this.EditData();
        }
        /// 
        /// 编辑数据方法
        /// 
        private void EditData()
        {
            if (Grid1.SelectedRowIndexArray.Length == 0)
            {
                Alert.ShowInTop("请选择一条记录!", MessageBoxIcon.Warning);
                return;
            }
            PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("TestRecordItem.aspx?TestRecordId={0}", Grid1.SelectedRowID, "编辑 - ")));
        }
        #endregion
        #region 归档
        /// 
        /// 右键编辑事件
        /// 
        /// 
        /// 
        //protected void btnMenuFile_Click(object sender, EventArgs e)
        //{
        //    if (Grid1.SelectedRowIndexArray.Length > 0)
        //    {
        //        string values = string.Empty;
        //        foreach (int rowIndex in Grid1.SelectedRowIndexArray)
        //        {
        //            string rowID = Grid1.DataKeys[rowIndex][0].ToString();
        //            values += rowID + "|";
        //        }
        //        if (!string.IsNullOrEmpty(values) && values.Length <= 1850)
        //        {
        //            PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("~/InformationProject/FileCabinetAChange.aspx?values={0}&menuId={1}", values, BLL.Const.ProjectTestRecordMenuId, "查看 - "), "归档", 600, 540));
        //        }
        //        else
        //        {
        //            Alert.ShowInTop("请一次至少一条,最多50条记录归档!", MessageBoxIcon.Warning);
        //        }
        //        BindGrid();
        //    }
        //}
        #endregion
        #region 查询事件
        /// 
        /// 查询
        /// 
        /// 
        /// 
        protected void TextBox_TextChanged(object sender, EventArgs e)
        {
            this.BindGrid();
        }
        protected void IsTemp_CheckedChanged(object sender, CheckedEventArgs e)
        {
            this.BindGrid();
        }
        #endregion
        #region 删除
        /// 
        /// 右键删除事件
        /// 
        /// 
        /// 
        protected void btnMenuDelete_Click(object sender, EventArgs e)
        {
            this.DeleteData();
        }
        /// 
        /// 删除方法
        /// 
        private void DeleteData()
        {
            if (Grid1.SelectedRowIndexArray.Length > 0)
            {
                foreach (int rowIndex in Grid1.SelectedRowIndexArray)
                {
                    string rowID = Grid1.DataKeys[rowIndex][0].ToString();
                    var getV = BLL.TestRecordService.GetTestRecordById(rowID);
                    if (getV != null)
                    {
                        BLL.LogService.AddSys_Log(this.CurrUser, "考试记录", rowID, BLL.Const.ProjectTestRecordMenuId, BLL.Const.BtnDelete);
                        BLL.TestRecordService.DeleteTestRecordByTestRecordId(rowID);
                    }
                }
                BindGrid();
                ShowNotify("删除数据成功!");
            }
        }
        #endregion
        #region 导出按钮
        /// 
        /// 导出事件
        /// 
        /// 
        /// 
        protected void btnMenuOut_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 = Grid1.RecordCount;
            BindGrid();
            Response.Write(GetGridTableHtml(Grid1));
            Response.End();
        }
        #endregion
        //protected void btnPrint_Click(object sender, EventArgs e)
        //{
        //    if (Grid1.SelectedRowIndexArray.Length == 0)
        //    {
        //        Alert.ShowInTop("请选择一条记录!", MessageBoxIcon.Warning);
        //        return;
        //    }
        //    PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("TestRecordPrint.aspx?TestRecordId={0}", Grid1.SelectedRowID, "编辑 - "), "考试试卷", 900, 650));
        //}
        protected void btnPrint_Click(object sender, EventArgs e)
        {
            if (Grid1.SelectedRowIndexArray.Length == 0)
            {
                Alert.ShowInTop("请至少选择一条记录!", MessageBoxIcon.Warning);
                return;
            }
            PrinterDocService.PrinterDocMethod(Const.ProjectTestRecordMenuId, Grid1.SelectedRowID, "试卷");
        }
        #region 按钮权限
        /// 
        /// 获取按钮权限
        /// 
        /// 
        /// 
        private void GetButtonPower()
        {
            var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.HSSETestRecordMenuId);
            if (buttonList.Count > 0)
            {
                 
                if (buttonList.Contains(BLL.Const.BtnDelete))
                {
                    this.btnMenuDelete.Hidden = false;
                }
                 
            }
        }
        #endregion
    }
}