using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using QM.Common;
using QMAPP.FJC.Entity;
using QMAPP.FJC.Entity.Basic;
using QMAPP.FJC.Entity.ProduceManage;
using QMAPP.FJC.Entity.Operation;
using QMAPP.ServicesAgent;
using QMAPP.WinForm.Common;
using QMAPP.Entity;
namespace QMAPP.WinForm.Forms.Operation
{
    /// <summary>
    /// 模块编号:M12-1
    /// 作    用:返修,作废
    /// 作    者:王庆男
    /// 编写日期:2015年06月08日
    ///</summary>
    public partial class RepairForm : Form
    {
        Product product = new Product();//编辑保存实体
        Main main = new Main();
        MendRecorder recorder = new MendRecorder();
        QMAPP.ServicesAgent.ServiceAgent agent = ClientContext.GetServiceAgent();
        /// <summary>
        /// 新建
        /// </summary>
        public RepairForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 新建
        /// </summary>
        public RepairForm(string processCode)
        {
            recorder.CURRENTPROCESS = processCode;
            InitializeComponent();
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="pid"></param>
        public RepairForm(string pid,string productType,string processCode)
        {
            if (productType.Equals(EnumGeter.ProductType.benti.GetHashCode().ToString()))
            {
                main.PID = pid;
            }
            else
            {
                product.PID = pid;
            }
            recorder.CURRENTPROCESS = processCode;
            InitializeComponent();
        }

        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form_Load(object sender, EventArgs e)
        {
            #region 绑定下拉列表
            BasicData basicData = new BasicData();
            //返修类别
            this.comPartStatus.DataSource = basicData.GetDictionaryList(EnumGeter.DictKind.PRODUCTSTATUS.ToString());
            this.comPartStatus.DisplayMember = "VALUE";
            this.comPartStatus.ValueMember = "KEY";
            #endregion

            #region 加载编辑
            //判断是否为编辑加载编辑数据
            if (string.IsNullOrEmpty(product.PID) == false)
            {
                product = agent.InvokeServiceFunction<Product>(B9BasicService.ProductBLL_Get.ToString(), product);
                //窗口赋值
                this.txtPartCode.Text = product.PRODUCTCODE;
                this.comPartStatus.SelectedValue = product.STATUS;
            }
            if (string.IsNullOrEmpty(main.PID) == false)
            {
                main = agent.InvokeServiceFunction<Main>(B9IPCService.MainBLL_Get.ToString(), main);
                //窗口赋值
                this.txtPartCode.Text = main.EPIDERMISCODE;
                this.comPartStatus.SelectedValue = main.STATUS;
            }
            #endregion
        }

        /// <summary>
        /// 窗体保存事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbSave_Click(object sender, EventArgs e)
        {
            //窗体数据校验
            string errorMessae = ErrorCheck();
            if (errorMessae != "")
            {
                MessageBox.Show(errorMessae, Resource1.ConfirmTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            string pid = "";
            //获取零件信息
            if (string.IsNullOrEmpty(product.PID) == false)
            {
                pid = product.PID;
                product.STATUS = comPartStatus.SelectedValue.ToString();
            }
            if (string.IsNullOrEmpty(main.PID) == false)
            {
                pid = main.PID;
                main.STATUS = comPartStatus.SelectedValue.ToString();
            }

            //添加返修记录
            recorder.PPID = pid;
            recorder.PRODUCTCODE = product.PRODUCTCODE;
            recorder.PRODUCTTYPE = product.PRODUCTTYPE;
            recorder.PRODUCELINE = EnumGeter.PRODUCELINE.B9.ToString();
            recorder.PRODUCESHIFTNAME = ClientContext.produceShift.PRODUCESHIFTNAME;
            recorder.PRODUCESHIFTTCODE = ClientContext.produceShift.PRODUCESHIFTTCODE;
            recorder.MENDREASON = this.txtReason.Text;
            recorder.MENDTYPE = this.comPartStatus.SelectedValue.ToString();
            recorder.MENDRESULT = "";

            #region 保存
            DataResult<int> result = new DataResult<int>() ;//判断是否成功
            if (string.IsNullOrEmpty(product.PID) == false)
            {
                result = agent.InvokeServiceFunction<DataResult<int>>(B9BasicService.ProductBLL_UpdateStatus.ToString(), product, recorder);
            }
            if (string.IsNullOrEmpty(main.PID) == false)
            {
                result = agent.InvokeServiceFunction<DataResult<int>>(B9IPCService.MainBLL_UpdateStatus.ToString(), main, recorder);
            }

            //保存成功
            MessageBox.Show(result.Msg);
            if (result.IsSuccess)
            {
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
            #endregion
        }

        /// <summary>
        /// 窗体关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        ///<summary>
        ///错误检测
        ///</summary>
        ///<returns></returns>
        private string ErrorCheck()
        {
            #region 校验
            string strMessage = "";
            //物料号
            if (this.txtPartCode.Text.Trim() == string.Empty)
            {
                strMessage += Resource1.ProductCodeNullValidate + "\r\n";
            }

            //零件状态
            if (this.comPartStatus.SelectedValue == null || this.comPartStatus.SelectedValue == string.Empty)
            {
                strMessage += Resource1.ProductStatusNullValidate + "\r\n";
            }
            return strMessage;
            #endregion
        }
    }
}