using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AntdUI;
using DH.Commons.Base;
using DH.Commons.Enums;
using DH.Commons.Models;
using XKRS.CanFly;

namespace DHSoftware.Views
{
    public partial class GlobalControl : UserControl
    {
        private Window window;
        private GlobalConfig global;
        public GlobalControl(Window _window, GlobalConfig _global)
        {
            window = _window;
            global = _global;
            InitializeComponent();
            BindEventHandler();
            InitData();
    
        }

        private void BindEventHandler()
        {
       
            btnAdd.Click += BtnAdd_Click;
            btnDelete.Click += BtnDelete_Click;
            TableInitProcess.CellButtonClick += ItemsTable_CellButtonClick;

            btnSartProcessAdd.Click += BtnSartProcessAdd_Click;
            btnSartProcessDelete.Click += BtnSartProcessDelete_Click;
            TableSartProcess.CellButtonClick += TableSartProcess_CellButtonClick;

            btnStopProcessAdd.Click += BtnStopProcessAdd_Click;
            btnStopProcessDelete.Click += BtnStopProcessDelete_Click;
            TableStopProcess.CellButtonClick += TableStopProcess_CellButtonClick;

            btnStartResetAdd.Click += BtnStartResetAdd_Click;
            btnStartResetDelete.Click += BtnStartResetDelete_Click;
            TableStartReset.CellButtonClick += TableStartReset_CellButtonClick;

            btnStopResetAdd.Click += BtnStopResetAdd_Click;
            btnStopResetDelete.Click += BtnStopResetDelete_Click;
            TableStopReset.CellButtonClick += TableStopReset_CellButtonClick;
        }

        private void TableStopReset_CellButtonClick(object sender, TableButtonEventArgs e)
        {
            var buttontext = e.Btn.Text;

            if (e.Record is PLCItem Item)
            {
                switch (buttontext)
                {
                    //暂不支持进入整行编辑,只支持指定单元格编辑,推荐使用弹窗或抽屉编辑整行数据
                    case "编辑":
                        var form = new MotionProcessEdit(window, "复位结束表操作-编辑", Item, ConfigModel.PLCBaseList?.FirstOrDefault()?.PLCItemList) { Size = new Size(500, 300) };
                        AntdUI.Drawer.open(new AntdUI.Drawer.Config(window, form)
                        {
                            OnLoad = () =>
                            {
                                AntdUI.Message.info(window, "进入复位结束表编辑", autoClose: 1);
                            },
                            OnClose = () =>
                            {
                                AntdUI.Message.info(window, "退出复位结束表编辑", autoClose: 1);
                            }
                        });
                        break;

                    case "删除":
                        var result = Modal.open(window, "删除警告!", "确认要删除选择的数据吗?", TType.Warn);
                        if (result == DialogResult.OK)
                        {
                            global.StopResetList.Remove(Item);
                        }
                        break;

                    case "上移":
                        if (e.RowIndex <= 1)
                        {
                            AntdUI.Message.warn(window, "已是第一条,无法上移!", autoClose: 3);
                            return;
                        }
                        MoveItemUp(global.StopResetList, Item);
                        break;

                    case "下移":
                        if (e.RowIndex > global.StopResetList.Count - 1)
                        {
                            AntdUI.Message.warn(window, "已是最后一条,无法下移!", autoClose: 3);
                            return;
                        }
                        MoveItemDown(global.StopResetList, Item);
                        break;
                }
            }
        }

        private void BtnStopResetDelete_Click(object? sender, EventArgs e)
        {
            if (global.StopResetList.Count == 0 || !global.StopResetList.Any(x => x.Selected))
            {
                AntdUI.Message.warn(window, "请选择要删除的行!", autoClose: 3);
                return;
            }

            var result = Modal.open(window, "删除警告!", "确认要删除选择的数据吗?", TType.Warn);
            if (result == DialogResult.OK)
            {
                // 使用反转for循环删除主列表中选中的项
                for (int i = global.StopResetList.Count - 1; i >= 0; i--)
                {
                    // 删除选中的主列表项
                    if (global.StopResetList[i].Selected)
                    {
                        global.StopResetList.RemoveAt(i);
                    }
                }
                // 提示删除完成
                AntdUI.Message.success(window, "删除成功!", autoClose: 3);
            }
        }

