﻿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.WebApiClient;
using LJH.Inventory.BusinessModel;
using LJH.Inventory.BusinessModel.SearchCondition;
using LJH.GeneralLibrary.Core.DAL;
using LJH.GeneralLibrary.Core.UI;

namespace LJH.Inventory.UI.Forms.General
{
    public partial class FrmProductDetail : FrmSheetDetailBase
    {
        public FrmProductDetail()
        {
            InitializeComponent();
        }

        #region 公共属性
        /// <summary>
        /// 获取或设置产品类别
        /// </summary>
        public ProductCategory Category { get; set; }

        public ProductClass ProductClass { get; set; }
        #endregion

        #region 私有方法
        #endregion

        #region 重写基类方法
        protected override bool CheckInput()
        {
            if (string.IsNullOrEmpty(txtID.Text))
            {
                MessageBox.Show("产品编号不能为空");
                txtID.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(txtName.Text))
            {
                MessageBox.Show("产品名称不能为空");
                txtName.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(txtCategory.Text))
            {
                MessageBox.Show("产品类别不能为空");
                return false;
            }
            if (string.IsNullOrEmpty(txtUnit.Text))
            {
                MessageBox.Show("采购单位不能为空");
                txtUnit.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(txt库存单位.Text))
            {
                MessageBox.Show("库存单位不能为空");
                txtUnit.Focus();
                return false;
            }
            return true;
        }

        protected override void InitControls()
        {
            txtCategory.Init(ProductClass);
            if (Category != null) txtCategory.Text = Category.Name;
            base.InitControls();
        }

        protected override void ShowButtonState()
        {
            base.ShowOperatorRights();
            btnOk.Enabled = Operator.Current.Permit(Permission.Product, PermissionActions.Edit);
            mnu_AttachmentAdd.Enabled = mnu_AttachmentAdd.Enabled && Operator.Current.Permit(Permission.Product, PermissionActions.EditAttachment);
            mnu_AttachmentDelete.Enabled = mnu_AttachmentDelete.Enabled && Operator.Current.Permit(Permission.Product, PermissionActions.EditAttachment);
            mnu_AttachmentOpen.Enabled = mnu_AttachmentOpen.Enabled && Operator.Current.Permit(Permission.Product, PermissionActions.ShowAttachment);
            mnu_AttachmentSaveAs.Enabled = mnu_AttachmentSaveAs.Enabled && Operator.Current.Permit(Permission.Product, PermissionActions.ShowAttachment);
        }

        protected override void ItemShowing()
        {
            Product p = UpdatingItem as Product;
            txtID.Text = p.ID;
            txtID.Enabled = false;
            txtName.Text = p.Name;
            txtCategory.Text = p.Category != null ? p.Category.Name : null;
            Category = p.Category;
            txtSpecification.Text = p.Specification;
            txtModel.Text = p.Model;
            txtUnit.Text = p.Unit;
            txt库存单位.Text = p.InventoryUnit;
            chkService.Checked = p.IsService != null && p.IsService.Value;
            txtMemo.Text = p.Memo;
            ShowAttachmentHeaders(p.ID, p.DocumentType, this.gridAttachment);
        }

        protected override Object GetItemFromInput()
        {
            Product p = null;
            if (UpdatingItem == null)
            {
                p = new Product();
                p.ClassID = ProductClass;
            }
            else
            {
                p = UpdatingItem as Product;
            }
            p.ID = txtID.Text != "自动创建" ? txtID.Text : string.Empty;
            p.Name = txtName.Text;
            p.CategoryID = txtCategory.SelectedCategoryID;
            p.Category = txtCategory.SelectedCategory;
            p.Specification = txtSpecification.Text;
            p.Model = txtModel.Text;
            p.Unit = txtUnit.Text;
            p.InventoryUnit = txt库存单位.Text;
            p.IsService = chkService.Checked || ProductClass == ProductClass.其它费用;
            p.Memo = txtMemo.Text;
            return p;
        }

        protected override CommandResult AddItem(object addingItem)
        {
            Product p = addingItem as Product;
            return new ProductBLL(AppSettings.Current.ConnStr).Add(p);
        }

        protected override CommandResult UpdateItem(object updatingItem)
        {
            Product p = updatingItem as Product;
            return new ProductBLL(AppSettings.Current.ConnStr).Update(p);
        }
        #endregion

        #region 与附件操作相关的方法和事件处理程序
        private void mnu_AttachmentAdd_Click(object sender, EventArgs e)
        {
            Product item = UpdatingItem as Product;
            if (item != null) PerformAddAttach(item.ID, item.DocumentType, gridAttachment);
        }

        private void mnu_AttachmentDelete_Click(object sender, EventArgs e)
        {
            PerformDelAttach(gridAttachment);
        }

        private void mnu_AttachmentSaveAs_Click(object sender, EventArgs e)
        {
            PerformAttachSaveAs(gridAttachment);
        }

        private void mnu_AttachmentOpen_Click(object sender, EventArgs e)
        {
            PerformAttachOpen(gridAttachment);
        }

        private void gridAttachment_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            PerformAttachOpen(gridAttachment);
        }
        #endregion
    }
}
