using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Common;
using com.hnshituo.core.webapp.vo;
using Infragistics.Win.UltraWinGrid;

namespace MeterModuleLibrary
{
    public partial class ucStaticTrackMeterInfo : UserControl
    {
        Log mylog = Log.GetInstance();

        public ucStaticTrackMeterInfo()
        {
            InitializeComponent();
        }

        #region 计量信息
        //计量预报
        PreRailwayScale preTack = null;

        //轨道衡计量预报表服务
        PreRailwayScaleService railwayScaleService = new PreRailwayScaleService();
        #endregion

        /// <summary>
        /// 预报编号信息
        /// </summary>
        public string predictionNo
        {
            get { return txtPREDICTION_NO.Text.Trim(); }
        }
        
        /// <summary>
        /// 物资名称
        /// </summary>
        public string matterName
        {
            get { return txtMATTER_NAME.Text.Trim(); }
        }


        private void txtPREDICTION_NO_KeyDown(object sender, KeyEventArgs e)
        {
            if (PbStaticRailwayCache.collect != null && PbStaticRailwayCache.collect.mainWgt > 0)
            {
                if (e.KeyValue == 13)
                {
                    string strM = txtPREDICTION_NO.Text.Trim();
                    PbStaticRailwayCache.strCode = strM;

                    setPreTrackInfo(new PreRailwayScale());

                    RESTfulResult<List<PreRailwayScale>> rm = railwayScaleService.doQueryWf(new PreRailwayScale { predictionNo = strM });
                    if (rm.Succeed)
                    {
                        if (rm.Data == null || rm.Data.Count == 0)
                        {
                            txtMEMO.Text = "未找到预报信息，请确定输入的信息是否有错";
                        }
                        else
                        {
                            if (rm.Data[0].valueFlag != "1" && rm.Data[0].valueFlag != "4") //1未使用，4正使用
                            {
                                txtMEMO.Text = "请确保当前预报已配车且未使用或正使用";
                            }
                            else
                            {
                                preTack = rm.Data[0];
                                setPreTrackInfo(preTack);
                                //seValidWhp();
                                //strType = "1";
                                //btnSave.Visible = validPredictionNo(preTack.predictionNo);
                            }
                        }
                    }

                    //strTxtCarNo = txtCarNo.Text.Trim();
                    //QueryCarNo();
                    //flagCarNo = true;
                }
            }
        }

        public void setPreTrackInfo(PreRailwayScale pts)
        {
            int count = 0;
            try
            {
                setMsgInfo(txtPREDICTION_NO, pts.predictionNo == null ? "" : pts.predictionNo, "");
                count = 1;
                setMsgInfo(txtBATCH_NO, pts.batchNo == null ? "" : pts.batchNo, "");
                count = 2;
                setMsgInfo(txtMATTER_NAME, pts.matterName == null ? "" : pts.matterName, "");
                count = 3;
                setMsgInfo(txtMETER_TYPE_NAME, pts.meterTypeName == null ? "" : pts.meterTypeName, "");
                count = 4;
                setMsgInfo(txtSHIPMENT_NUM, pts.shipmentNum == null ? "" : pts.shipmentNum.Value + "", "");
                count = 5;
                setMsgInfo(txtFORWARDING_UNIT_NAME, pts.forwardingUnitName == null ? "" : pts.forwardingUnitName, "");
                count = 6;
                setMsgInfo(txtRECEIVING_UINT_NAME, pts.receivingUintName == null ? "" : pts.receivingUintName, "");
                count = 7;
                setMsgInfo(txtLOAD_POINT_NAME, pts.loadPointName == null ? "" : pts.loadPointName, "");
                count = 8;
                setMsgInfo(txtCARRIER_UNIT_NAME, pts.carrierUnitName == null ? "" : pts.carrierUnitName, "");
                count = 9;
                setMsgInfo(txtMEMO, pts.memo == null ? "" : pts.memo, "");
                count = 10;
                setMsgInfo(txtSHIPMENT_NET_WEIGHT, pts.shipmentNetWeight == null ? "" : pts.shipmentNetWeight.Value + "", "");
                count = 11;
                setMsgInfo(txtSHIPMENT_GROSS_WEIGHT, pts.shipmentGrossWeight == null ? "" : pts.shipmentGrossWeight.Value + "", "");
                count = 12;
                setMsgInfo(chk_Sample, string.IsNullOrEmpty(pts.sampleNo) ? "false" : "true", "3");
                count = 13;
                setMsgInfo(CHK_CHEMICAL, "false", "3");
                count = 14;
            }
            catch (Exception ex)
            {
                mylog.WriteLog(11, PbStaticRailwayCache.lockCarNo + "静态衡setPreTrackInfo公共方法：" + "异常出现在【" + count + "】" + ex);
            }


            /*
            if (preTack.matterNo != null && PbStaticRailwayCache.limit != null && PbStaticRailwayCache.limit.Count > 0)
            {
                List<MeterBaseLimitChemical> mblc = PbStaticRailwayCache.limit.Where(s => s.matterNo == preTack.matterNo).ToList();
                //该物料是危化品
                if (mblc.Count > 0)
                {
                    CHK_CHEMICAL.Checked = true;
                }
            }
            //*/
        }

