using com.hnshituo.core.webapp.vo;
using Common;
using Common.vo.pb;
using Infragistics.Win;
using Infragistics.Win.UltraWinEditors;
using Infragistics.Win.UltraWinGrid;
using JC_MeasuringSystem;
using MeterModuleLibrary;
using MeterPlugInLibrary;
using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Threading;
using System.Windows.Forms;

namespace BeltScaleSystem

{
    public partial class frmBeltScaleActualStatistics : Form
    {
        private MeterBaseMatterInfoService meterBaseMatterInfo = new MeterBaseMatterInfoService();//物料服务
        private MeterWorkBeltActualService BeltActualService = new MeterWorkBeltActualService();

        private DataTable dtMaterial;//物料数据源
        private DataTable dtPoint;//计量点数据源
        public frmBeltScaleActualStatistics()
        {
            InitializeComponent();
        }
        //菜单栏事件
        private void ultraToolbarsManager1_ToolClick(object sender, Infragistics.Win.UltraWinToolbars.ToolClickEventArgs e)
        {
            switch (e.Tool.Key)
            {
                case "查询":
                    QueryScaleActualStatistics();
                    break;
               
                case "导出":
                    ExporterScaleActual();
                    break;
            }
        }

        //实绩查询 统计
        private void QueryScaleActualStatistics() {
            try
            {
                CommonPage<MeterWorkBeltActual> cp = new CommonPage<MeterWorkBeltActual>();
                MeterWorkBeltActual actual = new MeterWorkBeltActual();
                DataTable dtScaleTable = dataTable2.Clone();
                if (checkDate.Checked)
                {
                    if (DateTime.Compare(dtStartTime.Value, dtEndTime.Value) > 0)
                    {
                        MessageBox.Show("开始时间不能大于结束时间");
                        return;
                    }
                    
                    cp.startTime = dtStartTime.Value.ToString();
                    cp.endTime = dtEndTime.Value.ToString();
                }
                if (cmMatName.Text.ToString() != "全部")
                {
                    actual.newMatName = cmMatName.Text.ToString();
                }

                if (ckforwardingUnit.Checked)
                {
                    actual.newSupplier = "1";
                    this.beltActualGrid.DisplayLayout.Bands[0].Columns["newSupplier"].Hidden = false;
                }
                else {
                    this.beltActualGrid.DisplayLayout.Bands[0].Columns["newSupplier"].Hidden = true;
                }
                if (ckreceivingUint.Checked)
                {
                    actual.receivingUintName = "1";
                    this.beltActualGrid.DisplayLayout.Bands[0].Columns["receivingUintName"].Hidden = false;
                }
                else {
                    this.beltActualGrid.DisplayLayout.Bands[0].Columns["receivingUintName"].Hidden = true;
                }
                if (ckshipNo.Checked) {
                    actual.shipNo = "1";
                    this.beltActualGrid.DisplayLayout.Bands[0].Columns["shipNo"].Hidden = false;
                }
                else
                {
                    this.beltActualGrid.DisplayLayout.Bands[0].Columns["shipNo"].Hidden = true;
                }
                if (ckbaseSpotName.Checked) {
                    actual.baseSpotName = "1";
                    if (cmbPntName.Text.ToString() != "") {
                        actual.baseSpotName = cmbPntName.Text.ToString();
                    }
                    this.beltActualGrid.DisplayLayout.Bands[0].Columns["baseSpotName"].Hidden = false;
                }else{
                    this.beltActualGrid.DisplayLayout.Bands[0].Columns["baseSpotName"].Hidden = true;
                }
                if (ckmemo.Checked) {
                    actual.unloadSite = "1";
                    this.beltActualGrid.DisplayLayout.Bands[0].Columns["unloadSite"].Hidden = false;
                }else{
                    this.beltActualGrid.DisplayLayout.Bands[0].Columns["unloadSite"].Hidden = true;
                }
                actual.valueFlag = "1";//查询有效
                actual.checkFlag = "1";//已审核的数据
                actual.createTime=DateTime.Now;//已审核的数据
                cp.param = actual;
            RESTfulResult<List<MeterWorkBeltActual>> rms = BeltActualService.doQueryActualInfoAndGroup(cp);
                if (rms.Succeed)
                {
                    if (rms.Data != null && rms.Data.Count > 0)
                    {
                        dtScaleTable = rms.Data.ListToDataTable<MeterWorkBeltActual>();

                        ClsControlPack.CopyDataToDatatable(ref dtScaleTable, ref dataTable2, true);
                        ClsControlPack.RefreshAndAutoSize(beltActualGrid);

                    }
                    else
                    {
                        dtScaleTable.Clear();
                        ClsControlPack.CopyDataToDatatable(ref dtScaleTable, ref dataTable2, true);
                        ClsControlPack.RefreshAndAutoSize(beltActualGrid);
                    }
                }
                else {
                    MessageBox.Show("预报查询失败，请联系技术中心！" + rms.Message);
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("预报查询异常，请联系技术中心！" + ex.Message);
                return;
            }
        }
        //导出
        private void ExporterScaleActual() {
            string str = Interaction.InputBox("请输入要导出的文件名", "提示", "", 100, 100);
            string pqath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

            this.ultraGridExcelExporter1.Export(this.beltActualGrid, pqath + "\\" + str + ".xls");
            MessageBox.Show("导出成功，请查看桌面！是否有[" + str + ".xls]表格");
        }



        private void frmScalePredictionManage_Load(object sender, EventArgs e)
        {
            //设置grid除了选择列 其余不可编辑
            GridHelper.SetExcludeColumnsActive(beltActualGrid.DisplayLayout.Bands[0], "UNCK");
            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"));

            InitFromLoad();//加载物料
            QueryScaleActualStatistics();
        }


        #region 公共方法区
        //下拉框控件自定义option
        public static void valueToCombo(DataTable source, UltraComboEditor cbb)
        {
            cbb.DataSource = source;
            cbb.DisplayMember = "VALUE";
            cbb.ValueMember = "TEXT";
        }
        //时间筛选开关
        private void checkDate_CheckedChanged(object sender, EventArgs e)
        {
            if (checkDate.Checked)
            {
                dtStartTime.Enabled = true;
                dtEndTime.Enabled = true;
            }
            else
            {
                dtStartTime.Enabled = false;
                dtEndTime.Enabled = false;
            }
        }

        private void ckforwardingUnit_CheckedChanged(object sender, EventArgs e)
        {
            QueryScaleActualStatistics();
        }

        private void ckreceivingUint_CheckedChanged(object sender, EventArgs e)
        {
            QueryScaleActualStatistics();
        }

        private void ckshipNo_CheckedChanged(object sender, EventArgs e)
        {
            QueryScaleActualStatistics();
        }

        private void ckbaseSpotName_CheckedChanged(object sender, EventArgs e)
        {
            QueryScaleActualStatistics();
        }

        private void ckmemo_CheckedChanged(object sender, EventArgs e)
        {
            QueryScaleActualStatistics();
        }

        //控件初始化加载
        private void InitFromLoad() {
            try
            {
                RESTfulResult<List<MeterBaseMatterInfo>> rmx = meterBaseMatterInfo.doQueryListLike(new MeterBaseMatterInfo { validFlag = "1", pageNum = 1, pageSize = 9999 });

                if (rmx.Succeed && rmx.Data != null && rmx.Data.Count > 0)
                {
                    dtMaterial = rmx.Data.ListToDataTable<MeterBaseMatterInfo>();
                }
                if (dtMaterial != null)
                {


                    DataRow d2 = dtMaterial.NewRow();
                    d2["matterName"] = "全部";
                    d2["matterNo"] = "0";
                    dtMaterial.Rows.InsertAt(d2, 0);
                    cmMatName.DataSource = dtMaterial;
                    cmMatName.DisplayMember = "matterName";
                    cmMatName.ValueMember = "matterNo";
                }

                MeterBaseSpotInfoService meterBaseSpotInfoService = new MeterBaseSpotInfoService();//计量点信息
                RESTfulResult<List<MeterBaseSpotInfo>> spo = meterBaseSpotInfoService.doQueryWf(new MeterBaseSpotInfo { validFlag = "1", spotTypeNo = "001002005" });
                if (spo.Succeed && spo.Data != null && spo.Data.Count > 0)
                {
                    dtPoint = spo.Data.ListToDataTable<MeterBaseSpotInfo>();
                }
                if (dtPoint != null)
                {
                    cmbPntName.DataSource = dtPoint;
                    cmbPntName.DisplayMember = "baseSpotName";
                    cmbPntName.ValueMember = "baseSpotNo";
                }
                cmbPntName.SelectedIndex = -1;
            }
            catch (Exception ex)
            {
                MessageBox.Show("物料初始化失败  " + ex.ToString());
            }
        }




        #endregion
    }
}