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

namespace CarRemoteMeter
{
    public partial class frmCalibration : Form
    {
        Log lg = Log.GetInstance();

        public frmCalibration()
        {
            InitializeComponent();
        }

        bool bFlag = false;

        MeterWorkCalibrationBranch calibrationBranch { get; set; }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        public string userId = "", userName = "";
        
        //校秤主
        MeterWorkCalibrationMainService calibrationMainService = new MeterWorkCalibrationMainService();
        //校秤从
        MeterWorkCalibrationBranchService calibrationBranchService = new MeterWorkCalibrationBranchService();

        ComBaseInfoService cis = new ComBaseInfoService();

        private void btnSave_Click(object sender, EventArgs e)
        {
            lbMsgInfo.Visible = false;


            if (cbCalibrationType.Value == null || cbCalibrationType.Text.Trim() == "")
            {
                MessageBox.Show("请选择校秤类型");
                return;
            }

            if (cbCalibrationType.Value != null && cbCalibrationType.Value.ToString() == "001005002")
            {
                if (cbWorkMonitor.Value == null || cbWorkMonitor.Text.Trim() == "")
                {
                    MessageBox.Show("请选择对比计量点");
                    return;
                }
                if (string.IsNullOrEmpty(strCbCarNo))
                {
                    MessageBox.Show("互检校秤的车号不能为空");
                    return;
                }
            }

            DialogResult dr = MessageBox.Show("确认进行校秤吗？", "提示", MessageBoxButtons.OKCancel);
            if (dr == DialogResult.OK)
            {
                btnSave.Enabled = false;
                string baseContrastNo = PbCache.sportInfo.baseSpotNo;
                string baseContrastName = PbCache.sportInfo.baseSpotName;
                string dbWgt = PbCache.lockWgt + "";
                if (calibrationBranch != null) 
                {
                    baseContrastNo = calibrationBranch.baseSpotNo;
                    baseContrastName = calibrationBranch.baseSpotName;
                    dbWgt = calibrationBranch.calibrationWeight;
                }

                RESTfulResult<MeterWorkCalibrationBranch> rm = calibrationMainService.doSaveWf(
                    new MeterWorkCalibrationMain
                    {
                        baseSpotNo = baseContrastNo,
                        baseSpotName = baseContrastName,
                        spotTypeNo = cbSpotType.Value == null ? "001002001" : cbSpotType.Value.ToString().Trim(),
                        spotTypeName = cbSpotType.Text == "" ? "汽车衡" : cbSpotType.Text.Trim(),
                        calibrationTypeNo = cbCalibrationType.Value == null ? "" : cbCalibrationType.Value.ToString().Trim(),
                        calibrationTypeName = cbCalibrationType.Text, //校秤类型名称
                        baseContrastSpotNo = cbWorkMonitor.Value == null ? "" : cbWorkMonitor.Value.ToString().Trim(), //校秤对比计量点编号
                        baseContrastSpotName = cbWorkMonitor.Text.Trim() //校秤对比计量点名称
                    },
                    new MeterWorkCalibrationBranch
                    {
                        baseSpotNo = baseContrastNo,
                        baseSpotName = baseContrastName,
                        calibrationWeight = dbWgt + "",
                        spotTypeNo = cbSpotType.Value == null ? "001002001" : cbSpotType.Value.ToString().Trim(),
                        spotTypeName = cbSpotType.Text == "" ? "汽车衡" : cbSpotType.Text.Trim(),
                        calibrationTypeNo = cbCalibrationType.Value == null ? "" : cbCalibrationType.Value.ToString().Trim(),
                        calibrationTypeName = cbCalibrationType.Text,
                        createManNo = userId,
                        createManName = userName,
                        carNo = strCbCarNo
                    }
                ); ;

                if (rm.Succeed)
                {
                    #region 截取图片信息
                    try
                    {
                        calibrationBranch = rm.Data;

                        //*
                        //截取屏幕信息
                        Point screenPoint = this.PointToScreen(new Point());
                        Rectangle rect = new Rectangle(screenPoint, this.Size);
                        Image img = new Bitmap(rect.Width, rect.Height);
                        Graphics g = Graphics.FromImage(img);
                        g.CopyFromScreen(rect.X - 1, rect.Y - 1, 0, 0, rect.Size);//"D://file/1.jpg"
                        img.Save(string.Format("{0}imgShort\\tempImg\\{1}_{2}_{3}.jpg",
                            AppDomain.CurrentDomain.SetupInformation.ApplicationBase,
                            PbCache.sportInfo.baseSpotNo,
                            "C" + rm.Message,
                            (PbCache.videoChild.Count + 1)), System.Drawing.Imaging.ImageFormat.Jpeg);

                        //*
                        //最后进行截图操作
                        CameraShotCls cameraShot = new CameraShotCls();
                        cameraShot.CapMethod(rm.Message);
                        //*/
                    }
                    catch (Exception ex)
                    {
                        lg.WriteLog(5, PbCache.lockCarNo + "校秤截图失败");
                    }
                    #endregion

                    if (rm.Data == null || string.IsNullOrEmpty(rm.Data.baseSpotNo))
                    {
                        this.DialogResult = DialogResult.OK;
                        this.Close(); //校秤成功了，可能是单点通过，可能是同时通过
                    }
                    else //说明是单点校秤然后失败了，此时提醒选择下一个对比校秤点 
                    {
                        lbMsgInfo.Visible = true;
                        btnSave.Enabled = true;

                        if (!bFlag)
                        {
                            cbWorkMonitor.Enabled = true;
                            cbCalibrationType.Enabled = true;
                        }
                        else 
                        {
                            #region
                            RESTfulResult<List<MeterBaseMutualDiff>> rmX = mutualDiffService.doQueryWf(new MeterBaseMutualDiff { baseSpotOneNo = rm.Data.baseSpotNo });
                            ValueList vlist = new ValueList();
                            foreach (MeterBaseMutualDiff diff in rmX.Data)
                            {
                                vlist.ValueListItems.Add(diff.baseSpotOneNo, diff.baseSpotOneName);
                            }
                            cbWorkMonitor.ValueList = vlist;
                            #endregion
                        }
                    }
                }
                else
                {
                    MessageBox.Show("校秤异常：" + rm.Message);
                    btnSave.Enabled = true;
                }
            }
        }


