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

namespace StorageMeterSystem
{
    public partial class frmHotDeliverySingleWgt : Form
    {
        private MeterWorkStorageSingleWgtService singleWgtService = new MeterWorkStorageSingleWgtService(); //单只重量（标准重量）服务
        private ComBaseInfoService comBaseInfoService = new ComBaseInfoService(); //基础数服务
        private MeterBaseScalePointService scalePointService = new MeterBaseScalePointService(); //计量点信息服务

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

        public frmHotDeliverySingleWgt()
        {
            InitializeComponent();
        }

        private void frmHotDeliverySingleWgt_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"));

            userId = ((ST_MainForm)(this.MdiParent)).UserID;
            userName = ((ST_MainForm)(this.MdiParent)).UserName;
            appUser = ((ST_MainForm)(this.MdiParent)).AppUser;

            #region 载入基础信息

            //计量点信息载入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;
            }

            //热送磅规格信息载入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(cmbSpcNameUp, 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(cmbGradeNameUp, rrComGradeName.Data.ListToDataTable<ComBaseInfo>(), "baseCode", "baseName");
            }
            else
            {
                MessageBox.Show("热送磅钢种信息载入失败!");
                return;
            }

            ValueList vlistFlag1 = new ValueList();
            vlistFlag1.ValueListItems.Add("", "全部");
            vlistFlag1.ValueListItems.Add("0", "实际重量");
            vlistFlag1.ValueListItems.Add("1", "理论重量");
            this.ultraGridSingleWgt.DisplayLayout.Bands[0].Columns["weightType"].ValueList = vlistFlag1;
            //ClsControlPack.SetUltraGridRowFilter(ref ultraGridSingleWgt, true);

            ValueList vlistFlag3 = new ValueList();
            vlistFlag3.ValueListItems.Add("", "全部");
            vlistFlag3.ValueListItems.Add("0", "无效");
            vlistFlag3.ValueListItems.Add("1", "有效");
            this.ultraGridSingleWgt.DisplayLayout.Bands[0].Columns["valueFlag"].ValueList = vlistFlag3;
            //ClsControlPack.SetUltraGridRowFilter(ref ultraGridSingleWgt, true);




            #endregion 载入基础信息

            btnQuery();
        }

        #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;
                    }
                default: break;
            }
        }

        /// <summary>
        /// 全选点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbAll_CheckedChanged(object sender, EventArgs e)
        {
            foreach (UltraGridRow ugr in ultraGridSingleWgt.Rows)
            {
                if (!ugr.HiddenResolved)
                {
                    ugr.Cells["UNCK"].Value = cbAll.Checked;
                }
            }
        }

        #endregion 点击事件

        /// <summary>
        /// 查询
        /// </summary>
        private void btnQuery()
        {
            DataTable dtScale = dataTable1.Clone();
            CommonPage<MeterWorkStorageSingleWgt> cp = new CommonPage<MeterWorkStorageSingleWgt>();
            cp.param = new MeterWorkStorageSingleWgt(); //{ valueFlag = "1", baseSpotNo = cmbPoint.Value?.ToString() };
            cp.param.baseSpotNo = cmbPoint.Value + ""; //计量点
            cp.param.specName = cmbSpcNameUp.Value + "";//规格名称
            cp.param.gradeName = cmbGradeNameUp.Value + ""; //牌号名称（钢号名称）
            cp.param.weightType = "0";//重量类型（0=单只实际重量，1=理论重量）
            cp.param.createTime1 = dtStartTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
            cp.param.createTime2 = dtEndTime.Value.ToString("yyyy-MM-dd HH:mm:ss");

            RESTfulResult<List<MeterWorkStorageSingleWgt>> rms = singleWgtService.doQuery(cp.param);
            if (rms.Succeed)
            {
                if (rms.Data != null && rms.Data.Count > 0)
                {
                    dtScale = rms.Data.ListToDataTable<MeterWorkStorageSingleWgt>();

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

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