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 UserAddForm : Form
    {
        /// <summary>
        /// 角色数据库业务实例
        /// </summary>
        SysUserInfoService SysUserInfoService = SysUserInfoService.Instance;

        public UserAddForm(string[] roleList)
        {
            InitializeComponent();
            UserRoleComboBox.DataSource = roleList;
        }

        /// <summary>
        /// 确认添加按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ConfrimAddButton_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(UserNameTextBox.Text.Trim()))
            {
                MessageBox.Show("用户名编号不能为空！");
                return;
            }
            SysUserEntity userEntity = new SysUserEntity()
            {
                UserName = UserNameTextBox.Text.Trim(),
                Password = PasswordTextBox.Text.Trim(),
                UserRole = UserRoleComboBox.SelectedText
            };
            var list = SysUserInfoService.GetUserInfoByUserName(UserNameTextBox.Text);
            if (list.Count() != 0)
            {
                MessageBox.Show("不能有相同用户名！", "结果", MessageBoxButtons.OK);
                return;
            }
            if (SysUserInfoService.InsertUserInfo(userEntity))
            {
                MessageBox.Show("数据添加成功！", "结果", MessageBoxButtons.OK);
            }
            else
            {
                MessageBox.Show("数据添加失败！请看日志查询失败原因", "结果", MessageBoxButtons.OK);
            }
            this.Close();
            this.Dispose();
        }
    }
}
