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 RoleUpdateForm : 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();

        /// <summary>
        /// 要更改的数据
        /// </summary>
        SysRoleEntity _roleEntity;

        public RoleUpdateForm(SysRoleEntity sysRoleEntity)
        {
            InitializeComponent();
            this._roleEntity = sysRoleEntity;
            Init();
        }

        private void Init()
        {
            RolesDataGridView.AutoGenerateColumns = false;
            RoleNameTextBox.Text = _roleEntity.RoleName;
            RoleChars = _roleEntity.RoleSet.ToCharArray();
            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).Where(x => x.Id != _roleEntity.Id).Count() > 0)
            {
                MessageBox.Show("角色名重复！", "提示", MessageBoxButtons.OK);
                return;
            }

            _roleEntity.RoleName = RoleNameTextBox.Text;
            _roleEntity.RoleSet = new string(RoleChars);

            if (_sysUserRoleService.UpdateRoleInfo(_roleEntity))
            {
                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;
        }
    }
}
