using HighWayIot.Log4net;
using HighWayIot.Repository.service;
using Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HighWayIot.Winform.UserControlPages.ParamConfigPages
{
    public partial class ProductionScheduling : UserControl
    {
        ZxSchedulingService zxSchedulingService = ZxSchedulingService.Instance;

        ZxRecipeService zxRecipeService = ZxRecipeService.Instance;

        List<ZxRecipeEntity> zxRecipeEntities = new List<ZxRecipeEntity>();

        List<ZxSchedulingEntity> zxSchedulingEntity = new List<ZxSchedulingEntity>();

        BindingSource bindingSource = new BindingSource();

        public ProductionScheduling()
        {
            InitializeComponent();
            init();
        }

        private void init()
        {
            this.SchedulingDataGridView.DataError += new DataGridViewDataErrorEventHandler(SchedulingDataGridView_DataError);
            zxSchedulingEntity = zxSchedulingService.GetSchedulingInfo();
            SchedulingDataGridView.AutoGenerateColumns = false;
            bindingSource.DataSource = zxSchedulingEntity;
            SchedulingDataGridView.DataSource = null;
            SchedulingDataGridView.DataSource = bindingSource;
            ComboBoxBind();
        }

        /// <summary>
        /// 处理异常数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void SchedulingDataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            return;
        }

        /// <summary>
        /// 绑定下拉框数据源
        /// </summary>
        private void ComboBoxBind()
        {
            zxRecipeEntities = zxRecipeService.GetRecipeInfos();
            RecipeCode1.DataSource = null;
            RecipeCode1.DataSource = null;

            List<string> r1 = zxRecipeEntities.Select(x => x.RecipeCode).ToList();
            r1.Insert(0, "");
            RecipeCode1.DataSource = r1;
            List<string> r2 = zxRecipeEntities.Select(x => x.RecipeCode).ToList();
            r2.Insert(0, "");
            RecipeCode2.DataSource = r2;
        }

        private void UpdateConfigButton_Click(object sender, EventArgs e)
        {
            if (zxSchedulingService.UpdateSchedulingInfo(zxSchedulingEntity))
            {
                MessageBox.Show("更新成功");
            }
            else
            {
                MessageBox.Show("更新失败");
            }
        }

        /// <summary>
        /// 刷新页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SelectConfigButton_Click(object sender, EventArgs e)
        {
            zxSchedulingEntity = zxSchedulingService.GetSchedulingInfo();
            bindingSource.DataSource = null;
            bindingSource.DataSource = zxSchedulingEntity;
        }

        /// <summary>
        /// 实现单击一次显示下拉列表框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SchedulingDataGridView_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (SchedulingDataGridView.Columns[e.ColumnIndex] is DataGridViewComboBoxColumn && e.RowIndex != -1)
            {
                SendKeys.Send("{F4}");
            }
        }

        /**
        **第一步：
        **/
        ComboBox cbo = new ComboBox();
        private void SchedulingDataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (SchedulingDataGridView.CurrentCell.OwningColumn.Name.Contains("RecipeCode") && SchedulingDataGridView.CurrentCell.RowIndex != -1)
            {
                //保存当前的事件源。为了触发事件后。在取消
                cbo = e.Control as ComboBox;
                cbo.SelectedIndexChanged += new EventHandler(cbo_SelectedIndexChanged);
            }
        }

        /**
        **第二步：
        **/
        void cbo_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox combox = sender as ComboBox;
            //添加离开combox的事件（重要；）
            combox.Leave += new EventHandler(combox_Leave);
            try
            {
                //在这里就可以做值是否改变判断
                if (combox.SelectedItem != null)
                {
                    int row = SchedulingDataGridView.CurrentCell.RowIndex;
                    int column = SchedulingDataGridView.CurrentCell.ColumnIndex;

                    if(column == 2)
                    {
                        ZxRecipeEntity recipe = zxRecipeService.GetSingleInfoByRecipeCode(combox.Text);
                        if (recipe == null)
                        {
                            zxSchedulingEntity[row].RecipeName1 = string.Empty;
                        }
                        else
                        {
                            zxSchedulingEntity[row].RecipeName1 = recipe.RecipeName;
                        }
                    }

                    if(column == 4)
                    {
                        ZxRecipeEntity recipe = zxRecipeService.GetSingleInfoByRecipeCode(combox.Text);
                        if (recipe == null)
                        {
                            zxSchedulingEntity[row].RecipeName2 = string.Empty;
                        }
                        else
                        {
                            zxSchedulingEntity[row].RecipeName2 = recipe.RecipeName;
                        }
                    }
                    SchedulingDataGridView.Refresh();
                }
            }
            catch (Exception ex)
            {
                LogHelper.Instance.Error("硫化排程发生错误", ex);
            }
        }

        /**
        **第三步：离开combox时，把事件删除
        **/
        public void combox_Leave(object sender, EventArgs e)
        {
            ComboBox combox = sender as ComboBox;
            //做完处理，须撤销动态事件
            combox.SelectedIndexChanged -= new EventHandler(cbo_SelectedIndexChanged);
        }
    }
}