        public void setGridInfo(UltraGridRow ugr)
        {
            setMsgInfo(txtPREDICTION_NO, ugr.Cells["predictionNo"].Text.Trim(), "");
            setMsgInfo(txtBATCH_NO, ugr.Cells["batchNo"].Text.Trim(), "");
            setMsgInfo(txtMATTER_NAME, ugr.Cells["matterName"].Text.Trim(), "");
            setMsgInfo(txtMETER_TYPE_NAME, ugr.Cells["predictionType"].Text.Trim(), "");
            setMsgInfo(txtSHIPMENT_NUM, ugr.Cells["shipmentNum"].Text.Trim(), "");

            setMsgInfo(txtFORWARDING_UNIT_NAME, ugr.Cells["forwardingUnitName"].Text.Trim(), "");
            setMsgInfo(txtRECEIVING_UINT_NAME, ugr.Cells["receivingUintName"].Text.Trim(), "");
            setMsgInfo(txtLOAD_POINT_NAME, ugr.Cells["loadPointName"].Text.Trim(), "");
            setMsgInfo(txtCARRIER_UNIT_NAME, ugr.Cells["carrierUnitName"].Text.Trim(), "");
            setMsgInfo(txtMEMO, ugr.Cells["memo"].Text.Trim(), "");
            setMsgInfo(txtSHIPMENT_NET_WEIGHT, ugr.Cells["shipmentNetWeight"].Text.Trim(), "");
            setMsgInfo(txtSHIPMENT_GROSS_WEIGHT, ugr.Cells["shipmentGrossWeight"].Text.Trim(), "");
        }

        public void setMsgInfo(Control control, string ResultMessage, string sType)
        {
            setMsgMsg(control, ResultMessage, sType);
        }

        private void setMsgMsg(Control control, string ResultMessage, string sType)
        {
            int count = 0;
            try
            {
                if (control.InvokeRequired)
                {
                    Action<Control, string, string> action = new Action<Control, string, string>(setMsgInfo);
                    Invoke(action, new object[] { control, ResultMessage, sType });
                }
                else
                {
                    if (ResultMessage != "true" && ResultMessage != "false")
                    {
                        count = 0;
                        control.Text = ResultMessage == null ? "" : ResultMessage;
                    }
                    else
                    {
                        switch (sType)
                        {
                            case "1":
                                count = 1;
                                control.Enabled = ResultMessage == "true" ? true : false;
                                ; break;
                            case "2":
                                count = 2;
                                control.Visible = ResultMessage == "true" ? true : false;
                                ; break;
                            case "3":
                                count = 3;
                                ((CheckBox)control).Checked = ResultMessage == "true" ? true : false;
                                break;
                            default: break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                mylog.WriteLog(11, PbStaticRailwayCache.lockCarNo + "静态衡setMsgMsg公共方法：" + "异常出现在【" + count + "】" + ex);
            }

        }
    }
}
