using DevExpress.XtraEditors;
using DevExpress.XtraTreeList.Nodes;
using DeviceRepairAndOptimization.Data;
using DeviceRepairAndOptimization.Models;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace DeviceRepairAndOptimization.Pages.DriveMaintenance
{
    public partial class xuc_BindDrives : XtraUserControl
    {
        int did = 0;
        private List<DriveInfomationModel> lst;

        public int[] SelectRowAutoIds
        {
            get
            {
                // 获取选中的节点
                List<TreeListNode> checkedNodes = treeList1.GetAllCheckedNodes();
                int[] ids = checkedNodes.Select(x => x.Id).ToArray();

                List<DriveInfomationModel> checkedLst = new List<DriveInfomationModel>();
                foreach (int row_Id in ids)
                {
                    DriveInfomationModel item = (DriveInfomationModel)treeList1.GetRow(row_Id);
                    if (item.AutoID != 0)
                        checkedLst.Add(item);
                }

                return checkedLst.Select(x => x.AutoID).ToArray();
            }
        }

        /// <summary>
        /// 数据加载
        /// </summary>
        /// <returns></returns>
        List<DriveInfomationModel> GetDatas()
        {
            //return DriveInformationMaintenance.Instance().GetQueryTree(did);
            return new List<DriveInfomationModel>();
        }

        void BindData()
        {
            lst = GetDatas();

            treeList1.Nodes.Clear();
            treeList1.BeginUpdate();
            treeList1.KeyFieldName = "TreeId";
            treeList1.ParentFieldName = "ParentId";
            treeList1.CheckBoxFieldName = "IsChecked";
            treeList1.DataSource = lst;
            treeList1.EndUpdate();
            treeList1.ExpandAll();
            treeList1.BestFitColumns();
        }


        public xuc_BindDrives(int driveId)
        {
            InitializeComponent();
            did = driveId;
        }

        private void xuc_BindDrives_Load(object sender, System.EventArgs e)
        {
            BindData();
        }

        private void treeList1_BeforeCheckNode(object sender, DevExpress.XtraTreeList.CheckNodeEventArgs e)
        {
            e.State = (e.PrevState == CheckState.Checked ? CheckState.Unchecked : CheckState.Checked);
        }

        private void treeList1_AfterCheckNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)
        {
            SetCheckedChildNodes(e.Node, e.Node.CheckState);
            SetCheckedParentNodes(e.Node, e.Node.CheckState);
        }

        /// <summary>
        /// 设置子节点的状态
        /// </summary>
        /// <param name="node"></param>
        /// <param name="check"></param>
        private void SetCheckedChildNodes(DevExpress.XtraTreeList.Nodes.TreeListNode node, CheckState check)
        {
            for (int i = 0; i < node.Nodes.Count; i++)
            {
                node.Nodes[i].CheckState = check;
                SetCheckedChildNodes(node.Nodes[i], check);
            }
        }
        /// <summary>
        /// 设置父节点的状态
        /// </summary>
        /// <param name="node"></param>
        /// <param name="check"></param>
        private void SetCheckedParentNodes(DevExpress.XtraTreeList.Nodes.TreeListNode node, CheckState check)
        {
            if (node.ParentNode != null)
            {
                bool b = false;
                CheckState state;
                for (int i = 0; i < node.ParentNode.Nodes.Count; i++)
                {
                    state = (CheckState)node.ParentNode.Nodes[i].CheckState;
                    if (!check.Equals(state))
                    {
                        b = !b;
                        break;
                    }
                }
                node.ParentNode.CheckState = b ? CheckState.Indeterminate : check;
                SetCheckedParentNodes(node.ParentNode, check);
            }
        }

    }
}