        private void BtnStopResetAdd_Click(object? sender, EventArgs e)
        {
            if (ConfigModel.PLCBaseList?.FirstOrDefault()?.PLCItemList.Count == 0)
            {
                AntdUI.Message.warn(window, "点位表无数据,不允许新增!", autoClose: 3);
                return;
            }

            PLCItem Item = new PLCItem()
            {
                StartIndex = global.StopResetList.Count + 1,
                CellLinks = new CellLink[]
           {
                    new CellButton(Guid.NewGuid().ToString(), "上移", TTypeMini.Default),
                    new CellButton(Guid.NewGuid().ToString(), "下移", TTypeMini.Default),
                    new CellButton(Guid.NewGuid().ToString(), "编辑", TTypeMini.Primary),
                    new CellButton(Guid.NewGuid().ToString(), "删除", TTypeMini.Error)
           }
            };

            var form = new MotionProcessEdit(window, "复位结束表操作-新增", Item, ConfigModel.PLCBaseList?.FirstOrDefault()?.PLCItemList) { Size = new Size(450, 550) };

            AntdUI.Drawer.open(new AntdUI.Drawer.Config(window, form)
            {
                OnLoad = () =>
                {
                    AntdUI.Message.info(window, "进入复位结束表新增", autoClose: 1);
                },
                OnClose = () =>
                {
                    if (form.submit)
                    {
                        global.StopResetList.Add(Item);
                    }

                    AntdUI.Message.info(window, "退出复位结束表新增", autoClose: 1);
                }
            });
        }

        private void TableStartReset_CellButtonClick(object sender, TableButtonEventArgs e)
        {
            var buttontext = e.Btn.Text;

            if (e.Record is PLCItem Item)
            {
                switch (buttontext)
                {
                    //暂不支持进入整行编辑,只支持指定单元格编辑,推荐使用弹窗或抽屉编辑整行数据
                    case "编辑":
                        var form = new MotionProcessEdit(window, "复位开始表操作-编辑", Item, ConfigModel.PLCBaseList?.FirstOrDefault()?.PLCItemList) { Size = new Size(500, 300) };
                        AntdUI.Drawer.open(new AntdUI.Drawer.Config(window, form)
                        {
                            OnLoad = () =>
                            {
                                AntdUI.Message.info(window, "进入复位开始表编辑", autoClose: 1);
                            },
                            OnClose = () =>
                            {
                                AntdUI.Message.info(window, "退出复位开始表编辑", autoClose: 1);
                            }
                        });
                        break;

                    case "删除":
                        var result = Modal.open(window, "删除警告!", "确认要删除选择的数据吗?", TType.Warn);
                        if (result == DialogResult.OK)
                        {
                            global.StartResetList.Remove(Item);
                        }
                        break;

                    case "上移":
                        if (e.RowIndex <= 1)
                        {
                            AntdUI.Message.warn(window, "已是第一条,无法上移!", autoClose: 3);
                            return;
                        }
                        MoveItemUp(global.StartResetList, Item);
                        break;

                    case "下移":
                        if (e.RowIndex > global.StartResetList.Count - 1)
                        {
                            AntdUI.Message.warn(window, "已是最后一条,无法下移!", autoClose: 3);
                            return;
                        }
                        MoveItemDown(global.StartResetList, Item);
                        break;
                }
            }
        }

        private void BtnStartResetDelete_Click(object? sender, EventArgs e)
        {
            if (global.StartResetList.Count == 0 || !global.StartResetList.Any(x => x.Selected))
            {
                AntdUI.Message.warn(window, "请选择要删除的行!", autoClose: 3);
                return;
            }

            var result = Modal.open(window, "删除警告!", "确认要删除选择的数据吗?", TType.Warn);
            if (result == DialogResult.OK)
            {
                // 使用反转for循环删除主列表中选中的项
                for (int i = global.StartResetList.Count - 1; i >= 0; i--)
                {
                    // 删除选中的主列表项
                    if (global.StartResetList[i].Selected)
                    {
                        global.StartResetList.RemoveAt(i);
                    }
                }
                // 提示删除完成
                AntdUI.Message.success(window, "删除成功!", autoClose: 3);
            }
        }

