using BLL;
using FineUIPro.Web.BoSheng;
using Newtonsoft.Json.Linq;
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.Opinion
{
    public partial class OpinionCollection : PageBase
    {
        #region 定义项
        /// 
        /// 主键
        /// 
        public string OpinionCollectionId
        {
            get
            {
                return (string)ViewState["OpinionCollectionId"];
            }
            set
            {
                ViewState["OpinionCollectionId"] = value;
            }
        }
        #endregion
        #region 加载页面
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Funs.DropDownPageSize(this.ddlPageSize);
                this.btnMenuDelete.OnClientClick = Grid1.GetNoSelectionAlertReference("请至少选择一项!");
                this.btnMenuDelete.ConfirmText = String.Format("你确定要删除选中的  行数据吗?", Grid1.GetSelectedCountReference());
                BLL.UnitService.InitUnitByProjectIdUnitTypeDropDownList1(this.drpUnitId, this.CurrUser.LoginProjectId, true);
                BLL.ConstValue.InitConstNameDropDownList(this.drpProblemClass, "MenuType_P", true);
                ddlPageSize.SelectedValue = Grid1.PageSize.ToString();
                // 绑定表格
                BindGrid();
            }
        }
        /// 
        /// 绑定数据
        /// 
        private void BindGrid()
        {
            string strSql = "select Opinion.OpinionCollectionId"
                      + @",Opinion.ProjectId"
                      + @",Opinion.UnitId"
                      + @",Opinion.RoleId"
                      + @",Opinion.ProblemClass"
                      + @",Opinion.Opinion"
                      + @",Opinion.CompileMan"
                      + @",Opinion.CompileDate"
                      + @",Opinion.State"
                      + @",Opinion.ProjectName"
                      + @",Opinion.UnitName"
                      + @",Opinion.CompileManName"
                      + @",(CASE WHEN Opinion.State = " + BLL.Const.State_0 + " OR Opinion.State IS NULL THEN '待['+Opinion.CompileManName+']提交' WHEN Opinion.State =  " + BLL.Const.State_2 + " THEN '审核/审批完成' ELSE '待['+OperateUser.UserName+']办理' END) AS  FlowOperateName"
                      + @" from View_Opinion_OpinionCollection AS Opinion "
                      + @" LEFT JOIN Opinion_OpinionCollectionApprove AS FlowOperate ON Opinion.OpinionCollectionId=FlowOperate.OpinionCollectionId and ApproveDate is null"
                      + @" LEFT JOIN Sys_User AS OperateUser ON OperateUser.UserId=FlowOperate.ApproveMan WHERE 1=1";
            List listStr = new List();
            strSql += " AND Opinion.ProjectId = @ProjectId";
            listStr.Add(new SqlParameter("@ProjectId", this.CurrUser.LoginProjectId));
            if (!string.IsNullOrEmpty(this.txtCompileManName.Text.Trim()))
            {
                strSql += " AND Opinion.CompileManName like @userName";
                listStr.Add(new SqlParameter("@userName", "%" + this.txtCompileManName.Text.Trim() + "%"));
            }
            if (!string.IsNullOrEmpty(this.drpUnitId.SelectedValue) && this.drpUnitId.SelectedValue != BLL.Const._Null)
            {
                strSql += " AND Opinion.UnitId = @unitId";
                listStr.Add(new SqlParameter("@unitId", this.drpUnitId.SelectedValue));
            }
            if (!string.IsNullOrEmpty(this.drpProblemClass.SelectedValue) && this.drpProblemClass.SelectedValue != BLL.Const._Null)
            {
                strSql += " AND Opinion.ProblemClass = @ProblemClass";
                listStr.Add(new SqlParameter("@ProblemClass", this.drpProblemClass.SelectedValue));
            }
            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();
        }
        #endregion
        #region 页索引改变事件
        /// 
        /// 页索引改变事件
        /// 
        /// 
        /// 
        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            BindGrid();
        }
        #endregion
        #region 排序
        /// 
        /// 排序
        /// 
        /// 
        /// 
        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            BindGrid();
        }
        #endregion
        #region 分页选择下拉改变事件
        /// 
        /// 分页选择下拉改变事件
        /// 
        /// 
        /// 
        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
            BindGrid();
        }
        #endregion
        #region 增加
        /// 
        /// 增加
        /// 
        /// 
        /// 
        protected void btnNew_Click(object sender, EventArgs e)
        {
            PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("OpinionCollectionEdit.aspx", "编辑 - ")));
        }
        #endregion
        #region 编辑
        /// 
        /// Grid行双击事件
        /// 
        /// 
        /// 
        protected void Grid1_RowDoubleClick(object sender, GridRowClickEventArgs e)
        {
            this.EditData();
        }
        /// 
        /// 右键编辑事件
        /// 
        /// 
        /// 
        protected void btnMenuEdit_Click(object sender, EventArgs e)
        {
            this.EditData();
        }
        /// 
        /// 编辑
        /// 
        private void EditData()
        {
            if (Grid1.SelectedRowIndexArray.Length == 0)
            {
                Alert.ShowInTop("请至少选择一条记录!", MessageBoxIcon.Warning);
                return;
            }
            this.OpinionCollectionId = Grid1.SelectedRowID;
            var opinion = OpinionCollectionService.GetOpinionCollectionById(this.OpinionCollectionId);
            if (opinion != null)
            {
                PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("OpinionCollectionEdit.aspx?opinionCollectionId={0}", this.OpinionCollectionId, "编辑 - ")));
            }
        }
        #endregion
        #region 删除
        /// 
        /// 右键删除事件
        /// 
        /// 
        /// 
        protected void btnMenuDelete_Click(object sender, EventArgs e)
        {
            this.DeleteData();
        }
        /// 
        /// 删除方法
        /// 
        private void DeleteData()
        {
            if (Grid1.SelectedRowIndexArray.Length > 0)
            {
                bool isShow = false;
                if (Grid1.SelectedRowIndexArray.Length == 1)
                {
                    isShow = true;
                }
                foreach (int rowIndex in Grid1.SelectedRowIndexArray)
                {
                    string rowID = Grid1.DataKeys[rowIndex][0].ToString();
                    if (this.judgementDelete(rowID, isShow))
                    {
                        var opinionCollection = OpinionCollectionService.GetOpinionCollectionById(rowID);
                        if (opinionCollection != null)
                        {
                            OpinionCollectionApproveService.DeleteApproveByOpinionCollectionId(rowID);
                            OpinionCollectionService.DeleteOpinionCollectionById(rowID);
                            BindGrid();
                            ShowNotify("删除数据成功!(表格数据已重新绑定)", MessageBoxIcon.Success);
                        }
                    }
                }
            }
        }
        /// 
        /// 判断是否可删除
        /// 
        /// 
        /// 
        /// 
        private bool judgementDelete(string rowID, bool isShow)
        {
            string content = string.Empty;
            if (string.IsNullOrEmpty(content))
            {
                return true;
            }
            else
            {
                if (isShow)
                {
                    Alert.ShowInTop(content);
                }
                return false;
            }
        }
        #endregion
        #region 关闭弹出窗
        /// 
        /// 关闭弹出窗
        /// 
        /// 
        /// 
        protected void Window1_Close(object sender, WindowCloseEventArgs e)
        {
            BindGrid();
        }
        #endregion
        #region 查询事件
        /// 
        /// 查询事件
        /// 
        /// 
        /// 
        protected void Text_TextChanged(object sender, EventArgs e)
        {
            this.BindGrid();
        }
        #endregion
        #region 格式化字符串
        /// 
        /// 根据角色Id获取角色名称
        /// 
        /// 
        /// 
        protected string ConvertRoleName(object roleId)
        {
            string roleName = string.Empty;
            if (roleId != null)
            {
                roleName = BLL.RoleService.getRoleNamesRoleIds(roleId.ToString());
            }
            return roleName;
        }
        #endregion
    }
}