using HighWayIot.Repository.domain;
using HighWayIot.Repository.service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HighWayIot.Winform.UserControlPages.SysConfigPages
{
    public partial class UserUpDateForm : Form
    {
        /// <summary>
        /// 角色数据库业务实例
        /// </summary>
        SysUserInfoService _userInfoService = SysUserInfoService.Instance;

        /// <summary>
        /// 要修改的实体
        /// </summary>
        SysUserEntity _userEntity;

        public UserUpDateForm(SysUserEntity userEntity, string[] roleList)
        {
            InitializeComponent();
            this._userEntity = userEntity;
            UserRoleComboBox.DataSource = roleList;
            PasswordTextBox.Text = _userEntity.Password;
            UserNameTextBox.Text = _userEntity.UserName;
            for(int i = 0;  i < UserRoleComboBox.Items.Count; i++)
            {
                this.UserRoleComboBox.SelectedIndex = i;
                if(this.UserRoleComboBox.Text.Trim() == _userEntity.UserRole)
                {
                    break;
                }
            }
        }

        private void ConfrimUpdateButton_Click(object sender, EventArgs e)
        {
            _userEntity.Password = PasswordTextBox.Text;
            _userEntity.UserName = UserNameTextBox.Text;
            _userEntity.UserRole = UserRoleComboBox.Text.Trim();
            var list = _userInfoService.GetUserInfoByUserName(UserNameTextBox.Text);
            if (list.Where(x => x.Id != _userEntity.Id).Count() != 0)
            {
                MessageBox.Show("不能有相同用户名！", "结果", MessageBoxButtons.OK);
                return;
            }
            if (_userInfoService.UpdateUserInfo(this._userEntity))
            {
                MessageBox.Show("数据更新成功！", "结果", MessageBoxButtons.OK);
            }
            else
            {
                MessageBox.Show("数据更新失败！请看日志查询失败原因", "结果", MessageBoxButtons.OK);
            }
            this.Close();
            this.Dispose();
        }
    }
}
