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 BeltScaleSystem

{
    /// <summary>
    /// 皮带结净数据发布 （没审核）
    /// </summary>
    public partial class frmBeltScaleActualinfo_Release : Form
    {
        public frmBeltScaleActualinfo_Release()
        {
            InitializeComponent();
        }

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

        #region 自定义变量
        private MeterWorkBeltActualService BeltActualService = new MeterWorkBeltActualService();
        private DataTable dtMaterial;//物料数据源
        private DataTable dtcustomerSupplier;//发货单位数据源
        MeterBaseMatterInfoService meterBaseMatterInfo = new MeterBaseMatterInfoService();//物料服务
        MeterBaseScalePointService monitorService = new MeterBaseScalePointService();
        MeterBaseCustomerSupplierService customerSupplierService = new MeterBaseCustomerSupplierService();
        private CoreAppUser appUser = null;//登陆信息
        private string userId = "", userName = ""; //用户ID用户名
        #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();
                DataTable dtScaleTable1 = dataTable1.Clone();
                CommonPage<MeterWorkBeltActual> cp = new CommonPage<MeterWorkBeltActual>();
                CommonPage<MeterWorkBeltActual> cp1 = new CommonPage<MeterWorkBeltActual>();
                if (checkDate.Checked)
                {
                    cp.startTime = dtStartTime.Text.ToString();
                    cp.endTime = dtEndTime.Text.ToString();
                    cp1.startTime = dtStartTime.Text.ToString();
                    cp1.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";//查询未审核的数据
                actual.valueFlag = "1";//有效数据
                cp.param = actual;

                RESTfulResult<List<MeterWorkBeltActual>> rms = BeltActualService.doQueryAndMultiple(cp);

                actual.checkFlag = "1";//已审核的数据
                cp1.param = actual;
                RESTfulResult<List<MeterWorkBeltActual>> rms1 = BeltActualService.doQueryAndMultiple(cp1);
                if (rms.Succeed&& rms1.Succeed)
                {
                   // MessageBox.Show("sss" + rms.Data.Count + "---" + rms1.Data.Count);
                    if (rms.Data != null && rms.Data.Count > 0)
                    {
                        dtScaleTable = rms.Data.ListToDataTable<MeterWorkBeltActual>();

                        ClsControlPack.CopyDataToDatatable(ref dtScaleTable, ref dataTable2, true);
                        ClsControlPack.RefreshAndAutoSize(beltActualGrid);
                        foreach (UltraGridRow ugr in beltActualGrid.Rows)
                        {
                            if (ugr.Cells["newMatName"].Value.ToString().Contains("焦炭"))
                            {
                                ugr.Cells["unloadSite"].Value = ugr.Cells["unloadSite"].Value.ToString() + "含焦粉";

                            }
                        }
                        //foreach (UltraGridRow ugr in beltActualGrid.Rows)
                        //{
                        //    if (ugr.Cells["valueFlag"].Value.ToString() == "0")
                        //    {
                        //        ugr.Appearance.BackColor = Color.Red;
                        //    }
                        //}
                    }
                    else
                    {
                        dtScaleTable.Clear();
                        ClsControlPack.CopyDataToDatatable(ref dtScaleTable, ref dataTable2, true);
                        ClsControlPack.RefreshAndAutoSize(beltActualGrid);
                    }
                    if (rms1.Data != null && rms1.Data.Count > 0)
                    {
                        dtScaleTable1 = rms1.Data.ListToDataTable<MeterWorkBeltActual>();
                        string aa = "";
                        ClsControlPack.CopyDataToDatatable(ref dtScaleTable1, ref dataTable1, true);
                        foreach (UltraGridRow ugr in ultraGrid1.Rows)
                        {
                            if (ugr.Cells["newMatName"].Value.ToString().Contains("焦炭"))
                            {
                                aa = ugr.Cells["unloadSite"].Value.ToString();
                                ugr.Cells["unloadSite"].Value = aa + "含焦粉";

                            }
                        }
                        ClsControlPack.RefreshAndAutoSize(ultraGrid1);

                        //foreach (UltraGridRow ugr in ultraGrid1.Rows)
                        //{
                        //    if (ugr.Cells["valueFlag"].Value.ToString() == "0")
                        //    {
                        //        ugr.Appearance.BackColor = Color.Red;
                        //    }
                        //}
                    }
                    else
                    {
                        dtScaleTable.Clear();
                        ClsControlPack.CopyDataToDatatable(ref dtScaleTable1, ref dataTable1, true);
                        ClsControlPack.RefreshAndAutoSize(ultraGrid1);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("实绩查询失败，请联系技术中心！" + ex.Message);
                return;
            }
        }
     
        //审核
        private void ApproveScaleActual(int no) {
            try
            {
                List<MeterWorkBeltActual> list = new List<MeterWorkBeltActual>();//实绩实体
                string flag = "";
                //获取已选择的列
                List<UltraGridRow> uGrid = null;
                if (no == 1)
                {
                    uGrid = GridHelper.UltraGridGetChooseRows(beltActualGrid, "UNCK");
                }
                else if(no==2){ 
                    uGrid = GridHelper.UltraGridGetChooseRows(ultraGrid1, "UNCK");
                }
                if (uGrid.Count < 1)
                {
                    MessageBox.Show("请勾选需要操作的实绩！");
                    return;
                }
                if (no == 1) {
                    foreach (UltraGridRow ugr in uGrid)
                    {
                        flag = ugr.Cells["checkFlag"].Value.ToString();
                        if (flag != "0")
                        {
                            MessageBox.Show("不是未审核的实绩不允许审核！");
                            return;
                        }
                        list.Add(new MeterWorkBeltActual { checkFlag = "1", actualNo = ugr.Cells["actualNo"].Value.ToString(),checkManNo= userId,checkManName=userName,checkTime= DateTime.Now});
                       // MessageBox.Show(userId+"---"+ userName+"---");
                    }
                    RESTfulResult<int> rm = BeltActualService.doUpdateCheckFlags(list);
                    if (rm.Succeed)
                    {
                        MessageBox.Show("审核成功！");
                        QueryScaleActualinfo();
                        return;
                    }
                    else
                    {
                        MessageBox.Show("审核失败，请联系技术中心！" + rm.Message);
                        return;
                    }
                } else if (no==2) {
                    foreach (UltraGridRow ugr in uGrid)
                    {
                        flag = ugr.Cells["checkFlag"].Value.ToString();
                        if (flag != "1")
                        {
                            MessageBox.Show("不是审核的实绩不允许取消审核！");
                            return;
                        }
                        list.Add(new MeterWorkBeltActual { checkFlag = "0", actualNo = ugr.Cells["actualNo"].Value.ToString() });
                    }
                    RESTfulResult<int> rm = BeltActualService.doUpdateCheckFlags(list);
                    if (rm.Succeed)
                    {
                        MessageBox.Show("取消审核成功！");
                        QueryScaleActualinfo();
                        return;
                    }
                    else
                    {
                        MessageBox.Show("取消审核失败，请联系技术中心！" + rm.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]表格");
        }
        #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"));
            userId = ((ST_MainForm)(this.MdiParent)).UserID;
            userName = ((ST_MainForm)(this.MdiParent)).UserName;
            appUser = ((ST_MainForm)(this.MdiParent)).AppUser;
            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;
            ValueList vlistFlag5 = new ValueList();
            vlistFlag5.ValueListItems.Add("0", "未审核");
            vlistFlag5.ValueListItems.Add("1", "已审核");
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["checkFlag"].ValueList = vlistFlag5;

            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;
            }
        }
      

        #region 单选按钮 和文本框控制
        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

        #endregion
    }
}