using BLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web.UI.WebControls;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Newtonsoft.Json.Linq;
using System.Configuration;
namespace FineUIPro.Web.HSSE.SecuritySystem
{
    public partial class SafetyOrganizationPic : PageBase
    {
        #region 定义项
        /// 
        /// 主键
        /// 
        public string ProjectId
        {
            get
            {
                return (string)ViewState["ProjectId"];
            }
            set
            {
                ViewState["ProjectId"] = value;
            }
        }
        #endregion
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.ProjectId = this.CurrUser.LoginProjectId;
                if (!string.IsNullOrEmpty(Request.Params["projectId"]) && Request.Params["projectId"] != this.CurrUser.LoginProjectId)
                {
                    this.ProjectId = Request.Params["projectId"];
                }
                ////权限按钮方法
                this.GetButtonPower();
                this.InitTreeMenu();
            }
        }
        #region 加载树
        /// 
        /// 加载树
        /// 
        private void InitTreeMenu()
        {
            this.trSafetyOrganization.Nodes.Clear();
            TreeNode rootNode = new TreeNode
            {
                Text = "安全组织机构",
                NodeID = "0",
                Expanded = true
            };
            this.trSafetyOrganization.Nodes.Add(rootNode);
            BoundTree(rootNode.Nodes);
        }
        /// 
        /// 加载树
        /// 
        /// 
        /// 
        private void BoundTree(TreeNodeCollection nodes)
        {
            var unitLists = BLL.ProjectUnitService.GetProjectUnitListByProjectId(this.ProjectId);
            if (unitLists.Count() > 0)
            {
                if (BLL.ProjectUnitService.GetProjectUnitTypeByProjectIdUnitId(this.ProjectId, this.CurrUser.UnitId))
                {
                    unitLists = unitLists.Where(x => x.UnitId == this.CurrUser.UnitId).ToList();
                }
                TreeNode tn = null;
                foreach (var dr in unitLists)
                {
                    tn = new TreeNode();
                    var unitName = BLL.UnitService.GetUnitNameByUnitId(dr.UnitId);
                    if (unitName != null)
                    {
                        tn.Text = unitName;
                    }
                    tn.NodeID = dr.UnitId;
                    tn.EnableClickEvent = true;
                    var gunitType = BLL.ConstValue.GetConstByConstValueAndGroupId(dr.UnitType, BLL.ConstValue.Group_ProjectUnitType);
                    if (gunitType != null)
                    {
                        tn.ToolTip = gunitType.ConstText + ":" + unitName;
                    }
                    //tn.ToolTip = "编号:" + dr.SafetyOrganizationCode + ";
机构名称:" + dr.SafetyOrganizationName + ";
职责:" + dr.Duties + ";
组成文件:" + dr.BundleFile + ";
机构人员:" + dr.AgencyPersonnel;
                    nodes.Add(tn);
                }
            }
        }
        #endregion
        
        #region 获取按钮权限
        /// 
        /// 获取按钮权限
        /// 
        /// 
        /// 
        private void GetButtonPower()
        {
            
            var buttonList = BLL.CommonService.GetAllButtonList(this.ProjectId, this.CurrUser.UserId, BLL.Const.ProjectSafetyOrganizationMenuId);
            if (buttonList.Count() > 0)
            {
                if (buttonList.Contains(BLL.Const.BtnAdd))
                {
                    this.btnNewItem.Hidden = false;
                }
                 
                if (buttonList.Contains(BLL.Const.BtnDelete))
                {
                    this.btnDeleteItem.Hidden = false; 
                }
            }
        }
        #endregion
        
         
        /// 
        /// 增加明细
        /// 
        /// 
        /// 
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(trSafetyOrganization.SelectedNodeID))
            {
                var unit = Funs.DB.Base_Unit.FirstOrDefault(x => x.UnitId == trSafetyOrganization.SelectedNodeID);
                unit.OrganizationPic =null;
                Funs.DB.SubmitChanges(); 
                this.imgPhoto.ImageUrl =null;
            }
            else
            {
                Alert.ShowInTop("请选择一个单位!", MessageBoxIcon.Warning);
            }
        }
        protected void filePhoto_FileSelected(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(trSafetyOrganization.SelectedNodeID))
            {
                if (filePhoto.HasFile)
                {
                    string rootUrl = ConfigurationManager.AppSettings["localRoot"];
                    if (string.IsNullOrEmpty(rootUrl))
                    {
                        rootUrl = Funs.RootPath;
                    }
                    string fileName = filePhoto.ShortFileName;
                    if (!ValidateFileType(fileName))
                    {
                        // 清空文件上传控件
                        filePhoto.Reset();
                        ShowNotify("无效的文件类型!");
                        return;
                    }
                    fileName = fileName.Replace(":", "_").Replace(" ", "_").Replace("\\", "_").Replace("/", "_");
                    fileName = DateTime.Now.Ticks.ToString() + "_" + fileName;
                    filePhoto.SaveAs(rootUrl + "/FileUpload/SafetyOrganizationPic/" + fileName);
                    imgPhoto.ImageUrl = "~/FileUpload/SafetyOrganizationPic/" + fileName;
                    var unit = Funs.DB.Project_ProjectUnit.FirstOrDefault(x => x.UnitId == trSafetyOrganization.SelectedNodeID&&x.ProjectId== this.CurrUser.LoginProjectId);
                    unit.OrganizationPic = "FileUpload/SafetyOrganizationPic/" + fileName;
                    Funs.DB.SubmitChanges();
                    // 清空文件上传组件(上传后要记着清空,否则点击提交表单时会再次上传!!)
                    filePhoto.Reset();
                }
            }
            else
            {
                Alert.ShowInTop("请选择一个单位!", MessageBoxIcon.Warning);
            }
        }
        /// 
        /// Tree点击事件
        /// 
        /// 
        /// 
        protected void trSafetyOrganization_NodeCommand(object sender, TreeCommandEventArgs e)
        {
            if (!string.IsNullOrEmpty(trSafetyOrganization.SelectedNodeID))
            {
                var unit = ProjectUnitService.GetProjectUnitByUnitIdProjectId(this.CurrUser.LoginProjectId,trSafetyOrganization.SelectedNodeID);
                if (!string.IsNullOrEmpty(unit.OrganizationPic))
                {
                    this.imgPhoto.ImageUrl = "~/" + unit.OrganizationPic;
                }
                else
                {
                    this.imgPhoto.ImageUrl = null;
                }
            }
            else
            {
                this.imgPhoto.ImageUrl = null;
            }
        }
         
    }
}