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;
using BLL;
using Newtonsoft.Json.Linq;
namespace FineUIPro.Web.CQMS.Information
{
    public partial class ConfirmFormEdit : PageBase
    {
        #region 定义变量
        /// 
        /// 方案审查主键
        /// 
        public string ConfirmFormId
        {
            get
            {
                return (string)ViewState["ConfirmFormId"];
            }
            set
            {
                ViewState["ConfirmFormId"] = value;
            }
        }
        #endregion
        #region 定义集合
        /// 
        /// 定义会签意见集合
        /// 
        public static List approves = new List();
        #endregion
        public int ContactImg
        {
            get
            {
                return Convert.ToInt32(ViewState["ContactImg"]);
            }
            set
            {
                ViewState["ContactImg"] = value;
            }
        }
        /// 
        /// 明细集合
        /// 
        private static List details = new List();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                details.Clear();
                BLL.SecretLevelService.InitSecretLevelDropDownList(drpSecretLevel, true);
                UnitService.InitUnitByProjectIdUnitTypeDropDownList(drpUnit, CurrUser.LoginProjectId, BLL.Const.ProjectUnitType_2, true);
                WorkOrderService.InitWorkOrderList(drpWorkOrderIds, CurrUser.LoginProjectId, true);
                ContactImg = 0;
                //CommonService.GetAllButtonList(CurrUser.LoginProjectId, CurrUser.UserId, Const.ConfirmFormMenuId);
                txtCompileDate.Text = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
                ConfirmFormId = Request.Params["confirmFormId"];
                if (!string.IsNullOrWhiteSpace(ConfirmFormId))
                {
                    HFConfirmFormId.Text = ConfirmFormId;
                    Model.CQMS_ConfirmForm confirmForm = ConfirmFormService.GetConfirmFormByConfirmFormId(ConfirmFormId);
                    txtConfirmFormCode.Text = confirmForm.ConfirmFormCode;
                    if (!string.IsNullOrEmpty(confirmForm.SecretLevelId))
                    {
                        drpSecretLevel.SelectedValue = confirmForm.SecretLevelId;
                    }
                    if (!string.IsNullOrEmpty(confirmForm.UnitId))
                    {
                        drpUnit.SelectedValue = confirmForm.UnitId;
                    }
                    this.txtContractNo.Text = confirmForm.ContractNo;
                    if (!string.IsNullOrEmpty(confirmForm.WorkOrderIds))
                    {
                        this.drpWorkOrderIds.SelectedValueArray = confirmForm.WorkOrderIds.Split(',');
                    }
                    this.txtTaskTheme.Text = confirmForm.TaskTheme;
                    if (confirmForm.CompileDate != null)
                    {
                        txtCompileDate.Text = string.Format("{0:yyyy-MM-dd}", confirmForm.CompileDate);
                    }
                    if (confirmForm.Edition != null)
                    {
                        txtEdition.Text = confirmForm.Edition.ToString();
                    }
                    this.Grid1.DataSource = BLL.ConfirmFormDetailService.GetLists(ConfirmFormId);
                    this.Grid1.DataBind();
                    bindApprove();
                    if (confirmForm.State == Const.ConfirmForm_Compile)
                    {
                        this.Grid1.Columns[5].Hidden = true;
                        agree.Hidden = true;
                        options.Hidden = true;
                        optio.Hidden = true;
                        ConfirmFormService.Init(drpHandleType, Const.ConfirmForm_Compile, false);
                        UserService.InitUserProjectIdUnitIdDropDownList(drpHandleMan, CurrUser.LoginProjectId, this.drpUnit.SelectedValue, false);
                    }
                    else if (confirmForm.State == Const.VisaApplication_ReCompile)  //重报
                    {
                        this.Grid1.Columns[5].Hidden = true;
                        this.trNext.Hidden = true;
                        agree.Hidden = true;
                        options.Hidden = true;
                        optio.Hidden = true;
                    }
                    else if (confirmForm.State == Const.ConfirmForm_Audit1)
                    {
                        ConfirmFormService.Init(drpHandleType, Const.ConfirmForm_Audit1, false);
                        var userList = from x in Funs.DB.Sys_User
                                       join y in Funs.DB.Project_ProjectUnit
                                       on x.UnitId equals y.UnitId
                                       join p in Funs.DB.Project_ProjectUser
                                       on x.UserId equals p.UserId
                                       where (p.RoleId.Contains(Const.ConstructionManager) || p.RoleId.Contains(Const.ConstructionAssistantManager))
                                       && y.UnitType == Const.ProjectUnitType_1 && p.ProjectId == CurrUser.LoginProjectId && y.ProjectId == CurrUser.LoginProjectId
                                       select x;
                        drpHandleMan.DataValueField = "UserId";
                        drpHandleMan.DataTextField = "UserName";
                        drpHandleMan.DataSource = userList;
                        drpHandleMan.DataBind();
                    }
                    else if (confirmForm.State == Const.ConfirmForm_Audit2)
                    {
                        this.Toolbar4.Hidden = true;
                        this.Grid1.Columns[7].Hidden = true;
                        for (int i = 0; i < this.Grid1.Rows.Count; i++)
                        {
                            foreach (GridColumn column in Grid1.AllColumns)
                            {
                                if (column.ColumnID != "AuditOpinion" && column.ColumnID != "Remark")
                                {
                                    Grid1.Rows[i].CellCssClasses[column.ColumnIndex] = "f-grid-cell-uneditable";
                                }
                            }
                        }
                        NoEdit();
                        ConfirmFormService.Init(drpHandleType, Const.ConfirmForm_Audit2, false);
                        this.drpHandleMan.Enabled = false;
                        this.drpHandleMan.Required = false;
                        this.drpHandleMan.Items.Clear();
                    }
                }
                else
                {
                    agree.Hidden = true;
                    txtEdition.Text = "0";
                    options.Hidden = true;
                    optio.Hidden = true;
                    plApprove2.Hidden = true;
                    ConfirmFormService.Init(drpHandleType, Const.ConfirmForm_Compile, false);
                    //UserService.InitUserProjectIdUnitTypeDropDownList(drpHandleMan, CurrUser.LoginProjectId, BLL.Const.ProjectUnitType_1, false);
                    this.Grid1.Columns[5].Hidden = true;
                    //txtConfirmFormCode.Text = SQLHelper.RunProcNewId2("SpGetNewCode3ByProjectId", "dbo.CQMS_ConfirmForm", "Code", CurrUser.LoginProjectId);
                }
            }
        }
        private void NoEdit()
        {
            txtConfirmFormCode.Enabled = false;
            drpSecretLevel.Enabled = false;
            drpUnit.Enabled = false;
            drpWorkOrderIds.Enabled = false;
            txtContractNo.Enabled = false;
            txtTaskTheme.Enabled = false;
            txtCompileDate.Enabled = false;
            txtEdition.Enabled = false;
            ContactImg = -1;
        }
        /// 
        /// 审批列表
        /// 
        private void bindApprove()
        {
            var list = ConfirmFormApproveService.getListData(ConfirmFormId);
            gvApprove.DataSource = list;
            gvApprove.DataBind();
        }
        public string man(Object man)
        {
            string appman = string.Empty;
            if (UserService.GetUserByUserId(man.ToString()) != null)
            {
                appman = UserService.GetUserByUserId(man.ToString()).UserName;
            }
            return appman;
        }
        #region 保存/提交
        protected void btnSave_Click(object sender, EventArgs e)
        {
            //validate(Const.BtnSave, "save");
            if (CommonService.GetAllButtonPowerList(CurrUser.LoginProjectId, CurrUser.UserId, Const.ConfirmFormMenuId, Const.BtnSave))
            {
                SaveConfirmForm("save");
                PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
                Alert.ShowInTop("保存成功!", MessageBoxIcon.Success);
            }
            else
            {
                Alert.ShowInTop("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            validate(Const.BtnSubmit, "submmit");
        }
        /// 
        /// 保存验证
        /// 
        /// 
        /// 
        public void validate(string buttonName, string tip)
        {
            if (CommonService.GetAllButtonPowerList(CurrUser.LoginProjectId, CurrUser.UserId, Const.ConfirmFormMenuId, buttonName))
            {
                string err = string.Empty;
                if (this.drpSecretLevel.SelectedValue == BLL.Const._Null)
                {
                    err += "请选择密级,";
                }
                if (this.drpUnit.SelectedValue == BLL.Const._Null)
                {
                    err += "请选择分包单位,";
                }
                if (this.drpWorkOrderIds.SelectedValueArray.Length == 1 && this.drpWorkOrderIds.SelectedValue == BLL.Const._Null)
                {
                    err += "请选择工程签证工作委托单,";
                }
                //if (!AttachFileService.Getfile(HFConfirmFormId.Text, Const.ConfirmFormMenuId))
                //{
                //    err += "请上传附件,";
                //}
                //if (trOne.Nodes[0].Nodes.Count == 0 && trTwo.Nodes.Count == 0 && trThree.Nodes.Count == 0 &&
                //    trFour.Nodes.Count == 0 && trFive.Nodes.Count == 0 && trSixe.Nodes.Count == 0)
                //{
                //    err += "请选择总包会签人员,";
                //}
                if (!string.IsNullOrWhiteSpace(err))
                {
                    err = err.Substring(0, err.LastIndexOf(","));
                    err += "!";
                }
                if (!string.IsNullOrWhiteSpace(err))
                {
                    Alert.ShowInTop(err, MessageBoxIcon.Warning);
                    return;
                }
                if (!string.IsNullOrWhiteSpace(ConfirmFormId))
                {//更新时操作
                    if (tip == "save")
                    {
                        EditConstructSol("save");
                    }
                    else
                    {
                        EditConstructSol("submit");
                    }
                }
                else
                {
                    if (tip == "save")
                    {
                        SaveConfirmForm("save");
                    }
                    else
                    {
                        SaveConfirmForm("submit");
                    }
                    //添加时操作           
                }
                PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
                if (tip == "save")
                {
                    tip = "保存成功!";
                }
                else
                {
                    tip = "提交成功!";
                }
                Alert.ShowInTop(tip, MessageBoxIcon.Success);
            }
            else
            {
                Alert.ShowInTop("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
            }
        }
        /// 
        /// 编辑时候保存
        /// 
        private void EditConstructSol(string saveType)
        {
            Model.CQMS_ConfirmForm confirmForm = new Model.CQMS_ConfirmForm();
            confirmForm.ConfirmFormCode = txtConfirmFormCode.Text.Trim();
            confirmForm.SecretLevelId = this.drpSecretLevel.SelectedValue;
            confirmForm.ProjectId = CurrUser.LoginProjectId;
            confirmForm.UnitId = this.drpUnit.SelectedValue;
            confirmForm.ContractNo = this.txtContractNo.Text.Trim();
            string workOrderIds = string.Empty;
            foreach (var item in this.drpWorkOrderIds.SelectedValueArray)
            {
                if (item != BLL.Const._Null)
                {
                    workOrderIds += item + ",";
                }
            }
            if (!string.IsNullOrEmpty(workOrderIds))
            {
                confirmForm.WorkOrderIds = workOrderIds.Substring(0, workOrderIds.Length - 1);
            }
            confirmForm.TaskTheme = this.txtTaskTheme.Text.Trim();
            int edtion = Convert.ToInt32(txtEdition.Text);
            confirmForm.Edition = edtion;
            if (!string.IsNullOrEmpty(txtCompileDate.Text.Trim()))
            {
                confirmForm.CompileDate = Convert.ToDateTime(txtCompileDate.Text.Trim());
            }
            if (!string.IsNullOrEmpty(ConfirmFormId))
            {
                confirmForm.ConfirmFormId = ConfirmFormId;
                Model.CQMS_ConfirmForm confirmForm1 = ConfirmFormService.GetConfirmFormByConfirmFormId(ConfirmFormId);
                if (saveType == "submit")
                {
                    Model.CQMS_ConfirmFormApprove approve = ConfirmFormApproveService.GetConstructSoluAppByApproveMan(ConfirmFormId, CurrUser.UserId, Convert.ToInt32(confirmForm1.Edition));
                    if (approve != null)
                    {
                        approve.IsAgree = Convert.ToBoolean(this.rblIsAgree.SelectedValue);
                        approve.ApproveIdea = this.txtOptions.Text.Trim();
                        approve.ApproveDate = DateTime.Now;
                        ConfirmFormApproveService.UpdateConfirmFormApprove(approve);
                    }
                    if (confirmForm1.State == BLL.Const.ConfirmForm_Compile)
                    {
                        confirmForm.State = this.drpHandleType.SelectedValue;
                        Model.CQMS_ConfirmFormApprove reApprove = new Model.CQMS_ConfirmFormApprove();
                        reApprove.ConfirmFormId = confirmForm.ConfirmFormId;
                        reApprove.ApproveMan = this.drpHandleMan.SelectedValue;
                        reApprove.ApproveType = this.drpHandleType.SelectedValue;
                        reApprove.Edition = edtion;
                        ConfirmFormApproveService.AddConfirmFormApprove(reApprove);
                    }
                    else if (confirmForm1.State == BLL.Const.ConfirmForm_ReCompile)
                    {
                        //获取上个版次不同意的审批记录
                        List lastNotAggreeApproves = BLL.ConfirmFormApproveService.GetNotAggreeApprovesByConfirmFormIdAndEdtion(ConfirmFormId, confirmForm1.Edition.Value - 1);
                        confirmForm.State = lastNotAggreeApproves[0].ApproveType;
                        foreach (var item in lastNotAggreeApproves)
                        {
                            Model.CQMS_ConfirmFormApprove reApprove = new Model.CQMS_ConfirmFormApprove();
                            reApprove.ConfirmFormId = confirmForm.ConfirmFormId;
                            reApprove.ApproveMan = item.ApproveMan;
                            reApprove.ApproveType = item.ApproveType;
                            reApprove.Edition = edtion;
                            ConfirmFormApproveService.AddConfirmFormApprove(reApprove);
                        }
                    }
                    else if (confirmForm1.State == BLL.Const.ConfirmForm_Audit1)
                    {
                        if (this.rblIsAgree.SelectedValue == "true")    //同意
                        {
                            confirmForm.State = this.drpHandleType.SelectedValue;
                            Model.CQMS_ConfirmFormApprove reApprove = new Model.CQMS_ConfirmFormApprove();
                            reApprove.ConfirmFormId = confirmForm.ConfirmFormId;
                            if (!string.IsNullOrEmpty(this.drpHandleMan.SelectedValue))
                            {
                                reApprove.ApproveMan = this.drpHandleMan.SelectedValue;
                            }
                            reApprove.ApproveType = this.drpHandleType.SelectedValue;
                            reApprove.Edition = edtion;
                            ConfirmFormApproveService.AddConfirmFormApprove(reApprove);
                        }
                        else   //不同意
                        {
                            confirmForm.State = Const.ConfirmForm_ReCompile;
                            confirmForm.Edition = edtion + 1;
                            Model.CQMS_ConfirmFormApprove reApprove = new Model.CQMS_ConfirmFormApprove();
                            reApprove.ConfirmFormId = confirmForm.ConfirmFormId;
                            reApprove.ApproveMan = confirmForm1.CompileMan;
                            reApprove.ApproveType = Const.VisaApplication_ReCompile;
                            reApprove.Edition = edtion + 1;
                            ConfirmFormApproveService.AddConfirmFormApprove(reApprove);
                        }
                    }
                    else if (confirmForm1.State == BLL.Const.ConfirmForm_Audit2)
                    {
                        if (this.rblIsAgree.SelectedValue == "true")    //同意
                        {
                            confirmForm.State = this.drpHandleType.SelectedValue;
                            Model.CQMS_ConfirmFormApprove reApprove = new Model.CQMS_ConfirmFormApprove();
                            reApprove.ConfirmFormId = confirmForm.ConfirmFormId;
                            if (!string.IsNullOrEmpty(this.drpHandleMan.SelectedValue))
                            {
                                reApprove.ApproveMan = this.drpHandleMan.SelectedValue;
                            }
                            reApprove.ApproveType = this.drpHandleType.SelectedValue;
                            reApprove.Edition = edtion;
                            ConfirmFormApproveService.AddConfirmFormApprove(reApprove);
                        }
                        else   //不同意
                        {
                            confirmForm.State = Const.ConfirmForm_ReCompile;
                            confirmForm.Edition = edtion + 1;
                            Model.CQMS_ConfirmFormApprove reApprove = new Model.CQMS_ConfirmFormApprove();
                            reApprove.ConfirmFormId = confirmForm.ConfirmFormId;
                            reApprove.ApproveMan = confirmForm1.CompileMan;
                            reApprove.ApproveType = Const.VisaApplication_ReCompile;
                            reApprove.Edition = edtion + 1;
                            ConfirmFormApproveService.AddConfirmFormApprove(reApprove);
                        }
                    }
                    ConfirmFormService.UpdateConfirmForm(confirmForm);
                }
                LogService.AddSys_Log(CurrUser, confirmForm.ConfirmFormCode, ConfirmFormId, Const.ConfirmFormMenuId, "修改工程签证确认单");
            }
            BLL.ConfirmFormDetailService.DeleteConfirmFormDetailByConfirmFormId(ConfirmFormId);
            details = GetDetails();
            foreach (var item in details)
            {
                item.ConfirmFormId = ConfirmFormId;
                BLL.ConfirmFormDetailService.AddConfirmFormDetail(item);
            }
        }
        #endregion
        #region 添加时候的保存
        /// 
        /// 保存方案审查
        /// 
        /// 保存类型
        private void SaveConfirmForm(string saveType)
        {
            //if (tvHSE.CheckedNodes.Count == 0 || (tvHSE.CheckedNodes.Count > 0 && tvHSE.CheckedNodes[0].Value == "0"))
            //{
            //    ScriptManager.RegisterStartupScript(this, typeof(string), "_alert", "alert('请选择HSE会签人员!')", true);
            //    return;
            //}
            Model.CQMS_ConfirmForm confirmForm = new Model.CQMS_ConfirmForm();
            confirmForm.ConfirmFormCode = txtConfirmFormCode.Text.Trim();
            confirmForm.SecretLevelId = this.drpSecretLevel.SelectedValue;
            confirmForm.ProjectId = CurrUser.LoginProjectId;
            confirmForm.UnitId = this.drpUnit.SelectedValue;
            confirmForm.ContractNo = this.txtContractNo.Text.Trim();
            string workOrderIds = string.Empty;
            foreach (var item in this.drpWorkOrderIds.SelectedValueArray)
            {
                if (item != BLL.Const._Null)
                {
                    workOrderIds += item + ",";
                }
            }
            if (!string.IsNullOrEmpty(workOrderIds))
            {
                confirmForm.WorkOrderIds = workOrderIds.Substring(0, workOrderIds.Length - 1);
            }
            confirmForm.TaskTheme = this.txtTaskTheme.Text.Trim();
            int edtion = Convert.ToInt32(txtEdition.Text);
            confirmForm.Edition = edtion;
            if (!string.IsNullOrEmpty(txtCompileDate.Text.Trim()))
            {
                confirmForm.CompileDate = Convert.ToDateTime(txtCompileDate.Text.Trim());
            }
            if (saveType == "submit")
            {
                confirmForm.State = Const.ConfirmForm_Audit1;
            }
            else
            {
                confirmForm.State = Const.ConfirmForm_Compile;
            }
            if (!string.IsNullOrEmpty(HFConfirmFormId.Text))
            {
                confirmForm.ConfirmFormId = HFConfirmFormId.Text;
            }
            else
            {
                confirmForm.ConfirmFormId = SQLHelper.GetNewID(typeof(Model.CQMS_ConfirmForm));
            }
            confirmForm.CompileMan = CurrUser.UserId;
            confirmForm.Edition = Convert.ToInt32(txtEdition.Text);
            if (!string.IsNullOrEmpty(HFConfirmFormId.Text))
            {
                ConfirmFormService.UpdateConfirmForm(confirmForm);
            }
            else
            {
                ConfirmFormService.AddConfirmForm(confirmForm);
            }
            BLL.ConfirmFormDetailService.DeleteConfirmFormDetailByConfirmFormId(confirmForm.ConfirmFormId);
            details = GetDetails();
            foreach (var item in details)
            {
                item.ConfirmFormId = confirmForm.ConfirmFormId;
                BLL.ConfirmFormDetailService.AddConfirmFormDetail(item);
            }
            if (saveType == "submit")
            {
                Model.CQMS_ConfirmFormApprove approve1 = new Model.CQMS_ConfirmFormApprove();
                approve1.ConfirmFormId = confirmForm.ConfirmFormId;
                approve1.ApproveDate = DateTime.Now;
                approve1.ApproveMan = this.CurrUser.UserId;
                approve1.ApproveType = Const.ConfirmForm_Compile;
                approve1.Edition = Convert.ToInt32(txtEdition.Text);
                ConfirmFormApproveService.AddConfirmFormApprove(approve1);
                Model.CQMS_ConfirmFormApprove approve2 = new Model.CQMS_ConfirmFormApprove();
                approve2.ConfirmFormId = confirmForm.ConfirmFormId;
                approve2.ApproveMan = this.drpHandleMan.SelectedValue;
                approve2.ApproveType = Const.ConfirmForm_Audit1;
                approve2.Edition = Convert.ToInt32(txtEdition.Text);
                ConfirmFormApproveService.AddConfirmFormApprove(approve2);
            }
            else
            {
                Model.CQMS_ConfirmFormApprove approve1 = new Model.CQMS_ConfirmFormApprove();
                approve1.ConfirmFormId = confirmForm.ConfirmFormId;
                approve1.ApproveMan = this.CurrUser.UserId;
                approve1.ApproveType = Const.ConfirmForm_Compile;
                approve1.Edition = Convert.ToInt32(txtEdition.Text);
                ConfirmFormApproveService.AddConfirmFormApprove(approve1);
            }
            LogService.AddSys_Log(CurrUser, confirmForm.ConfirmFormCode, ConfirmFormId, Const.ConfirmFormMenuId, "添加工程签证确认单");
            //}
            //提交
            //if (saveType == "submit")
            //{
            //    countersign(confirmForm.ConfirmFormId);
            //}
            //LogService.AddSys_Log(CurrUser, confirmForm.ConfirmFormCode, ConfirmFormId, Const.ConfirmFormMenuId, "编制工程签证确认单");
        }
        #endregion
        protected void imgBtnFile_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(HFConfirmFormId.Text))   //新增记录
            {
                HFConfirmFormId.Text = SQLHelper.GetNewID(typeof(Model.CQMS_ConfirmForm));
            }
            PageContext.RegisterStartupScript(WindowAtt.GetShowReference(
            String.Format("../../AttachFile/webuploader.aspx?type={0}&toKeyId={1}&path=FileUpload/ConfirmForm&menuId={2}",
            ContactImg, HFConfirmFormId.Text, Const.ConfirmFormMenuId)));
        }
        protected void btnapprove_Click(object sender, EventArgs e)
        {
            //HFConfirmFormId.Text
            var approve = ConfirmFormApproveService.GetConfirmFormApproveByApproveMan(HFConfirmFormId.Text, CurrUser.UserId);
            if (approve != null)
            {
                var approveId = approve.ConfirmFormApproveId;
                PageContext.RegisterStartupScript(WindowAtt.GetShowReference(
                String.Format("../../AttachFile/webuploader.aspx?type={0}&toKeyId={1}&path=FileUpload/ConfirmForm&menuId={2}",
                0, approveId, Const.ConfirmFormMenuId)));
            }
        }
        protected void drpUnit_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.drpUnit.SelectedValue != BLL.Const._Null)
            {
                UserService.InitUserProjectIdUnitIdDropDownList(drpHandleMan, CurrUser.LoginProjectId, this.drpUnit.SelectedValue, false);
            }
            else
            {
                drpHandleMan.Items.Clear();
            }
        }
        protected void gvApprove_RowCommand(object sender, GridCommandEventArgs e)
        {
            object[] keys = gvApprove.DataKeys[e.RowIndex];
            string fileId = string.Empty;
            if (keys == null)
            {
                return;
            }
            else
            {
                fileId = keys[0].ToString();
            }
            PageContext.RegisterStartupScript(WindowAtt.GetShowReference(
                 String.Format("../../AttachFile/webuploader.aspx?type={0}&toKeyId={1}&path=FileUpload/ConfirmForm&menuId={2}",
                 -1, fileId, Const.ConfirmFormMenuId)));
        }
        protected void drpHandleType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.drpHandleType.SelectedValue == BLL.Const.ConfirmForm_Complete) //审批完成
            {
                this.drpHandleMan.Enabled = false;
                this.drpHandleMan.Required = false;
                this.drpHandleMan.Items.Clear();
            }
            else
            {
                this.drpHandleMan.Enabled = true;
                this.drpHandleMan.Required = true;
                UserService.InitUserProjectIdUnitTypeDropDownList(drpHandleMan, CurrUser.LoginProjectId, BLL.Const.ProjectUnitType_1, false);
            }
        }
        protected void rblIsAgree_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.rblIsAgree.SelectedValue == "false")
            {
                this.drpHandleType.SelectedValue = BLL.Const.ConfirmForm_ReCompile;
                this.drpHandleMan.Enabled = true;
                this.drpHandleMan.Required = true;
                Model.CQMS_ConfirmFormApprove approve = BLL.ConfirmFormApproveService.GetCompile(this.ConfirmFormId);
                if (approve != null)
                {
                    List list = new List();
                    Model.Sys_User compileMan = BLL.UserService.GetUserByUserId(approve.ApproveMan);
                    list.Add(compileMan);
                    drpHandleMan.DataValueField = "UserId";
                    drpHandleMan.DataTextField = "UserName";
                    this.drpHandleMan.DataSource = list;
                    this.drpHandleMan.DataBind();
                }
            }
            else
            {
                this.drpHandleType.SelectedIndex = 0;
                if (this.drpHandleType.SelectedValue == BLL.Const.ConfirmForm_Complete)
                {
                    this.drpHandleMan.Enabled = false;
                    this.drpHandleMan.Required = false;
                    this.drpHandleMan.Items.Clear();
                }
                else if (this.drpHandleType.SelectedValue == BLL.Const.ConfirmForm_Audit1)
                {
                    UserService.InitUserProjectIdUnitIdDropDownList(drpHandleMan, CurrUser.LoginProjectId, this.drpUnit.SelectedValue, false);
                }
                else
                {
                    var userList = from x in Funs.DB.Sys_User
                                   join y in Funs.DB.Project_ProjectUnit
                                   on x.UnitId equals y.UnitId
                                   join p in Funs.DB.Project_ProjectUser
                                   on x.UserId equals p.UserId
                                   where (p.RoleId.Contains(Const.ConstructionManager) || p.RoleId.Contains(Const.ConstructionAssistantManager))
                                   && y.UnitType == Const.ProjectUnitType_1 && p.ProjectId == CurrUser.LoginProjectId && y.ProjectId == CurrUser.LoginProjectId
                                   select x;
                    drpHandleMan.DataValueField = "UserId";
                    drpHandleMan.DataTextField = "UserName";
                    drpHandleMan.DataSource = userList;
                    drpHandleMan.DataBind();
                }
            }
        }
        #region  明细事件
        protected void btnNew_Click(object sender, EventArgs e)
        {
            details = GetDetails();
            Model.CQMS_ConfirmFormDetail detail = new Model.CQMS_ConfirmFormDetail();
            detail.ConfirmFormDetailId = SQLHelper.GetNewID(typeof(Model.CQMS_ConfirmFormDetail));
            detail.SortIndex = details.Count();
            details.Add(detail);
            this.Grid1.DataSource = details;
            this.Grid1.DataBind();
        }
        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            details = GetDetails();
            string itemId = Grid1.DataKeys[e.RowIndex][0].ToString();
            if (e.CommandName == "delete")
            {
                foreach (Model.CQMS_ConfirmFormDetail detail in details)
                {
                    if (detail.ConfirmFormDetailId == itemId)
                    {
                        details.Remove(detail);
                        break;
                    }
                }
                Grid1.DataSource = details;
                Grid1.DataBind();
            }
        }
        /// 
        /// 检查并保存集合
        /// 
        private List GetDetails()
        {
            List details = new List();
            foreach (JObject mergedRow in Grid1.GetMergedData())
            {
                JObject values = mergedRow.Value("values");
                int i = mergedRow.Value("index");
                Model.CQMS_ConfirmFormDetail detail = new Model.CQMS_ConfirmFormDetail();
                detail.ConfirmFormDetailId = this.Grid1.Rows[i].RowID;
                detail.JobContent = values.Value("JobContent");
                detail.Describe = values.Value("Describe");
                detail.Unit = values.Value("Unit");
                detail.Workload = values.Value("Workload");
                detail.AuditOpinion = values.Value("AuditOpinion");
                detail.Remark = values.Value("Remark");
                details.Add(detail);
            }
            return details;
        }
        #endregion
    }
}