using com.hnshituo.core.webapp.vo;
using Common;
using Infragistics.Win;
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 StorageMeterSystem
{
    public partial class frmCalibration : Form
    {
        public frmCalibration()
        {
            InitializeComponent();
        }

        Log lg = Log.GetInstance();

        //校秤主
        MeterWorkCalibrationMainService calibrationMainService = new MeterWorkCalibrationMainService();
        //校秤从
        MeterWorkCalibrationBranchService calibrationBranchService = new MeterWorkCalibrationBranchService();
        MeterBaseMutualDiffService mutualDiffService = new MeterBaseMutualDiffService();//互检差值

        ComBaseInfoService cis = new ComBaseInfoService();
        MeterWorkCalibrationBranch calibrationBranch { get; set; }
        public string userId = "", userName = "";

        private void timer1_Tick(object sender, EventArgs e)
        {
            txtWgt.Value = (decimal)Math.Round(PbStorageCache.collect.mainWgt, 1);
        }

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


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

            DialogResult dr = MessageBox.Show("确认进行校秤吗？", "提示", MessageBoxButtons.OKCancel);
            if (dr == DialogResult.OK)
            {
                btnSave.Enabled = false;

                string baseContrastNo = PbStorageCache.sportInfo.baseSpotNo;
                string baseContrastName = PbStorageCache.sportInfo.baseSpotName;
                string dbWgt = PbStorageCache.collect.mainWgt + "";
                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 ? "001002003" : 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 ? "001002003" : 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
                    }
                );

                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,
                            PbStorageCache.sportInfo.baseSpotNo,
                            "S" + rm.Message,
                            (PbStorageCache.videoChild.Count + 1)), System.Drawing.Imaging.ImageFormat.Jpeg);

                        //循环摄像头，然后根据摄像头有多少，则取硬盘录像机多少个通道数据
                        for (uint i = 1; i <= CarCache.cameraShots.Count; i++)
                        {
                            cameraShotMain.CapPicFromVideo(i,
                                string.Format("{0}\\imgShort\\formalImg\\{1}_{2}_{3}",
                                    AppDomain.CurrentDomain.SetupInformation.ApplicationBase,
                                    PbStorageCache.sportInfo.baseSpotNo,
                                    "S" + rm.Message, i)
                            );
                        }

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

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

                        lbMsgInfo.Visible = true;
                        btnSave.Enabled = true;
                    }
                }
                else
                {
                    MessageBox.Show("校秤异常：" + rm.Message);
                    btnSave.Enabled = true;
                }
            }
        }

        public CameraShotCls cameraShotMain { get; set; }

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

        private void frmCalibration_Load(object sender, EventArgs e)
        {
            timer1.Start();

            ValueList vlistSpot = new ValueList();
            vlistSpot.ValueListItems.Add("001002003", "成品秤");
            cbSpotType.ValueList = vlistSpot;
            cbSpotType.Value = "001002003";
            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;


            RESTfulResult<List<MeterWorkCalibrationBranch>> rmMwcb = calibrationBranchService.doQueryWf(new MeterWorkCalibrationBranch { baseSpotNo = PbStorageCache.sportInfo.baseSpotNo });
            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;
                }
            }
        }

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

                RESTfulResult<List<MeterBaseMutualDiff>> rm = mutualDiffService.doQueryWf(new MeterBaseMutualDiff { baseSpotOneNo = PbStorageCache.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;
            }
        }
    }
}
