using com.hnshituo.core.webapp.vo;
using Common;
using Infragistics.Win;
using Infragistics.Win.UltraWinEditors;
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 CarMonitor

{
    public partial class meterWorkCarActualSelect : Form
    {
        public meterWorkCarActualSelect()
        {
            InitializeComponent();
        }

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


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


        #region 菜单栏方法
        //预报查询
        private void Query() {
            try
            {
                if (DateTime.Compare(dtStartTime.Value, dtEndTime.Value) > 0)
                {
                    MessageBox.Show("开始时间不能大于结束时间");
                    return;
                }
                MeterWorkCarActual vo = new MeterWorkCarActual();
                DataTable dtScaleTable = dataTable1.Clone();
                CommonPage<MeterWorkCarActual> cp = new CommonPage<MeterWorkCarActual>();
                if (checkDate.Checked)
                {
                    cp.startTime = dtStartTime.Text.ToString();
                    cp.endTime = dtEndTime.Text.ToString();
                    vo.netTime = DateTime.Now;
                }
                if (cbfahuo.Text.ToString() != "全部" && cbfahuo.Text.ToString() != "") {
                    vo.forwardingUnitName = cbfahuo.Text.ToString();
                }
                
                if (cbshouh.Text.ToString() != "全部" && cbshouh.Text.ToString()!="")
                {
                    vo.receivingUintName = cbshouh.Text.ToString();
                }
                if (cmMatName.Text.ToString() != "全部" && cmMatName.SelectedIndex != -1)
                {
                    vo.matterName = cmbState.Value.ToString();
                }
                if (cbmeterTypeName.Text.ToString() != "全部" && cbmeterTypeName.Text.ToString() != "")
                {
                  //  vo.meterTypeName = cbmeterTypeName.Text.ToString();
                    vo.meterTypeNo = cbmeterTypeName.Value.ToString();
                }
                if (textcarno.Text.ToString().Trim() != "")
                {
                    vo.carNo = textcarno.Text.ToString().Trim();
                }
                if (textship.Text.ToString().Trim() != "")
                {
                    vo.shipNo = textship.Text.ToString().Trim();
                }
                if (textloadpoint.Text.ToString().Trim() != "")
                {
                    vo.loadPointName = textloadpoint.Text.ToString().Trim();
                }
                if (cmbState.Text.ToString().Trim()!="") {
                    if (cmbState.Value.ToString().Trim() != "")
                    {
                        vo.valueFlag = cmbState.Value.ToString().Trim();
                    }
                }
               
                cp.param = vo;
                RESTfulResult<List<MeterWorkCarActual>> rms = service.doQueryAndMultiple(cp);
                if (rms.Succeed)
                {
                    if (rms.Data != null && rms.Data.Count > 0)
                    {
                        dtScaleTable = rms.Data.ListToDataTable<MeterWorkCarActual>();

                        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);
                    }
                }
                this.ultraGridBeltPre.DisplayLayout.Override.SummaryDisplayArea = SummaryDisplayAreas.BottomFixed;
                // this.ultraGridBeltPre.DisplayLayout.UseFixedHeaders = true;
                // this.ultraGridBeltPre.DisplayLayout.Bands[0].Columns["carNo"].Header.Fixed = true;
                //this.ultraGridBeltPre.DisplayLayout.Bands[0].Columns[0].Header.FixedHeaderIndicator = FixedHeaderIndicator.InSwapDropDown;
            }
            catch (Exception ex)
            {
                MessageBox.Show("预报查询失败，请联系技术中心！" + ex.Message);
                return;
            }
        }
      
        //导出预报数据
        private void Exporter() {
            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)
        {
            //设置不可编辑
            GridHelper.SetExcludeColumnsActive(ultraGridBeltPre.DisplayLayout.Bands[0], "");
            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("0", "有效");
            dt.Rows.Add("1", "作废");
            valueToCombo(dt, cmbState);
            this.cmbState.SelectedIndex = 0; 


            // //转换 
            // //0=毛重；1=常规皮重；2=期限皮重；3=历史皮重；4=标识皮重；5=自重皮重
            // ValueList vlistFlag1 = new ValueList();
            //vlistFlag1.ValueListItems.Add("0", "毛重");
            //vlistFlag1.ValueListItems.Add("1", "常规皮重");
            //vlistFlag1.ValueListItems.Add("2", "期限皮重");
            //vlistFlag1.ValueListItems.Add("3", "历史皮重");
            //vlistFlag1.ValueListItems.Add("4", "标识皮重");
            //vlistFlag1.ValueListItems.Add("5", "自重皮重");
            //this.ultraGridBeltPre.DisplayLayout.Bands[0].Columns["weightType"].ValueList = vlistFlag1;

            ////1：远程计量；2：智能计量；3：手工录入；4：放行智能计量；5：滞后匹配  
            //ValueList vlistFlag2 = new ValueList();
            //vlistFlag1.ValueListItems.Add("1", "远程计量");
            //vlistFlag1.ValueListItems.Add("2", "智能计量");
            //vlistFlag1.ValueListItems.Add("3", "手工录入");
            //vlistFlag1.ValueListItems.Add("4", "放行智能计量");
            //vlistFlag1.ValueListItems.Add("5", "滞后匹配");
            //this.ultraGridBeltPre.DisplayLayout.Bands[0].Columns["meterMode"].ValueList = vlistFlag2;

            //// 0：批次预报；1：单次预报；2：联运预报；3：分检预报
            //ValueList vlistFlag3 = new ValueList();
            //vlistFlag1.ValueListItems.Add("0", "批次预报");
            //vlistFlag1.ValueListItems.Add("1", "单次预报");
            //vlistFlag1.ValueListItems.Add("2", "联运预报");
            //vlistFlag1.ValueListItems.Add("3", "分检预报");
            //this.ultraGridBeltPre.DisplayLayout.Bands[0].Columns["predictionType"].ValueList = vlistFlag3;

            InitFromLoad();//加载
            //Query();
        }
        //控件初始化加载
        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>();
                }
                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>();
                    dtcustomerSupplier2 = dtcustomerSupplier.Copy();
                }
                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";

                    DataRow d2 = dtcustomerSupplier2.NewRow();
                    d2["customerSupplierName"] = "全部";
                    d2["customerSupplierNo"] = "0";
                    dtcustomerSupplier2.Rows.InsertAt(d2, 0);
                    cbshouh.DataSource = dtcustomerSupplier2;
                    cbshouh.DisplayMember = "customerSupplierName";
                    cbshouh.ValueMember = "customerSupplierNo";
                }
                RESTfulResult<List<ComBaseInfo>> rrComGradeName = comBaseInfoService.doQueryBaseList(new ComBaseInfo { validFlag = "1", pBaseCode = "001006" });
                if (rrComGradeName.Succeed && rrComGradeName.Data != null && rrComGradeName.Data.Count > 0)
                {
                    rrComGradeName.Data.Sort((a, b) => a.baseName.CompareTo(b.baseName));
                    ClsControlPack.SetUltraComboDataSource(cbmeterTypeName, rrComGradeName.Data.ListToDataTable<ComBaseInfo>(), "baseCode", "baseName");
                }
                else
                {
                    MessageBox.Show("基础信息载入失败!");
                    return;
                }
            }
            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";
        }

        //时间控件开关
        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
    }
}