using Models;
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;

namespace AssistClient
{
    public partial class FrmProductList : Form
    {
        Service.ProductService service = new Service.ProductService();
        public Models.Product Product { get; private set; }
        public FrmProductList()
        {
            InitializeComponent();
            dataGridView1.AutoGenerateColumns = false;  
        }
        private void initDataView()
        {
            Product = null;
            var list = service.GetListNav();
            tsslCount.Text = $"共 {list.Count} 条记录";
            dataGridView1.DataSource = new BindingSource(list, null);
        } 
        private void FrmProductList_Load(object sender, EventArgs e)
        {
            initDataView();
        } 
        private void tsbtnAdd_Click(object sender, EventArgs e)
        {
            //FrmProductInfo frm = new FrmProductInfo();
            //frm.ShowDialog();
            //initDataView();
        }

        private void tsbtnDel_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.dataGridView1.CurrentRow == null)
                    return;

                if (MessageBox.Show($"确认删除？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    var list = ((BindingSource)dataGridView1.DataSource).DataSource as List<Product>;
                    int liIndex = this.dataGridView1.CurrentRow.Index;//获取当前选中行的索引   
                    if (!service.DelNav(list[liIndex]))
                        throw new Exception("删除失败！");
                    MessageBox.Show("删除成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    initDataView();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void tsbtnSetting_Click(object sender, EventArgs e)
        {
            if(this.dataGridView1.CurrentRow == null) return;

            var list = ((BindingSource)dataGridView1.DataSource).DataSource as List<Product>;
            int liIndex = this.dataGridView1.CurrentRow.Index;//获取当前选中行的索引   
            FrmProductStep frm = new FrmProductStep(list[liIndex]);
            frm.ShowDialog();
            initDataView();
        }
        private void tsbtnRevise_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.dataGridView1.CurrentRow == null)
                    throw new Exception("请选择要校正的产品！");

                var list = ((BindingSource)dataGridView1.DataSource).DataSource as List<Product>;
                int liIndex = this.dataGridView1.CurrentRow.Index;//获取当前选中行的索引   
                if (list[liIndex].ReviseStepId==null || list[liIndex].ReviseStepId<1)
                    throw new Exception("此产品未设置校正流程！");

                if (MessageBox.Show($"确认执行校正产品流程？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    Product=list[liIndex];
                    this.DialogResult = DialogResult.Yes;
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void tsbtnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            //if (!this.tsbtnAdd.Enabled) return;
            //var list = ((BindingSource)dataGridView1.DataSource).DataSource as List<Product>;
            //int liIndex = this.dataGridView1.CurrentRow.Index;//获取当前选中行的索引    
            //FrmProductInfo frm = new FrmProductInfo(list[liIndex]);
            //frm.ShowDialog(this);
            //initDataView();
        }
          
        private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            var list = ((BindingSource)dataGridView1.DataSource).DataSource as List<Product>;
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (list[i].StepInfo != null) 
                    dataGridView1.Rows[i].Cells["colStepName"].Value = list[i].StepInfo.Name;
                //if (list[i].ReviseStepInfo != null)
                //    dataGridView1.Rows[i].Cells["colReviseStepName"].Value = list[i].ReviseStepInfo.Name;
                if (list[i].AssistStepInfo != null)
                    dataGridView1.Rows[i].Cells["colAssistStepName"].Value = list[i].AssistStepInfo.Name;
               
            }
        }

       
    }
}
