using BLL;
using Model;
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 SubThreeChecksFourDecisionEdit : PageBase
    {
        /// 
        /// 主键
        /// 
        public string DecisionId
        {
            get { return (string)ViewState["DecisionId"]; }
            set { ViewState["DecisionId"] = value; }
        }
        /// 
        /// 子系统主键
        /// 
        public string SubSystemId
        {
            get { return (string)ViewState["SubSystemId"]; }
            set { ViewState["SubSystemId"] = value; }
        }
        /// 
        /// 是否查看
        /// 
        public bool IsView
        {
            get { return (bool)ViewState["IsView"]; }
            set { ViewState["IsView"] = value; }
        }
        /// 
        /// 问题图片是否可编辑
        /// 
        public int QuestionEditImg
        {
            get { return (int)ViewState["QuestionEditImg"]; }
            set { ViewState["QuestionEditImg"] = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.DecisionId = Request["DecisionId"];
                this.SubSystemId = Request["SubSystemId"];
                //是否查看
                this.IsView = string.IsNullOrWhiteSpace(Request["IsView"]) ? false : bool.Parse(Request["IsView"]);
                //数据绑定
                PageInit();
                //确认情况绑定
                gvDataBrid();
                //判断是否查看
                if (IsView)
                {
                    btnSave.Hidden = true;
                    btnSubmit.Hidden = true;
                    QuestionEditImg = -1;
                    ddlResponsibilityUnit.Enabled = false;
                    txtQuestionDesc.Enabled = false;
                    txtRestrictCondition.Enabled = false;
                    txtLevel.Enabled = false;
                    txtSpeciality.Enabled = false;
                    txtAskDestructionTime.Enabled = false;
                    lblRealityDestructionTime.Enabled = false;
                    ddlResponsibilityUser.Enabled = false;
                    ddlProposeUser.Enabled = false;
                    ddlGeneraUser.Enabled = false;
                    ddlSupervisionUser.Enabled = false;
                    ddlOwnerUser.Enabled = false;
                    lblDecisionIsClose.Enabled = false;
                }
            }
        }
        /// 
        /// 默认绑定
        /// 
        public void PageInit()
        {
            QuestionEditImg = 0;
            //施工单位
            UnitService.InitUnitByProjectIdUnitTypeDropDownList(ddlResponsibilityUnit, this.CurrUser.LoginProjectId, BLL.Const.ProjectUnitType_2, false);
            //获取人员信息
            var userList = from user in Funs.DB.Sys_User
                           join projectrole in Funs.DB.Project_ProjectUser on user.UserId equals projectrole.UserId
                           where projectrole.ProjectId == this.CurrUser.LoginProjectId
                           select user;
            var userids = new List() { Const.sysglyId, Const.hfnbdId, Const.sedinId };
            var addUserList = Funs.DB.Sys_User.Where(x => userids.Contains(x.UserId));
            var users = userList.ToList();
            if (addUserList.Count() > 0)
            {
                users.AddRange(addUserList);
            }
            //人员下拉框绑定
            if (users.Count() > 0)
            {
                //责任人
                ddlResponsibilityUser.DataTextField = "UserName";
                ddlResponsibilityUser.DataValueField = "UserId";
                ddlResponsibilityUser.DataSource = users;
                ddlResponsibilityUser.DataBind();
                ddlResponsibilityUser.SelectedValue = this.CurrUser.UserId;
                //提出人
                ddlProposeUser.DataTextField = "UserName";
                ddlProposeUser.DataValueField = "UserId";
                ddlProposeUser.DataSource = users;
                ddlProposeUser.DataBind();
                ddlProposeUser.SelectedValue = this.CurrUser.UserId;
                //总包
                ddlGeneraUser.DataTextField = "UserName";
                ddlGeneraUser.DataValueField = "UserId";
                ddlGeneraUser.DataSource = users;
                ddlGeneraUser.DataBind();
                //监理
                ddlSupervisionUser.DataTextField = "UserName";
                ddlSupervisionUser.DataValueField = "UserId";
                ddlSupervisionUser.DataSource = users;
                ddlSupervisionUser.DataBind();
                //业主
                ddlOwnerUser.DataTextField = "UserName";
                ddlOwnerUser.DataValueField = "UserId";
                ddlOwnerUser.DataSource = users;
                ddlOwnerUser.DataBind();
            }
            var query = from a in Funs.DB.PreRun_SubThreeChecksFourDecision
                        join b in Funs.DB.Base_Project on a.ProjectId equals b.ProjectId
                        where a.DecisionId == this.DecisionId
                        select new { a, b };
            var model = query.FirstOrDefault();
            if (model != null)
            {
                lblProjectName.Text = model.b.ProjectName;
                ddlResponsibilityUnit.SelectedValue = model.a.ResponsibilityUnit;
                txtQuestionDesc.Text = model.a.QuestionDesc;
                txtRestrictCondition.Text = model.a.RestrictCondition;
                txtLevel.Text = model.a.Level;
                txtSpeciality.Text = model.a.Speciality;
                txtAskDestructionTime.Text = model.a.AskDestructionTime != null ? model.a.AskDestructionTime.Value.ToString("yyyy-MM-dd") : string.Empty;
                lblRealityDestructionTime.Text = model.a.RealityDestructionTime != null ? model.a.RealityDestructionTime.Value.ToString("yyyy-MM-dd") : string.Empty;
                ddlResponsibilityUser.SelectedValue = model.a.ResponsibilityUser;
                ddlProposeUser.SelectedValue = model.a.ProposeUser;
                ddlGeneraUser.SelectedValue = model.a.GeneraUser;
                ddlSupervisionUser.SelectedValue = model.a.SupervisionUser;
                ddlOwnerUser.SelectedValue = model.a.OwnerUser;
                lblDecisionIsClose.Text = model.a.DecisionIsClose == 1 ? "已关闭" : "未关闭";
            }
        }
        /// 
        /// 确认情况绑定
        /// 
        public void gvDataBrid()
        {
            string strSql = @"select a.ConfirmId,a.ProjectId,a.DecisionId,a.ConfirmDesc,a.ConfirmTime,a.ConfirmUser,b.UserName as ConfirmUserName,a.ConfirmType,(case a.ConfirmType when 1 then '提出人处理' when 2 then '责任人确认' when 3 then '提出人确认' when 4 then '总包确认' when 5 then '监理确认' when 6 then '业主确认' else '' end) as ConfirmTypeName,a.ConfirmState,(case a.ConfirmState when 1 then '确认通过' when 2 then '确认退回' else '' end) as ConfirmStateName,a.AddUser,a.AddTime from PreRun_DecisionConfirmRecords as a left join Sys_User as b on a.ConfirmUser=b.UserId where a.ProjectId=@ProjectId and a.DecisionId=@DecisionId ";
            List listStr = new List();
            listStr.Add(new SqlParameter("@ProjectId", this.CurrUser.LoginProjectId));
            listStr.Add(new SqlParameter("@DecisionId", this.DecisionId));
            strSql += " order by a.AddTime asc";
            SqlParameter[] parameter = listStr.ToArray();
            DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter);
            //提出人处理
            var tcrclTb = tb.AsEnumerable().Where(row => row.Field("ConfirmType") == 1);
            gvTcrcl.DataSource = tcrclTb;
            gvTcrcl.DataBind();
            //提出人确认
            var tcrqrTb = tb.AsEnumerable().Where(row => row.Field("ConfirmType") == 3);
            gvTcrqr.DataSource = tcrqrTb;
            gvTcrqr.DataBind();
            //责任人确认
            var zrrqrTb = tb.AsEnumerable().Where(row => row.Field("ConfirmType") == 2);
            gvZrrConfirm.DataSource = zrrqrTb;
            gvZrrConfirm.DataBind();
            //其他人员确认
            int[] type = { 4, 5, 6 };
            var approveTb = tb.AsEnumerable().Where(row => type.Contains(row.Field("ConfirmType")));
            gvOtherConfirm.DataSource = approveTb;
            gvOtherConfirm.DataBind();
        }
        /// 
        /// 问题图片查看
        /// 
        protected void imgBtnQuestionFile_Click(object sender, EventArgs e)
        {
            PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("../../AttachFile/webuploader.aspx?type=-1&toKeyId={0}&path=FileUpload/CheckControl&menuId={1}", this.DecisionId + "q", Const.InspectTailTerm)));
        }
        /// 
        /// 整改图片查看
        /// 
        protected void imgBtnRectifyFile_Click(object sender, EventArgs e)
        {
            PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("../../AttachFile/webuploader.aspx?type=-1&toKeyId={0}&path=FileUpload/CheckControl&menuId={1}", this.DecisionId + "h", Const.InspectTailTerm)));
        }
        /// 
        /// 保存
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(ddlResponsibilityUnit.SelectedValue))
            {
                ShowNotify("请选择责任单位!", MessageBoxIcon.Warning);
                return;
            }
            if (string.IsNullOrWhiteSpace(ddlResponsibilityUser.SelectedValue))
            {
                ShowNotify("请选择责任人!", MessageBoxIcon.Warning);
                return;
            }
            if (string.IsNullOrWhiteSpace(ddlProposeUser.SelectedValue))
            {
                ShowNotify("请选择提出人!", MessageBoxIcon.Warning);
                return;
            }
            if (string.IsNullOrWhiteSpace(ddlGeneraUser.SelectedValue))
            {
                ShowNotify("请选择总包!", MessageBoxIcon.Warning);
                return;
            }
            if (string.IsNullOrWhiteSpace(ddlSupervisionUser.SelectedValue))
            {
                ShowNotify("请选择监理!", MessageBoxIcon.Warning);
                return;
            }
            if (string.IsNullOrWhiteSpace(ddlOwnerUser.SelectedValue))
            {
                ShowNotify("请选择业主!", MessageBoxIcon.Warning);
                return;
            }
            var isAdd = false;
            var model = Funs.DB.PreRun_SubThreeChecksFourDecision.FirstOrDefault(x => x.DecisionId == this.DecisionId);
            if (model == null)
            {
                isAdd = true;
                model = new PreRun_SubThreeChecksFourDecision();
                model.DecisionId = this.DecisionId = Guid.NewGuid().ToString();
                model.ProjectId = this.CurrUser.LoginProjectId;
                model.SubSystemId = this.SubSystemId;
            }
            model.ResponsibilityUser = ddlResponsibilityUser.SelectedValue;
            model.ProposeUser = ddlProposeUser.SelectedValue;
            model.GeneraUser = ddlGeneraUser.SelectedValue;
            model.SupervisionUser = ddlSupervisionUser.SelectedValue;
            model.OwnerUser = ddlOwnerUser.SelectedValue;
            model.ResponsibilityUnit = ddlResponsibilityUnit.SelectedValue;
            model.QuestionDesc = txtQuestionDesc.Text;
            model.Level = txtLevel.Text;
            model.Speciality = txtSpeciality.Text;
            model.AskDestructionTime = DateTime.Parse(txtAskDestructionTime.Text);
            model.RestrictCondition = txtRestrictCondition.Text;
            model.AddUser = this.CurrUser.LoginProjectId;
            model.AddTime = DateTime.Now;
            model.ResponsibilityProposeSatate = 0;
            model.DecisionIsClose = 0;
            if (isAdd)
            {
                Funs.DB.PreRun_SubThreeChecksFourDecision.InsertOnSubmit(model);
            }
            Funs.DB.SubmitChanges();
            ShowNotify("操作成功!", MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        /// 
        /// 保存并提交提出人处理
        /// 
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(ddlResponsibilityUnit.SelectedValue))
            {
                ShowNotify("请选择责任单位!", MessageBoxIcon.Warning);
                return;
            }
            if (string.IsNullOrWhiteSpace(ddlResponsibilityUser.SelectedValue))
            {
                ShowNotify("请选择责任人!", MessageBoxIcon.Warning);
                return;
            }
            if (string.IsNullOrWhiteSpace(ddlProposeUser.SelectedValue))
            {
                ShowNotify("请选择提出人!", MessageBoxIcon.Warning);
                return;
            }
            if (string.IsNullOrWhiteSpace(ddlGeneraUser.SelectedValue))
            {
                ShowNotify("请选择总包!", MessageBoxIcon.Warning);
                return;
            }
            if (string.IsNullOrWhiteSpace(ddlSupervisionUser.SelectedValue))
            {
                ShowNotify("请选择监理!", MessageBoxIcon.Warning);
                return;
            }
            if (string.IsNullOrWhiteSpace(ddlOwnerUser.SelectedValue))
            {
                ShowNotify("请选择业主!", MessageBoxIcon.Warning);
                return;
            }
            var isAdd = false;
            var model = Funs.DB.PreRun_SubThreeChecksFourDecision.FirstOrDefault(x => x.DecisionId == this.DecisionId);
            if (model == null)
            {
                isAdd = true;
                model = new PreRun_SubThreeChecksFourDecision();
                model.DecisionId = this.DecisionId = Guid.NewGuid().ToString();
                model.ProjectId = this.CurrUser.LoginProjectId;
            }
            model.ResponsibilityUser = ddlResponsibilityUser.SelectedValue;
            model.ProposeUser = ddlProposeUser.SelectedValue;
            model.GeneraUser = ddlGeneraUser.SelectedValue;
            model.SupervisionUser = ddlSupervisionUser.SelectedValue;
            model.OwnerUser = ddlOwnerUser.SelectedValue;
            model.ResponsibilityUnit = ddlResponsibilityUnit.SelectedValue;
            model.QuestionDesc = txtQuestionDesc.Text;
            model.Level = txtLevel.Text;
            model.Speciality = txtSpeciality.Text;
            model.AskDestructionTime = DateTime.Parse(txtAskDestructionTime.Text);
            model.RestrictCondition = txtRestrictCondition.Text;
            model.AddUser = this.CurrUser.LoginProjectId;
            model.AddTime = DateTime.Now;
            model.ResponsibilityProposeSatate = 1;
            if (isAdd)
            {
                Funs.DB.PreRun_SubThreeChecksFourDecision.InsertOnSubmit(model);
            }
            Funs.DB.SubmitChanges();
            ShowNotify("操作成功!", MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        /// 
        /// 关闭
        /// 
        protected void WindowAtt_Close(object sender, WindowCloseEventArgs e)
        {
        }
    }
}