using HZH_Controls;
using HZH_Controls.Controls;
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 WinForms
{
    public partial class SelectWork : Form
    {
        public String selectValue;
        public SelectWork()
        {
            InitializeComponent();
            setDataGridView();
            BindData(1);
        }

        private void setDataGridView()
        {
            List<DataGridViewColumnEntity> lstCulumns = new List<DataGridViewColumnEntity>();
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "customer_name", HeadText = "客户名称", Width = 80, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "name_", HeadText = "品种及规格", Width = 120, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "width_", HeadText = "幅宽", Width = 80, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "number_", HeadText = "编号", Width = 100, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "process_date", HeadText = "投坯日", Width = 180, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "process_volume", HeadText = "投坯数", Width = 80, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "ratio_", HeadText = "伸长率", Width = 80, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "colour_", HeadText = "色泽", Width = 80, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "estimate_quantity", HeadText = "应交成品数", Width = 120, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "remark_", HeadText = "备注", Width = 200, WidthType = SizeType.Absolute });
            this.ucDataGridView1.Columns = lstCulumns;
        }

        private void BindData(int pageIndex)
        {
            int pageSize = this.ucPagerControl21.PageSize;
            string strWhere = "a.del_flag = 0 ";
            string strOrderBy = "create_time desc ";
            if(!String.IsNullOrEmpty(this.txtName.Text.Trim()))
            {
                strWhere += String.Format("AND b.name_ LIKE '%{0}%' ", this.txtName.Text.Trim());
            }

            Jpsoft.BLL.Work bll = new Jpsoft.BLL.Work();
            DataSet dataSet = bll.GetListByPage(pageSize, pageIndex, strWhere, strOrderBy);
            DataTable dataTable = dataSet.Tables[0];
            this.ucDataGridView1.DataSource = dataTable;

            this.ucPagerControl21.PageCount = Convert.ToInt32(Math.Ceiling(bll.GetRecordCount(strWhere) / this.ucPagerControl21.PageSize.ToDouble()));
            this.ucPagerControl21.PageIndex = pageIndex;
        }

        private void ucPagerControl21_ShowSourceChanged(object currentSource)
        {
            int pageIndex = ((UCPagerControl2)currentSource).PageIndex;
            BindData(pageIndex);
        }

        private void btnSearch_BtnClick(object sender, EventArgs e)
        {
            BindData(1);
        }

        private void btnSubmit_BtnClick(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(this.selectValue))
            {
                MessageBox.Show("请选择一个工单！");
            }
            else
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        private void btnCancel_BtnClick(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void ucDataGridView1_ItemClick(object sender, DataGridViewEventArgs e)
        {
            DataTable dt = (DataTable)this.ucDataGridView1.DataSource;
            this.selectValue = dt.Rows[e.RowIndex]["id_"].ToString();
        }
    }
}
