using DynStatDisk.App.Com;
using DynStatDisk.App.Model;
using NPOI.XSSF.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;

namespace DynStatDisk.App
{
    public partial class frmMachineModelConfig : Form
    {
        public frmMachineModelConfig()
        {
            InitializeComponent();
        }

        public event EventHandler MachineModelConfigChanged;

        //单例模式
        private static frmMachineModelConfig frm = null;
        public static frmMachineModelConfig CreateInstrance()
        {
            if (frm == null || frm.IsDisposed)
            {
                frm = new frmMachineModelConfig();
            }
            return frm;
        }

        private List<DynStatDiskModel> InitialLoadList = new List<DynStatDiskModel>();
        private long SelectedId;



        private void frmMachineModelConfig_Load(object sender, EventArgs e)
        {
            LoadComboxBoxData();
            listView1.Columns.Clear();
            LoadListView1Column();
            LoadData();
        }



        private void LoadListView1Column()
        {
            ColumnHeader h0 = new ColumnHeader();
            ColumnHeader h1 = new ColumnHeader();
            ColumnHeader h2 = new ColumnHeader();
            ColumnHeader h3 = new ColumnHeader();
            ColumnHeader h4 = new ColumnHeader();
            ColumnHeader h5 = new ColumnHeader();
            ColumnHeader h6 = new ColumnHeader();
            ColumnHeader h7 = new ColumnHeader();
            ColumnHeader h8 = new ColumnHeader();
            ColumnHeader h9 = new ColumnHeader();
            ColumnHeader h10 = new ColumnHeader();
            ColumnHeader h11 = new ColumnHeader();
            ColumnHeader h12 = new ColumnHeader();
            ColumnHeader h13 = new ColumnHeader();

            h1.TextAlign = HorizontalAlignment.Center;
            h2.TextAlign = HorizontalAlignment.Center;
            h3.TextAlign = HorizontalAlignment.Center;
            h4.TextAlign = HorizontalAlignment.Center;
            h5.TextAlign = HorizontalAlignment.Center;
            h6.TextAlign = HorizontalAlignment.Center;
            h7.TextAlign = HorizontalAlignment.Center;
            h8.TextAlign = HorizontalAlignment.Center;
            h9.TextAlign = HorizontalAlignment.Center;
            h10.TextAlign = HorizontalAlignment.Center;
            h11.TextAlign = HorizontalAlignment.Center;
            h12.TextAlign = HorizontalAlignment.Center;
            h13.TextAlign = HorizontalAlignment.Center;

            h0.Width = 20;
            h1.Width = 100;
            h2.Width = 100;
            h3.Width = 100;
            h4.Width = 0;
            h5.Width = 420;
            h6.Width = 150;
            h7.Width = 0;
            h8.Width = 0;
            h9.Width = 40;
            h10.Width = 100;
            h11.Width = 100;
            h12.Width = 100;
            h13.Width = 0;

            h0.Text = "";
            h1.Text = "机种番号";
            h2.Text = "间隙测试";
            h3.Text = "动静盘符号";
            h4.Text = "";
            h5.Text = "";
            h6.Text = "";
            h7.Text = "";
            h8.Text = "";
            h9.Text = "";

            h10.Text = "活塞油沟";
            h11.Text = "吸油管压入";
            h12.Text = "创建时间";
            h13.Text = "";
            //h14.Text = "";

            listView1.Columns.AddRange(new ColumnHeader[] { h0, h1, h2, h3, h4 });
            listView1.View = View.Details;
        }

        private void InsertlistView1Result(long guid, string ModelNo, string GapInfo, string DynStatDiskKeyMatch)
        {
            ListViewItem lvi = new ListViewItem();
            lvi.ImageIndex = 0;
            lvi.Text = "";
            lvi.SubItems.Add(ModelNo.ToString());
            lvi.SubItems.Add(GapInfo.ToString());
            lvi.SubItems.Add(DynStatDiskKeyMatch!=null ?DynStatDiskKeyMatch.ToString():"");
            lvi.SubItems.Add(guid.ToString());
            this.listView1.Items.Add(lvi);
        }

        private void LoadData()
        {
            listView1.Items.Clear();
            //InitialLoadList.Clear();
            //加载数据

            InitialLoadList = FSqlContext.FDb.Select<DynStatDiskModel>().ToList();

            if (InitialLoadList.Count > 0)
            {
                var data = InitialLoadList.OrderBy(a => a.CreateTime).ToList();
                foreach (var item in data)
                {
                    InsertlistView1Result(item.Id,
                        item.ModelNo,
                        item.GapInfo,
                        item.DynStatDiskKeyMatch
                      );
                }
            }
        }

