using BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
namespace FineUIPro.Web.HJGL.HotHardManage
{
    public partial class HotHardManageAudit : PageBase
    {
        #region 定义项
        /// 
        /// 主键
        /// 
        public string HotHardID
        {
            get
            {
                return (string)ViewState["HotHardID"];
            }
            set
            {
                ViewState["HotHardID"] = value;
            }
        }
        /// 
        /// 明细集合
        /// 
        private static List viewHotHardItems = new List();
        #endregion
        #region 加载
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BLL.UserService.InitUserDropDownList(this.drpAuditMan, this.CurrUser.LoginProjectId, true);
                this.txtReportDate.Text = string.Format("{0:yyyy-MM}", DateTime.Now);
                this.txtAuditDate.Text = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
                this.drpAuditMan.SelectedValue = this.CurrUser.UserId;
                InitTreeMenu();//加载树
            }
        }
        #endregion
        #region 加载树装置-单位-工作区
        /// 
        /// 加载树
        /// 
        private void InitTreeMenu()
        {
            if (!string.IsNullOrEmpty(this.txtReportDate.Text.Trim()))
            {
                DateTime? startTime = Funs.GetNewDateTime(this.txtReportDate.Text.Trim());
                DateTime? endTime = startTime.HasValue ? startTime.Value.AddMonths(1) : System.DateTime.Now;
                this.tvControlItem.Nodes.Clear();
                TreeNode rootNode = new TreeNode();
                rootNode.Text = "单位-装置-月份";
                rootNode.NodeID = "0";
                rootNode.Expanded = true;
                this.tvControlItem.Nodes.Add(rootNode);
                List units = null;
                var unit = BLL.UnitService.GetUnitByUnitId(this.CurrUser.UnitId);
                if (unit == null || unit.UnitTypeId == BLL.Const.ProjectUnitType_1)
                {
                    units = BLL.UnitService.GetUnitByProjectIdUnitTypeList(this.CurrUser.LoginProjectId, "2");
                }
                else
                {
                    units = (from x in Funs.DB.Base_Unit where x.UnitId == this.CurrUser.UnitId select x).ToList();
                }
                List hotHardLists = new List(); ///硬度委托            
                hotHardLists = (from x in Funs.DB.HotHard
                                where x.ProjectId == this.CurrUser.LoginProjectId && x.HotHardDate >= startTime && x.HotHardDate < endTime
                                select x).ToList();
                if (units != null)
                {
                    foreach (var item in units)
                    {
                        TreeNode rootUnitNode = new TreeNode();//定义根节点
                        rootUnitNode.Text = item.UnitName;
                        rootUnitNode.NodeID = item.UnitId;
                        rootUnitNode.Expanded = true;
                        rootUnitNode.ToolTip = "施工单位";
                        rootNode.Nodes.Add(rootUnitNode);
                        var lists = hotHardLists.Where(x => x.HotHardUnit == item.UnitId).ToList();
                        this.BindNodes(rootUnitNode, lists);
                    }
                }
                else
                {
                    Alert.ShowInTop("请先增加施工单位!", MessageBoxIcon.Warning);
                    return;
                }
            }
            else
            {
                Alert.ShowInTop("请选择委托月份!", MessageBoxIcon.Warning);
                return;
            }
        }
        #endregion
        #region 绑定树节点
        /// 
        ///  绑定树节点
        /// 
        /// 
        private void BindNodes(TreeNode node, List hotHardLists)
        {
            if (node.ToolTip == "施工单位")
            {
                var installId = (from x in hotHardLists select x.InstallationId).Distinct();
                if (installId.Count() > 0)
                {
                    var install = from x in Funs.DB.Project_Installation where installId.Contains(x.InstallationId) orderby x.InstallationCode select x;
                    foreach (var q in install)
                    {
                        TreeNode newNode = new TreeNode();
                        newNode.Text = q.InstallationCode + q.InstallationName;
                        newNode.NodeID = q.InstallationId + "|" + node.NodeID; ;
                        newNode.ToolTip = "装置";
                        newNode.Expanded = true;
                        node.Nodes.Add(newNode);
                        this.BindNodes(newNode, hotHardLists);
                    }
                }
            }
            else if (node.ToolTip == "装置")
            {
                string installationId = Funs.GetStrListByStr(node.NodeID, '|')[0];
                var hotListMonth = (from x in hotHardLists
                                    where x.InstallationId == installationId && x.HotHardUnit == node.ParentNode.NodeID
                                    select string.Format("{0:yyyy-MM}", x.HotHardDate)).Distinct();
                foreach (var item in hotListMonth)
                {
                    TreeNode newNode = new TreeNode();
                    newNode.Text = item;
                    newNode.NodeID = item + "|" + node.NodeID; ;
                    newNode.ToolTip = "月份";
                    node.Nodes.Add(newNode);
                    this.BindNodes(newNode, hotHardLists);
                }
            }
            else if (node.ToolTip == "月份")
            {
                string installationId = Funs.GetStrListByStr(node.ParentNode.NodeID, '|')[0];
                var days = (from x in hotHardLists
                            where x.InstallationId == installationId && x.HotHardUnit == node.ParentNode.ParentNode.NodeID
                            orderby x.HotHardDate descending
                            select x.HotHardDate).Distinct();
                foreach (var item in days)
                {
                    TreeNode newNode = new TreeNode();
                    newNode.Text = string.Format("{0:yyyy-MM-dd}", item);
                    newNode.NodeID = item.ToString() + "|" + node.NodeID; ;
                    newNode.ToolTip = "日期";
                    node.Nodes.Add(newNode);
                    this.BindNodes(newNode, hotHardLists);
                }
            }
            else if (node.ToolTip == "日期")
            {
                string installationId = Funs.GetStrListByStr(node.ParentNode.ParentNode.NodeID, '|')[0];
                var dReports = from x in hotHardLists
                               where x.InstallationId == installationId && x.HotHardUnit == node.ParentNode.ParentNode.ParentNode.NodeID
                               && x.HotHardDate == Funs.GetNewDateTime(node.Text)
                               orderby x.HotHardCode descending
                               select x;
                foreach (var item in dReports)
                {
                    TreeNode newNode = new TreeNode();
                    if (!string.IsNullOrEmpty(item.HotHardCode))
                    {
                        newNode.Text = item.HotHardCode;
                    }
                    else
                    {
                        newNode.Text = "未知";
                    }
                    if (!item.AuditDate.HasValue || string.IsNullOrEmpty(item.AuditMan))
                    {
                        newNode.Text = "" + newNode.Text + "";
                        node.Text = "" + node.Text + "";
                        node.ParentNode.Text = "" + node.ParentNode.Text + "";
                    }
                    newNode.NodeID = item.HotHardID;
                    newNode.EnableClickEvent = true;
                    node.Nodes.Add(newNode);
                }
            }
        }
        #endregion
        #region 查询树节点
        protected void Tree_TextChanged(object sender, EventArgs e)
        {
            this.InitTreeMenu();
        }
        #endregion
        #region 点击树节点
        /// 
        /// 点击树节点
        /// 
        /// 
        /// 
        protected void tvControlItem_NodeCommand(object sender, TreeCommandEventArgs e)
        {
            viewHotHardItems = new List();
            this.HotHardID = this.tvControlItem.SelectedNodeID;
            if (!string.IsNullOrEmpty(this.HotHardID))
            {
                Model.HotHard hotHard = BLL.HotHardManageEditService.GetHotHardByID(this.HotHardID);
                if (hotHard != null)
                {
                    if (!string.IsNullOrEmpty(hotHard.HotHardUnit))
                    {
                        this.txtHotHardUnitName.Text = BLL.UnitService.GetUnitNameByUnitId(hotHard.HotHardUnit);
                    }
                    if (!string.IsNullOrEmpty(hotHard.InstallationId))
                    {
                        this.txtInstallationName.Text = BLL.Project_InstallationService.GetInstallationByInstallationId(hotHard.InstallationId).InstallationName;
                    }
                    this.txtHotHardCode.Text = hotHard.HotHardCode;
                    if (!string.IsNullOrEmpty(hotHard.CheckUnit))
                    {
                        this.txtCheckUnitName.Text = BLL.UnitService.GetUnitNameByUnitId(hotHard.CheckUnit);
                    }
                    if (!string.IsNullOrEmpty(hotHard.HotHardMan))
                    {
                        this.txtHotHardManName.Text = BLL.UserService.GetUserNameByUserId(hotHard.HotHardMan);
                    }
                    this.txtHotHardDate.Text = hotHard.HotHardDate.HasValue ? string.Format("{0:yyyy-MM-dd}", hotHard.HotHardDate) : string.Format("{0:yyyy-MM-dd}", DateTime.Now);
                    this.txtNDTMethod.Text = hotHard.NDTMethod;
                    this.txtNDTRate.Text = hotHard.NDTRate;
                    this.txtStandards.Text = hotHard.Standards;
                    this.txtInspectionNum.Text = hotHard.InspectionNum;
                    this.txtCheckNum.Text = hotHard.CheckNum;
                    this.txtTestWeldNum.Text = hotHard.TestWeldNum;
                    this.cblDetectionTime.SelectedValueArray = hotHard.DetectionTime.Split(',');
                    this.txtSendee.Text = hotHard.Sendee;
                    if (!string.IsNullOrEmpty(hotHard.AuditMan))
                    {
                        this.drpAuditMan.SelectedValue = hotHard.AuditMan;
                    }
                    this.txtAuditDate.Text = hotHard.AuditDate.HasValue ? string.Format("{0:yyyy-MM-dd}", hotHard.AuditDate) : string.Format("{0:yyyy-MM-dd}", DateTime.Now);
                    viewHotHardItems.Clear();
                    viewHotHardItems = BLL.HotHardManageEditService.GetView_HotHardItemByHotHardID(this.HotHardID);
                    if (viewHotHardItems.Count > 0)
                    {
                        this.Grid1.DataSource = viewHotHardItems;
                        this.Grid1.DataBind();
                    }
                    //隐藏/显示审核/取消审核按钮
                    if (!string.IsNullOrEmpty(hotHard.AuditMan) && hotHard.AuditDate.HasValue)
                    {
                        this.btnAudit.Hidden = true;
                        this.btnCancelAudit.Hidden = false;
                    }
                    else
                    {
                        this.btnAudit.Hidden = false;
                        this.btnCancelAudit.Hidden = true;
                    }
                }
            }
        }
        #endregion
        #region 审核
        /// 
        /// 审核
        /// 
        /// 
        /// 
        protected void btnAudit_Click(object sender, EventArgs e)
        {
            if (this.GetButtonPower(BLL.Const.BtnAuditing))
            {
                if (!string.IsNullOrEmpty(this.HotHardID))
                {
                    var updateHotHard = BLL.HotHardManageEditService.GetHotHardByID(this.HotHardID);
                    if (updateHotHard != null)
                    {
                        updateHotHard.HotHardID = this.HotHardID;
                        if (!string.IsNullOrEmpty(this.txtAuditDate.Text) && this.drpAuditMan.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpAuditMan.SelectedValue))
                        {
                            updateHotHard.AuditDate = Funs.GetNewDateTime(this.txtAuditDate.Text);
                            updateHotHard.AuditMan = this.drpAuditMan.SelectedValue;
                            BLL.HotHardManageEditService.AuditHotHard(updateHotHard);
                            this.btnCancelAudit.Hidden = false;
                            this.btnAudit.Hidden = true;
                            InitTreeMenu();
                            ShowNotify("审核完成!", MessageBoxIcon.Success);
                        }
                        else
                        {
                            Alert.ShowInTop("请填写审核人和审核日期!", MessageBoxIcon.Warning);
                        }
                    }
                    else
                    {
                        Alert.ShowInTop("请选择要审核的单据!", MessageBoxIcon.Warning);
                    }
                }
            }
            else
            {
                Alert.ShowInTop("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
            }
        }
        #endregion
        #region 取消审核
        /// 
        /// 取消审核
        /// 
        /// 
        /// 
        protected void btnCancelAudit_Click(object sender, EventArgs e)
        {
            if (this.GetButtonPower(BLL.Const.BtnCancelAuditing))
            {
                if (!string.IsNullOrEmpty(this.HotHardID))
                {
                    var updateHotHard = BLL.HotHardManageEditService.GetHotHardByID(HotHardID);
                    if (updateHotHard != null)
                    {
                        updateHotHard.HotHardID = this.HotHardID;
                        updateHotHard.AuditDate = null;
                        updateHotHard.AuditMan = null;
                        this.btnCancelAudit.Hidden = true;
                        this.btnAudit.Hidden = false;
                        InitTreeMenu();
                        BLL.HotHardManageEditService.AuditHotHard(updateHotHard);
                        ShowNotify("取消审核完成!", MessageBoxIcon.Success);
                    }
                }
            }
            else
            {
                Alert.ShowInTop("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
            }
        }
        #endregion
        #region 获取按钮权限
        /// 
        /// 获取按钮权限
        /// 
        /// 
        /// 
        private bool GetButtonPower(string button)
        {
            return BLL.CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.HJGL_HotHardManageAuditMenuId, button);
        }
        #endregion
    }
}