using com.hnshituo.core.webapp.vo;
using Common;
using Common.vo.pb;
using Infragistics.Win.UltraWinGrid;
using JC_MeasuringSystem;
using MeterModuleLibrary;
using MeterPlugInLibrary;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Threading;
using System.Windows.Forms;

namespace BeltScaleSystem

{
    public partial class frmBeltInstrument : Form
    {
        public frmBeltInstrument()
        {
            InitializeComponent();
        }
       

        private Log lg = Log.GetInstance();
        private BaseDbCls bd = new BaseDbCls();
        private string strIpAddress = "";

        private MeterBaseScalePointService monitorService = new MeterBaseScalePointService();

        private void frmMonitor_Load(object sender, EventArgs e)
        {
            PbCacheMonitor.frmMsgClosingTime = DateTime.Now;

            #region 载入计量点的信息

            bd.setBaseDbScalePoint();
            setGridData();
            strIpAddress = GetIP();
            #endregion 载入计量点的信息

            //thVoice.Start();
        }

        /// <summary>
        /// 计量点信息
        /// </summary>
        public void setGridData()
        {
            DataTable dt = dtJGPointInfo.Clone();
            if (PbCacheMonitor.sportInfoList!=null&& PbCacheMonitor.sportInfoList.Count>0&& PbCacheMonitor.ltMeterBaseScalePoint != null && PbCacheMonitor.ltMeterBaseScalePoint.Count > 0)
            {

                foreach (MeterBaseScalePoint mwm in PbCacheMonitor.ltMeterBaseScalePoint)
                {
                    DataRow dr = dt.NewRow();
                    dr["scalePointNo"] = mwm.scalePointNo;
                    dr["baseSpotNo"] = mwm.baseSpotNo;
                    dr["baseSpotName"] = mwm.baseSpotName;
                    dr["monitorUserNo"] = mwm.monitorUserNo;
                    dr["collectionCodeA"] = mwm.collectionCode;
                    dr["collectionCodeB"] = mwm.collectionCodeB;
                    dt.Rows.Add(dr);
                }               
            }

            ClsControlPack.CopyDataToDatatable(ref dt, ref this.dtJGPointInfo, true);
        }


        private void ultraGridPW_CellChange(object sender, CellEventArgs e)
        {
            ultraGridPW.UpdateData();
            UltraGridRow ugr = ultraGridPW.ActiveRow;
            if (ugr != null)
            {
                if (ugr.Cells["UNCK"].Value.ToString().ToLower() == "false")
                {
                    ucBeltInstrumentInfo u = (ucBeltInstrumentInfo)flPanel.Controls["uc" + ugr.Cells["baseSpotNo"].Text];
                    flPanel.Controls.RemoveByKey("uc" + ugr.Cells["baseSpotNo"].Text);
                    u.Close();
                }
                else {
                    ucBeltInstrumentInfo uc = new ucBeltInstrumentInfo();
                    uc.Name = "uc" + ugr.Cells["baseSpotNo"].Text;
                    uc.collectionCodeA = ugr.Cells["collectionCodeA"].Text.Trim();
                    uc.collectionCodeB = ugr.Cells["collectionCodeB"].Text.Trim();
                    uc.sIpAddress = strIpAddress;
                    uc.sPointNo = ugr.Cells["baseSpotNo"].Text;
                    uc.sPointName = ugr.Cells["baseSpotName"].Text;
                    uc.BorderStyle = BorderStyle.FixedSingle;
                    flPanel.Controls.Add(uc);

                }
               
            }
        }


        /// <summary>
        /// 界面关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmMonitor_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {


                List<UltraGridRow> uGrid = GridHelper.UltraGridGetChooseRows(ultraGridPW, "UNCK");
                if (uGrid.Count == 0)
                {
                    return;
                }
                foreach (UltraGridRow ugr in uGrid)
                {
                    if (ugr.Cells["UNCK"].Value.ToString().ToUpper() == "TRUE")
                    {
                        ucBeltInstrumentInfo u = (ucBeltInstrumentInfo)flPanel.Controls["uc" + ugr.Cells["baseSpotNo"].Text];
                        flPanel.Controls.RemoveByKey("uc" + ugr.Cells["baseSpotNo"].Text);
                        u.Close();
                        ugr.Cells["UNCK"].Value = false;
                    }
                }
            }
            catch (Exception ex)
            {
                lg.WriteLog(4, "关闭皮带秤计量异常：" + ex.Message);
            }
            finally
            {

            }
        }


        /// <summary>
        /// 获取IP地址
        /// </summary>
        /// <returns></returns>
        public string GetIP()
        {
            try
            {
                IPHostEntry ipHost = Dns.Resolve(Dns.GetHostName());
                IPAddress ipAddr = ipHost.AddressList[0];
                return ipAddr.ToString();
            }
            catch (Exception ex)
            {
                Random rd = new Random();
                MessageBox.Show("GetIP方法异常，请关闭界面并稍后打开，或联系管理员！\r\n异常原因:\r\n!" + ex);
                lg.WriteLog(13, "GetIP方法异常：" + ex.Message);
                return rd.Next(99999, 999999) + "";
            }
        }
    }
}