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

namespace QMAPP.WinForm.Forms.Injection
{
    public partial class SingleSlushMoldConfigEdit : Form
    {
        int maxStationValue = 0;

        int maxMoldValue = 0;

        MoldConfig moldConfig;

        public SingleSlushMoldConfigEdit()
        {
            InitializeComponent();
            moldConfig = new MoldConfig();
            this.Text = "新增模腔号设置";
        }

        public SingleSlushMoldConfigEdit(MoldConfig entity)
        {
            InitializeComponent();
            moldConfig = entity;
            this.Text = "修改模腔号设置";
        }

        private void SingleSlushMoldConfigEdit_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;

            this.comStatus.DisplayMember = "VALUE";
            this.comStatus.ValueMember = "KEY";
            List<object> comUsedList = new List<object>();
            comUsedList.Add(new { VALUE = "", KEY = "" });
            comUsedList.Add(new { VALUE = "是", KEY = "1" });
            comUsedList.Add(new { VALUE = "否", KEY = "0" });
            this.comStatus.DataSource = comUsedList;
            this.comStatus.SelectedIndex = 0;

            this.comCurrent.DisplayMember = "VALUE";
            this.comCurrent.ValueMember = "KEY";
            List<object> comCurrentList = new List<object>();
            comCurrentList.Add(new { VALUE = "", KEY = "" });
            comCurrentList.Add(new { VALUE = "否", KEY = "0" });
            comCurrentList.Add(new { VALUE = "是", KEY = "1" });
            this.comCurrent.DataSource = comCurrentList;
            this.comCurrent.SelectedIndex = 0;

            #region 服务查询
            QMAPP.ServicesAgent.ServiceAgent agent = ClientContext.GetServiceAgent();
            MoldConfig mold = agent.InvokeServiceFunction<MoldConfig>(B9BasicService.MoldConfigBLL_GetMaxMold.ToString());


            this.comMoldValue.DisplayMember = "VALUE";
            this.comMoldValue.ValueMember = "KEY";

            this.comMoldValue2.DisplayMember = "VALUE";
            this.comMoldValue2.ValueMember = "KEY";

            this.comMoldStation.DisplayMember = "VALUE";
            this.comMoldStation.ValueMember = "KEY";

            
            List<object> moldList = new List<object>();
            List<object> mold2List = new List<object>();
            List<object> moldStation = new List<object>();
           

            moldStation.Add(new { VALUE = "", KEY = "" });

            if (mold.MAXMOLDSTATION == 0)
            {
                mold.MAXMOLDSTATION = 30;
            }

            for (int i = 1; i <= mold.MAXMOLDSTATION; i++)
            {
                moldStation.Add(new { VALUE = i.ToString(), KEY = i.ToString() });
            }

            moldList.Add(new { VALUE = "", KEY = "" });
            mold2List.Add(new { VALUE = "", KEY = "" });


            if (mold.MAXMOLDVALUE == 0)
            {
                mold.MAXMOLDVALUE = 16;
            }
            for (int i = 1; i <= mold.MAXMOLDVALUE; i++)
            {
                moldList.Add(new { VALUE = i.ToString(), KEY = i.ToString() });
                mold2List.Add(new { VALUE = i.ToString(), KEY = i.ToString() });
            }

            comMoldValue.DataSource = moldList;
            comMoldValue2.DataSource = mold2List;
            comMoldStation.DataSource = moldStation;

            maxStationValue = mold.MAXMOLDSTATION;
            maxMoldValue = mold.MAXMOLDVALUE;

            #endregion

            if (string.IsNullOrEmpty(moldConfig.PID) == false)
            {
                comMoldStation.SelectedValue = moldConfig.MOLDSTATION;
                comHOrL.SelectedValue = moldConfig.HB;
                comMoldValue.SelectedValue = moldConfig.MOLDVALUE.Substring(0, moldConfig.MOLDVALUE.IndexOf("+"));
                comMoldValue2.SelectedValue = moldConfig.MOLDVALUE.Substring(moldConfig.MOLDVALUE.IndexOf("+") + 1);
                //indexNumber.Value = moldConfig.INDEXVALUE;
                comStatus.SelectedValue = moldConfig.STATUS.ToString();
                comCurrent.SelectedValue = moldConfig.USED.ToString();
                comIndexNumber.Text = moldConfig.INDEXVALUE.ToString();
            }

        }