        private void BtnStartResetAdd_Click(object? sender, EventArgs e)
        {
            if (ConfigModel.PLCBaseList?.FirstOrDefault()?.PLCItemList.Count == 0)
            {
                AntdUI.Message.warn(window, "点位表无数据,不允许新增!", autoClose: 3);
                return;
            }


            PLCItem Item = new PLCItem()
            {
                StartIndex = global.StartResetList.Count + 1,
                CellLinks = new CellLink[]
           {
                    new CellButton(Guid.NewGuid().ToString(), "上移", TTypeMini.Default),
                    new CellButton(Guid.NewGuid().ToString(), "下移", TTypeMini.Default),
                    new CellButton(Guid.NewGuid().ToString(), "编辑", TTypeMini.Primary),
                    new CellButton(Guid.NewGuid().ToString(), "删除", TTypeMini.Error)
           }
            };

            var form = new MotionProcessEdit(window, "复位开始表操作-新增", Item, ConfigModel.PLCBaseList?.FirstOrDefault()?.PLCItemList) { Size = new Size(450, 550) };

            AntdUI.Drawer.open(new AntdUI.Drawer.Config(window, form)
            {
                OnLoad = () =>
                {
                    AntdUI.Message.info(window, "进入复位开始表新增", autoClose: 1);
                },
                OnClose = () =>
                {
                    if (form.submit)
                    {
                        global.StartResetList.Add(Item);
                    }

                    AntdUI.Message.info(window, "退出复位开始表新增", autoClose: 1);
                }
            });
        }

        private void TableStopProcess_CellButtonClick(object sender, TableButtonEventArgs e)
        {
            var buttontext = e.Btn.Text;

            if (e.Record is PLCItem Item)
            {
                switch (buttontext)
                {
                    //暂不支持进入整行编辑,只支持指定单元格编辑,推荐使用弹窗或抽屉编辑整行数据
                    case "编辑":
                        var form = new MotionProcessEdit(window, "流程结束表操作-编辑", Item, ConfigModel.PLCBaseList?.FirstOrDefault()?.PLCItemList) { Size = new Size(500, 300) };
                        AntdUI.Drawer.open(new AntdUI.Drawer.Config(window, form)
                        {
                            OnLoad = () =>
                            {
                                AntdUI.Message.info(window, "进入流程结束表编辑", autoClose: 1);
                            },
                            OnClose = () =>
                            {
                                AntdUI.Message.info(window, "退出流程结束表编辑", autoClose: 1);
                            }
                        });
                        break;

                    case "删除":
                        var result = Modal.open(window, "删除警告!", "确认要删除选择的数据吗?", TType.Warn);
                        if (result == DialogResult.OK)
                        {
                            global.StopProcessList.Remove(Item);
                        }
                        break;

                    case "上移":
                        if (e.RowIndex <= 1)
                        {
                            AntdUI.Message.warn(window, "已是第一条,无法上移!", autoClose: 3);
                            return;
                        }
                        MoveItemUp(global.StopProcessList, Item);
                        break;

                    case "下移":
                        if (e.RowIndex > global.StopProcessList.Count - 1)
                        {
                            AntdUI.Message.warn(window, "已是最后一条,无法下移!", autoClose: 3);
                            return;
                        }
                        MoveItemDown(global.StopProcessList, Item);
                        break;
                }
            }
        }

        private void BtnStopProcessDelete_Click(object? sender, EventArgs e)
        {
            if (global.StopProcessList.Count == 0 || !global.StopProcessList.Any(x => x.Selected))
            {
                AntdUI.Message.warn(window, "请选择要删除的行!", autoClose: 3);
                return;
            }

            var result = Modal.open(window, "删除警告!", "确认要删除选择的数据吗?", TType.Warn);
            if (result == DialogResult.OK)
            {
                // 使用反转for循环删除主列表中选中的项
                for (int i = global.StopProcessList.Count - 1; i >= 0; i--)
                {
                    // 删除选中的主列表项
                    if (global.StopProcessList[i].Selected)
                    {
                        global.StopProcessList.RemoveAt(i);
                    }
                }
                // 提示删除完成
                AntdUI.Message.success(window, "删除成功!", autoClose: 3);
            }
        }