        private void frmCalibration_Load(object sender, EventArgs e)
        {
            //calibrationBranch = null;

            timer1.Start();
            //cbSpotType
            ValueList vlistSpot = new ValueList();
            vlistSpot.ValueListItems.Add("001002001", "汽车衡");
            cbSpotType.ValueList = vlistSpot;
            cbSpotType.Value = "001002001";
            cbSpotType.Enabled = false;

            //校秤类型
            RESTfulResult<List<PbModelDb>> rm = cis.doQueryWf(new ComBaseInfo { pBaseCode = "001005" });
            ValueList vlistCalibration = new ValueList();
            foreach (PbModelDb pb in rm.Data)
            {
                vlistCalibration.ValueListItems.Add(pb.id, pb.text);
            }
            cbCalibrationType.ValueList = vlistCalibration;

            if (!string.IsNullOrEmpty(PbCache.collect.carno))
            {
                CarNoChange(PbCache.collect.carno);
            }

            txtCarNo.LostFocus += new EventHandler(txtCarNo_KeyDown);
        }

        MeterBaseMutualDiffService mutualDiffService = new MeterBaseMutualDiffService();

        bool bCarChange = false;
        private void timer1_Tick(object sender, EventArgs e)
        {
            txtWgt.Value = PbCache.collect.weight;
            if (!string.IsNullOrEmpty(PbCache.collect.carno) && PbCache.collect.carno.Length > 1 && !bCarChange)
            {
                cbCarNo.Text = PbCache.collect.carno.Substring(0, 1);
                txtCarNo.Text = PbCache.collect.carno.Substring(1, PbCache.collect.carno.Length - 1);
                bCarChange = true;

                strCbCarNo = cbCarNo.Text.Trim() + txtCarNo.Text.Trim();
                CarNoChange(strCbCarNo);
            }
        }

        private void frmCalibration_FormClosing(object sender, FormClosingEventArgs e)
        {
            timer1.Stop();
        }

        string strCbCarNo = "";
        private void cbCarNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (PbCache.collect != null && PbCache.collect.weight > 0)
            {
                if (cbCarNo.SelectedIndex > -1)
                {
                    strCbCarNo = cbCarNo.Text.Trim()+txtCarNo.Text.Trim();
                    CarNoChange(strCbCarNo);
                }
            }
        }

        private void txtCarNo_KeyDown(object sender, EventArgs e)
        {
            if (PbCache.collect != null && PbCache.collect.weight > 0)
            {
                strCbCarNo = cbCarNo.Text.Trim() + txtCarNo.Text.Trim();
                CarNoChange(strCbCarNo);
            }
        }

        private void CarNoChange(string carNo) 
        {
            if (!string.IsNullOrEmpty(carNo))
            {
                RESTfulResult<List<MeterWorkCalibrationBranch>> rmMwcb = calibrationBranchService.doQueryWf(new MeterWorkCalibrationBranch { baseSpotNo = PbCache.sportInfo.baseSpotNo, carNo = carNo });
                if (rmMwcb.Succeed)
                {
                    if (rmMwcb.Data != null && rmMwcb.Data.Count > 0)
                    {
                        cbCalibrationType.Value = rmMwcb.Data[0].calibrationTypeNo;
                        cbWorkMonitor.Value = rmMwcb.Data[0].baseSpotNo;

                        cbCalibrationType.Enabled = false;
                        cbWorkMonitor.Enabled = false;
                        bFlag = true;
                    }
                    else 
                    {
                        cbCalibrationType.Enabled = true;
                        cbWorkMonitor.Enabled = true;
                    }
                }
            }
        }

        private void cbCalibrationType_SelectionChanged(object sender, EventArgs e)
        {
            //互检
            if (cbCalibrationType.Value != null && cbCalibrationType.Value.ToString() == "001005002" && !bFlag)
            {
                label1.Visible = true;
                cbWorkMonitor.Visible = true;

                RESTfulResult<List<MeterBaseMutualDiff>> rm = mutualDiffService.doQueryWf(new MeterBaseMutualDiff { baseSpotOneNo = PbCache.sportInfo.baseSpotNo });
                if (rm.Data == null || rm.Data.Count == 0) 
                {
                    MessageBox.Show("互检差值表未配置当前计量点对应的互检计量点数据");
                    return;
                }

                ValueList vlist = new ValueList();
                foreach (MeterBaseMutualDiff diff in rm.Data)
                {
                    vlist.ValueListItems.Add(diff.baseSpotOneNo, diff.baseSpotOneName);
                }
                cbWorkMonitor.ValueList = vlist;
            }
            else 
            {
                cbWorkMonitor.SelectedIndex = -1;
                cbWorkMonitor.ValueList = null;

                label1.Visible = false;
                cbWorkMonitor.Visible = false;
            }
        }
    }
}
