using BLL;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace FineUIPro.Web.TestRun.DriverSub
{
    public partial class DriverSubEdit :PageBase
    {
        public string DriverSubPlanId {
            get
            {
                return (string)ViewState["DriverSubPlanId"];
            }
            set
            {
                ViewState["DriverSubPlanId"] = value;
            }
        }
        public string DriverSubId
        {
            get
            {
                return (string)ViewState["DriverSubId"];
            }
            set
            {
                ViewState["DriverSubId"] = value;
            }
        }
        
        #region 加载
        /// 
        /// 页面加载
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.btnClose.OnClientClick = ActiveWindow.GetHideReference();
                DriverSubPlanId = Request.Params["DriverSubPlanId"];
                if (!string.IsNullOrEmpty(DriverSubPlanId))
                {
                     InitTreeMenu();
                }
            }
        }
        #endregion
        void InitTreeMenu()
        {
            this.tvControlItem.Nodes.Clear();
            var subPlanModel= DriverSubPlanService.GetDriverSubPlanById(DriverSubPlanId);
            if (subPlanModel != null)
            {
                foreach (var item in subPlanModel.SubcontractingTypes.Split(','))
                {
                    var name = DropListService.drpDriverSubNameList().Where(x => x.Value == item) .First().Text;    
                    FineUIPro.TreeNode node = new FineUIPro.TreeNode();
                    node.NodeID = item;
                    node.Text = name;
                    node.CommandName = name;
                    node.Expanded = true;
                    this.tvControlItem.Nodes.Add(node);
                    BindNode(node, item);
                }
            }
        }
        void BindNode(TreeNode node,string type )
        {
           var SubContractorsList=  DriverSubContactService.GetDriverSubContactByDriverSubPlanId(DriverSubPlanId)
                .Where(x => x.SubcontractingType == type && x.IsBidirectional==true).Select(x => x.DriverSubContractorsId).ToArray();
           foreach (var item in SubContractorsList)
           {
               string unitName= BLL.DriversubcontractorsService.GetDriverSub_DriverSubContractorsById(item).SubUnitName;
                FineUIPro.TreeNode node1 = new FineUIPro.TreeNode();
                node1.NodeID = item;
                node1.Text = unitName;
                node1.CommandName = type; 
                node1.EnableClickEvent=true;
                node.Nodes.Add(node1);
           }
        }
        protected void tvControlItem_NodeCommand(object sender, TreeCommandEventArgs e)
        {
           var list=  BLL.DriverSubService.GetDriverSubEvaluationData(DriverSubPlanId, tvControlItem.SelectedNodeID);
           var model=  BLL.DriverSubService.GetDriverSubBySubPlanIdAndTractorsId(DriverSubPlanId,tvControlItem.SelectedNodeID);
           if (model!=null)
           {
               DriverSubId = model.DriverSubId;
               txtDriverContractCode.Text = model.DriverContractCode;
               txtSubContractName.Text = model.SubContractName;
               txtEvaluator.Text = model.Evaluator;
           }
           else
           {
               DriverSubId = "";
           }
           if (list.Count>0 )
           { 
               Grid1.DataSource = list;
               Grid1.DataBind();
           }
           else
           { 
               Grid1.DataSource = BLL.DriverSubService.GetDriverSubEvaluationData(tvControlItem.SelectedNode.CommandName);
               Grid1.DataBind();
               
           }
        }
        #region 附件上传
        /// 
        /// 附件上传
        /// 
        /// 
        /// 
        protected void btnAttach_Click(object sender, EventArgs e)
        {
            Save();
            PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("../../AttachFile/webuploader.aspx?type=0&toKeyId={0}&path=FileUpload/TestRun/DriverSub/DriverSub&menuId={1}", DriverSubId, BLL.Const.DriverSubMenuId)));
        }
        #endregion
        #region 保存
        /// 
        /// 保存按钮
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            Save();
            tvControlItem_NodeCommand(null,null);
            ShowNotify("保存成功!", MessageBoxIcon.Success);
           // PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        void Save()
        {
            JArray EditorArr = Grid1.GetMergedData();
            //JArray 转换成List
            List list = new List();
            foreach (JObject item in EditorArr)
            {
                Model.DriverSubEvaluationData data = new Model.DriverSubEvaluationData();
                data.Number = Convert.ToInt32(item["values"]["Number"]);
                data.Matter = item["values"]["Matter"].ToString();
                data.Grade = item["values"]["Grade"].ToString();
                list.Add(data);
            }
            //list转换成json数据
            string json = BLL.DriverSubService.GetDriverSubEvaluationDataJson(list);
            Model.DriverSub_DriverSub newData = new Model.DriverSub_DriverSub();
            newData.ProjectId = this.CurrUser.LoginProjectId;
            newData.DriverSubPlanId = DriverSubPlanId;
            newData.DriverSubContractorsId = tvControlItem.SelectedNodeID;
            newData.EvaluationData = json;
            newData.DriverContractCode = txtDriverContractCode.Text;
            newData.SubContractName = txtSubContractName.Text;
            newData.Evaluator = txtEvaluator.Text;
            if (string.IsNullOrEmpty(DriverSubId))
            {
                newData.DriverSubId = SQLHelper.GetNewID(typeof(Model.DriverSub_DriverSub));
                DriverSubId= newData.DriverSubId;
                DriverSubService.AddDriverSub(newData);
            }
            else
            {
                newData.DriverSubId = DriverSubId; 
                DriverSubService.UpdateDriverSub(newData);
            }
            
        }
        #endregion
        protected void btnOut_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(DriverSubId))
            {
                ShowNotify("请先编辑数据!", MessageBoxIcon.Success);
                return;
            }
            BLL.DriverSubService.PrintFile(DriverSubPlanId, tvControlItem.SelectedNodeID);
        }
    }
}