        private void BtnStopProcessAdd_Click(object? sender, EventArgs e)
        {
            if (ConfigModel.PLCBaseList?.FirstOrDefault()?.PLCItemList.Count == 0)
            {
                AntdUI.Message.warn(window, "点位表无数据,不允许新增!", autoClose: 3);
                return;
            }


            PLCItem Item = new PLCItem()
            {
                StartIndex = global.StopProcessList.Count + 1,
                CellLinks = new CellLink[]
           {
                    new CellButton(Guid.NewGuid().ToString(), "上移", TTypeMini.Default),
                    new CellButton(Guid.NewGuid().ToString(), "下移", TTypeMini.Default),
                    new CellButton(Guid.NewGuid().ToString(), "编辑", TTypeMini.Primary),
                    new CellButton(Guid.NewGuid().ToString(), "删除", TTypeMini.Error)
           }
            };

            var form = new MotionProcessEdit(window, "流程结束表操作-新增", Item, ConfigModel.PLCBaseList?.FirstOrDefault()?.PLCItemList) { Size = new Size(450, 550) };

            AntdUI.Drawer.open(new AntdUI.Drawer.Config(window, form)
            {
                OnLoad = () =>
                {
                    AntdUI.Message.info(window, "进入流程结束表新增", autoClose: 1);
                },
                OnClose = () =>
                {
                    if (form.submit)
                    {
                        global.StopProcessList.Add(Item);
                    }

                    AntdUI.Message.info(window, "退出流程结束表新增", autoClose: 1);
                }
            });
        }

        private void TableSartProcess_CellButtonClick(object sender, TableButtonEventArgs e)
        {
            var buttontext = e.Btn.Text;

            if (e.Record is PLCItem Item)
            {
                switch (buttontext)
                {
                    //暂不支持进入整行编辑,只支持指定单元格编辑,推荐使用弹窗或抽屉编辑整行数据
                    case "编辑":
                        var form = new MotionProcessEdit(window, "流程开始表操作-编辑", Item, ConfigModel.PLCBaseList?.FirstOrDefault()?.PLCItemList) { Size = new Size(500, 300) };
                        AntdUI.Drawer.open(new AntdUI.Drawer.Config(window, form)
                        {
                            OnLoad = () =>
                            {
                                AntdUI.Message.info(window, "进入流程开始表编辑", autoClose: 1);
                            },
                            OnClose = () =>
                            {
                                AntdUI.Message.info(window, "退出流程开始表编辑", autoClose: 1);
                            }
                        });
                        break;

                    case "删除":
                        var result = Modal.open(window, "删除警告!", "确认要删除选择的数据吗?", TType.Warn);
                        if (result == DialogResult.OK)
                        {
                            global.StartProcessList.Remove(Item);
                        }
                        break;

                    case "上移":
                        if (e.RowIndex <= 1)
                        {
                            AntdUI.Message.warn(window, "已是第一条,无法上移!", autoClose: 3);
                            return;
                        }
                        MoveItemUp(global.StartProcessList, Item);
                        break;

                    case "下移":
                        if (e.RowIndex > global.StartProcessList.Count - 1)
                        {
                            AntdUI.Message.warn(window, "已是最后一条,无法下移!", autoClose: 3);
                            return;
                        }
                        MoveItemDown(global.StartProcessList, Item);
                        break;
                }
            }
        }

        private void BtnSartProcessDelete_Click(object? sender, EventArgs e)
        {
            if (global.StartProcessList.Count == 0 || !global.StartProcessList.Any(x => x.Selected))
            {
                AntdUI.Message.warn(window, "请选择要删除的行!", autoClose: 3);
                return;
            }

            var result = Modal.open(window, "删除警告!", "确认要删除选择的数据吗?", TType.Warn);
            if (result == DialogResult.OK)
            {
                // 使用反转for循环删除主列表中选中的项
                for (int i = global.StartProcessList.Count - 1; i >= 0; i--)
                {
                    // 删除选中的主列表项
                    if (global.StartProcessList[i].Selected)
                    {
                        global.StartProcessList.RemoveAt(i);
                    }
                }
                // 提示删除完成
                AntdUI.Message.success(window, "删除成功!", autoClose: 3);
            }
        }

