using HighWayIot.Repository.domain;
using HighWayIot.Repository.service;
using HighWayIot.Winform.Business;
using HighWayIot.Winform.UserControlPages.SysConfigPages;
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 UserConfigPage : UserControl
    {
        /// <summary>
        /// 用户数据库业务实例
        /// </summary>
        private SysUserInfoService sysUserInfoService = SysUserInfoService.Instance;

        /// <summary>
        /// 角色数据库业务实例
        /// </summary>
        private SysUserRoleService sysUserRoleService = SysUserRoleService.Instance;

        /// <summary>
        /// 用户信息列表
        /// </summary>
        private List<SysUserEntity> List = new List<SysUserEntity>();

        /// <summary>
        /// 角色名称列表
        /// </summary>
        private string[] RoleNames;

        public UserConfigPage()
        {
            InitializeComponent();
            Init();
        }

        private void Init()
        {
            UserInfoDataGridView.AutoGenerateColumns = false;
            List = sysUserInfoService.GetUserInfos();

            RoleNames = sysUserRoleService.GetRoleInfos().Select(x => x.RoleName).ToArray();
            RoleNames = GeneralUtils.HeadAddEmptyString(RoleNames);

            SelectUserLoginBeginTime.Value = DateTime.Now - TimeSpan.FromDays(3);

            this.SelectUserRole.DataSource = RoleNames;
            UserInfoDataGridView.DataSource = null;
            UserInfoDataGridView.DataSource = List;
        }

        private void UpdateUser_Click(object sender, EventArgs e)
        {
            SysUserEntity entity = new SysUserEntity();
            int a = UserInfoDataGridView.CurrentRow.Index;
            int.TryParse(UserInfoDataGridView.Rows[a].Cells["Id"].Value.ToString(), out int id);
            entity.Id = id;
            entity.UserName = UserInfoDataGridView.Rows[a].Cells["UserName"].Value.ToString();
            entity.UserRole = UserInfoDataGridView.Rows[a].Cells["UserRole"].Value.ToString();
            entity.Password = UserInfoDataGridView.Rows[a].Cells["Password"].Value.ToString();
            UserUpDateForm form = new UserUpDateForm(entity, RoleNames);
            form.ShowDialog();
            List = sysUserInfoService.GetUserInfos();
            UserInfoDataGridView.DataSource = null;
            UserInfoDataGridView.DataSource = List;
        }

        private void AddUser_Click(object sender, EventArgs e)
        {
            UserAddForm form = new UserAddForm(RoleNames);
            form.ShowDialog();
            List = sysUserInfoService.GetUserInfos();
            UserInfoDataGridView.DataSource = null;
            UserInfoDataGridView.DataSource = List;
        }

        private void DeleteUser_Click(object sender, EventArgs e)
        {
            int a = UserInfoDataGridView.CurrentRow.Index;
            if (MessageBox.Show($"确定要删除所选{List[a].UserName}行的数据？", "确认", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
            {
                return;
            }
            int.TryParse(UserInfoDataGridView.Rows[a].Cells["Id"].Value.ToString(), out a);
            sysUserInfoService.DeleteUserInfoById(a);
            List = sysUserInfoService.GetUserInfos();
            UserInfoDataGridView.DataSource = null;
            UserInfoDataGridView.DataSource = List;
        }

        private void SelectUser_Click(object sender, EventArgs e)
        {
            List = sysUserInfoService.GetUserInfos(SelectUserName.Text, SelectUserRole.Text, SelectUserLoginBeginTime.Value, SelectUserLoginEndTime.Value, IsCheckByLoginTime.Checked);
            UserInfoDataGridView.DataSource = null;
            UserInfoDataGridView.DataSource = List;
        }
    }
}
