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.WinForm.Common;
using QMAPP.FJC.Entity;
using QMAPP.FJC.Entity.Basic;
using QMFrameWork.Data;
using QMAPP.ServicesAgent;
using QMFrameWork.Common.Serialization;
using QMAPP.KB.Entity;

namespace QMAPP.WinForm.Forms.Injection
{
    public partial class SingleSlushMoldConfig : Form
    {

        MoldConfig searchModel;

        public SingleSlushMoldConfig()
        {
            InitializeComponent();
            DGSlushMolding.AutoGenerateColumns = false;
            SetSearchModel();
            BindGirdData();
        }

        private void SingleSlushMoldConfig_Load(object sender, EventArgs e)
        {
            this.comHOrL.DisplayMember = "VALUE";
            this.comHOrL.ValueMember = "KEY";

            List<object> comHOrList = new List<object>();
            comHOrList.Add(new { VALUE = "", KEY = "" });
            comHOrList.Add(new { VALUE = "高配+低配", KEY = "HB" });
            comHOrList.Add(new { VALUE = "低配+低配", KEY = "BB" });
            comHOrList.Add(new { VALUE = "高配+高配", KEY = "HH" });
            this.comHOrL.DataSource = comHOrList;
            this.comHOrL.SelectedIndex = 0;
        }

        #region 按钮事件

        /// <summary>
        /// 查询按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbSearch_Click(object sender, EventArgs e)
        {
            SetSearchModel();
            BindGirdData();
        }

        /// <summary>
        /// 设置查询条件
        /// </summary>
        private void SetSearchModel()
        {
            searchModel = new MoldConfig();
            var hb = comHOrL.SelectedItem;
            if (hb != null)
            {
                searchModel.HB = GetPropertyValue(hb, "KEY").ToString();
            }
            searchModel.STATUS = -1;

        }

        /// <summary>
        /// 新增按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbAdd_Click(object sender, EventArgs e)
        {
            SingleSlushMoldConfigEdit editForm = new SingleSlushMoldConfigEdit();
            DialogResult result = editForm.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                
                BindGirdData();
            }
        }

        /// <summary>
        /// 编辑按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbEdit_Click(object sender, EventArgs e)
        {

            if (this.DGSlushMolding.SelectedRows.Count > 0)
            {
                string selectKey = this.DGSlushMolding.SelectedRows[0].Cells["ID"].Value.ToString();

                QMAPP.ServicesAgent.ServiceAgent agent = ClientContext.GetServiceAgent();
                DataResult<MoldConfig> resultMoldConfig = agent.InvokeServiceFunction<DataResult<MoldConfig>>(B9BasicService.MoldConfigBLL_Get.ToString(), new MoldConfig() { PID = selectKey });
                MoldConfig entity = resultMoldConfig.Result;

                SingleSlushMoldConfigEdit editForm = new SingleSlushMoldConfigEdit(entity);
                DialogResult result = editForm.ShowDialog();
                if (result == System.Windows.Forms.DialogResult.OK)//判断是否投料窗口已做操作
                {
                    BindGirdData();
                }
            }
        }

        /// <summary>
        /// 删除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbDelete_Click(object sender, EventArgs e)
        {
            if (this.DGSlushMolding.SelectedRows.Count <= 0)
                return;
            if (MessageBox.Show(Resource1.ConfirmDelete, Resource1.ConfirmTitle, MessageBoxButtons.OKCancel) != DialogResult.OK)
                return;
            #region 执行删除
            string selectKey = this.DGSlushMolding.SelectedRows[0].Cells["ID"].Value.ToString();
            //删除校验判断该投料批次号是否已经生产出产品,如果已生产出产品则不允许删除,如果不允许则弹出提示'该批次号物料已使用,不能删除!'
            QMAPP.ServicesAgent.ServiceAgent agent = ClientContext.GetServiceAgent();
            DataResult<int> result = agent.InvokeServiceFunction<DataResult<int>>(B9BasicService.MoldConfigBLL_Delete.ToString(), new MoldConfig() { PID = selectKey });
            MessageBox.Show(result.Msg);
            if (result.IsSuccess)
            {
                BindGirdData();
            }
            #endregion
        }

        #endregion


        #region 绑定数据

        private DataPage BindGirdData()
        {
            List<MoldConfig> recorders = null;//查询结果列表
            DataPage dataPage = new DataPage();
            //获取前台分页设置信息
            dataPage.PageIndex = 1;
            dataPage.PageSize = 100;
            try
            {
                #region 服务查询
                QMAPP.ServicesAgent.ServiceAgent agent = ClientContext.GetServiceAgent();
                dataPage = agent.InvokeServiceFunction<DataPage>(B9BasicService.MoldConfigBLL_GetList.ToString(), searchModel);
                recorders = JsonConvertHelper.GetDeserialize<List<MoldConfig>>(dataPage.Result.ToString());
                foreach (MoldConfig mo in recorders)
                {
                    mo.USEDSTRING = (mo.USED == 0) ? "否" : "是";
                    mo.STATUSSTRING = (mo.STATUS == 0) ? "否" : "是";

                    if (mo.HB == "HB")
                    {
                        mo.HBSTRING = "高配+低配";
                    }
                    else if (mo.HB == "BB")
                    {
                           mo.HBSTRING = "低配+低配";
                    }
                    else
                    {
                           mo.HBSTRING = "高配+高配";
                    }
                }
                #endregion
                this.DGSlushMolding.DataSource = recorders;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return dataPage;
        }


        #endregion


        public object GetPropertyValue(object info, string field)
        {
            if (info == null) return null;
            Type t = info.GetType();
            IEnumerable<System.Reflection.PropertyInfo> property = from pi in t.GetProperties() where pi.Name.ToLower() == field.ToLower() select pi;
            return property.First().GetValue(info, null);
        }

    }
}