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 QMAPP.WinForm.Common;
using QMAPP.FJC.Entity.Basic;
using QMAPP.ServicesAgent;
using QMFrameWork.Data;
using QMAPP.FJC.Entity.Operation;
using QMFrameWork.Common.Serialization;
using QMAPP.Entity;

namespace QMAPP.WinForm.Forms.Operation
{
    /// <summary>
    /// 描述:加工统计
    /// 作者:王庆男
    /// 时间:2016年3月2日
    /// </summary>
    public partial class MainOperationStaticForm : Form
    {
        private ServiceAgent wcfAgent = null;
        private MachineInfo _machine = null;
        private ProcessSet _pSet = null;
        public MainOperationStaticForm()
        {
            InitializeComponent();
            if (ClientContext.MachineInfo != null)
            {
                _machine = ClientContext.MachineInfo;
            }

            Init();
        }

        public MainOperationStaticForm(MachineInfo machine)
        {
            InitializeComponent();
            _machine = machine;
            Init();
        }

        public MainOperationStaticForm(MachineInfo machine,ProcessSet pSet)
        {
            InitializeComponent();
            _machine = machine;
            _pSet = pSet;
            Init();
        }

        private void Init()
        {

            DGData.AutoGenerateColumns = false;

            wcfAgent = ClientContext.GetServiceAgent();

            DateTime dtStart = DateTime.Now;
            if (dtStart < Convert.ToDateTime(dtStart.ToShortDateString() + " 7:30:00"))
            {
                dpOpeStart.Value = dtStart;
                dpOpeEnd.Value = dtStart;

                comHourStart.Text = "0";
                comHourEnd.Text = "7";
                comMinuteStart.Text = "0";
                comMinuteEnd.Text = "30";
            }

            else if (dtStart > Convert.ToDateTime(dtStart.ToShortDateString() + " 7:30:00") && dtStart.Hour < 16)
            {
                dpOpeStart.Value = dtStart;
                dpOpeEnd.Value = dtStart;

                comHourStart.Text = "7";
                comMinuteStart.Text = "30";
                comHourEnd.Text = "16";
                comMinuteEnd.Text = "0";
            }

            else if (dtStart.Hour >= 16)
            {
                dpOpeStart.Value = dtStart;
                dpOpeEnd.Value = dtStart.AddDays(1);

                comHourStart.Text = "16";
                comHourEnd.Text = "0";
                comMinuteStart.Text = "0";
                comMinuteEnd.Text = "0";
            }

       

            //this.dateOpeStart.ValueX = dtStart;
            //this.dateOpeEnd.ValueX = dtEnd;
            BindDataGrid();
        }

        #region 绑定数据
        /// <summary>
        /// 绑定数据
        /// </summary>
        private void BindDataGrid()
        {
            MainOperation condtion = new MainOperation();
            condtion.CREATEUSER = ClientContext.LoginInfo.UserID;
            condtion.MACHINECODDE = _machine.MACHINECODDE;
            condtion.DATEOPESTART = Convert.ToDateTime(dpOpeStart.Value.ToString("yyyy-MM-dd") + " " + comHourStart.Text.ToString() + ":" + comMinuteStart.Text.ToString() + ":00");
            condtion.DATEOPEEND = Convert.ToDateTime(dpOpeEnd.Value.ToString("yyyy-MM-dd") + " " + comHourEnd.Text.ToString() + ":" + comMinuteEnd.Text.ToString() + ":00");
            var dataResult = wcfAgent.InvokeServiceFunction<DataResult<DataTable>>(QMAPP.ServicesAgent.B9BasicService.MainOperationBLL_GetListForstatisticWithColor.ToString(),
                condtion);

            //new MainOperation
            //    {
            //        DATEOPESTART = String.IsNullOrEmpty(dateOpeStart.Value.ToString("yyyy-MM-dd HH:mm:ss").Trim()) ? DateTime.MinValue : Convert.ToDateTime(dateOpeStart.Value.ToString("yyyy-MM-dd HH:mm:ss").Trim()),
            //        DATEOPEEND = String.IsNullOrEmpty(dateOpeEnd.Value.ToString("yyyy-MM-dd HH:mm:ss").Trim()) ? DateTime.MinValue : Convert.ToDateTime(dateOpeEnd.Value.ToString("yyyy-MM-dd HH:mm:ss").Trim()),
            //        CREATEUSER = ClientContext.LoginInfo.UserID,
            //        MACHINECODDE =  _machine.MACHINECODDE
            //    }

            if (!dataResult.IsSuccess)
            {
                MessageBox.Show(dataResult.Msg);
            }

            DataTable dt = dataResult.Result;

            if (dt.Rows.Count > 0)
            {
                int sum = 0;
                foreach (DataRow dr in dt.Rows)
                {
                    sum += Convert.ToInt32(dr["加工数量"]);
                }

                DataRow ndr = dt.NewRow();
                //ndr["颜色"] = "合计";
                ndr["加工数量"] = sum;
                dt.Rows.Add(ndr);
            }
            

            DGData.DataSource = dt;

            DGData.ClearSelection();

            //设置数值型局右显示
        }
        #endregion

        #region 查询
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbSearch_Click(object sender, EventArgs e)
        {
           
            BindDataGrid();
        }
        #endregion

        #region 窗体加载事件
        private void MainOperationStaticForm_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
        }
        #endregion

        #region 设置序号
        private void DGData_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, this.DGData.RowHeadersWidth - 4, e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), new Font("宋体", 12, FontStyle.Bold), rectangle, this.DGData.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }
        #endregion


        private void tbtnSearch_Click(object sender, EventArgs e)
        {
            Form f = Application.OpenForms["OperationForm"];  //查找是否打开过Form1窗体
            if (f == null)  //没打开过
            {
                OperationForm form = new OperationForm();
                form.MdiParent = this.ParentForm;
                form.Show();   //重新new一个Show出来
            }
            else
            {
                f.WindowState = FormWindowState.Normal;
                f.Focus();   //打开过就让其获得焦点
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            Form f = Application.OpenForms["MainOperationCompleteForm"];  //查找是否打开过Form1窗体
            if (f == null)  //没打开过
            {
                MainOperationCompleteForm form = new MainOperationCompleteForm();
                form.MdiParent = this.ParentForm;
                form.Show();   //重新new一个Show出来
            }
            else
            {
                f.WindowState = FormWindowState.Normal;
                f.Focus();   //打开过就让其获得焦点
            }
        }
    }
}