        private void LoadComboxBoxData()
        {
            this.cbxGapTest.Items.Clear();

            this.cbxGapTest.Items.Add("检测");//赋值
            this.cbxGapTest.SelectedIndex = 0;//设置下标

            this.cbxGapTest.Items.Add("不检测");//赋值
            this.cbxGapTest.SelectedIndex = 1;//下标

            //this.cbxGapTest.Items.Add("C");//赋值
            //this.cbxGapTest.SelectedIndex = 2;//下标

            //this.cbxGapTest.Items.Add("无");//赋值
            //this.cbxGapTest.SelectedIndex = 3;//下标

            this.cbxGapTest.SelectedIndex = -1;//默认显示LHK


            //this.cbxInsulatPad.Items.Clear();

            //this.cbxInsulatPad.Items.Add("A");//赋值
            //this.cbxInsulatPad.SelectedIndex = 0;//设置下标

            //this.cbxInsulatPad.Items.Add("B");//赋值
            //this.cbxInsulatPad.SelectedIndex = 1;//下标

            //this.cbxInsulatPad.Items.Add("C");//赋值
            //this.cbxInsulatPad.SelectedIndex = 2;//下标

            //this.cbxInsulatPad.Items.Add("D");//赋值
            //this.cbxInsulatPad.SelectedIndex = 3;//下标

            //this.cbxInsulatPad.Items.Add("E");//赋值
            //this.cbxInsulatPad.SelectedIndex = 4;//下标

            //this.cbxInsulatPad.Items.Add("F");//赋值
            //this.cbxInsulatPad.SelectedIndex = 5;//下标

            //this.cbxInsulatPad.Items.Add("无");//赋值
            //this.cbxInsulatPad.SelectedIndex = 6;//下标

            //this.cbxInsulatPad.SelectedIndex = -1;//默认显示LHK



            //this.cbxDynStatDiskConver.Items.Clear();

            //this.cbxDynStatDiskConver.Items.Add("A");//赋值
            //this.cbxDynStatDiskConver.SelectedIndex = 0;//设置下标

            //this.cbxDynStatDiskConver.Items.Add("B");//赋值
            //this.cbxDynStatDiskConver.SelectedIndex = 1;//下标

            //this.cbxDynStatDiskConver.Items.Add("C");//赋值
            //this.cbxDynStatDiskConver.SelectedIndex = 2;//下标

            //this.cbxDynStatDiskConver.Items.Add("无");//赋值
            //this.cbxDynStatDiskConver.SelectedIndex = 3;//下标

            //this.cbxDynStatDiskConver.SelectedIndex = -1;//默认显示LHK

        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.SelectedIndices.Count > 0 && listView1.SelectedIndices != null && listView1.FocusedItem != null)
            {
                txtModelNo.Text = listView1.FocusedItem.SubItems[1].Text;
                //txtESOPPath.Text = listView1.FocusedItem.SubItems[5].Text;
                //cbxLevel.SelectedItem = listView1.FocusedItem.SubItems[3].Text;

                switch (listView1.FocusedItem.SubItems[2].Text)
                {
                    case "检测":
                        cbxGapTest.SelectedIndex = 0;
                        break;
                    case "不检测":
                        cbxGapTest.SelectedIndex = 1;
                        break;
                    //case "C":
                    //    cbxGapTest.SelectedIndex = 2;
                    //    break;
                    //case "无":
                    //    cbxGapTest.SelectedIndex = 3;
                    //    break;
                    default:
                        break;
                }

                txtDynStatDiskKeyMatch.Text= listView1.FocusedItem.SubItems[3].Text;

                //switch (listView1.FocusedItem.SubItems[3].Text)
                //{
                //    case "A":
                //        cbxInsulatPad.SelectedIndex = 0;
                //        break;
                //    case "B":
                //        cbxInsulatPad.SelectedIndex = 1;
                //        break;
                //    case "C":
                //        cbxInsulatPad.SelectedIndex = 2;
                //        break;
                //    case "D":
                //        cbxInsulatPad.SelectedIndex = 3;
                //        break;
                //    case "E":
                //        cbxInsulatPad.SelectedIndex = 4;
                //        break;
                //    case "F":
                //        cbxGapTest.SelectedIndex = 5;
                //        break;
                //    case "无":
                //        cbxGapTest.SelectedIndex = 6;
                //        break;
                //    default:
                //        break;
                //}


                //switch (listView1.FocusedItem.SubItems[4].Text)
                //{
                //    case "A":
                //        cbxDynStatDiskConver.SelectedIndex = 0;
                //        break;
                //    case "B":
                //        cbxDynStatDiskConver.SelectedIndex = 1;
                //        break;
                //    case "C":
                //        cbxDynStatDiskConver.SelectedIndex = 2;
                //        break;
                //    case "无":
                //        cbxDynStatDiskConver.SelectedIndex = 3;
                //        break;
                //    default:
                //        break;
                //}

                var guid = listView1.FocusedItem.SubItems[4].Text;
                SelectedId = long.Parse(guid);

            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtModelNo.Text.Trim()) &&
                //!string.IsNullOrEmpty(txtESOPPath.Text.Trim()) &&
                !string.IsNullOrEmpty(cbxGapTest.SelectedItem.ToString().Trim())
                //!string.IsNullOrEmpty(cbxInsulatPad.SelectedItem.ToString().Trim()) &&
                //!string.IsNullOrEmpty(cbxDynStatDiskConver.SelectedItem.ToString().Trim())
                )
            {
                var ReturnData = FSqlContext.FDb.Insert<DynStatDiskModel>(
                    new DynStatDiskModel()
                    {
                        ModelNo = txtModelNo.Text.Trim(),
                        GapInfo = cbxGapTest.SelectedItem.ToString().Trim(),
                        DynStatDiskKeyMatch= txtDynStatDiskKeyMatch.Text.Trim(),
                        //InsulatPad = cbxInsulatPad.SelectedItem.ToString().Trim(),
                        //DynStatDiskConver = cbxDynStatDiskConver.SelectedItem.ToString().Trim(),
                        //ESOPPath = txtESOPPath.Text.ToString().Trim()
                    }
                    ).ExecuteInserted();

                LoadData();
            }
            else
            {
                MessageBox.Show("有空的数据存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            //if (CurrentUserLevel == "管理员")
            //{
            FSqlContext.FDb.Update<DynStatDiskModel>()
               .Set(a => a.ModelNo, txtModelNo.Text.Trim())
               .Set(a => a.GapInfo, cbxGapTest.SelectedItem.ToString().Trim())
               .Set(a => a.DynStatDiskKeyMatch, txtDynStatDiskKeyMatch.Text.Trim())
               //.Set(a => a.DynStatDiskConver, cbxDynStatDiskConver.SelectedItem.ToString().Trim())
               //.Set(a => a.ESOPPath, txtESOPPath.Text.ToString().Trim())
               .Where(a => a.Id == SelectedId)
               .ExecuteAffrows();

            LoadData();
            //}
            //else
            //{
            //    MessageBox.Show("你没有权限编辑！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //}

        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == MessageBox.Show("你确定要删除当前选中的数据行？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information))
            {
                FSqlContext.FDb.Delete<DynStatDiskModel>(SelectedId).ExecuteAffrows();

                LoadData();
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            MachineModelConfigChanged(this,new EventArgs());
            this.Close();
        }

        private void txtESOPPath_Click(object sender, EventArgs e)
        {
            //OpenFileDialog openfiledialog = new OpenFileDialog();//声明并初始化openfiledialog类
            //openfiledialog.InitialDirectory = @"E:\";//设置文件打开初始目录为E盘
            //openfiledialog.Title = "打开图片文件";//设置打开文件对话框标题
            //openfiledialog.Filter = "jpg Files(*.jpg)|*.jpg|png Files(*.png)|*.png"; //设置文件过滤类型
            ////openfiledialog.FilterIndex = 2;//根据文件类型索引设置文件过滤类型
            //openfiledialog.RestoreDirectory = true;//设置对话框是否记忆之前打开的目录
            //if (openfiledialog.ShowDialog() == DialogResult.OK)//当点击文件对话框的确定按钮时打开相应的文件
            //{
            //    //tbfilepath.Text = openfiledialog.FileName;//获取选择文件的完整路径名（含文件名称）
            //    txtESOPPath.Text = openfiledialog.FileName;
            //    //tbfilename.Text = tbfilepath.Text.Substring(tbfilepath.Text.LastIndexOf("\\") + 1);
            //}
        }
    }
}
