using com.hnshituo.core.webapp.vo;
using Common;
using Infragistics.Win.UltraWinGrid;
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;

namespace MeterModuleLibrary
{
    public delegate void EventOpenWgt(string e);

    public partial class ucCarMeterInfo : UserControl
    {
        public ucCarMeterInfo()
        {
            InitializeComponent();
        }

        private frmNumberKey fnk = null;
        private bool bFnk = false;

        private frmApplianceCheck fac = null;
        private bool bFac = false;

        private frmScaleQuery fah = null;
        private bool bFah = false;

        private frmPhNo fpn = null;
        private bool bFpn = false;

        public event EventOpenWgt openWgt;

        public event EventOpenWgt openTareWgt;

        /// <summary>
        /// 斗重
        /// </summary>
        public double dWeight = 0;

        public void setFormControlValue(UltraGridRow ugr)
        {
            txtPREDICTION_NO.Text = "";
            String bussinessNo = ugr.Cells["predictionNo"].Text.Trim();
            if (String.IsNullOrEmpty(bussinessNo))
            {
                bussinessNo = ugr.Cells["matterNo"].Text.Trim() + ugr.Cells["forwardingUnitNo"].Text.Trim() + ugr.Cells["receivingUintNo"].Text.Trim();
            }
            if (!String.IsNullOrEmpty(bussinessNo) && bussinessNo.Length == 9)
            {
                txtPREDICTION_NO.Text = bussinessNo;
            }

            txtMATTER_NAME.Text = ugr.Cells["matterName"].Text.Trim();
            txtFORWARDING_UNIT_NAME.Text = ugr.Cells["forwardingUnitName"].Text.Trim();
            txtRECEIVING_UINT_NAME.Text = ugr.Cells["receivingUintName"].Text.Trim();
            lp = new List<PbModelDb>();
            lp.Add(new PbModelDb { id = "matterNo", text = ugr.Cells["matterNo"].Text.Trim() });
            lp.Add(new PbModelDb { id = "matterName", text = ugr.Cells["matterName"].Text.Trim() });
            lp.Add(new PbModelDb { id = "forwardingUnitNo", text = ugr.Cells["forwardingUnitNo"].Text.Trim() });
            lp.Add(new PbModelDb { id = "forwardingUnitName", text = ugr.Cells["forwardingUnitName"].Text.Trim() });
            lp.Add(new PbModelDb { id = "receivingUintNo", text = ugr.Cells["receivingUintNo"].Text.Trim() });
            lp.Add(new PbModelDb { id = "receivingUintName", text = ugr.Cells["receivingUintName"].Text.Trim() });
            lp.Add(new PbModelDb { id = "meterTypeNo", text = ugr.Cells["meterTypeNo"].Text.Trim() });
            lp.Add(new PbModelDb { id = "meterTypeName", text = ugr.Cells["meterTypeName"].Text.Trim() });

            setAddWgtBtnVisable();
        }

        public void setFormControlValue(MeterWorkCarActualFirst first)
        {
            txtMATTER_NAME.Text = first.matterName;
            txtFORWARDING_UNIT_NAME.Text = first.forwardingUnitName;
            txtRECEIVING_UINT_NAME.Text = first.receivingUintName;
            lp = new List<PbModelDb>();
            lp.Add(new PbModelDb { id = "matterNo", text = first.matterNo });
            lp.Add(new PbModelDb { id = "matterName", text = first.matterName });
            lp.Add(new PbModelDb { id = "forwardingUnitNo", text = first.forwardingUnitNo });
            lp.Add(new PbModelDb { id = "forwardingUnitName", text = first.forwardingUnitName });
            lp.Add(new PbModelDb { id = "receivingUintNo", text = first.receivingUintNo });
            lp.Add(new PbModelDb { id = "receivingUintName", text = first.receivingUintName });
            lp.Add(new PbModelDb { id = "meterTypeNo", text = first.meterTypeNo });
            lp.Add(new PbModelDb { id = "meterTypeName", text = first.meterTypeName });

            txtBATCH_NO.Text = first.batchNo;
            if (!String.IsNullOrEmpty(first.predictionNo) && first.predictionNo.Length == 9)
            {
                txtPREDICTION_NO.Text = first.predictionNo; //2021年6月7日 杨秀东添加，用于临时委托，存到数据库中去
            }

            setAddWgtBtnVisable();

            //如果第一次计量是毛重
            if (first.weightType == "0" && lbWgt.Visible)
            {
                if (first.addWeight != null && first.addWeight.Value > 0)
                {
                    lbWgt.Text = first.addWeight.Value + "";

                    if (!string.IsNullOrEmpty(first.addWgtNo))
                    {
                        foreach (string str in first.addWgtNo.Split(','))
                        {
                            if (lb1.Items.Count < 7)
                            {
                                lb1.Items.Add(str);
                            }
                            else
                            {
                                lb2.Items.Add(str);
                            }
                        }
                    }
                }
                else
                    lbWgt.Text = "";

                lbWgt.Enabled = false;
                btnCheck.Visible = false;
            }
        }