        private void BtnSartProcessAdd_Click(object? sender, EventArgs e)
        {
            if (ConfigModel.PLCBaseList?.FirstOrDefault()?.PLCItemList.Count == 0)
            {
                AntdUI.Message.warn(window, "点位表无数据,不允许新增!", autoClose: 3);
                return;
            }


            PLCItem Item = new PLCItem()
            {
                StartIndex = global.StartProcessList.Count + 1,
                CellLinks = new CellLink[]
           {
                    new CellButton(Guid.NewGuid().ToString(), "上移", TTypeMini.Default),
                    new CellButton(Guid.NewGuid().ToString(), "下移", TTypeMini.Default),
                    new CellButton(Guid.NewGuid().ToString(), "编辑", TTypeMini.Primary),
                    new CellButton(Guid.NewGuid().ToString(), "删除", TTypeMini.Error)
           }
            };

            var form = new MotionProcessEdit(window, "流程开始表操作-新增", Item, ConfigModel.PLCBaseList?.FirstOrDefault()?.PLCItemList) { Size = new Size(450, 550) };

            AntdUI.Drawer.open(new AntdUI.Drawer.Config(window, form)
            {
                OnLoad = () =>
                {
                    AntdUI.Message.info(window, "进入流程开始表新增", autoClose: 1);
                },
                OnClose = () =>
                {
                    if (form.submit)
                    {
                        global.StartProcessList.Add(Item);
                    }

                    AntdUI.Message.info(window, "退出流程开始表新增", autoClose: 1);
                }
            });
        }

        private void ItemsTable_CellButtonClick(object sender, TableButtonEventArgs e)
        {
            var buttontext = e.Btn.Text;

            if (e.Record is PLCItem Item)
            {
                switch (buttontext)
                {
                    //暂不支持进入整行编辑,只支持指定单元格编辑,推荐使用弹窗或抽屉编辑整行数据
                    case "编辑":
                        var form = new MotionProcessEdit(window, "流程加载表操作-编辑", Item, ConfigModel.PLCBaseList?.FirstOrDefault()?.PLCItemList) { Size = new Size(500, 300) };
                        AntdUI.Drawer.open(new AntdUI.Drawer.Config(window, form)
                        {
                            OnLoad = () =>
                            {
                                AntdUI.Message.info(window, "进入流程加载表编辑", autoClose: 1);
                            },
                            OnClose = () =>
                            {
                                AntdUI.Message.info(window, "退出流程加载表编辑", autoClose: 1);
                            }
                        });
                        break;

                    case "删除":
                        var result = Modal.open(window, "删除警告!", "确认要删除选择的数据吗?", TType.Warn);
                        if (result == DialogResult.OK)
                        {
                            global.InitProcessList.Remove(Item);
                        }
                        break;

                    case "上移":
                        if (e.RowIndex <= 1)
                        {
                            AntdUI.Message.warn(window, "已是第一条,无法上移!", autoClose: 3);
                            return;
                        }
                        MoveItemUp(global.InitProcessList, Item);
                        break;

                    case "下移":
                        if (e.RowIndex > global.InitProcessList.Count - 1)
                        {
                            AntdUI.Message.warn(window, "已是最后一条,无法下移!", autoClose: 3);
                            return;
                        }
                        MoveItemDown(global.InitProcessList, Item);
                        break;
                }
            }
        }

        // 上移项
        public static void MoveItemUp(BindingList<PLCItem> list, PLCItem item)
        {
            int index = list.IndexOf(item);
            if (index > 0)
            {
                // 移除并插入到前一位
                list.RemoveAt(index);
                list.Insert(index - 1, item);
                UpdateStartIndexes(list); // 更新序号
            }
        }

        // 下移项
        public static void MoveItemDown(BindingList<PLCItem> list, PLCItem item)
        {
            int index = list.IndexOf(item);
            if (index < list.Count - 1)
            {
                // 移除并插入到后一位
                list.RemoveAt(index);
                list.Insert(index + 1, item);
                UpdateStartIndexes(list); // 更新序号
            }
        }

        // 更新所有项的序号
        public static void UpdateStartIndexes(BindingList<PLCItem> list)
        {
            for (int i = 0; i < list.Count; i++)
            {
                PLCItem item = list[i];
                if (item.StartIndex != i + 1)
                {
                    item.StartIndex = i + 1; // 触发 PropertyChanged 事件
                }
            }
        }

