public static int Insert(DeviceData d)
        {
            int result = -1;
            //p_insert_device
            try
            {
                using (MySqlConnection conn = new MySqlConnection(App.setting.connectString))
                {
                    conn.Open();
                    string query = "`p_insert_device`";
                    using (MySqlCommand cmd = new MySqlCommand(query, conn))
                    {
                        cmd.Parameters.Add(new MySqlParameter("@_d_name", MySqlDbType.VarChar, 255) { Direction = System.Data.ParameterDirection.Input, Value = d.Name });
                        cmd.Parameters.Add(new MySqlParameter("@_d_ip", MySqlDbType.VarChar, 30) { Direction = System.Data.ParameterDirection.Input, Value = d.IP });
                        cmd.Parameters.Add(new MySqlParameter("@_d_type", MySqlDbType.Int32) { Direction = System.Data.ParameterDirection.Input, Value =d.Type });
                        cmd.Parameters.Add(new MySqlParameter("@_d_pass", MySqlDbType.VarChar, 100) { Direction = System.Data.ParameterDirection.Input, Value = d.Pass });
                        cmd.Parameters.Add(new MySqlParameter("@_d_comment", MySqlDbType.Text) { Direction = System.Data.ParameterDirection.Input, Value = d.Comment });
                        cmd.Parameters.Add(new MySqlParameter("@_d_id", MySqlDbType.Int32) { Direction = System.Data.ParameterDirection.Output, Value = 0 });
                        cmd.CommandType = System.Data.CommandType.StoredProcedure;
                        cmd.ExecuteScalar();
                        result = Convert.ToInt32(cmd.Parameters["@_d_id"].Value);

                    };
                    conn.Close();
                };

            }
            catch (Exception)
            {
            }
            return result;
        }
 private void LoadGUI(DeviceData d)
 {
     if (this.Device == null)
     {
         this.UITitle.Text = "Thêm thiết bị mới";
         this.UIName.reset();
         this.UIIP.reset();
         this.UIType.SelectedIndex =- 1;
         UIContent.Height = 445;
     }
     else
     {
         this.UIName.Text = d.Name;
         this.UIIP.Text = d.IP;
         this.UITitle.Text = d.Name;
         this.UIType.SelectedItem = d.TypeDevice;
         UIContent.Height = 505;
     }
 }
        private void LoadGUI(DeviceData d)
        {
            if (d.TypeDevice != null && !string.IsNullOrEmpty(d.TypeDevice.Icon))
            {
                this.UIIcon.Text = d.TypeDevice.Icon;
            }

            this.UIIP.Text = d.IP;
            this.UITitle.Text = d.Name;
            this.UITime.Text = d.Time.format();
            this.UIType.Text = d.TypeDevice.Name;
            if (d.User != null)
                this.UIUser.Text = d.User.Full_Name;
            else
                this.UIUser.Text = string.Empty;

            if (d.Status)
            {
                this.UIStatus.Text = Define.Fonts["fa-unlock"].Code;
            }
            else
            {
                this.UIStatus.Text = Define.Fonts["fa-lock"].Code;
            }
        }
 private void UIBtnSave_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
 {
     if (string.IsNullOrEmpty(this.UIName.Text))
     {
         MessageBox.Show("Tên thiết bị không được để trống");
         return;
     }
     if (this.UIType.SelectedIndex == -1)
     {
         MessageBox.Show("Chon loại thiết bị");
         return;
     }
     if (!this.UIIP.Text.isIP())
     {
         MessageBox.Show("Địa chỉ IP không đúng định dạng");
         return;
     }
     if (string.IsNullOrEmpty(this.UIPass.Password) && this.Device == null)
     {
         MessageBox.Show("Mật khẩu không được để trống");
         return;
     }
     if (this.UIPass.Password != this.UIPass_Again.Password) 
     {
         MessageBox.Show("Mật khẩu không khớp. Kiểm tra lại!");
         return;
     }
     if (this.Device != null && this.Device.Pass != this.UIPass_Old.Password.MD5String())
     {
         MessageBox.Show("Mật khẩu đúng. Vui lòng kiểm tra lại!");
         return;
     }
     if(this.Device== null)
     {
         DeviceData d = new DeviceData();
         d.Name = this.UIName.Text;
         d.IP = this.UIIP.Text;
         d.Pass = this.UIPass.Password.MD5String();
         d.Type = (this.UIType.SelectedItem as TypeDevice).ID;
         int result= DeviceData.Insert(d);
         if(result <=0)
         {
             MessageBox.Show("Không thể kết nối CSDL");
             return;
         }
         d.ID = result;
         if (this.CloseEvent != null)
         {
             this.CloseEvent(this, d);
         }
     }else
     {
         this.d.Name = this.UIName.Text;
         this.d.Type = (this.UIType.SelectedItem as TypeDevice).ID;
         this.d.IP = this.UIIP.Text;
         if (!string.IsNullOrEmpty(this.UIPass.Password))
         {
             this.d.Pass = this.UIPass.Password.MD5String();
         }
         this.d.save();
         if (this.CloseEvent != null)
         {
             this.CloseEvent(this, this.d);
         }
     }
 }
 private void Item_CloseEvent(object sender, DeviceData e)
 {
     if (e != null)
     {
         bool isEdit = false;
         foreach (UIDevice uid in this.list_Box_Item.Items)
         {
             if (uid.Device.ID == e.ID)
             {
                 isEdit = true;
                 uid.Device = e;
             }
         }
         if (!isEdit)
         {
             this.Datas = App.curUser.LoadDevices(this.from, ref this.to, out this.total);
             LoadGUI();
         }
     }
     this.UIRoot.Children.Remove(sender as UIDeviceEdit);
 }
 private void Item_ViewInfoEvent(object sender, DeviceData e)
 {
     if (e != null)
     {
         UIDeviceEdit item = new UIDeviceEdit();
         item.setLeft(0);
         item.setTop(90);
         item.Height = 578;
         item.Width = 1366;
         item.Device = e;
         item.CloseEvent += Item_CloseEvent;
         this.UIRoot.Children.Add(item);
     }
 }
        private void Item_DeleteEvent(object sender, DeviceData e)
        {
            if (e != null)
            {
                MessageBoxResult messageBoxResult = MessageBox.Show("Bạn có muốn xoá thiết bị này không?", "Xoá thiết bị", System.Windows.MessageBoxButton.YesNo);
                if (messageBoxResult == MessageBoxResult.Yes)
                {

                    UIDevice uid = (this.list_Box_Item.SelectedItem as UIDevice);
                    if (this.list_Box_Item.SelectedIndex != 0)
                    {
                        this.list_Box_Item.SelectedIndex = 0;
                    }
                    else if (this.list_Box_Item.Items.Count > 1)
                    {
                        this.list_Box_Item.SelectedIndex = 1;
                    }
                    uid.Animation_Opacity_View_Frame(false, () =>
                    {
                        this.Datas.Remove(uid.Device);
                        this.list_Box_Item.Items.Remove(uid);
                        if (this.to < this.total)
                        {
                            this.Datas = App.curUser.LoadDevices(this.from, ref this.to, out this.total);
                            this.LoadGUI();
                        }
                    });

                }
            }
        }
        public static List<DeviceData> toDevices(this MySqlDataReader reader)
        {
            if (reader.HasRows)
            {
                List<DeviceData> datas = new List<DeviceData>();
                while (reader.Read())
                {
                    if (!reader.IsDBNull(0))
                    {
                        DeviceData d = new DeviceData();
                        d.ID = reader.GetInt32(Define.device_id);
                        d.Name = reader.GetString(Define.device_name);
                        d.Status = reader.GetBoolean(Define.device_status);
                        d.Time = reader.GetDateTime(Define.device_time);
                        d.Type = reader.GetInt32(Define.device_type);
                        d.IP = reader.GetString(Define.device_ip);
                        d.Pass = reader.GetString(Define.device_pass);
                        try
                        {
                            d.Comment = reader.GetString(Define.device_comment);
                        }
                        catch (Exception)
                        {
                        }
                        datas.Add(d);

                    }
                }
                return datas;
            }
            return null;
        }