        public string getAddWgtPk = "";

        private List<string> ls = new List<string>();

        private void btnCheck_Click(object sender, EventArgs e)
        {
            fac = new frmApplianceCheck();
            fac.ltAddWeightNo = ls == null ? null : ls;

            bFac = true;
            if (fac.ShowDialog() == DialogResult.OK)
            {
                bFac = false;
                lbUTENSIL_UTENSIL_NAME.Text = "";
                lbWgt.Text = "0";
                lb1.Items.Clear();
                lb2.Items.Clear();
                getAddWgtPk = "";

                DataTable dt = fac.dtChecked;
                ls = fac.ltAddWeightNo;

                if (dt != null && dt.Rows.Count > 0)
                {
                    double dWgt = 0;
                    foreach (DataRow dr in dt.Rows)
                    {
                        if (lbUTENSIL_UTENSIL_NAME.Text == "")
                        {
                            lbUTENSIL_UTENSIL_NAME.Text = dr["utensilUtensilName"].ToString();
                        }
                        if (lb1.Items.Count < 7)
                        {
                            lb1.Items.Add(dr["utensilNo"].ToString());
                        }
                        else
                        {
                            lb2.Items.Add(dr["utensilNo"].ToString());
                        }

                        dWgt += Convert.ToDouble(dr["utensilWeight"].ToString());

                        getAddWgtPk += dr["addWeightNo"].ToString() + ",";
                    }
                    lbWgt.Text = dWgt + "";
                    openWgt(dWgt + "");
                }
            }
        }

        public string getJz()
        {
            string sJz = "";
            foreach (string str in lb1.Items)
            {
                sJz += str + ",";
            }
            foreach (string str in lb2.Items)
            {
                sJz += str + ",";
            }

            return (sJz != "" ? sJz.Substring(0, sJz.Length - 1) : sJz);
        }

        public double getLbWgtValue
        {
            get
            {
                try
                {
                    return string.IsNullOrEmpty(lbWgt.Text.Trim()) ? 0 : Convert.ToDouble(lbWgt.Text.Trim());
                }
                catch
                {
                    return 0;
                }
            }
        }

        public List<PbModelDb> lp = null;

        /// <summary>
        /// 【业务编号】按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnNo_Click(object sender, EventArgs e)
        {
            fah = new frmScaleQuery();
            bFah = true;
            if (fah.ShowDialog() == DialogResult.OK)
            {
                bFah = false;
                lp = new List<PbModelDb>();

                txtPREDICTION_NO.Text = fah.strValue;
                if (fah.ht != null && fah.ht.Count == 3)
                {
                    txtMATTER_NAME.Text = fah.ht[2].text;
                    txtFORWARDING_UNIT_NAME.Text = fah.ht[0].text;
                    txtRECEIVING_UINT_NAME.Text = fah.ht[1].text;

                    lp.Add(new PbModelDb { id = "matterNo", text = fah.ht[2].id });
                    lp.Add(new PbModelDb { id = "matterName", text = fah.ht[2].text });
                    lp.Add(new PbModelDb { id = "forwardingUnitNo", text = fah.ht[0].id });
                    lp.Add(new PbModelDb { id = "forwardingUnitName", text = fah.ht[0].text });
                    lp.Add(new PbModelDb { id = "receivingUintNo", text = fah.ht[1].id });
                    lp.Add(new PbModelDb { id = "receivingUintName", text = fah.ht[1].text });
                }
                else
                {
                    txtMATTER_NAME.Text = "";
                    txtFORWARDING_UNIT_NAME.Text = "";
                    txtRECEIVING_UINT_NAME.Text = "";

                    lp.Add(new PbModelDb { id = "matterNo", text = "" });
                    lp.Add(new PbModelDb { id = "matterName", text = "" });
                    lp.Add(new PbModelDb { id = "forwardingUnitNo", text = "" });
                    lp.Add(new PbModelDb { id = "forwardingUnitName", text = "" });
                    lp.Add(new PbModelDb { id = "receivingUintNo", text = "" });
                    lp.Add(new PbModelDb { id = "receivingUintName", text = "" });
                }

                setAddWgtBtnVisable();

                #region 如果有可用期限皮，且没有一次计量数据，且当前重量大于期限皮重1000kg 则将期限皮重量填充皮重

                MeterWorkCarActualFirstService workCarActualFirstService = new MeterWorkCarActualFirstService();
                RESTfulResult<List<MeterWorkCarActualFirst>> RESTfulResult = workCarActualFirstService.doQueryWf(new MeterWorkCarActualFirst { carNo = PbCache.collect.carno, valueFlag = "1" });
                if (RESTfulResult.Succeed)
                {
                    if (RESTfulResult.Data == null || RESTfulResult.Data.Count == 0)
                    {
                        openTareWgt("");
                        if (PbCache.isTermTare)
                        {
                            MeterBaseTermTareDataService termTareDate = new MeterBaseTermTareDataService();
                            RESTfulResult<List<MeterBaseTermTareData>> rmD = termTareDate.doQueryWf(new MeterBaseTermTareData { carNo = PbCache.collect.carno });
                            if (rmD.Succeed)
                            {
                                if (rmD.Data != null && rmD.Data.Count > 0 && (PbCache.collect.weight - rmD.Data[0].meterWeight.Value >= 1000))
                                {
                                    openTareWgt(rmD.Data[0].meterWeight.Value + "");
                                }
                            }
                        }
                    }
                }

                #endregion 如果有可用期限皮，且没有一次计量数据，且当前重量大于期限皮重1000kg 则将期限皮重量填充皮重
            }
        }