        private void BtnDelete_Click(object? sender, EventArgs e)
        {
            if (global.InitProcessList.Count == 0 || !global.InitProcessList.Any(x => x.Selected))
            {
                AntdUI.Message.warn(window, "请选择要删除的行!", autoClose: 3);
                return;
            }

            var result = Modal.open(window, "删除警告!", "确认要删除选择的数据吗?", TType.Warn);
            if (result == DialogResult.OK)
            {
                // 使用反转for循环删除主列表中选中的项
                for (int i = global.InitProcessList.Count - 1; i >= 0; i--)
                {
                    // 删除选中的主列表项
                    if (global.InitProcessList[i].Selected)
                    {
                        global.InitProcessList.RemoveAt(i);
                    }
                }
                // 提示删除完成
                AntdUI.Message.success(window, "删除成功!", autoClose: 3);
            }
        }

        private void BtnAdd_Click(object? sender, EventArgs e)
        {
            if (ConfigModel.PLCBaseList?.FirstOrDefault()?.PLCItemList.Count == 0)
            {
                AntdUI.Message.warn(window, "点位表无数据,不允许新增!", autoClose: 3);
                return;
            }


            PLCItem Item = new PLCItem()
            {
                StartIndex = global.InitProcessList.Count + 1,
                CellLinks = new CellLink[]
                { new CellButton(Guid.NewGuid().ToString(), "上移", TTypeMini.Default),
                  new CellButton(Guid.NewGuid().ToString(), "下移", TTypeMini.Default),
                new CellButton(Guid.NewGuid().ToString(), "编辑", TTypeMini.Primary),
                new CellButton(Guid.NewGuid().ToString(), "删除", TTypeMini.Error)
                }
            };

            var form = new MotionProcessEdit(window, "流程加载表操作-新增", Item, ConfigModel.PLCBaseList?.FirstOrDefault()?.PLCItemList) { Size = new Size(450, 550) };

            AntdUI.Drawer.open(new AntdUI.Drawer.Config(window, form)
            {
                OnLoad = () =>
                {
                    AntdUI.Message.info(window, "进入流程加载表新增", autoClose: 1);
                },
                OnClose = () =>
                {
                    if (form.submit)
                    {
                        global.InitProcessList.Add(Item);
                    }

                    AntdUI.Message.info(window, "退出流程加载表新增", autoClose: 1);
                }
            });
        }


