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;
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 frmScalePredictionSelect_nofh : Form
    {
        public frmScalePredictionSelect_nofh()
        {
            InitializeComponent();
        }

        //菜单栏事件
        private void ultraToolbarsManager1_ToolClick(object sender, Infragistics.Win.UltraWinToolbars.ToolClickEventArgs e)
        {
            switch (e.Tool.Key)
            {
                case "查询":
                    QueryScalePrediction();
                    break;
              
                case "导出":
                    ExporterScalePrediction();
                    break;
               
            }
        }


        #region 自定义变量
        // private ComBaseInfoService comBaseInfoService = new ComBaseInfoService(); //基础数服务
        PreBeltScalePreService PreBeltScalePreService = new PreBeltScalePreService();//预报服务
        private CoreAppUser appUser = null;//登陆信息
        private string userId = "", userName = ""; //用户ID用户名
        private DataTable dtMaterial;//物料数据源
        private DataTable dtMaterial2;//物料数据源
        private DataTable dtcustomerSupplier;//发货单位数据源
        private DataTable dtPoint;//计量点数据源
        MeterBaseMatterInfoService matterInfoService = new MeterBaseMatterInfoService();//物料服务
        MeterBaseCustomerSupplierService customerSupplierService = new MeterBaseCustomerSupplierService();
        #endregion


        #region 菜单栏方法
        //预报查询
        private void  QueryScalePrediction() {
            try
            {
                if (DateTime.Compare(dtStartTime.Value, dtEndTime.Value) > 0)
                {
                    MessageBox.Show("开始时间不能大于结束时间");
                    return;
                }
                PreBeltScaleForecast forecast = new PreBeltScaleForecast();
                DataTable dtScaleTable = dataTable1.Clone();
                CommonPage<PreBeltScaleForecast> cp = new CommonPage<PreBeltScaleForecast>();
                if (checkDate.Checked)
                {
                    cp.startTime = dtStartTime.Text.ToString();
                    cp.endTime = dtEndTime.Text.ToString();
                }
                if (cmMatName.Text.ToString() != "全部") {
                    forecast.newMatName = cmMatName.Text.ToString();
                }
                
                if (cbshouh.Text.ToString() != "全部" )
                {
                    forecast.receivingUintName = cbshouh.Text.ToString();
                }
                if (ShipnoTxt_1.Text.ToString() != null && ShipnoTxt_1.Text.ToString().Trim() != "")//船号
                {
                    forecast.shipNo = ShipnoTxt_1.Text.ToString();
                }
                if (cmbState.Text.ToString() != "全部" && cmbState.SelectedIndex != -1)
                {
                    forecast.valueFlag = cmbState.Value.ToString();
                }
                cp.param = forecast; // new PreBeltScaleForecast { }; // ！=0
                RESTfulResult<List<PreBeltScaleForecast>> rms = PreBeltScalePreService.doQueryWflike(cp);
                if (rms.Succeed)
                {
                    if (rms.Data != null && rms.Data.Count > 0)
                    {
                        dtScaleTable = rms.Data.ListToDataTable<PreBeltScaleForecast>();

                        ClsControlPack.CopyDataToDatatable(ref dtScaleTable, ref dataTable1, true);
                        ClsControlPack.RefreshAndAutoSize(ultraGridBeltPre);

                        foreach (UltraGridRow ugr in ultraGridBeltPre.Rows)
                        {
                            if (ugr.Cells["valueFlag"].Value.ToString() == "0")
                            {
                                ugr.Appearance.BackColor = Color.Red;
                            }
                            else if (ugr.Cells["valueFlag"].Value.ToString() == "3")
                            {
                                ugr.Appearance.BackColor = Color.YellowGreen;
                            }
                            else
                            {
                                ugr.Appearance.BackColor = Color.White;
                            }
                        }

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

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


        #region 公共方法区
        private void frmScalePredictionManage_Load(object sender, EventArgs e)
        {
            //设置grid除了选择列 其余不可编辑
            GridHelper.SetExcludeColumnsActive(ultraGridBeltPre.DisplayLayout.Bands[0], "UNCK");
            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"));
            
            DataTable dt = new DataTable();
            dt.Columns.Add("TEXT");
            dt.Columns.Add("VALUE");
            dt.Rows.Add("-1", "全部");
            dt.Rows.Add("0", "无效");
            dt.Rows.Add("1", "未使用");
            dt.Rows.Add("2", "使用中");
            dt.Rows.Add("3", "已完成");
            valueToCombo(dt, cmbState);
            this.cmbState.SelectedIndex = 0;


            //预报表转换
            ValueList vlistFlag1 = new ValueList();
            vlistFlag1.ValueListItems.Add("0", "无效");
            vlistFlag1.ValueListItems.Add("1", "未使用");
            vlistFlag1.ValueListItems.Add("2", "使用中");
            vlistFlag1.ValueListItems.Add("3", "已完成");
            this.ultraGridBeltPre.DisplayLayout.Bands[0].Columns["valueFlag"].ValueList = vlistFlag1;

            InitFromLoad();//加载
            QueryScalePrediction();
        }
        //控件初始化加载
        private void InitFromLoad() {
            try
            {
                RESTfulResult<List<MeterBaseMatterInfo>> rmx = matterInfoService.doQueryListLike(new MeterBaseMatterInfo { validFlag = "1", pageNum = 1, pageSize = 9999 });
                RESTfulResult<List<MeterBaseCustomerSupplier>>  rmx2 = customerSupplierService.doQuery(new MeterBaseCustomerSupplier { validFlag = "1"});
                MeterBaseSpotInfoService meterBaseSpotInfoService = new MeterBaseSpotInfoService();//计量点信息
                RESTfulResult<List<MeterBaseSpotInfo>> spo = meterBaseSpotInfoService.doQueryWf(new MeterBaseSpotInfo { validFlag = "1", spotTypeNo = "001002005" });

                if (rmx.Succeed && rmx.Data != null && rmx.Data.Count > 0)
                {
                    dtMaterial = rmx.Data.ListToDataTable<MeterBaseMatterInfo>();
                    dtMaterial2 = dtMaterial.Copy();
                }
                if (dtMaterial != null)
                {
                    DataRow d1 = dtMaterial.NewRow();
                    d1["matterName"] = "全部";
                    d1["matterNo"] = "0";
                    dtMaterial.Rows.InsertAt(d1, 0);

                    cmMatName.DataSource = dtMaterial;
                    cmMatName.DisplayMember = "matterName";
                    cmMatName.ValueMember = "matterNo";

                }
                if (rmx2.Succeed && rmx2.Data != null && rmx2.Data.Count > 0)
                {
                    dtcustomerSupplier = rmx2.Data.ListToDataTable<MeterBaseCustomerSupplier>();
                }
                if (dtcustomerSupplier != null)
                {
                    DataRow d1 = dtcustomerSupplier.NewRow();
                    d1["customerSupplierName"] = "全部";
                    d1["customerSupplierNo"] = "0";
                    dtcustomerSupplier.Rows.InsertAt(d1, 0);
                    cbfahuo.DataSource = dtcustomerSupplier;
                    cbfahuo.DisplayMember = "customerSupplierName";
                    cbfahuo.ValueMember = "customerSupplierNo";

                    DataTable dt2 = new DataTable();
                    dt2.Columns.Add("TEXT");
                    dt2.Columns.Add("VALUE");
                    dt2.Rows.Add("0", "西区炼铁");
                    dt2.Rows.Add("1", "东区炼铁");
                    valueToCombo1(dt2, cbshouh);
                    this.cbshouh.SelectedIndex = -1;

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("初始化失败  " + ex.ToString());
            }
        }
     
        //下拉框控件自定义option
        public static void valueToCombo(DataTable source, UltraComboEditor cbb)
        {
            cbb.DataSource = source;
            cbb.DisplayMember = "VALUE";
            cbb.ValueMember = "TEXT";
        }
        //下拉框控件自定义option
        public static void valueToCombo1(DataTable source, ComboBox 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;
            }
        }
      

     

        #endregion
    }
}