using ICSharpCode.Core;
using Mesnac.Action.ChemicalWeighing.Entity;
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 Mesnac.Action.ChemicalWeighing.UserManage
{
    public partial class FrmUser : Form
    {
        #region 字段定义

        private ActionType _actionType = ActionType.Add;    //操作类型,0-为添加,1-为修改
        private SimplePstUser _user = null;             //用户对象

        #endregion
        public FrmUser()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="actionType">操作类型,0-为添加</param>
        public FrmUser(ActionType actionType)
        {
            InitializeComponent();
            this._actionType = actionType;
        }

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="actionType">操作类型,1-为修改</param>
        /// <param name="user">用户信息传入的</param>
        public FrmUser(ActionType actionType, SimplePstUser user)
        {
            InitializeComponent();
            this._actionType = actionType;
            this._user = user;
        }

        /// <summary>
        /// 用户信息
        /// </summary>
        public SimplePstUser User
        {
            get { return this._user; }
        }

        /// <summary>
        /// 窗体加载时 加载下拉框的数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmInsertUser_Load(object sender, EventArgs e)
        {
            this.InitUI();
            this.InitData();
        }

        #region 方法定义

        /// <summary>
        /// 初始化界面文本
        /// </summary>
        public void InitUI()
        {
            if (this._actionType == ActionType.Add)
            {
                this.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_UserManage_FrmUser_Text_Add"));                //添加用户
            }
            else if (this._actionType == ActionType.Modify)
            {
                this.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_UserManage_FrmUser_Text_Modify"));                //修改用户信息
            }
            this.label1.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_UserManage_FrmUser_label1_Text"));          //用户名
            this.label2.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_UserManage_FrmUser_label2_Text"));          //密码
            this.label3.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_UserManage_FrmUser_label3_Text"));          //确认密码
            this.label4.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_UserManage_FrmUser_label4_Text"));          //用户类型
            
        }

        /// <summary>
        /// 初始化用户类型下拉框
        /// </summary>
        public void InitData()
        {
            List<SimplePstRole> lst = null;
            lst = UserHelper.GetUserRoleList();
            this.uTypeCB.Items.Clear();
            foreach (SimplePstRole r in lst)
            {
                this.uTypeCB.Items.Add(r);
            }
            //当操作类型为修改时,将选中行的数据填入到修改窗体的对应控件中
            if (this._actionType == ActionType.Modify)
            {
                foreach (SimplePstRole r in this.uTypeCB.Items)
                {
                    if (r.RoleName == this._user.URoleName)
                    {
                        this.uTypeCB.SelectedItem = r;
                        break;
                    }
                }
                this.uNameTB.Text = _user.UName;
                this.uPwdTB.Text = _user.UPwd;
                this.uPwdTB2.Text = _user.UPwd;
            }
        }

        #endregion

        /// <summary>
        /// 确认按钮点击后返回OK
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnInsertUser_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(uTypeCB.Text))
            {
                MessageBox.Show("请选择用户类型!!");
                this.uTypeCB.Focus();
                return;
            }
            //获取当前选中的用户类型对象
            SimplePstRole role = (SimplePstRole) uTypeCB.SelectedItem;
            //用户属性的对应获取
            string uName = uNameTB.Text;
            string uPwd = uPwdTB.Text;
            string uPwd2 = uPwdTB2.Text;
            int roleID = role.ID;
            string uType = role.RoleName;
            //获取包含所有用户对象的集合
            List<SimplePstUser> list = UserHelper.getUserList();
            
            if (string.IsNullOrEmpty(uName))
            {
                MessageBox.Show("用户名不能为空,请输入用户名!");
                return;
            }
            if (string.IsNullOrEmpty(uPwd) || string.IsNullOrEmpty(uPwd2))
            {
                MessageBox.Show("密码不能为空,请输入密码!");
                return;
            }
            
            if (uPwd != uPwd2)
            {
                MessageBox.Show("两次密码输入不同!请检查后重新输入!");
                return;
            }
            //创建对象
            SimplePstUser user = new SimplePstUser {
                UName = uNameTB.Text,
                UPwd = uPwdTB.Text,
                URoleName = uType,
                URoleID = roleID
            };

            //当操作类型为新增用户时,验证用户名唯一性
            //当修改用户时,提交的用户名和选中行的不一样时,需要验证唯一性
            if (_actionType == ActionType.Add || (_actionType == ActionType.Modify && _user.UName != user.UName))
            {
                //用户名唯一性验证
                if (list.Exists(u => u.UName == user.UName))
                {
                    MessageBox.Show("用户名已存在!!请更换用户名!!");
                    return;
                }
            }
            this._user = user;
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

 

    }
}