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

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

        #region 自定义变量
        private MeterWorkBeltActual BeltActualInfo = new MeterWorkBeltActual();//实绩实体
        private MeterWorkBeltActualService BeltActualService = new MeterWorkBeltActualService();
        private DataTable dtMaterial;//物料数据源
        private DataTable dtcustomerSupplier;//发货单位数据源
        MeterBaseMatterInfoService meterBaseMatterInfo = new MeterBaseMatterInfoService();//物料服务
        MeterBaseScalePointService monitorService = new MeterBaseScalePointService();
        private Log l = Log.GetInstance(); //日志
        MeterBaseCustomerSupplierService customerSupplierService = new MeterBaseCustomerSupplierService();
        #endregion

        #region 菜单栏方法
        //实绩查询
        private void QueryScaleActualinfo() {
            try
            {
                if (DateTime.Compare(dtStartTime.Value, dtEndTime.Value) > 0)
                {
                    MessageBox.Show("开始时间不能大于结束时间");
                    return;
                }
                MeterWorkBeltActual actual = new MeterWorkBeltActual();
                DataTable dtScaleTable = dataTable2.Clone();
                CommonPage<MeterWorkBeltActual> cp = new CommonPage<MeterWorkBeltActual>();
                if (checkDate.Checked)
                {
                    cp.startTime = dtStartTime.Text.ToString();
                    cp.endTime = dtEndTime.Text.ToString();
                    if (cbDateFlag.SelectedIndex == 0) {
                        actual.startTime = dtStartTime.Value;//不为空即可
                    }
                    if (cbDateFlag.SelectedIndex == 1)
                    {
                        actual.endTime = dtStartTime.Value;//不为空即可
                    }
                    if (cbDateFlag.SelectedIndex == 2)
                    {
                        actual.checkTime = dtStartTime.Value;//审核时间  就是发布时间   不为空即可
                    }
                }
                if (cmMatName.Text.ToString() != "全部")
                {
                    actual.newMatName = cmMatName.Text.ToString();
                }
                if (cbfahuo.Text.ToString() != "全部")
                {
                    actual.newSupplier = cbfahuo.Text.ToString();
                }
                if (cbshouh.Text.ToString() != "全部")
                {
                    actual.receivingUintName = cbshouh.Text.ToString();
                }
                if (ckShipNo.Checked) {
                    if (ShipnoTxt_1.Text.ToString() != null && ShipnoTxt_1.Text.ToString().Trim() != "")//船号
                    {
                      actual.shipNo = ShipnoTxt_1.Text.ToString(); 
                    }
                }
                
                if (cmbState.Text.ToString() != "全部" && cmbState.Text.ToString() != "" )
                {
                    actual.valueFlag = cmbState.Value.ToString();
                }
                if (!Rbtn_AB.Checked)
                {
                    if (Rbtn_A.Checked) {
                        actual.scaleName = Rbtn_A.Text.ToString().Substring(0,1);
                    }
                    if (Rbtn_B.Checked)
                    {
                        actual.scaleName = Rbtn_B.Text.ToString().Substring(0, 1);
                    }
                }
                if (moteTxt_1.Text.ToString().ToString().Trim() != "")
                {
                    actual.unloadSite = moteTxt_1.Text.ToString().Trim();
                }
              //  actual.checkFlag = "0";//查询未审核的数据   11.23更改为 所有数据
                cp.param = actual;
                RESTfulResult<List<MeterWorkBeltActual>> rms = BeltActualService.doQueryAndMultiple(cp);
                if (rms.Succeed)
                {
                    if (rms.Data != null && rms.Data.Count > 0)
                    {
                        l.WriteLog("皮带秤计量异常_", "实绩数量" + rms.Data.Count);
                        //for (int i = 0; i < rms.Data.Count; i++)
                        //{
                        //    l.WriteLog("皮带秤计量异常_", "rms" + rms.Data[i].unloadSite == null ? "" : rms.Data[i].unloadSite + rms.Data[i].memo == null ? "" : rms.Data[i].memo);
                        //    rms.Data[i].unloadSite = rms.Data[i].unloadSite == null ? "" : rms.Data[i].unloadSite + rms.Data[i].memo == null ? "" : rms.Data[i].memo;
                        //}
                        dtScaleTable = rms.Data.ListToDataTable<MeterWorkBeltActual>();
                       // dtScaleTable = list1.ListToDataTable<MeterWorkBeltActual>();

                        ClsControlPack.CopyDataToDatatable(ref dtScaleTable, ref dataTable2, true);
                        string aa = "";
                        foreach (UltraGridRow ugr in beltActualGrid.Rows)
                        {
                            if (ugr.Cells["newMatName"].Value.ToString().Contains("焦炭")) {
                                aa = ugr.Cells["unloadSite"].Value.ToString();
                                ugr.Cells["unloadSite"].Value = aa + "含焦粉";
                               // l.WriteLog("皮带秤计量异常_", "1:" + aa);
                             //   l.WriteLog("皮带秤计量异常_", "2:" + ugr.Cells["newMatName"].Value.ToString());


                            }
                            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;
                            }
                        }
                        ClsControlPack.RefreshAndAutoSize(beltActualGrid);
                    }
                    else
                    {
                        dtScaleTable.Clear();
                        ClsControlPack.CopyDataToDatatable(ref dtScaleTable, ref dataTable2, true);
                        ClsControlPack.RefreshAndAutoSize(beltActualGrid);
                    }
                }
            }
            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]表格");
        }
        #endregion

        #region 公共方法区
        //初始化加载
        private void frmBeltScaleActualinfo_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")); 

            DataTable dt = new DataTable();
            dt.Columns.Add("TEXT");
            dt.Columns.Add("VALUE");
            dt.Rows.Add("-1", "全部");
            dt.Rows.Add("0", "无效");
            dt.Rows.Add("1", "有效");
            valueToCombo(dt, cmbState);
            this.cmbState.SelectedIndex = -1;
            
            DataTable dt2 = new DataTable();
            dt2.Columns.Add("TEXT");
            dt2.Columns.Add("VALUE");
            dt2.Rows.Add("0", "开始时间");
            dt2.Rows.Add("1", "结束时间");
            dt2.Rows.Add("2", "发布时间");
            valueToCombo(dt2, cbDateFlag);
            this.cbDateFlag.SelectedIndex = 0;


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

            ValueList vlistFlag4 = new ValueList();
            vlistFlag4.ValueListItems.Add("0", "未审核");
            vlistFlag4.ValueListItems.Add("1", "已审核");
            this.beltActualGrid.DisplayLayout.Bands[0].Columns["checkFlag"].ValueList = vlistFlag4;

            InitFromLoad();//加载物料
            QueryScaleActualinfo();
        }
        //下拉框控件自定义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 InitFromLoad()
        {
            try
            {
                RESTfulResult<List<MeterBaseMatterInfo>> rmx = meterBaseMatterInfo.doQueryListLike(new MeterBaseMatterInfo { validFlag = "1", pageNum = 1, pageSize = 9999 });
                RESTfulResult<List<MeterBaseCustomerSupplier>> rmx2 = customerSupplierService.doQuery(new MeterBaseCustomerSupplier { validFlag = "1"});
               

                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>();
                }
                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());
            }
        }
   
        //日期选择框
        private void checkDate_CheckedChanged(object sender, EventArgs e)
        {
            if (checkDate.Checked)
            {
                cbDateFlag.Enabled = true;
                dtStartTime.Enabled = true;
                dtEndTime.Enabled = true;
            }
            else
            {
                cbDateFlag.Enabled = false;
                dtStartTime.Enabled = false;
                dtEndTime.Enabled = false;
            }
        }
       
        //船号选择框
        private void ckShipNo_CheckedChanged(object sender, EventArgs e)
        {
            if (ckShipNo.Checked)
            {
                ShipnoTxt_1.Enabled = true;
            }
            else
            {
                ShipnoTxt_1.Enabled = false;
            }
        }
        

        private void Rbtn_AB_CheckedChanged(object sender, EventArgs e)
        {
            if (Rbtn_AB.Checked) {
                Rbtn_A.Checked = false;
                Rbtn_B.Checked = false;
            }
        }

        private void Rbtn_A_CheckedChanged(object sender, EventArgs e)
        {
            if (Rbtn_A.Checked)
            {
                Rbtn_AB.Checked = false;
                Rbtn_B.Checked = false;
            }
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            if (Rbtn_B.Checked)
            {
                Rbtn_AB.Checked = false;
                Rbtn_A.Checked = false;
            }
        }


        #endregion

       
    }
}