using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LYFZ.Software.MainBusiness.InventoryManagement
{
    public partial class InventoryPurchaseOrderManagementFrmMain : LYFZ.Software.UI.InventoryManagement.InventoryPurchaseOrderManagementFrmMain
    {
        LYFZ.BLL.BLL_ErpOrder orbll = new BLL.BLL_ErpOrder();
        LYFZ.BLL.BLL_ErpPurchaseOrder porbll = new BLL.BLL_ErpPurchaseOrder();
        LYFZ.BLL.BLL_ErpPurchaseProduct pptbll = new BLL.BLL_ErpPurchaseProduct();
        public InventoryPurchaseOrderManagementFrmMain()
        {
            this.Load += InventoryPurchaseOrderManagementFrmMain_Load;
            this.Shown += InventoryPurchaseOrderManagementFrmMain_Shown;
            this.cmbtrrevOrderStatus.ComboBoxTree_NodeMouseClick += cmbtrrevOrderStatus_ComboBoxTree_NodeMouseClick;
            this.btnPurchaseRequest.Click += btnPurchaseRequest_Click;
            this.btnProcurementOrderUpdate.Click += btnProcurementOrderUpdate_Click;
            this.btnPurchaseProduct.Click += btnPurchaseProduct_Click;
            this.btnPurchaseCheckAccept.Click += btnPurchaseCheckAccept_Click;
            this.btnByVendorPurchase.Click += btnByVendorPurchase_Click;
            this.btnByVendorCheckAccept.Click += btnByVendorCheckAccept_Click;
            this.btnPrint.Click += btnPrint_Click;
            this.btnDelete.Click += btnDelete_Click;
            this.btnClose.Click += btnClose_Click;
            this.Resize += InventoryPurchaseOrderManagementFrmMain_Resize;
        }
        /// <summary>
        /// 相片集合
        /// </summary>
        ImageList mlistImage = new ImageList();
        /// <summary>
        /// 窗体加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void InventoryPurchaseOrderManagementFrmMain_Load(object sender, EventArgs e)
        {
            this.btnPurchaseRequest.Enabled = LYFZ.BLL.BLL_ErpUser.GetRights(LYFZ.BLL.BLL_ErpUser.CurrentUserRights.StockCompetence, CustomAttributes.OperatingAuthority.Add);
            this.btnProcurementOrderUpdate.Enabled = LYFZ.BLL.BLL_ErpUser.GetRights(LYFZ.BLL.BLL_ErpUser.CurrentUserRights.StockCompetence, CustomAttributes.OperatingAuthority.Add);
            this.btnDelete.Enabled = LYFZ.BLL.BLL_ErpUser.GetRights(LYFZ.BLL.BLL_ErpUser.CurrentUserRights.PurchasingManagementCompetence, CustomAttributes.OperatingAuthority.Delete);
            this.btnPurchaseProduct.Enabled = LYFZ.BLL.BLL_ErpUser.GetRights(LYFZ.BLL.BLL_ErpUser.CurrentUserRights.PurchasingManagementCompetence, CustomAttributes.OperatingAuthority.PurchasingManagement);
            this.btnByVendorPurchase.Enabled = LYFZ.BLL.BLL_ErpUser.GetRights(LYFZ.BLL.BLL_ErpUser.CurrentUserRights.PurchasingManagementCompetence, CustomAttributes.OperatingAuthority.PurchasingManagement);
            this.btnPurchaseCheckAccept.Enabled = LYFZ.BLL.BLL_ErpUser.GetRights(LYFZ.BLL.BLL_ErpUser.CurrentUserRights.PurchasingManagementCompetence, CustomAttributes.OperatingAuthority.AcceptanceManagement);
            this.btnByVendorCheckAccept.Enabled = LYFZ.BLL.BLL_ErpUser.GetRights(LYFZ.BLL.BLL_ErpUser.CurrentUserRights.PurchasingManagementCompetence, CustomAttributes.OperatingAuthority.AcceptanceManagement);

            this.listViewData.View = View.Details;
            this.listViewData.GridLines = true;
            string[] StrColumns = "采购单号,采购申请人,采购申请时间,采购状态,".Split(',');
            for (int i = 0; i < StrColumns.Length; i++)
            {
                ColumnHeader ch = new ColumnHeader();
                ch.Text = StrColumns[i].Trim();
                switch (StrColumns[i].Trim())
                {
                    case "采购单号":
                    case "采购申请人":
                    case "采购申请时间":
                    case "采购状态":
                        ch.Width = 150;
                        break;
                    default:
                        ch.Width = this.listViewData.Width - (150 * 4) - 23;
                        break;
                }
                ch.TextAlign = HorizontalAlignment.Center;
                this.listViewData.Columns.Add(ch);
            }
            this.mlistImage.ImageSize = new Size(1, 25);
            this.mlistImage.ColorDepth = ColorDepth.Depth32Bit;
            this.listViewData.SmallImageList = mlistImage;
        }
        /// <summary>
        /// 窗体加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void InventoryPurchaseOrderManagementFrmMain_Shown(object sender, EventArgs e)
        {
            bool b = LYFZ.Software.MainBusiness.VersionControl.StaticVersion.UIFunctionVersion(
                        VersionControl.VersionFunctionEnum.库存管理库存采购流程
                    );
            if (!b)
            {
                this.Close();
                return;
            }

            LYFZ.Software.MainBusiness.DoorCityProcess.PublicCodeClasses.BindComboxTreeView(this.cmbtrrevOrderStatus, "3:申请中,0:采购中,1:采购OK", IsFirstNodeNull: true, IsFirstNodeName: "全部", IsSplit: true, StrSplitSymbol: ':');
            this.cmbtrrevOrderStatus.TextFindTag("全部");
            this.PublicFunctionRows();


        }
        /// <summary>
        /// 采购单状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void cmbtrrevOrderStatus_ComboBoxTree_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            this.PublicFunctionRows();
        }
        /// <summary>
        /// 申请采购
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnPurchaseRequest_Click(object sender, EventArgs e)
        {
            LYFZ.Software.MainBusiness.InventoryManagement.InventoryDetailFrmMain frm = new InventoryDetailFrmMain();
            frm.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
            frm.Location = new Point(LYFZ.EnumPublic.FormLocation_X, LYFZ.EnumPublic.FormLocation_Y);
            frm.Size = new Size(LYFZ.EnumPublic.FormSize_Width, LYFZ.EnumPublic.FormSize_Height);
            frm.IsShowUcNavigationTool = false;
            frm.ShowDialog();
            if (frm.IsSaveed)
            { this.PublicFunctionRows(); }
            else
            { this.listViewData.Select(); }
        }
        /// <summary>
        /// 采购申请单修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnProcurementOrderUpdate_Click(object sender, EventArgs e)
        {
            if (this.listViewData.Items.Count > 0 && this.listViewData.SelectedItems.Count > 0)
            {
                LYFZ.Software.MainBusiness.InventoryManagement.SetSmallForm.InventoryRequisitionUpdateSmallForm frm = new SetSmallForm.InventoryRequisitionUpdateSmallForm();
                frm.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
                frm.Location = new Point(LYFZ.EnumPublic.FormLocation_X, LYFZ.EnumPublic.FormLocation_Y);
                frm.Size = new Size(LYFZ.EnumPublic.FormSize_Width, LYFZ.EnumPublic.FormSize_Height);
                frm.StrOrderNumber = this.listViewData.SelectedItems[0].Text.Trim();
                frm.ShowDialog();
                if (frm.IsSaveed)
                { this.PublicFunctionRows(); }
                else
                { this.listViewData.Select(); }
            }
        }
        /// <summary>
        /// 采购商品
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnPurchaseProduct_Click(object sender, EventArgs e)
        {
            if (this.listViewData.Items.Count > 0 && this.listViewData.SelectedItems.Count > 0)
            {
                LYFZ.Software.MainBusiness.InventoryManagement.SetSmallForm.InventoryPurchaseProductSmallForm frm = new SetSmallForm.InventoryPurchaseProductSmallForm();
                frm.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
                frm.Location = new Point(LYFZ.EnumPublic.FormLocation_X, LYFZ.EnumPublic.FormLocation_Y);
                frm.Size = new Size(LYFZ.EnumPublic.FormSize_Width, LYFZ.EnumPublic.FormSize_Height);
                frm.StrOrderNumber = this.listViewData.SelectedItems[0].Text.Trim();
                frm.ShowDialog();
                if (frm.IsSaveed)
                { this.PublicFunctionRows(); }
                else
                { this.listViewData.Select(); }
            }
        }
        /// <summary>
        /// 采购验收
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnPurchaseCheckAccept_Click(object sender, EventArgs e)
        {
            if (this.listViewData.Items.Count > 0 && this.listViewData.SelectedItems.Count > 0)
            {
                LYFZ.Software.MainBusiness.InventoryManagement.SetSmallForm.InventoryPurchaseProductCheckAcceptSmallForm frm = new SetSmallForm.InventoryPurchaseProductCheckAcceptSmallForm();
                frm.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
                frm.Location = new Point(LYFZ.EnumPublic.FormLocation_X, LYFZ.EnumPublic.FormLocation_Y);
                frm.Size = new Size(LYFZ.EnumPublic.FormSize_Width, LYFZ.EnumPublic.FormSize_Height);
                frm.StrOrderNumber = this.listViewData.SelectedItems[0].Text.Trim();
                frm.ShowDialog();
                if (frm.IsSaveed)
                { this.PublicFunctionRows(); }
                else
                { this.listViewData.Select(); }
            }
        }
        /// <summary>
        /// 按厂家采购
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnByVendorPurchase_Click(object sender, EventArgs e)
        {
            LYFZ.Software.MainBusiness.InventoryManagement.SetSmallForm.InventoryByVendorPurchaseSmallForm frm = new SetSmallForm.InventoryByVendorPurchaseSmallForm();
            frm.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
            frm.Location = new Point(LYFZ.EnumPublic.FormLocation_X, LYFZ.EnumPublic.FormLocation_Y);
            frm.Size = new Size(LYFZ.EnumPublic.FormSize_Width, LYFZ.EnumPublic.FormSize_Height);
            frm.ShowDialog();
            if (frm.IsSaveed)
            { this.PublicFunctionRows(); }
        }
        /// <summary>
        /// 按厂家验收
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnByVendorCheckAccept_Click(object sender, EventArgs e)
        {
            LYFZ.Software.MainBusiness.InventoryManagement.SetSmallForm.InventoryByVendorCheckAcceptSmallForm frm = new SetSmallForm.InventoryByVendorCheckAcceptSmallForm();
            frm.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
            frm.Location = new Point(LYFZ.EnumPublic.FormLocation_X, LYFZ.EnumPublic.FormLocation_Y);
            frm.Size = new Size(LYFZ.EnumPublic.FormSize_Width, LYFZ.EnumPublic.FormSize_Height);
            frm.ShowDialog();
            if (frm.IsSaveed)
            { this.PublicFunctionRows(); }
        }
        /// <summary>
        /// 打印
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnPrint_Click(object sender, EventArgs e)
        {
            if (this.listViewData.Items.Count > 0 && this.listViewData.SelectedItems.Count > 0)
            {
                LYFZ.Software.MainBusiness.ReportPrint.ReportFixedFormat.PrintFixedFormat(LYFZ.EnumPublic.PrintTypeEnum.库存采购申请单, this.listViewData.SelectedItems[0].Text.Trim());
            }
            else
            { MessageBoxCustom.Show("无打印的数据!"); return; }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.listViewData.Items.Count > 0 && this.listViewData.SelectedItems.Count > 0)
            {
                LYFZ.Model.Model_ErpPurchaseOrder modelOrder = porbll.GetModel(Convert.ToInt32(this.listViewData.SelectedItems[0].Tag));
                if (modelOrder.Pord_PurchaseStatus.Trim() == "0")
                { MessageBoxCustom.Show("采购单已在采购中,不能删除采购单!"); return; }
                if (modelOrder.Pord_PurchaseStatus.Trim() == "1")
                { MessageBoxCustom.Show("采购单已采购OK,不能删除采购单!"); return; }

                List<Helper.CommandInfo> clist = new List<Helper.CommandInfo>();
                clist.Add(porbll.GetDeleteCommandInfo("ID", "=", modelOrder.ID));
                clist.Add(pptbll.GetDeleteCommandInfo("Ppt_PurchaseNumber", "=", modelOrder.Pord_PurchaseNumber));
                if (clist.Count > 0)
                {
                    if (MessageBoxCustom.Show("采购单删除后,则无法找!您确定要删除吗?", "温馨提示", MessageBoxButtons.YesNo) == DialogResult.No)
                    { return; }
                    if (LYFZ.BLL.BaseBllOperate.ExecuteSqlTran(clist) > 0)
                    {
                        MessageBoxCustom.Show("删除成功!");
                        this.listViewData.Items.Remove(this.listViewData.SelectedItems[0]);
                        LYFZ.Software.MainBusiness.DoorCityProcess.PublicCodeClasses.ListViewDrawSingleOrDoubleRows(this.listViewData);
                    }
                    else
                    { MessageBoxCustom.Show("删除失败!"); }

                }
            }
        }
        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// 窗体加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void InventoryPurchaseOrderManagementFrmMain_Resize(object sender, EventArgs e)
        {
            for (int i = 0; i < this.listViewData.Columns.Count; i++)
            {
                switch (this.listViewData.Columns[i].Text.Trim())
                {
                    case "采购单号":
                    case "采购申请人":
                    case "采购申请时间":
                    case "采购状态":
                        this.listViewData.Columns[i].Width = 150;
                        break;
                    default:
                        this.listViewData.Columns[i].Width = this.listViewData.Width - (150 * 4) - 23;
                        break;
                }
            }
            switch (this.panelCloseed.Location.Y)
            {
                case 5: this.flowLayoutPanel1.Height = 40; break;
                case 41: this.flowLayoutPanel1.Height = 75; break;
            }
        }
        /// <summary>
        /// 创建行
        /// </summary>
        void PublicFunctionRows()
        {
            this.listViewData.Items.Clear();
            string StrWhere = "";
            if (!string.IsNullOrEmpty(this.cmbtrrevOrderStatus.Text.Trim()) && this.cmbtrrevOrderStatus.Text.Trim() != "全部")
            { StrWhere += "Pord_PurchaseStatus = '" + this.cmbtrrevOrderStatus.Tag.ToString().Trim() + "'"; }
            DataTable dt = orbll.GetView_Custom("tb_ErpPurchaseOrder Left Join tb_ErpUser on User_EmployeeID = Pord_RequisitionName", StrWhere: StrWhere, ShowColumnName: "tb_ErpPurchaseOrder.ID,Pord_PurchaseNumber AS 采购单号,User_Name AS 采购申请人,dbo.fn_CheckDateTime(Pord_RequisitionTime) AS 申请采购时间,Pord_PurchaseStatus AS 采购状态", filedOrder: "Pord_RequisitionTime Desc").Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ListViewItem lvItem = new ListViewItem();
                if ((i % 2) > 0)
                { lvItem.BackColor = System.Drawing.Color.PapayaWhip; }
                else
                { lvItem.BackColor = System.Drawing.Color.PowderBlue; }

                lvItem.Tag = dt.Rows[i]["ID"].ToString().Trim();
                lvItem.Text = dt.Rows[i]["采购单号"].ToString().Trim();

                ListViewItem.ListViewSubItem ListSubItem = new ListViewItem.ListViewSubItem();
                ListSubItem.Name = "采购申请人";
                ListSubItem.Text = dt.Rows[i]["采购申请人"].ToString().Trim();
                lvItem.SubItems.Add(ListSubItem);

                ListSubItem = new ListViewItem.ListViewSubItem();
                ListSubItem.Name = "采购申请时间";
                ListSubItem.Text = dt.Rows[i]["申请采购时间"].ToString().Trim();
                lvItem.SubItems.Add(ListSubItem);

                ListSubItem = new ListViewItem.ListViewSubItem();
                ListSubItem.Name = "采购状态";
                ListSubItem.Text = Enum.GetName(typeof(LYFZ.EnumPublic.Ppt_PurchaseStatus), Convert.ToInt32(dt.Rows[i]["采购状态"]));
                lvItem.SubItems.Add(ListSubItem);

                if (dt.Rows[i]["采购状态"].ToString().Trim() == "0" || dt.Rows[i]["采购状态"].ToString().Trim() == "3")
                { lvItem.ForeColor = Color.Red; }

                this.listViewData.Items.Add(lvItem);
            }
        }
    }
}