        public string meterTypeName = "";

        public void setMeterType(string meterType)
        {
            txtMETER_TYPE.Text = meterType;
            meterTypeName = meterType;
        }

        #region 设置按钮是否可点击

        private bool btnCheckVisible = false;

        public bool BtnCheckVisible
        {
            get { return btnCheckVisible; }
            set
            {
                btnCheckVisible = value;
                SetText(value);
            }
        }

        private void SetText(bool bflag)
        {
            if (btnCheck.InvokeRequired)
            {
                Action<bool> action = new Action<bool>(SetText);
                Invoke(action, new object[] { bflag });
            }
            else
            {
                btnCheck.Visible = bflag;
            }
        }

        private bool btnNoVisible = false;

        public bool BtnNoVisible
        {
            get { return btnNoVisible; }
            set
            {
                btnNoVisible = value;
                SetNoText(value);
            }
        }

        private void SetNoText(bool bflag)
        {
            if (btnCheck.InvokeRequired)
            {
                Action<bool> action = new Action<bool>(SetNoText);
                Invoke(action, new object[] { bflag });
            }
            else
            {
                btnNo.Visible = bflag;
            }
        }

        #endregion 设置按钮是否可点击

        private void lbWgt_Enter(object sender, EventArgs e)
        {
            bFnk = true;
            fnk = new frmNumberKey();
            fnk.bControl = true;
            fnk.sValue = lbWgt.Text.Trim();
            if (fnk.ShowDialog() == DialogResult.OK)
            {
                bFnk = false;
                try
                {
                    lbWgt.Text = fnk.sValue;
                }
                catch { }
            }
        }

        private void setAddWgtBtnVisable()
        {
            btnCheck.Visible = false;
            lbWgt.Visible = false;
            lbWgt.Text = "";
            PbCache.addWgt = 0;

            PbCache.isTermTare = false;
            if (lp != null && lp.Count > 0)
            {
                string sql = @"select matter_no id,
                                   allowed_tare_weight text,
                                   allowed_add_weight text2
                              from meter_base_matter_info
                             where valid_Flag = '1' and matter_no = '" + lp[0].text + "'";

                PbModelDbService<List<PbModelDb>> pb = new PbModelDbService<List<PbModelDb>>();
                RESTfulResult<List<PbModelDb>> rm = pb.executeSqlDataWf(sql);
                if (rm.Succeed)
                {
                    if (rm.Data != null && rm.Data.Count > 0)
                    {
                        if (rm.Data[0].text == "1")
                        {
                            PbCache.isTermTare = true;
                        }
                        if (rm.Data[0].text2 == "1")
                        {
                            btnCheck.Visible = true;
                            lbWgt.Visible = true;
                        }
                    }
                }
            }
        }

        private void lbWgt_TextChanged(object sender, EventArgs e)
        {
            openWgt(lbWgt.Text.Trim());
        }

        private void btnPC_Click(object sender, EventArgs e)
        {
            bFpn = true;
            fpn = new frmPhNo();
            if (fpn.ShowDialog() == DialogResult.OK)
            {
                bFpn = false;
                txtBATCH_NO.Text = string.IsNullOrEmpty(fpn.strValue) ? "" : fpn.strValue;
            }
        }

        /// <summary>
        /// 批次编号
        /// </summary>
        /// <returns></returns>
        public string batchNo()
        {
            return txtBATCH_NO.Text.Trim();
        }

        /// <summary>
        /// 业务号
        /// </summary>
        /// <returns></returns>
        public string predictionNo()
        {
            return txtPREDICTION_NO.Text.Trim();
        }

        public void ClosingFrm()
        {
            try
            {
                if (bFnk) fnk.Close();
            }
            catch { }

            try
            {
                if (bFac) fac.Close();
            }
            catch { }

            try
            {
                if (bFah) fah.Close();
            }
            catch { }

            try
            {
                if (bFpn) fpn.Close();
            }
            catch { }
        }
    }
}