using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Office.Utils;
using Mesnac.Action.ChemicalWeighing.LjDry;
using Mesnac.Action.ChemicalWeighing.LjMaterial;

namespace Mesnac.Action.ChemicalWeighing.LjProdcutLine
{
    public partial class FrmProductLine : Form
    {
        public FrmProductLine()
        {
            InitializeComponent();
            Init();
        }

        private int _id = 0;
        public FrmProductLine(int id) : this()
        {
            _id = id;
            ProductLineView view = ProductLineDb.GetById(id);
            this.TxtName.Text = view.Name;
            List<int> checkstrs = view.DryId.Split(',')
                .Select(x => Convert.ToInt32(x)).ToList();

            for (int i = 0; i < checkedListBox1.Items.Count; i++)
            {
                for (int j = 0; j < checkstrs.Count(); j++)
                {
                    int va = checkstrs[j];
                    if (((MyNameValue)checkedListBox1.Items[i]).Id ==va)
                    {
                        checkedListBox1.SetItemChecked(i,true);
                        break;
                    }
                }
            }
        }
        
        private void Init()
        {
            var myNameValues = DrySettingDb.GetDry();
            checkedListBox1.DataSource = myNameValues;
            checkedListBox1.ValueMember = "Id";
            checkedListBox1.DisplayMember = "Name";
            
        }

        private void FrmProductLine_Load(object sender, EventArgs e)
        {
            
           
        }

       
        private void btnOK_Click(object sender, EventArgs e)
        {
            StringBuilder idSb = new StringBuilder();
            StringBuilder valSb = new StringBuilder();
            for (int i = 0, counti = checkedListBox1.Items.Count; i < counti; i++)
            {
                if (checkedListBox1.GetItemChecked(i))
                {
                  
                    valSb.AppendFormat("{0},", checkedListBox1.GetItemText(checkedListBox1.Items[i]));

                    MyNameValue   dv   =   ((MyNameValue)checkedListBox1.Items[i]);
                    idSb.AppendFormat("{0},",dv.Id);
                   
                }
            }

            if (idSb.Length == 0)
            {
                MessageBox.Show("请选择对应的设备");
                return;
            }

            string ids = idSb.ToString().Substring(0, idSb.Length - 1);
            string names = valSb.ToString().Substring(0, valSb.Length - 1);

            string name = TxtName.Text;
            if (string.IsNullOrEmpty(name))
            {
                MessageBox.Show("请填写名字");
                return;
            }

            ProductLineView view = new ProductLineView()
            {
                Name = name,
                DryName = names,
                CreateTime = DateTime.Now,
                DryId = ids
            };

            if (_id > 0)
            {
                ProductLineDb.Update(view);
            }
            else
            {
                ProductLineDb.Add(view);
            }

            this.DialogResult = DialogResult.OK;
        }

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