using DevExpress.XtraBars.ToolbarForm;
using DevExpress.XtraTreeList.Nodes;
using DeviceRepair.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using TsSFCDevice.Client.Biz.Base.Utils;
using TsSFCDevice.Client.Biz.Impl;

namespace TsSFCDevice.Client.Launch.Device
{
    public partial class pageRouteAssign : ToolbarForm
    {
        public DeviceRouteInfo CurrentSelectModel = null;
        List<DeviceRouteInfo> routes = null;
        int CurrentRoute = 0;

        public pageRouteAssign(int Route = 0)
        {
            InitializeComponent();
            CurrentRoute = Route;
            this.Load += PageRouteAssign_Load;
        }

        private void PageRouteAssign_Load(object sender, EventArgs e)
        {
            InitializeTreeDatas();

            if (CurrentRoute != 0)
            {
                foreach (TreeListNode node in treeList1.Nodes)
                {
                    SelectNodeByID(node, CurrentRoute);
                }
            }
        }

        private void SelectNodeByID(TreeListNode node, int targetID)
        {
            // 假设ID存储在节点的第一个列（索引0）
            if (node.GetValue("AutoID")?.ToString() == targetID.ToString())
            {
                treeList1.SetNodeCheckState(node, CheckState.Checked);
                treeList1.SetFocusedNode(node);
                //node.Checked = true;
                return;
            }

            // 递归处理子节点
            foreach (TreeListNode childNode in node.Nodes)
            {
                SelectNodeByID(childNode, targetID);
            }
        }

        void InitializeTreeDatas()
        {
            try
            {
                List<string> auths = new List<string>();
                if (Utility.SystemRuntimeInfo.AuthValidate(Utility.SystemRuntimeInfo.DEVICE_OEM))
                    auths.Add("OEM");
                if (Utility.SystemRuntimeInfo.AuthValidate(Utility.SystemRuntimeInfo.DEVICE_KH))
                    auths.Add("KH");
                routes = DevRepository.Instance.Get_DEVICE_Route(auths)?.ToList();
                if (routes == null || routes.Count == 0)
                {
                    throw new Exception("获取数据失败,请重试！");
                }

                treeList1.DataSource = routes;
                treeList1.KeyFieldName = "AutoID";
                treeList1.ParentFieldName = "ParentID";
                treeList1.ExpandAll();
            }
            catch (Exception ex)
            {
                XtraMessageBoxHelper.Error(ex.Message);
            }
        }

        private void treeList1_CustomDrawNodeCheckBox(object sender, DevExpress.XtraTreeList.CustomDrawNodeCheckBoxEventArgs e)
        {
            if (e.Node.Level == 1)
            {
                e.Handled = true;
                return;
            }
        }

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            List<TreeListNode> treeListNodes = treeList1.GetAllCheckedNodes();
            if ((treeListNodes?.Count ?? 0) > 0)
            {
                TreeListNode node = treeListNodes.FirstOrDefault(x => x.HasChildren == false);
                if (node == null)
                {
                    XtraMessageBoxHelper.Warn("选中数据不能为空！");
                    return;
                }

                CurrentSelectModel = routes.FirstOrDefault(x => x.AutoID.ToString() == node.GetValue("AutoID").ToString());
                if (CurrentSelectModel == null)
                {
                    XtraMessageBoxHelper.Warn("选中数据不能为空！");
                    return;
                }
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                XtraMessageBoxHelper.Warn("选中数据不能为空！");
            }
        }

        private void treeList1_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            treeList1.UncheckAll();
            e.Node.Checked = true;
        }

        private void treeList1_BeforeCheckNode(object sender, DevExpress.XtraTreeList.CheckNodeEventArgs e)
        {
            treeList1.UncheckAll();
        }
    }
}
