using KGIS.Framework.DBOperator;
using KGIS.Framework.Utils;
using KGIS.Framework.Utils.Helper;
using System;
using System.Collections.ObjectModel;
using System.Data;
using System.Windows;
namespace Kingo.Plugin.DataDictionary.Views
{
    /// 
    /// DicHelpTree.xaml 的交互逻辑
    /// 
    public partial class DicHelpTree : BaseWindow
    {
        /// 
        /// 委托函数
        /// 
        /// 名称
        /// ID
        public delegate void DelegateGetValue(string sName, string sID);
        /// 
        /// 获取返回值事件
        /// 
        public event DelegateGetValue GetValueHandler;
        /// 
        /// 父级ID
        /// 
        public string OWNERDIC { get; set; }
        //编辑时使用,排除自己的数据显示
        public string MINEID { get; set; }
        public DicHelpTree()
        {
            InitializeComponent();
        }
        //取消按钮
        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
        //清空事件
        private void btnClean_Click(object sender, RoutedEventArgs e)
        {
            if (GetValueHandler != null)
            {
                GetValueHandler("", "");
            }
            this.Close();
        }
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (GetValueHandler != null)
            {
                TreeNode item = this.tvHelpTree.SelectedItem as TreeNode;
                if (item != null && item.Data != null)
                {
                    DataRowView drv = item.Data as DataRowView;
                    string sID = drv["ID"].ToString();
                    GetValueHandler(item.Name, sID);
                }
            }
            this.Close();
        }
        //加载
        private void BaseWindow_Loaded(object sender, RoutedEventArgs e)
        {
            //tvHelpTree
            IRDBHelper rdbHelper = null;
            try
            {
                string connStr = SysConfigsOprator.GetDBConnectionByName("MDBOledbConnection");
                connStr = string.Format(connStr, SysAppPath.GetCurrentAppPath() + @"DataBase\System.mdb"); ;
                rdbHelper = RDBFactory.CreateDbHelper(connStr, DatabaseType.MSAccess);
                DataTable dtDict = rdbHelper.ExecuteDatatable("Sys_DicDetail", "select * from Sys_DicDetail", false);
                ObservableCollection itemList = new ObservableCollection();
                DataView dvDict = dtDict.DefaultView;
                dvDict.RowFilter = " OWNERDIC='" + OWNERDIC + "' and (PID is null or PID='')";
                if (dvDict.Count > 0)
                {
                    foreach (DataRowView view in dvDict)
                    {
                        int id = Convert.ToInt32(view["ID"].ToString());
                        string name = view["NAME"].ToString() + "[" + view["CODE"].ToString() + "]";
                        if (MINEID == view["OBJECTID"].ToString())
                        {
                            continue;
                        }
                        TreeNode childNodeItem = new TreeNode()
                        {
                            Name = name,
                            Icon = "pack://application:,,,/KGIS.PlatformPlugin;component/Resources/zrz.png",
                            IsExpanded = false,
                            Data = view
                        };
                        ForeachPropertyNode(childNodeItem, id, dtDict);
                        itemList.Add(childNodeItem);
                    }
                }
                //ForeachPropertyNode(node, id);
                //itemList.Add(node);
                this.tvHelpTree.ItemsSource = itemList;
            }
            catch (Exception ex)
            {
                MessageHelper.ShowError("数据加载异常!" + ex.Message);
                LogAPI.Debug(ex);
            }
            finally
            {
                if (rdbHelper != null)
                {
                    rdbHelper.DisConnect();
                }
            }
        }
        //无限极增加子节点
        private void ForeachPropertyNode(TreeNode node, int pid, DataTable dtDict)
        {
            DataView dvDict = dtDict.DefaultView;
            dvDict.RowFilter = " PID='" + pid.ToString() + "'";
            if (dvDict.Count > 0)
            {
                node.Nodes = new System.Collections.ObjectModel.ObservableCollection();
                foreach (DataRowView view in dvDict)
                {
                    int id = Convert.ToInt32(view["ID"].ToString());
                    string name = view["NAME"].ToString() + "[" + view["CODE"].ToString() + "]"; ;
                    int parentId = Convert.ToInt32(view["PID"].ToString());
                    if (MINEID == view["ID"].ToString())
                    {
                        continue;
                    }
                    TreeNode childNodeItem = new TreeNode()
                    {
                        Name = name,
                        Icon = "pack://application:,,,/KGIS.PlatformPlugin;component/Resources/zrz.png",
                        IsExpanded = false,
                        Data = view
                    };
                    ForeachPropertyNode(childNodeItem, id, dtDict);
                    node.Nodes.Add(childNodeItem);
                }
            }
        }
    }
}