        private void tsbClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void tsbSave_Click(object sender, EventArgs e)
        {

            string errorMessae = ErrorCheck();
            if (errorMessae != "")
            {
                MessageBox.Show(errorMessae, Resource1.ConfirmTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            #region 获取界面信息

            moldConfig.MOLDSTATION = comMoldStation.Text;
            var hb = comHOrL.SelectedItem;
            moldConfig.HB = GetPropertyValue(hb, "KEY").ToString();
            moldConfig.MOLDVALUE = comMoldValue.Text + "+" + comMoldValue2.Text;
            moldConfig.INDEXVALUE = Convert.ToInt32(comIndexNumber.Text);
            var status = comStatus.SelectedItem;
            moldConfig.STATUS = Convert.ToInt32(GetPropertyValue(status, "KEY").ToString());
            var used = comCurrent.SelectedItem;
            moldConfig.USED = Convert.ToInt32(GetPropertyValue(used, "KEY").ToString());

            #endregion

            #region 保存
            QMAPP.ServicesAgent.ServiceAgent agent = ClientContext.GetServiceAgent();

            DataResult result;//判断是否成功
            if (string.IsNullOrEmpty(moldConfig.PID))
            {
                result = agent.InvokeServiceFunction<DataResult>(B9BasicService.MoldConfigBLL_Insert.ToString(), moldConfig);
            }
            else
            {
                result = agent.InvokeServiceFunction<DataResult>(B9BasicService.MoldConfigBLL_Update.ToString(), moldConfig);
            }
            MessageBox.Show(result.Msg);
            //保存成功
            if (result.IsSuccess)
            {
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
            #endregion  

        }

        private string ErrorCheck()
        {
            string errorMsg = "";
            if (comMoldStation.SelectedIndex == 0)
            {
                errorMsg += "请选择模架号!\r\n";
            }

            if (comHOrL.SelectedIndex == 0)
            {
                errorMsg += "请选择高低配!\r\n";
            }

            if (comMoldValue.SelectedIndex == 0)
            {
                errorMsg += "请选择模腔号!\r\n";
            }

            if (comMoldValue2.SelectedIndex == 0)
            {
                errorMsg += "请选择模腔号!\r\n";
            }

            if (comMoldValue2.SelectedIndex == comMoldValue.SelectedIndex)
            {
                errorMsg += "请选择不同的模腔号!\r\n";
            }

            if (comIndexNumber.SelectedIndex == 0)
            {
                errorMsg += "请填写排列顺序!\r\n";
            }

            if (comStatus.SelectedIndex == 0)
            {
                errorMsg += "请选择是否可用!\r\n";
            }

            if (comCurrent.SelectedIndex == 0)
            {
                errorMsg += "请选择是否当前生产!\r\n";
            }


            DataPage dataPage = new DataPage();
            //获取前台分页设置信息
            dataPage.PageIndex = 1;
            dataPage.PageSize = 100;
            List<MoldConfig> recorders = null;//查询结果列表
            QMAPP.ServicesAgent.ServiceAgent agent = ClientContext.GetServiceAgent();
            dataPage = agent.InvokeServiceFunction<DataPage>(B9BasicService.MoldConfigBLL_GetList.ToString(), new MoldConfig());
            recorders = JsonConvertHelper.GetDeserialize<List<MoldConfig>>(dataPage.Result.ToString());


            if (string.IsNullOrEmpty(moldConfig.PID))
            {
                if (recorders.Count(o => o.MOLDSTATION == comMoldStation.Text) > 0)
                {
                    errorMsg += string.Format("模架号{0}配置已存在!\r\n", comMoldStation.Text);
                }

                if (recorders.Count(o => o.INDEXVALUE.ToString() == Convert.ToInt32(comIndexNumber.Text).ToString()) > 0)
                {
                    errorMsg += string.Format("排列顺序号{0}已存在!\r\n", Convert.ToInt32(comIndexNumber.Text).ToString());
                }
            }
            else
            {
                if (recorders.Count(o => o.PID != moldConfig.PID && o.MOLDSTATION == comMoldStation.Text) > 0)
                {
                    errorMsg += string.Format("模架号{0}配置已存在!\r\n", comMoldStation.Text);
                }

                if (recorders.Count(o => o.PID != moldConfig.PID && o.INDEXVALUE == Convert.ToInt32(comIndexNumber.Text)) > 0)
                {
                    errorMsg += string.Format("排列顺序号{0}已存在!\r\n", Convert.ToInt32(comIndexNumber.Text).ToString());
                }
            }
            

            return errorMsg;

        }

        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);
        }
    }
}