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

namespace StorageMeterSystem
{
    public partial class frmHisQuery : Form
    {
        MeterWorkStorageActualService actualService = new MeterWorkStorageActualService();
        public frmHisQuery()
        {
            InitializeComponent();
        }

        private void ultraToolbarsManager1_ToolClick(object sender, Infragistics.Win.UltraWinToolbars.ToolClickEventArgs e)
        {
            switch (e.Tool.Key)
            {
                case "btnQuery":
                    {
                        btnQuery();
                        break;
                    }
                case "btnCancel": 
                    {
                        btnUpdate("0");
                    } break;
                case "btnUnCancel": 
                    {
                        btnUpdate("1");
                    } break;
                    /*
                case "btnPrintOne": 
                    { 
                        
                    } break;
                    //*/
                case "btnPrintSome": 
                    {
                        Print();
                    }
                    break;
                default: break;
            }
        }

        #region 打印新
        private void Print() 
        {
            string bathNo = "";
            DataTable dtPrint = new DataTable();
            dtPrint.Columns.Add("序号");
            dtPrint.Columns.Add("桶号");
            dtPrint.Columns.Add("净重(Kg)");
            dtPrint.Columns.Add("毛重(Kg)");
            dtPrint.Columns.Add("计量员");
            dtPrint.Columns.Add("计量日期");


            UltraGridRow ugrFirst = null;
            int i = 1;
            ultraGridSj.UpdateData();
            double dbGross = 0, dbNet = 0;
            foreach (UltraGridRow ugr in ultraGridSj.Rows) 
            {
                if (ugr.Cells["UNCK"].Value.ToString().Trim().ToUpper() == "TRUE" && !ugr.HiddenResolved)
                {
                    if (dtPrint.Rows.Count == 0) 
                    {
                        bathNo = ugr.Cells["batchNo"].Text.Trim();
                        ugrFirst = ugr;
                    }

                    if (bathNo != ugr.Cells["batchNo"].Text.Trim()) 
                    {
                        MessageBox.Show("请勾选同一个批次的数据进行打印");
                        return;
                    }
                    
                    DataRow drNew = dtPrint.NewRow();
                    drNew["序号"] = i++;
                    drNew["桶号"] = ugr.Cells["bucketNo"].Text.Trim();
                    drNew["净重(Kg)"] = ugr.Cells["netWeight"].Text.Trim();
                    drNew["毛重(Kg)"] = ugr.Cells["grossWeight"].Text.Trim();
                    drNew["计量员"] = ugr.Cells["createManName"].Text.Trim();
                    drNew["计量日期"] = ugr.Cells["productDate"].Text.Trim();
                    dtPrint.Rows.Add(drNew);
                    dbGross += Convert.ToDouble(ugr.Cells["grossWeight"].Text.Trim());
                    dbNet += Convert.ToDouble(ugr.Cells["netWeight"].Text.Trim());
                }
            }

            if (dtPrint.Rows.Count > 0)
            {
                DataRow drNew = dtPrint.NewRow();
                drNew["序号"] = "合计";
                drNew["桶号"] = "";
                drNew["净重(Kg)"] = Math.Round(dbNet, 1);
                drNew["毛重(Kg)"] = Math.Round(dbGross, 1);
                drNew["计量员"] = "";
                drNew["计量日期"] = "";
                dtPrint.Rows.Add(drNew);

                //预览效果
                new PrintHelper().PrintPriview(dtPrint, ugrFirst.Cells["productName"].Text.Trim() + "计量单", ugrFirst);

                //打印效果
                //new PrintHelper().Print(dtPrint, ugrFirst.Cells["productName"].Text.Trim() + "计量单", ugrFirst);
            }
            else 
            {
                MessageBox.Show("请勾选同批次需打印的数据");
            }
        }
        #endregion

        private void btnUpdate(string vFlag) 
        {
            List<MeterWorkStorageActual> lt = new List<MeterWorkStorageActual>();
            ultraGridSj.UpdateData();
            foreach (UltraGridRow ugr in ultraGridSj.Rows) 
            {
                if (ugr.Cells["UNCK"].Value.ToString().Trim().ToUpper() == "TRUE") 
                {
                    lt.Add(new MeterWorkStorageActual { valueFlag = vFlag, actualNo = ugr.Cells["actualNo"].Text.Trim() });
                }
            }
            RESTfulResult<int> rms = actualService.doUpdateWf(lt);
            if (rms.Succeed)
            {
                btnQuery();
            }
            else 
            {
                MessageBox.Show("操作失败：" + rms.Message);
            }
        }


