using HighWayIot.Repository.domain;
using HighWayIot.Repository.service;
using HighWayIot.Winform.Business;
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 RoleAddForm : Form
    {
        /// <summary>
        /// Sql业务类
        /// </summary>
        SysUserRoleService _sysUserRoleService = SysUserRoleService.Instance;

        /// <summary>
        /// XML读取类
        /// </summary>
        XmlUtil xmlUtil = XmlUtil.Instance;

        /// <summary>
        /// 规则字符数组
        /// </summary>
        char[] RoleChars = new char[80];

        /// <summary>
        /// 页面规则偏移量配置
        /// </summary>
        List<RoleConfig> ConfigList;

        /// <summary>
        /// 前端展示DataTable
        /// </summary>
        DataTable dt = new DataTable();

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

        private void Init()
        {
            RolesDataGridView.AutoGenerateColumns = false;
            for (int i = 0; i < 80; i++)
            {
                RoleChars[i] = '0';
            }
            ConfigList = xmlUtil.ConfigReader();

            dt.Columns.Add("TableName", typeof(string));
            dt.Columns.Add("IsUseable", typeof(bool));

            GridViewRefresh();
        }

        /// <summary>
        /// 确认添加按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ConfrimAddButton_Click(object sender, EventArgs e)
        {

            foreach (RoleConfig config in ConfigList)
            {
                for (int i = 0; i < ConfigList.Count; i++)
                {
                    if (config.PageName == RolesDataGridView.Rows[i].Cells[0].Value.ToString().Trim())
                    {
                        RoleChars[config.RoleIndex] = Convert.ToBoolean(RolesDataGridView.Rows[i].Cells[1].Value) ? '1' : '0';
                    }
                }
            }

            if (string.IsNullOrEmpty(RoleNameTextBox.Text))
            {
                MessageBox.Show("角色名不能为空！", "提示", MessageBoxButtons.OK);
                return;
            }

            if (_sysUserRoleService.GetRoleInfos(RoleNameTextBox.Text).Count >= 1)
            {
                MessageBox.Show("角色名重复！", "提示", MessageBoxButtons.OK);
                return;
            }

            SysRoleEntity role = new SysRoleEntity()
            {
                RoleName = RoleNameTextBox.Text,
                RoleSet = new string(RoleChars),
            };

            if (_sysUserRoleService.InsertRoleInfo(role))
            {
                MessageBox.Show("角色添加成功", "提示", MessageBoxButtons.OK);
            }
            else
            {
                MessageBox.Show("角色添加失败", "提示", MessageBoxButtons.OK);
            }

            this.Close();
            this.Dispose();
        }

        /// <summary>
        /// 全选按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SelectAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 80; i++)
            {
                RoleChars[i] = '1';
            }
            GridViewRefresh();
        }

        /// <summary>
        /// 取消全选按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SelectNone_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 80; i++)
            {
                RoleChars[i] = '0';
            }
            GridViewRefresh();
        }

        /// <summary>
        /// 前端页面刷新
        /// </summary>
        private void GridViewRefresh()
        {
            dt.Rows.Clear();

            foreach (var config in ConfigList)
            {
                var dr = dt.NewRow();
                dr[0] = config.PageName;
                dr[1] = RoleChars[config.RoleIndex] == '1';
                dt.Rows.Add(dr);
            }

            RolesDataGridView.DataSource = null;
            RolesDataGridView.DataSource = dt;
        }
    }
}
