using DH.RBAC.Common;
using DH.RBAC.Logic.Sys;
using DH.RBAC.Model.Sys;
using DH.RBAC.Utility.Other;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms; 

namespace DH.RBAC.Page.Sys.Organize
{
    public partial class AddOrganizeForm : UIForm
    {
        private SysOrganizeLogic organizeLogic;
        public AddOrganizeForm()
        {
            InitializeComponent();
            organizeLogic = new SysOrganizeLogic();
        }

        #region 标题栏
        private void btnClose_Click(object sender, EventArgs e)
        {
            FormHelper.subForm = null;
            ParentPage.btnQuery_Click(null, null);
            this.Close();
        }
        private Point mPoint;
        private void titlePanel_MouseDown(object sender, MouseEventArgs e)
        {
            mPoint = new Point(e.X, e.Y);
        }

        private void titlePanel_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Location = new Point(this.Location.X + e.X - mPoint.X, this.Location.Y + e.Y - mPoint.Y);
            }
        }

        private void btnClose_MouseEnter(object sender, EventArgs e)
        {
            btnClose.BackColor = Color.FromArgb(231, 231, 231);
        }

        private void btnClose_MouseLeave(object sender, EventArgs e)
        {
            btnClose.BackColor = Color.Transparent;

        }
        #endregion


        public OrganizePage ParentPage { get; set; }
        public string Id { get; set; }

        /// <summary>
        /// 画面加载,读取用户信息,显示在界面上
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddOrganizeForm_Load(object sender, EventArgs e)
        {
            comboType.Items.Add("公司");
            comboType.Items.Add("部门");
            comboType.Items.Add("小组");
            comboType.SelectedIndex = 1;

            if (StringHelper.IsNullOrEmpty(Id))
            {
                lblTitle.Text = "新增机构";
                txtEnCode.Enabled = true;
            }
            else
            {
                lblTitle.Text = "修改机构";
                txtEnCode.Enabled = false;
            }
            //获取部门下拉列表的值
            if (StringHelper.IsNullOrEmpty(Id))
            {
                return;
            }
            SysOrganize entity = organizeLogic.Get(Id);
            if (entity == null)
            {
                AntdUI.Message.warn(this, "网络或服务器异常,请稍后重试!", autoClose: 3);
              
                btnClose_Click(null, null);
                return;
            }
            //给文本框赋值
            txtEnCode.Text = entity.EnCode;
            txtName.Text = entity.FullName;
            comboType.SelectedIndex = entity.Type.Value;
            txtManagerId.Text = entity.ManagerId;
            txtTelePhone.Text = entity.TelePhone;
            txtWeChat.Text = entity.WeChat;
            txtEmail.Text = entity.Email;
            txtFax.Text = entity.Fax;
            txtAddress.Text = entity.Address;
            txtSortCode.Value = entity.SortCode.Value;
            txtRemark.Text = entity.Remark;
        }


        /// <summary>
        /// 确定按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnConfirm_Click(object sender, EventArgs e)
        {
            if (StringHelper.IsNullOrEmpty(Id))
            {
                DoAdd();
            }
            else
            {
                DoUpdate();
            }
        }

        /// <summary>
        /// 执行更新操作
        /// </summary>
        private void DoUpdate()
        {
            bool flag = ChechEmpty();
            if (!flag)
            {
                return;
            }
            SysOrganize model = new SysOrganize();
            model.Id = Id;
            model.EnCode = txtEnCode.Text;
            model.FullName = txtName.Text;
            model.Type = comboType.SelectedIndex;
            model.ManagerId = txtManagerId.Text;
            model.TelePhone = txtTelePhone.Text;
            model.WeChat = txtWeChat.Text;
            model.Email = txtEmail.Text;
            model.Fax = txtFax.Text;
            model.Address = txtAddress.Text;
            model.SortCode = txtSortCode.Value;
            model.Remark = txtRemark.Text;
            model.ModifyUserId = GlobalConfig.CurrentUser.Id;
            int row = organizeLogic.AppUpdate(model, model.ModifyUserId);
            if (row == 0)
            {
                AntdUI.Message.warn(this, "对不起,操作失败!", autoClose: 3);

           
                return;
            }

          
            btnClose_Click(null, null);

        }

        /// <summary>
        /// 数据校验
        /// </summary>
        /// <param name="checkPassword"></param>
        /// <returns></returns>
        private bool ChechEmpty()
        {
            if (StringHelper.IsNullOrEmpty(txtEnCode.Text))
            {
                AntdUI.Message.warn(this, "编码不能为空!", autoClose: 3);

                return false;
            }
            if (StringHelper.IsNullOrEmpty(txtName.Text))
            {
                AntdUI.Message.warn(this, "名称不能为空!", autoClose: 3);

                return false;
            }
            if (StringHelper.IsNullOrEmpty(comboType.SelectedItem.ToString()))
            {
                AntdUI.Message.warn(this, "类型不能为空!", autoClose: 3);

                return false;
            }
            return true;
        }


        /// <summary>
        /// 执行新增操作
        /// </summary>
        private void DoAdd()
        {
            bool flag = ChechEmpty();
            if (!flag)
                return;
            SysOrganize model = new SysOrganize();
            model.EnCode = txtEnCode.Text;
            model.FullName = txtName.Text;
            model.Type = comboType.SelectedIndex;
            model.ManagerId = txtManagerId.Text;
            model.TelePhone = txtTelePhone.Text;
            model.WeChat = txtWeChat.Text;
            model.Email = txtEmail.Text;
            model.Fax = txtFax.Text;
            model.Address = txtAddress.Text;
            model.SortCode = txtSortCode.Value;
            model.Remark = txtRemark.Text;
            model.CreateUserId = GlobalConfig.CurrentUser.Id;
            int row = organizeLogic.AppInsert(model, model.CreateUserId);
            if (row == 0)
            {
                AntdUI.Message.warn(this, "对不起,操作失败!", autoClose: 3);

                return;
            }
           
            btnClose_Click(null, null);
        }
    }
}