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

        MeterWorkPreWgttypeService meterWorkPreWeightTypeService = new MeterWorkPreWgttypeService();

        //是否选对了
        public bool _isSuccess { get; set; }

        public string weightType = "";

        string _matName = "", _chkMatName = "";

        public frmMatMsgInfo(string matName) 
        {
            InitializeComponent();
            _matName = matName;
        }


        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string carNo = "湘G66666";
            string preTrackScaleNo = "";
            string weightTypes = weightType;
            int selectNum = 0;
            if (ultraGrid1.Rows.Count > 0)
            {
                foreach (UltraGridRow ugr in ultraGrid1.Rows)
                {
                    string s = ugr.Cells["select"].Value.ToString();
                    if (s == "True")
                    {
                        preTrackScaleNo = ugr.Cells["predictionNo"].Value.ToString();
                        selectNum++;
                    }
                }
            }
            if (weightTypes == "")
            {
                MessageBox.Show("请选择重量类型");
                return;
            }
            if (selectNum == 1)
            {
                string res = add(weightTypes, preTrackScaleNo, carNo);
                if (res == "")
                {
                    MessageBox.Show("新增成功");
                    this.DialogResult = DialogResult.OK;
                    btnSave.Enabled = false;
                    btnCancel.Enabled = false;
                    ultraGrid1.UpdateData();
                    this.Close();
                }
                else
                {
                    MessageBox.Show(res);
                }
            }
            else
            {
                MessageBox.Show("只能选择一条委托");
            }
        }

        /// <summary>
        /// 定时关闭（15000ms）
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void lbMat1_Click(object sender, EventArgs e)
        {
            //_chkMatName = lbMat1.Text;
            //lbMat1.BackColor = Color.PaleTurquoise;
            //lbMat2.BackColor = Color.LightBlue;
        }

        private void lbMat2_Click(object sender, EventArgs e)
        {
            //_chkMatName = lbMat2.Text;
            //lbMat2.BackColor = Color.PaleTurquoise;
            //lbMat1.BackColor = Color.LightBlue;
        }

        private void btnTare_Click(object sender, EventArgs e)
        {
            weightType = "0";
            btnTare.BackColor = Color.Red;
            btnGross.BackColor = Color.Gray;
        }

        private void btnGross_Click(object sender, EventArgs e)
        {
            weightType = "1";
            btnGross.BackColor = Color.Red;
            btnTare.BackColor = Color.Gray;
        }

        private void frmMatMsgInfo_Load(object sender, EventArgs e)
        {
            string sql = string.Format(@"select t.PREDICTION_NO predictionNo,
                                         t.MATTER_NAME matterName,
                                         t.RECEIVING_UINT_NAME receivingUintName,
                                         t.FORWARDING_UNIT_NAME forwardingUnitName,
                                         t.TRUST_DATE_TIME trustDateTime
                                         from PRE_TRACK_SCALE t
                                         where value_flag = '1' and CAR_NO = '{0}' and LINE_DESC = '0'", "湘G66666");

            DataTable dtV = dtPrediction.Clone();

            PbModelDbService<List<PreTrackScale>> pb = new PbModelDbService<List<PreTrackScale>>();
            RESTfulResult<List<PreTrackScale>> rmType = pb.executeSqlDataWf(sql);
            if (rmType.Succeed && rmType.Data != null && rmType.Data.Count > 0)
            {
                dtV = rmType.Data.ListToDataTable<PreTrackScale>();
            }

            ClsControlPack.CopyDataToDatatable(ref dtV, ref this.dtPrediction, true);
            ClsControlPack.RefreshAndAutoSize(ultraGrid1);
        }

        public string add(string weightType,string preTrackScaleNo,string carNo)
        {
            MeterWorkPreWgttype meterWorkPreWeightType = new MeterWorkPreWgttype();
            if (weightType == "0")
            {
                meterWorkPreWeightType.weightTypeName = "毛重";
                meterWorkPreWeightType.weightTypeNo = "001033001";


                meterWorkPreWeightType.preTrackScaleNo = preTrackScaleNo;
                meterWorkPreWeightType.carNo = carNo;
                meterWorkPreWeightType.createManPhone = "";
                meterWorkPreWeightType.createManName = "admin";
            }
            else if (weightType == "1")
            {
                meterWorkPreWeightType.weightTypeName = "皮重";
                meterWorkPreWeightType.weightTypeNo = "001033002";
                meterWorkPreWeightType.carNo = carNo;
                meterWorkPreWeightType.createManPhone = "";
                meterWorkPreWeightType.createManName = "admin";
            }
            if (meterWorkPreWeightType.weightTypeName == null || meterWorkPreWeightType.weightTypeName == "")
            {
                return "请选择重量类型";
            }
            RESTfulResult<MeterWorkPreWgttype> rm = meterWorkPreWeightTypeService.doAddWeightType(meterWorkPreWeightType);

            if (rm.Succeed)
            {
                return "";
            }
            else
            {
                return rm.ResultMessage;
            }
        }
    }
}
