using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QMAPP.Entity;
using QMFrameWork.Data;
using QMAPP.FJC.Entity.Operation;
using QMFrameWork.Common.Serialization;
using QMAPP.ServicesAgent;
using QM.Common;
using QMAPP.WinForm.Common;

namespace QMAPP.WinForm.Forms.Mend
{
    public partial class ProductCheckForm : Form
    {
        ProductCheck searchModel;
        public ProductCheckForm()
        {
            InitializeComponent();
        }

        #region  加载界面信息
        /// <summary>
        /// 加载界面信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ProductCheckForm_Load(object sender, EventArgs e)
        {
            //时间搜索条件
            DateTime dtStart = DateTime.Now;
            dpOpeStart.Value = dtStart;
            dpOpeEnd.Value = dtStart.AddDays(1);
            BasicData basicData = new BasicData();

            #region 绑定下拉列表
            ////初始化控件属性
            this.dataGridView1.AutoGenerateColumns = false;
            new KeyEvent().SetKeyToTxt(this);
            this.cobResult.Items.Add(new ListItem("0", "合格"));
            this.cobResult.Items.Add(new ListItem("1", "不合格"));
            #endregion

            //加载首件过程下拉列表
            this.comCheckType.DataSource = basicData.GetDictionaryList("PRODUCTCHECKTYPENEW");
            this.comCheckType.DisplayMember = "value";
            this.comCheckType.ValueMember = "key";
            this.comCheckType.SelectedIndex = 3;


            //初始化分页
            this.pager1.Init();
            //加载默认查询条件
            DataResult result = SetSearchModel();
            if (result.IsSuccess != false)
            {
                BindGirdData();
                dataGridView1.ClearSelection();
            }
        }

        #endregion

        #region 绑定数据

        private DataPage BindGirdData()
        {
            List<ProductCheck> recorders = null;//查询结果列表
            DataPage dataPage = new DataPage();
            //获取前台分页设置信息
            dataPage = pager1.DataPage;
            try
            {
                #region 服务查询
                QMAPP.ServicesAgent.ServiceAgent agent = ClientContext.GetServiceAgent();
                dataPage = agent.InvokeServiceFunction<DataPage>("ProductCheckBLL_GetWinformList", searchModel, dataPage);
                recorders = JsonConvertHelper.GetDeserialize<List<ProductCheck>>(dataPage.Result.ToString());
                #endregion

                this.dataGridView1.DataSource = recorders;
                this.pager1.DataPage = dataPage;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dataPage;
        }

        #endregion

        #region  查询
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbSearch_Click(object sender, EventArgs e)
        {
            DataResult result = SetSearchModel();

            if (result.IsSuccess == false)
            {
                MessageBox.Show("请输入正确时间段值!");
                return;
            }
            else
            {
                BindGirdData();
                dataGridView1.ClearSelection();
            }
        }

        #endregion 

        #region 设置查询条件
        /// <summary>
        /// 设置查询条件
        /// </summary>
        private DataResult SetSearchModel()
        {
            DataResult result = new DataResult();
            result.IsSuccess = true;

            searchModel = new ProductCheck();

            searchModel.CREATEDATESTART = Convert.ToDateTime(dpOpeStart.Value.ToString("yyyy-MM-dd") + " 00:00:00");
            searchModel.CREATEDATEEND = Convert.ToDateTime(dpOpeEnd.Value.ToString("yyyy-MM-dd") + " 00:00:00");

            //条码
            if (string.IsNullOrEmpty(this.txtProductCode.Text.Trim()) == false)
            {
                searchModel.PRODUCTCODE = this.txtProductCode.Text.Trim();
            }
            //零件类别
            //if (this.comProType.SelectedValue != null && string.IsNullOrEmpty(this.comProType.SelectedValue.ToString()) == false)
            //{
            //    searchModel.PRODUCTTYPE = this.comProType.SelectedValue.ToString();
            //}
            if (string.IsNullOrEmpty(this.textMaterial.Text.Trim()) == false)
            {
                searchModel.MATERIAL_CODE = this.textMaterial.Text.Trim();
            }
            //结果
            if (this.cobResult.SelectedItem != null)
            {
                searchModel.CHECKRESULT = this.cobResult.SelectedIndex.ToString();
            }

            if (!string.IsNullOrEmpty(this.comCheckType.SelectedItem.ToString()))
            {
                searchModel.PRODUCTCHECKTYPE = this.comCheckType.SelectedIndex.ToString();
            }

            if (searchModel.CREATEDATESTART >= searchModel.CREATEDATEEND)
            {
                result.IsSuccess = false;
            }

            return result;
        }

        #endregion 

        #region 删除
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnScarp_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows.Count <= 0)
            {
                MessageBox.Show("请选择要删除的信息!");
                return;
            }
            if (MessageBox.Show(Resource1.ConfirmDelete, Resource1.ConfirmTitle, MessageBoxButtons.OKCancel) != DialogResult.OK)
                return;
            //执行删除
            string pid = this.dataGridView1.SelectedRows[0].Cells["id"].Value.ToString();
            QMAPP.ServicesAgent.ServiceAgent agent = ClientContext.GetServiceAgent();
            DataResult result = agent.InvokeServiceFunction<DataResult>("ProductCheckBLL_Delete", pid);
            if (result.IsSuccess == true)
            {
                DataResult validresult = SetSearchModel();

                if (validresult.IsSuccess == false)
                {
                    MessageBox.Show("请输入正确的投料对比时间!");
                    return;
                }
                else
                {
                    BindGirdData();
                    dataGridView1.ClearSelection();
                }
            }
            else
            {
                MessageBox.Show("删除失败!");
                return;
            }
        }
        #endregion 

        #region 抽检
        /// <summary>
        /// 抽检
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbCheck_Click(object sender, EventArgs e)
        {
            ProductCheckAddForm editForm = new ProductCheckAddForm();
            editForm.ShowDialog();

            //DataResult result = SetSearchModel();

            //if (result.IsSuccess == false)
            //{
            //    MessageBox.Show("请输入正确的投料对比时间!");
            //    return;
            //}
            //else
            //{
            BindGirdData();
            dataGridView1.ClearSelection();
            //}
        }
        #endregion 

        #region 查看明细
        /// <summary>
        /// 查看明细
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbMend_Click(object sender, EventArgs e)
        {

            if (this.dataGridView1.SelectedRows.Count > 0)
            {
                ProductCheckDetailForm editForm = new ProductCheckDetailForm(this);
                editForm.ShowDialog();
            }
            else
            {
                string errorMessae = "请选择一条信息查看明细!";
                MessageBox.Show(errorMessae, Resource1.ConfirmTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
        }
        #endregion 

    }
}