using Common;
using Infragistics.Win.UltraWinGrid;
using System;
using System.Collections;
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;
using com.hnshituo.core.webapp.vo;

namespace MeterModuleLibrary
{
    public partial class frmApplianceCheck : Form
    {
        public frmApplianceCheck()
        {
            InitializeComponent();
        }

        private void btnClean_Click(object sender, EventArgs e)
        {
            dataTable2.Clear();
            dtChecked = dataTable2;
            ltAddWeightNo = new List<string>();
            setTable1();
        }

        public DataTable dtChecked = null;

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            //回传数据给调动的界面
            dtChecked = dataTable2;
            this.Close(); 
        }

        private void txtUtensil_Enter(object sender, EventArgs e)
        {
            frmNumberKey fnk = new frmNumberKey();
            if (fnk.ShowDialog() == DialogResult.OK)
            {
                txtUtensil.Text = fnk.sValue;
                label3.Focus();
                setTable1();
            }
            else
            {
                label3.Focus();
            }
        }

        private void frmApplianceCheck_Load(object sender, EventArgs e)
        {
            label3.Focus();
            setTable1();
        }

        public List<string> ltAddWeightNo = null;

        private void setTable1()
        {
            MeterBaseAddWeightService meterBaseAddWeight = new MeterBaseAddWeightService();
            CommonPage<MeterBaseAddWeight> cp = new CommonPage<MeterBaseAddWeight>();
            MeterBaseAddWeight mbw = new MeterBaseAddWeight();
            mbw.utensilNo = txtUtensil.Text.Trim();
            mbw.utensilTypeNo = "001009003";
            cp.param = mbw;

            RESTfulResult<DataTable> rm = meterBaseAddWeight.doQueryWf(cp);
            if (rm.Succeed)
            {
                dataTable1.Clear();
                DataTable dt = dataTable1.Clone();

                #region 点击打开数据
                if (ltAddWeightNo != null) 
                {
                    if (rm.Data != null && rm.Data.Rows.Count > 0)
                    {
                        foreach (string str in ltAddWeightNo)
                        {
                            DataRow[] dr = rm.Data.Select("addWeightNo='" + str + "'");
                            foreach (DataRow drI in dr) 
                            {
                                dataTable2.Rows.Add(drI.ItemArray);
                            }
                        }
                    }
                }
                #endregion


                if (dataTable2.Rows.Count == 0)
                {
                    dt = rm.Data;
                }   
                else
                {
                    foreach(DataRow dr in rm.Data.Rows)
                    {
                        bool bflag = true;
                        foreach(DataRow drc in dataTable2.Rows)
                        {
                            if (dr["addWeightNo"].ToString() == drc["addWeightNo"].ToString())
                            {
                                bflag = false;
                                break;
                            }
                        }
                        if (bflag)
                        {
                            dt.Rows.Add(dr.ItemArray);
                        }
                    }
                }
                ClsControlPack.CopyDataToDatatable(ref dt, ref this.dataTable1, true);
            }
            else
            {
                MessageBox.Show("数据获取异常：" + rm.ResultMessage);
            }
        }

