using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace TSDome
{
    public partial class User : Form
    {
        public int m_lUserID=0;
        public NetSDK.NET_SDK_USER_MANAGE_INFO m_userlist;
        ModifyUser m_dlgModifyUser = new ModifyUser();
        ModifyGroup m_dlgModifyGroup = new ModifyGroup();
        Password m_dlgPassword = new Password();
        AddUser m_dlgAddUser = new AddUser();
        AddGroup m_dlgAddGroup = new AddGroup();
        DecGroup m_dlgDecGroup = new DecGroup();
        public User()
        {
            InitializeComponent();
        }

        private void buttonUpdate_Click(object sender, EventArgs e)
        {
            update();
        }
        public void update()
        {
            NetSDK.NET_SDK_USER_MANAGE_INFO userlist = new NetSDK.NET_SDK_USER_MANAGE_INFO();
            IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(NetSDK.NET_SDK_USER_MANAGE_INFO)));
            uint  lpBytesReturned=new uint();
            int nRet =NetSDK.NET_SDK_GetDevConfig(m_lUserID, (uint)NetSDK.NET_SDK_CONFIG_TYPE.NET_SDK_CONFIG_USER,-1,pt,(uint)Marshal.SizeOf(typeof(NetSDK.NET_SDK_USER_MANAGE_INFO)),ref lpBytesReturned, 1000);
            if(nRet!=0)
            {
                userlist = (NetSDK.NET_SDK_USER_MANAGE_INFO)Marshal.PtrToStructure(pt, typeof(NetSDK.NET_SDK_USER_MANAGE_INFO));
                listView1.Items.Clear();
                for(int i=0;i<userlist.userNum;i++)
                {
                    listView1.Items.Add(new ListViewItem(new string[] { (i+1).ToString(), new string(userlist.userList[i].name), new string(userlist.userList[i].groupname)}));
                }
                m_userlist = userlist;
            }
        }

        private void buttonModifyUser_Click(object sender, EventArgs e)
        {
            m_dlgModifyUser.m_lUserID = m_lUserID;
            m_dlgModifyUser.m_userlist = m_userlist;
            m_dlgModifyUser.Init();
            m_dlgModifyUser.ShowDialog();
            update();
        }

        private void buttonModifyRole_Click(object sender, EventArgs e)
        {
            m_dlgModifyGroup.m_lUserID = m_lUserID;
            m_dlgModifyGroup.m_userlist = m_userlist;
            m_dlgModifyGroup.Init();
            m_dlgModifyGroup.ShowDialog();
            update();
        }

        private void buttonPassword_Click(object sender, EventArgs e)
        {
            m_dlgPassword.m_lUserID = m_lUserID;
            m_dlgPassword.m_userlist = m_userlist;
            m_dlgPassword.Init();
            m_dlgPassword.ShowDialog();
            update();
        }

        private void buttonAddUser_Click(object sender, EventArgs e)
        {
            m_dlgAddUser.m_lUserID = m_lUserID;
            m_dlgAddUser.m_userlist = m_userlist;
            m_dlgAddUser.Init();
            m_dlgAddUser.ShowDialog();
            update();
        }

        private void buttonAddRole_Click(object sender, EventArgs e)
        {
            m_dlgAddGroup.m_lUserID = m_lUserID;
            m_dlgAddGroup.m_userlist = m_userlist;
            m_dlgAddGroup.Init();
            m_dlgAddGroup.ShowDialog();
            update();
        }

        private void buttonDeleteUser_Click(object sender, EventArgs e)
        {
            if(listView1.SelectedItems.Count==0)
            {
                MessageBox.Show("Please select a user！");
                return;
            }
            else
            {
                NetSDK.NET_SDK_USER_INFO userinfo=new NetSDK.NET_SDK_USER_INFO();
                userinfo = m_userlist.userList[listView1.SelectedItems[0].Index];
                IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(NetSDK.NET_SDK_USER_INFO)));
                Marshal.StructureToPtr(userinfo, pt, false);
                int nRet = NetSDK.NET_SDK_SetDevConfig(m_lUserID, (uint)NetSDK.NET_SDK_CONFIG_TYPE.NET_SDK_CONFIG_DELETE_USER, -1, pt, Marshal.SizeOf(typeof(NetSDK.NET_SDK_USER_INFO)), 2000);
                if(nRet>0)
                {
                    update();
                }
                else
                {
                    MessageBox.Show("failed to delete");
                }
            }
        }

        private void buttonDeleteRole_Click(object sender, EventArgs e)
        {
            m_dlgDecGroup.m_lUserID = m_lUserID;
            m_dlgDecGroup.m_userlist = m_userlist;
            m_dlgDecGroup.Init();
            m_dlgDecGroup.ShowDialog();
            update();
        }
    }
}