        private void InitData()
        {
            
            

            TableInitProcess.Columns = new ColumnCollection() {
              new ColumnCheck("Selected"){Fixed = true},
                new Column("StartIndex", "序号", ColumnAlign.Center),
                new ColumnSwitch("StartExecute", "启用", ColumnAlign.Center),
                new Column("Name", "名称", ColumnAlign.Center),
                new Column("Type", "类型",ColumnAlign.Center){Visible=false},
                new Column("Address", "地址",ColumnAlign.Center){Visible=false},
                new Column("Value", "值",ColumnAlign.Center),
                new Column("CellLinks", "操作", ColumnAlign.Center)
            };
            if (global.InitProcessList.Count > 0)
            {
                foreach (var item in global.InitProcessList)
                {
                    item.CellLinks = new CellLink[] {
                             new CellButton(Guid.NewGuid().ToString(), "上移", TTypeMini.Default),
                        new CellButton(Guid.NewGuid().ToString(), "下移", TTypeMini.Default),
                        new CellButton(Guid.NewGuid().ToString(), "编辑", TTypeMini.Primary) ,
                        new CellButton(Guid.NewGuid().ToString(), "删除", TTypeMini.Error)
                    };
                }
            }
            TableInitProcess.Binding(global.InitProcessList);

            TableSartProcess.Columns = new ColumnCollection() {
                new ColumnCheck("Selected"){Fixed = true},
                new Column("StartIndex", "序号", ColumnAlign.Center),
                new ColumnSwitch("StartExecute", "启用", ColumnAlign.Center),
                new Column("Name", "名称", ColumnAlign.Center),
                new Column("Type", "类型",ColumnAlign.Center){Visible=false},
                new Column("Address", "地址",ColumnAlign.Center){Visible=false},
                new Column("Value", "值",ColumnAlign.Center),
                new Column("CellLinks", "操作", ColumnAlign.Center)
            };
            if (global.StartProcessList.Count > 0)
            {
                foreach (var item in global.StartProcessList)
                {
                    item.CellLinks = new CellLink[] {
                        new CellButton(Guid.NewGuid().ToString(), "上移", TTypeMini.Default),
                        new CellButton(Guid.NewGuid().ToString(), "下移", TTypeMini.Default),
                        new CellButton(Guid.NewGuid().ToString(), "编辑", TTypeMini.Primary) ,
                        new CellButton(Guid.NewGuid().ToString(), "删除", TTypeMini.Error)
                    };
                }
            }
            //TableSartProcess.Binding(global.StartProcessList);
            TableSartProcess.Binding(global.StartProcessList);
            TableStopProcess.Columns = new ColumnCollection() {
                new ColumnCheck("Selected"){Fixed = true},
                new Column("StartIndex", "序号", ColumnAlign.Center),
                new ColumnSwitch("StartExecute", "启用", ColumnAlign.Center),
                new Column("Name", "名称", ColumnAlign.Center),
                new Column("Type", "类型",ColumnAlign.Center){Visible=false},
                new Column("Address", "地址",ColumnAlign.Center){Visible=false},
                new Column("Value", "值",ColumnAlign.Center),
                new Column("CellLinks", "操作", ColumnAlign.Center)
            };
            if (global.StopProcessList.Count > 0)
            {
                foreach (var item in global.StopProcessList)
                {
                    item.CellLinks = new CellLink[] {
                        new CellButton(Guid.NewGuid().ToString(), "上移", TTypeMini.Default),
                        new CellButton(Guid.NewGuid().ToString(), "下移", TTypeMini.Default),
                        new CellButton(Guid.NewGuid().ToString(), "编辑", TTypeMini.Primary) ,
                        new CellButton(Guid.NewGuid().ToString(), "删除", TTypeMini.Error)
                    };
                }
            }
            TableStopProcess.Binding(global.StopProcessList);

            TableStartReset.Columns = new ColumnCollection() {
               new ColumnCheck("Selected"){Fixed = true},
                new Column("StartIndex", "序号", ColumnAlign.Center),
                new ColumnSwitch("StartExecute", "启用", ColumnAlign.Center),
                new Column("Name", "名称", ColumnAlign.Center),
                new Column("Type", "类型",ColumnAlign.Center){Visible=false},
                new Column("Address", "地址",ColumnAlign.Center){Visible=false},
                new Column("Value", "值",ColumnAlign.Center),
                new Column("CellLinks", "操作", ColumnAlign.Center)
            };
            if (global.StartResetList.Count > 0)
            {
                foreach (var item in global.StartResetList)
                {
                    item.CellLinks = new CellLink[] {
                        new CellButton(Guid.NewGuid().ToString(), "上移", TTypeMini.Default),
                        new CellButton(Guid.NewGuid().ToString(), "下移", TTypeMini.Default),
                        new CellButton(Guid.NewGuid().ToString(), "编辑", TTypeMini.Primary) ,
                        new CellButton(Guid.NewGuid().ToString(), "删除", TTypeMini.Error)
                    };
                }
            }
            TableStartReset.Binding(global.StartResetList);

            TableStopReset.Columns = new ColumnCollection() {
               new ColumnCheck("Selected"){Fixed = true},
                new Column("StartIndex", "序号", ColumnAlign.Center),
                new ColumnSwitch("StartExecute", "启用", ColumnAlign.Center),
                new Column("Name", "名称", ColumnAlign.Center),
                new Column("Type", "类型",ColumnAlign.Center){Visible=false},
                new Column("Address", "地址",ColumnAlign.Center){Visible=false},
                new Column("Value", "值",ColumnAlign.Center),
                new Column("CellLinks", "操作", ColumnAlign.Center)
            };
            if (global.StopResetList.Count > 0)
            {
                foreach (var item in global.StopResetList)
                {
                    item.CellLinks = new CellLink[] {
                       new CellButton(Guid.NewGuid().ToString(), "上移", TTypeMini.Default),
                        new CellButton(Guid.NewGuid().ToString(), "下移", TTypeMini.Default),
                        new CellButton(Guid.NewGuid().ToString(), "编辑", TTypeMini.Primary) ,
                        new CellButton(Guid.NewGuid().ToString(), "删除", TTypeMini.Error)
                    };
                }
            }
            TableStopReset.Binding(global.StopResetList);
        }

       
    }
}