        private void btnLeft_Click(object sender, EventArgs e)
        {
            try
            {
                ArrayList selectedRows = ClsControlPack.GetSelectedRows(ultraGrid1, "addWeightNo", false);
                if (selectedRows.Count == 0)
                {
                    MessageBox.Show("未选中任何一条数据！");
                    return;
                }

                foreach (string ugr in selectedRows)
                {
                    foreach (DataRow dr in dataTable2.Rows)
                    {
                        if (ugr == dr["addWeightNo"].ToString())
                        {
                            if (ltAddWeightNo != null)
                            {
                                ltAddWeightNo.Remove(dr["addWeightNo"].ToString());
                            }
                            dataTable1.Rows.Add(dr.ItemArray);
                            dataTable2.Rows.Remove(dr);

                            break;
                        }
                    }
                }
                ClsControlPack.RefreshAndAutoSize(ultraGrid1);
                ClsControlPack.RefreshAndAutoSize(maintableult);

                setWgtNum();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnRight_Click(object sender, EventArgs e)
        {
            try
            {
                ArrayList selectedRows = ClsControlPack.GetSelectedRows(maintableult, "addWeightNo", false);

                if (selectedRows.Count == 0)
                {
                    MessageBox.Show("未选中任何一条数据！");
                    return;
                }

                foreach (string ugr in selectedRows)
                {
                    foreach (DataRow dr in dataTable1.Rows)
                    {
                        if (ugr == dr["addWeightNo"].ToString())
                        {
                            dataTable2.Rows.Add(dr.ItemArray);
                            if (ltAddWeightNo == null) 
                            {
                                ltAddWeightNo = new List<string>();
                            }
                            ltAddWeightNo.Add(dr["addWeightNo"].ToString());

                            dataTable1.Rows.Remove(dr);
                            break;
                        }
                    }
                }
                
                ClsControlPack.RefreshAndAutoSize(ultraGrid1);
                ClsControlPack.RefreshAndAutoSize(maintableult);

                setWgtNum();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void setWgtNum() 
        {
            txtWgtNum.Text = "";
            double db = 0;
            foreach (DataRow dr in dataTable2.Rows)
            {
                db += Convert.ToDouble(dr["utensilweight"].ToString());
            }
            txtWgtNum.Text = db + "";
        }

        private void btnSomeQ_Click(object sender, EventArgs e)
        {
            frmQjNo fnk = new frmQjNo();
            if (fnk.ShowDialog() == DialogResult.OK)
            {
                label3.Focus();
                if (!string.IsNullOrEmpty(fnk.strValue))
                {
                    string sWhere = "";
                    foreach (string st in fnk.strValue.Split(','))
                    {
                        if (st.Trim().Length > 0)
                        {
                            sWhere += "'" + st + "',";
                        }
                    }
                    if (sWhere.Length > 0)
                    {
                        setTable2(sWhere.Substring(0, sWhere.Length - 1));
                    }
                }
            }
            else 
            {
                label3.Focus();
            }
        }

        private void setTable2(string sWhere)
        {

            string sql = @"select add_weight_no addWeightNo,
                                  utensil_no utensilNo,
                                  utensil_name utensilName,
                                  utensil_weight utensilWeight,
                                  utensil_type_no utensilTypeNo,
                                  utensil_utensil_name utensilUtensilName,
                                  value_flag valueFlag,
                                  memo,
                                  create_man_no createMaN_no,
                                  create_man_name createManName,
                                  create_time createTime,
                                  update_man_no updateManNo,
                                  update_man_name updateManName,
                                  update_time updateTime,
                                  delete_man_no deleteManNo,
                                  delete_man_name deleteManName,
                                  delete_time deleteTime,
                                  utensil_name_no utensilNameNo
                              from Meter_Base_Add_Weight where utensil_type_no = '001009003' and utensil_no in (" + sWhere + ")";
            PbModelDbService<List<MeterBaseAddWeight>> pbPrintService = new PbModelDbService<List<MeterBaseAddWeight>>();
            RESTfulResult<List<MeterBaseAddWeight>> rm = pbPrintService.executeSqlDataWf(sql);
            DataTable dt = dataTable2.Clone();
            if (rm.Succeed)
            {
                if (rm.Succeed && rm.Data != null && rm.Data.Count > 0)
                {
                    dt = rm.Data.ListToDataTable();

                    foreach (DataRow dr in dt.Rows)
                    {
                        DataRow[] drs = dataTable2.Select("utensilNo='" + dr["utensilNo"].ToString().Trim() + "'");
                        if (drs == null || drs.Length == 0) 
                        {
                            dataTable2.Rows.Add(dr.ItemArray);
                            if (ltAddWeightNo == null)
                            {
                                ltAddWeightNo = new List<string>();
                            }
                            
                            int icount = ltAddWeightNo.Where(s => s.Trim() == dr["utensilNo"].ToString().Trim()).Count();
                            if (icount == 0)
                            {
                                ltAddWeightNo.Add(dr["addWeightNo"].ToString());
                            }
                        }
                        
                    }
                }
            }
        }
    }
}
