using Models;
using SqlSugar;
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 ProductionControl
{
    public partial class FrmOrderList : Form
    {
        Service.OrderService service = new Service.OrderService();
        public FrmOrderList()
        {
            InitializeComponent();
            dataGridView1.AutoGenerateColumns = false; 
            //控制日期或时间的显示格式
            this.dateTimePicker1.CustomFormat = this.dateTimePicker2.CustomFormat = "yyyy-MM-dd HH:mm:ss";
            //使用自定义格式
            this.dateTimePicker1.Format = this.dateTimePicker2.Format = DateTimePickerFormat.Custom;
            //时间控件的启用
            this.dateTimePicker1.ShowUpDown = this.dateTimePicker2.ShowUpDown = true;
            dateTimePicker1.Value = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd")); 
            dateTimePicker2.Value = Convert.ToDateTime(DateTime.Now.AddDays(1).ToString("yyyy-MM-dd"));
        }
        private void initDataView()
        {
            //显示的数据 
            this.cobProductList.DisplayMember = "Name";
            this.cobProductList.ValueMember = "Id"; 
            var lstProduct = service.GetProductList();
            lstProduct.Insert(0, new Product() { Name = "全部", Code = "", CreateUserCode = "", ModifyUserCode = "" });
            this.cobProductList.DataSource = lstProduct;

            this.cobStepList.DisplayMember = "Name";
            this.cobStepList.ValueMember = "Id";
            var listStep = service.GetStepList();
            listStep.Insert(0, new Step() { Name = "全部", Code = "", CreateUserCode = "", ModifyUserCode = "" });
            this.cobStepList.DataSource = listStep;
        }
        private void queryData()
        { 
            //创建表达式
            var exp1 = Expressionable.Create<Order>() 
                            .AndIF((int)cobProductList.SelectedValue>0, it => it.ProductId == (int)cobProductList.SelectedValue)
                            .AndIF((int)cobStepList.SelectedValue > 0, it => it.StepId == (int)cobStepList.SelectedValue)  
                            .AndIF(dateTimePicker1.Checked, it => it.CreateTime >= dateTimePicker1.Value)
                            .AndIF(dateTimePicker2.Checked, it => it.CreateTime < dateTimePicker2.Value)
                            .ToExpression();//注意 这一句 不能少

            int liTotalCount = 0;
            var list = service.GetListNav(this.uiPagination1.CurrentPage, this.uiPagination1.PageSize, ref liTotalCount, exp1);
            this.uiPagination1.TotalCount = liTotalCount; 

            dataGridView1.DataSource = new BindingSource(list, null);
        } 

        private void FrmProductList_Load(object sender, EventArgs e)
        {
            initDataView();
            queryData();
        }

        private void tsbtnQuery_Click(object sender, EventArgs e)
        {
            queryData();
        }
        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 tsbtnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            //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<Order>;
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (list[i].ProductInfo != null) dataGridView1.Rows[i].Cells["colProductName"].Value = list[i].ProductInfo.Name;
                if (list[i].StepInfo != null) dataGridView1.Rows[i].Cells["colStepName"].Value = list[i].StepInfo.Name;
            }
        }

        
    }
}
