using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Mesnac.Action.ChemicalWeighing.LjMaterial;
using Mesnac.Action.ChemicalWeighing.LjMetageFormula;
using Mesnac.Action.ChemicalWeighing.LjMixFormula;
using Mesnac.Action.ChemicalWeighing.LjPlanning.Db;
using Mesnac.Action.ChemicalWeighing.LjProdcutLine;

namespace Mesnac.Action.ChemicalWeighing.LjPlanning
{
    public partial class FrmPlanning : Form
    {
        public FrmPlanning()
        {
            InitializeComponent();
            FillController();
        }

        private int _id;

        private LjPlanningView view = new LjPlanningView();
        public FrmPlanning(int id) : this()
        {
            _id=id;
            var ljPlanningView = LjPlanningDb.GetById(id);
            if (ljPlanningView != null)
            {
                txtCarNumber.Text = ljPlanningView.NumCar.ToString();
                txtName.Text= ljPlanningView.PlanName.ToString();
                Checked(DrpFormula, ljPlanningView.FormulaId);
                //cbUnit

                Checked(cbUnit, ljPlanningView.Unit);
                string cbClass = ljPlanningView.ClassName;
                if (cbClass == "早")
                {
                    this.cbClass.SelectedIndex = 0;
                }
                else
                {
                    if (cbClass == "中")
                    {
                        this.cbClass.SelectedIndex = 1;
                    }
                    else
                    {
                        this.cbClass.SelectedIndex = 2;
                    }
                }

            }
        }


        private void Checked(ComboBox alBox,int checkId)
        {
            foreach (MyNameValue combo in alBox.Items)
            {
                if (combo.Id == checkId)
                    if (combo.Id == checkId)
                {
                    alBox.SelectedItem = combo;
                    break;
                }
            }
        }

      

        private void FillController()
        {
           
            FillComboBox(DrpFormula);
            FillComboBox(cbClass);
            FillComboBox(cbUnit);
            List<MyNameValue> lsClass = new List<MyNameValue>();
            lsClass.Add(new MyNameValue()
            {
                Id=1,
                Name="早"
            });
            lsClass.Add(new MyNameValue()
            {
                Id = 2,
                Name = "中"
            });
            lsClass.Add(new MyNameValue()
            {
                Id = 3,
                Name = "晚"
            });

            cbClass.DataSource = lsClass;

            List<MyNameValue> lsUnit = new List<MyNameValue>();
            lsUnit.Add(new MyNameValue()
            {
                Id = 1,
                Name = "1#"
            });
            lsUnit.Add(new MyNameValue()
            {
                Id = 2,
                Name = "2#"
            });
            lsUnit.Add(new MyNameValue()
            {
                Id = 3,
                Name = "3#"
            });

            cbUnit.DataSource = lsUnit;
            DrpFormula.DataSource = LjFormulaDb.GetDrp();
        }


        private void FillComboBox(ComboBox combo)
        {
            combo.ValueMember = "Id";
            combo.DisplayMember = "Name";
        }

        private void DrpProductLine_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            var selectedIndex = DrpFormula.SelectedIndex;
            
            
        }

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

        private void btnOK_Click(object sender, System.EventArgs e)
        {
            string carNumber = txtCarNumber.Text.Trim();

            if (string.IsNullOrEmpty(carNumber))
            {
                MessageBox.Show(@"请输入车次");
                return;
            }

            if (!int.TryParse(carNumber, out var carNo))
            {
                MessageBox.Show(@"请输入正确的车次");
                return;
            }


            view.PlanName = txtName.Text; 
            view.CreateTime = DateTime.Now;
            view.NumCar = carNo;
            view.FormulaId = Convert.ToInt32(DrpFormula.SelectedValue);
            view.FormulaName = DrpFormula.Text;
            view.ClassName = cbClass.Text;
            view.Unit = Convert.ToInt32(cbUnit.SelectedValue);

            //需要设计状态  0 未开始 -1 异常退出  1 完成  

            if (_id == 0)
            {
                view.CreateTime = DateTime.Now;
                view.Status = "0";
                LjPlanningDb.Add(view);
            }
            else
            {
                view.UpdateTime=    DateTime.Now;
                view.Status = "0";
                view.Id = _id;
                LjPlanningDb.Update(view);
            }

            this.DialogResult = DialogResult.OK;

        }
    }
}