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

namespace StorageMeterSystem
{
    public partial class frmHotDeliveryActualQueryGroup : Form
    {
        private MeterWorkStorageActualService actualService = new MeterWorkStorageActualService(); //预报服务
        private ComBaseInfoService comBaseInfoService = new ComBaseInfoService(); //基础数服务
        private MeterBaseScalePointService scalePointService = new MeterBaseScalePointService(); //计量点信息服务
        private MeterWorkStorageActual storageActualG = new MeterWorkStorageActual(); //预报实体类

        private Log l = Log.GetInstance(); //日志
        private string userId = "", userName = ""; //用户ID用户名
        private CoreAppUser appUser = null;//登陆信息

        public frmHotDeliveryActualQueryGroup()
        {
            InitializeComponent();
        }

        private void frmHotDeliveryActualQueryGroup_Load(object sender, EventArgs e)
        {
            userId = ((ST_MainForm)(this.MdiParent)).UserID;
            userName = ((ST_MainForm)(this.MdiParent)).UserName;
            appUser = ((ST_MainForm)(this.MdiParent)).AppUser;

            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"));

            this.ultraGridScale.DisplayLayout.ViewStyleBand = ViewStyleBand.OutlookGroupBy;
            this.ultraGridScale.DisplayLayout.GroupByBox.Hidden = true;
            #region 载入基础信息
            //热送磅规格信息载入cmbSpcNameUp
            RESTfulResult<List<ComBaseInfo>> rrComSpcName = comBaseInfoService.doQueryBaseList(new ComBaseInfo { validFlag = "1", pBaseCode = "001018" });
            if (rrComSpcName.Succeed && rrComSpcName.Data != null && rrComSpcName.Data.Count > 0)
            {
                rrComSpcName.Data.Sort((a, b) => a.baseName.CompareTo(b.baseName));
                ClsControlPack.SetUltraComboDataSource(cmbSpcName, rrComSpcName.Data.ListToDataTable<ComBaseInfo>(), "baseCode", "baseName");
            }
            else
            {
                MessageBox.Show("热送磅规格信息载入失败!");
                return;
            }

            //热送磅钢种信息载入cmbGradeNameUp
            RESTfulResult<List<ComBaseInfo>> rrComGradeName = comBaseInfoService.doQueryBaseList(new ComBaseInfo { validFlag = "1", pBaseCode = "001017" });
            if (rrComGradeName.Succeed && rrComGradeName.Data != null && rrComGradeName.Data.Count > 0)
            {
                rrComGradeName.Data.Sort((a, b) => a.baseName.CompareTo(b.baseName));
                ClsControlPack.SetUltraComboDataSource(cmbGradeName, rrComGradeName.Data.ListToDataTable<ComBaseInfo>(), "baseCode", "baseName");
            }
            else
            {
                MessageBox.Show("热送磅钢种信息载入失败!");
                return;
            }


            //计量点信息载入cmbPoint;cmbPointUp
            RESTfulResult<List<MeterBaseScalePoint>> rr = scalePointService.doQueryWf(new MeterBaseScalePoint { validFlag = "1", spotTypeNo = "001002004" });
            if (rr.Succeed && rr.Data != null && rr.Data.Count > 0)
            {
                ClsControlPack.SetUltraComboDataSource(cmbPoint, rr.Data.ListToDataTable<MeterBaseScalePoint>(), "baseSpotNo", "baseSpotName");
            }
            else
            {
                MessageBox.Show("计量点载入失败!");
                return;
            }

            ValueList vlistFlag2 = new ValueList();
            vlistFlag2.ValueListItems.Add("", "全部");
            vlistFlag2.ValueListItems.Add("0", "未审核");
            vlistFlag2.ValueListItems.Add("1", "已审核");
            cmbCheckState.ValueList = vlistFlag2;
            cmbCheckState.SelectedIndex = 0;

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

            ValueList vlistFlag4 = new ValueList();
            vlistFlag4.ValueListItems.Add("0", "未审核");
            vlistFlag4.ValueListItems.Add("1", "已审核");
            this.ultraGridScale.DisplayLayout.Bands[0].Columns["checkFlag"].ValueList = vlistFlag4;
            //ClsControlPack.SetUltraGridRowFilter(ref ultraGridScale, true);

            #endregion 载入基础信息
        }

        #region 点击事件

        /// <summary>
        /// 点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ultraToolbarsManager1_ToolClick(object sender, Infragistics.Win.UltraWinToolbars.ToolClickEventArgs e)
        {
            switch (e.Tool.Key)
            {
                case "btnQuery": //btnInsert
                    {
                        btnQuery();
                        break;
                    }
                case "btnExcelOut": //导出
                    {
                        string str = Interaction.InputBox("请输入要导出的文件名", "提示", "", 100, 100);
                        string pqath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                        this.ultraGridExcelExporter1.Export(this.ultraGridScale, pqath + "\\" + str + ".xls");
                        MessageBox.Show("导出成功，请查看桌面！是否有[" + str + ".xls]表格");
                    }
                    break;
                default: break;
            }
        }

        /// <summary>
        /// 是否开启过滤
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ckFilter_CheckedChanged(object sender, EventArgs e)
        {
            if (ckFilter.Checked)
            {
                this.ultraGridScale.DisplayLayout.Override.AllowRowFiltering = DefaultableBoolean.True;
            }
            else
            {
                this.ultraGridScale.DisplayLayout.Override.AllowRowFiltering = DefaultableBoolean.False;
            }

        }
        #endregion 点击事件

        /// <summary>
        /// 查询数据
        /// </summary>

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

            if (DateTime.Compare(dtStartTime.Value, dtEndTime.Value) > 0)
            {
                MessageBox.Show("开始时间不能大于结束时间");
                return;
            }
            if ((dtEndTime.Value - dtStartTime.Value).Days > 30)
            {
                MessageBox.Show("时间跨度不能大于30天");
                return;
            }

            CommonPage<MeterWorkStorageActual> cp = new CommonPage<MeterWorkStorageActual>();

            cp.param = new MeterWorkStorageActual(); //{ valueFlag = "1", baseSpotNo = cmbPoint.Value?.ToString() };
            cp.param.valueFlag ="1";

            cp.param.checkFlag = cmbCheckState.Value + "";
            cp.param.baseSpotNo = cmbPoint.Value + "";

            cp.param.specNo = cmbSpcName.Value + "";
            cp.param.gradeNo = cmbGradeName.Value + "";
            cp.param.heatNo = tbHeatNo.Text + "";

            cp.param.startTime = dtStartTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
            cp.param.endTime = dtEndTime.Value.ToString("yyyy-MM-dd HH:mm:ss");

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

                    ClsControlPack.CopyDataToDatatable(ref dtScale, ref dataTable1, true);
                    //ClsControlPack.RefreshAndAutoSize(ultraGridScale);

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