using BLL;
using BLL.ZHGL.Question;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
namespace FineUIPro.Web.ZHGL.Question
{
    public partial class QuestionDBItemSave : PageBase
    {
        #region 定义变量
        /// 
        /// 主键
        /// 
        public string QuestionDBItemId
        {
            get
            {
                return (string)ViewState["QuestionDBItemId"];
            }
            set
            {
                ViewState["QuestionDBItemId"] = value;
            }
        }
        /// 
        /// 主表主键
        /// 
        public string QuestionDBId
        {
            get
            {
                return (string)ViewState["QuestionDBId"];
            }
            set
            {
                ViewState["QuestionDBId"] = value;
            }
        }
        /// 
        /// 附件
        /// 
        private string AttachUrl
        {
            get
            {
                return (string)ViewState["AttachUrl"];
            }
            set
            {
                ViewState["AttachUrl"] = value;
            }
        }
        /// 
        /// 单选题分值
        /// 
        private int SValue
        {
            get
            {
                return (int)ViewState["SValue"];
            }
            set
            {
                ViewState["SValue"] = value;
            }
        }
        /// 
        /// 多选题分值
        /// 
        private int MValue
        {
            get
            {
                return (int)ViewState["MValue"];
            }
            set
            {
                ViewState["MValue"] = value;
            }
        } 
        /// 
        /// 判断题分值
        /// 
        private int JValue
        {
            get
            {
                return (int)ViewState["JValue"];
            }
            set
            {
                ViewState["JValue"] = value;
            }
        }
        #endregion
        #region 加载页面
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SValue = 1;
                MValue = 2;
                JValue = 1;
                var testRule = Funs.DB.Sys_TestRule.FirstOrDefault();
                if (testRule != null)
                {
                    SValue = testRule.SValue ;
                    MValue = testRule.MValue;
                    JValue = testRule.JValue;
                }
                this.GetButtonPower();
                LoadData();
                BindEnumrableToDropDownList();
                this.QuestionDBItemId = Request.QueryString["QuestionDBItemId"];
                this.QuestionDBId = Request.QueryString["QuestionDBId"];
                if (!String.IsNullOrEmpty(this.QuestionDBItemId))
                {
                    var q =  QuestionService.getQuestionDBItemById(this.QuestionDBItemId);
                    if (q != null)
                    {
                        txtTrainingItemCode.Text = q.QuestionDBItemCode;
                        if (!string.IsNullOrEmpty(q.TestType))
                        {
                            this.rblTestType.SelectedValue = q.TestType;
                            if (q.TestType == "1")
                            {
                               // this.lbScore.Text = SValue.ToString();
                                this.trE.Hidden = true;
                            }
                            else if (q.TestType == "2")
                            {
                                //this.lbScore.Text = MValue.ToString();
                            }
                            else if (q.TestType == "3")
                            {
                                //this.lbScore.Text = JValue.ToString();
                                this.trC.Hidden = true;
                                this.trD.Hidden = true;
                                this.trE.Hidden = true;
                            }
                            System.Web.UI.WebControls.ListItem[] myList2 = BLL.TestTrainingItemService.GetAnswerItemsList(this.rblTestType.SelectedValue);
                            RadioButtonList2.DataTextField = "Text";
                            RadioButtonList2.DataValueField = "Value";
                            RadioButtonList2.DataSource = myList2;
                            RadioButtonList2.DataBind();
                            if (!string.IsNullOrEmpty(q.AnswerItems))
                            {
                                string[] ids2 = q.AnswerItems.Split(',');
                                DropDownBox2.Values = ids2;
                            }
                        }
                        txtAbstracts.Text = q.Abstracts;
                    
                        this.txtAItem.Text = q.AItem;
                        this.txtBItem.Text = q.BItem;
                        this.txtCItem.Text = q.CItem;
                        this.txtDItem.Text = q.DItem;
                        this.txtEItem.Text = q.EItem;
                        this.AttachUrl = q.AttachUrl;
                        if (!string.IsNullOrEmpty(this.AttachUrl))
                        {
                            this.trImageUrl.Visible = true;
                            this.divFile.InnerHtml = BLL.UploadAttachmentService.ShowAttachment("../../", this.AttachUrl);
                            this.divBeImageUrl.InnerHtml = BLL.UploadAttachmentService.ShowImage("../../", this.AttachUrl);
                        }
                    }
                }
                else
                {
                    this.txtScore.Text = SValue.ToString();
                    this.trE.Hidden = true;
                }
            }
        }
        /// 
        /// 删除附件
        /// 
        /// 
        /// 
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            this.trImageUrl.Visible = false;
            this.AttachUrl = string.Empty;
            this.divFile.InnerHtml = string.Empty;
            this.divBeImageUrl.InnerHtml = string.Empty; ;
        }
        private void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
        }
        private void BindEnumrableToDropDownList()
        {
         
            System.Web.UI.WebControls.ListItem[] myList2 = BLL.TestTrainingItemService.GetAnswerItemsList(this.rblTestType.SelectedValue);
            RadioButtonList2.DataTextField = "Text";
            RadioButtonList2.DataValueField = "Value";
            RadioButtonList2.DataSource = myList2;
            RadioButtonList2.DataBind();
        }
        #endregion
        #region 保存
        /// 
        /// 保存数据
        /// 
        private void SaveData(bool isClosed)
        {
            Model.Question_QuestionDBItem trainingItem = new Question_QuestionDBItem
            {
                QuestionDBItemCode = txtTrainingItemCode.Text.Trim(),
                Abstracts = txtAbstracts.Text.Trim(),
                TestType = this.rblTestType.SelectedValue,
                AItem = txtAItem.Text.Trim(),
                BItem = txtBItem.Text.Trim(),
                CItem = txtCItem.Text.Trim(),
                DItem = txtDItem.Text.Trim(),
                EItem = txtEItem.Text.Trim(),
                AttachUrl = this.AttachUrl,
              
            };
          
            if (!string.IsNullOrEmpty(DropDownBox2.Text))
            {
                trainingItem.AnswerItems = String.Join(",", DropDownBox2.Values);
            }
            else
            {
                trainingItem.AnswerItems = string.Empty;
            }
            if (!string.IsNullOrEmpty(txtScore.Text))
            {
                trainingItem.Score = decimal.Parse(txtScore.Text);
            }
            if (String.IsNullOrEmpty(QuestionDBItemId))
            {
                trainingItem.QuestionDBItemId = SQLHelper.GetNewID(typeof(Model.Question_QuestionDBItem));
                trainingItem.QuestionDBId = this.QuestionDBId;
                this.QuestionDBItemId = trainingItem.QuestionDBItemId;
                QuestionService.AddQuestionDBItem(trainingItem);
                BLL.LogService.AddSys_Log(this.CurrUser, trainingItem.QuestionDBItemId, trainingItem.QuestionDBId, BLL.Const.TestTrainingMenuId, BLL.Const.BtnAdd);
            }
            else
            {
                Model.Question_QuestionDBItem t = QuestionService.getQuestionDBItemById(QuestionDBItemId);
                trainingItem.QuestionDBItemId = QuestionDBItemId;
                if (t != null)
                {
                    trainingItem.QuestionDBId = t.QuestionDBId;
                }
                QuestionService.UpdateQuestionDBItem(trainingItem);
                BLL.LogService.AddSys_Log(this.CurrUser, trainingItem.QuestionDBItemCode, trainingItem.QuestionDBItemId, BLL.Const.TestTrainingMenuId, BLL.Const.BtnModify);
            }
            if (isClosed)
            {
                PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            SaveData(true);
        }
        #endregion
        #region 附件上传
        /// 
        /// 附件上传
        /// 
        /// 
        /// 
        protected void btnFile_Click(object sender, EventArgs e)
        {
            if (btnFile.HasFile)
            {
                this.AttachUrl = BLL.UploadFileService.UploadAttachment(BLL.Funs.RootPath, this.btnFile, this.AttachUrl, UploadFileService.TrainingFilePath);
                if (!string.IsNullOrEmpty(this.AttachUrl))
                {
                    this.trImageUrl.Visible = true;
                    this.divFile.InnerHtml = BLL.UploadAttachmentService.ShowAttachment("../../", this.AttachUrl);
                    this.divBeImageUrl.InnerHtml = BLL.UploadAttachmentService.ShowImage("../../", this.AttachUrl);
                }
            }
        }
        #endregion
        #region 按钮权限
        /// 
        /// 获取按钮权限
        /// 
        /// 
        /// 
        private void GetButtonPower()
        {
            var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.QuestionDBMenuId);
            if (buttonList.Count() > 0)
            {
                if (buttonList.Contains(BLL.Const.BtnSave))
                {
                    this.btnSave.Hidden = false;
                }
            }
        }
        #endregion
        #region 验证教材名称是否存在
        /// 
        /// 验证教材名称是否存在
        /// 
        /// 
        /// 
        protected void TextBox_TextChanged(object sender, EventArgs e)
        {
            //var q = Funs.DB.Training_TrainingItem.FirstOrDefault(x => x.IsPass == true && x.TrainingId == this.TrainingId && x.TrainingItemName == this.txtTrainingItemName.Text.Trim() && (x.TrainingItemId != this.TrainingItemId || (this.TrainingItemId == null && x.TrainingItemId != null)));
            //if (q != null)
            //{
            //    ShowNotify("输入的教材名称已存在!", MessageBoxIcon.Warning);
            //}
        }
        #endregion
        /// 
        /// 题型变换事件
        /// 
        /// 
        /// 
        protected void rblTestType_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.trC.Hidden = false;
            this.trD.Hidden = false;
            this.trE.Hidden = false;
            this.txtAItem.Text = string.Empty;
            this.txtBItem.Text = string.Empty;
            this.txtCItem.Text = string.Empty;
            this.txtDItem.Text = string.Empty;
            this.txtEItem.Text = string.Empty;
            string testType = this.rblTestType.SelectedValue;
            System.Web.UI.WebControls.ListItem[] myList2 = BLL.TestTrainingItemService.GetAnswerItemsList(testType);
            RadioButtonList2.DataTextField = "Text";
            RadioButtonList2.DataValueField = "Value";
            RadioButtonList2.DataSource = myList2;
            RadioButtonList2.DataBind();
            DropDownBox2.Text = string.Empty;
            DropDownBox2.Values = null;
            if (testType == "1")
            {
                this.txtScore.Text = SValue.ToString();
                this.trE.Hidden = true;
            }
            else if (testType == "2")
            {
                this.txtScore.Text = MValue.ToString();
            }
            else if (testType == "3")
            {
                this.txtScore.Text = JValue.ToString();
                this.trC.Hidden = true;
                this.trD.Hidden = true;
                this.trE.Hidden = true;
                this.txtAItem.Text = "对";
                this.txtBItem.Text = "错";
            }
        }
    }
}