        private void btnQuery()
        {
            DataTable dtSj = dataTable1.Clone();

            CommonPage<MeterWorkStorageActual> cp = new CommonPage<MeterWorkStorageActual>();
            cp.startTime = dtStartTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
            cp.endTime = dtEndTime.Value.ToString("yyyy-MM-dd HH:mm:ss");

            if (DateTime.Compare(dtStartTime.Value, dtEndTime.Value) > 0)
            {
                MessageBox.Show("开始时间不能大于结束时间");
                return;
            }
            if ((dtEndTime.Value - dtStartTime.Value).Days> 7)
            {
                MessageBox.Show("时间跨度不能大于7天");
                return;
            }
            if (string.IsNullOrEmpty(cmbPoint.Value?.ToString())) 
            {
                MessageBox.Show("请选择计量点");
                return;
            }

            cp.param = new MeterWorkStorageActual { valueFlag = "1", baseSpotNo = cmbPoint.Value?.ToString(), productNo = cmbMatterNo.Value?.ToString(), batchNo = txtBatch.Text.Trim() };

            RESTfulResult<List<MeterWorkStorageActual>> rms = actualService.doQueryTimeWf(cp);
            if (rms.Succeed)
            {
                if (rms.Data != null && rms.Data.Count > 0)
                {
                    dtSj = rms.Data.ListToDataTable<MeterWorkStorageActual>();

                    ClsControlPack.CopyDataToDatatable(ref dtSj, ref dataTable1, true);
                    ClsControlPack.RefreshAndAutoSize(ultraGridSj);

                    foreach (UltraGridRow ugr in ultraGridSj.Rows) 
                    {
                        if (ugr.Cells["valueFlag"].Value.ToString() == "0")
                        {
                            ugr.Appearance.BackColor = Color.Red;
                        }
                        else 
                        {
                            ugr.Appearance.BackColor = Color.White;
                        }
                    }
                }
                else 
                {
                    dtSj.Clear();
                    ClsControlPack.CopyDataToDatatable(ref dtSj, ref dataTable1, true);
                    ClsControlPack.RefreshAndAutoSize(ultraGridSj);
                }
            }
        }

        private void frmHisQuery_Load(object sender, EventArgs e)
        {
            dtStartTime.Value = Convert.ToDateTime((DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00"));
            dtEndTime.Value = Convert.ToDateTime((DateTime.Now.ToString("yyyy-MM-dd") + " 23:59:59"));

            #region 
            string sql = @"SELECT base_code baseCode,base_name baseName,memo FROM Com_Base_Info where p_base_code='001025' and valid_Flag='1'";
            PbModelDbService<List<ComBaseInfo>> pb = new PbModelDbService<List<ComBaseInfo>>();
            RESTfulResult<List<ComBaseInfo>> rmType = pb.executeSqlDataWf(sql);
            if (rmType.Succeed && rmType.Data != null && rmType.Data.Count > 0)
            {
                ClsControlPack.SetUltraComboDataSource(cmbMatterNo, rmType.Data.ListToDataTable<ComBaseInfo>(), "baseCode", "baseName");
            }
            else
            {
                MessageBox.Show("产品信息载入失败");
                return;
            }

            MeterWorkStockOnlineService staticMonitorService = new MeterWorkStockOnlineService();
            RESTfulResult<List<MeterWorkStockOnline>> rr = staticMonitorService.doQueryWf(new MeterWorkStockOnline { validFlag = "1" });
            if (rr.Succeed && rr.Data != null && rr.Data.Count > 0)
            {
                ClsControlPack.SetUltraComboDataSource(cmbPoint, rr.Data.ListToDataTable<MeterWorkStockOnline>(), "baseSpotNo", "baseSpotName");
            }
            else
            {
                MessageBox.Show("计量点载入失败");
                return;
            }

            ValueList vlistFlag3 = new ValueList();
            vlistFlag3.ValueListItems.Add("0", "作废");
            vlistFlag3.ValueListItems.Add("1", "有效");
            this.ultraGridSj.DisplayLayout.Bands[0].Columns["valueFlag"].ValueList = vlistFlag3;

            //ClsControlPack.SetUltraGridRowFilter(ref ultraGridSj, true);
            #endregion
        }

        private void cbAll_CheckedChanged(object sender, EventArgs e)
        {
            foreach (UltraGridRow ugr in ultraGridSj.Rows)
            {
                if (!ugr.HiddenResolved)
                {
                    ugr.Cells["UNCK"].Value = cbAll.Checked;
                }
            }
        }
    }
}
