﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LJH.Inventory.BusinessModel;
using LJH.Inventory.WebApiClient;
using LJH.GeneralLibrary.Core.DAL;
using LJH.GeneralLibrary.Core.UI;

namespace LJH.Inventory.UI.Forms
{
    public partial class FrmOperatorDetail : FrmDetailBase
    {
        private OperatorBLL bll = new OperatorBLL(AppSettings.Current.ConnStr);
        private string _subPwd = new string('*', 10);

        public FrmOperatorDetail()
        {
            InitializeComponent();
        }

        #region 重写基类的方法
        protected override void InitControls()
        {
            comRoleList.Init();
            if (IsAdding)
            {
                this.btnChangePwd.Visible = false;
                this.txtPassword.Size = this.txtOperatorID.Size;
            }
            base.InitControls();
        }

        public override void ShowOperatorRights()
        {
            base.ShowOperatorRights();
            this.btnChangePwd.Enabled = Operator.Current.Permit(Permission.Operator, PermissionActions.Edit);
            this.btnOk.Enabled = Operator.Current.Permit(Permission.Operator, PermissionActions.Edit);
        }

        protected override void ItemShowing()
        {
            Operator info = (Operator)UpdatingItem;
            this.txtOperatorID.Text = info.ID;
            this.txtOperatorID.Enabled = false;
            this.txtOperatorID.BackColor = Color.White;
            this.txtName.Text = info.Name;
            this.txtPassword.Text = _subPwd;
            this.txtPassword.Enabled = false;
            this.txtPassword.BackColor = Color.White;
            this.comRoleList.SelectedRoleID = info.RoleID;
        }

        protected override object GetItemFromInput()
        {
            Operator info = null;
            if (CheckInput())
            {
                if (IsAdding)
                {
                    info = new Operator();
                    info.ID = txtOperatorID.Text.Trim();
                    info.SetPassword(txtPassword.Text.Trim());
                }
                else
                {
                    info = UpdatingItem as Operator;
                    if (txtPassword.Text.Trim() != _subPwd)
                    {
                        info.SetPassword (txtPassword.Text.Trim());
                    }
                }
                info.Name = txtName.Text.Trim();
                info.Role = comRoleList.Role;
                info.RoleID = comRoleList.SelectedRoleID;
                info.显示送货单类型 = null;
            }
            return info;
        }

        protected override CommandResult AddItem(object addingItem)
        {
            return bll.Add((Operator)addingItem);
        }

        protected override CommandResult UpdateItem(object updatingItem)
        {
            var opt = updatingItem as Operator;
            var ret = bll.Update(opt);
            if (ret.Result == ResultCode.Successful && opt.ID == Operator.Current.ID)
            {
                Operator.Current = new OperatorBLL(AppSettings.Current.ConnStr).GetByID(opt.ID).QueryObject;
            }
            return ret;
        }

        protected override bool CheckInput()
        {
            if (txtOperatorID.Text.Trim().Length == 0)
            {
                MessageBox.Show("操作员登录ID不能为空!");
                return false;
            }

            if (txtPassword.Text.Trim().Length == 0)
            {
                MessageBox.Show("操作员密码不能为空!");
                return false;
            }
            return true;
        }

        private void btnChangePwd_Click(object sender, EventArgs e)
        {
            FrmChangePwd frm = new FrmChangePwd();
            frm.Operator = UpdatingItem as Operator;
            frm.ShowDialog();
        }
        #endregion
    }
}
