using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DBFactory;
using Microsoft.VisualBasic;
using System.ServiceModel;
namespace wcfControlMonitorClient
{
    /// <summary>
    /// Creator:Richard.liu
    /// 模仿管理下达自动任务
    /// </summary>
    public partial class FrmWMSOutTask : Form
    {

        DBOperator dbo = CStaticClass.dbo;


        #region ====变量定义

        CommonClassLib.UCellPanel cellPanel = null;

        #endregion
        private static FrmWMSOutTask _formInstance;

        public static FrmWMSOutTask FormInstance
        {
            get
            {
                if (_formInstance == null)
                {
                    _formInstance = new FrmWMSOutTask();
                }
                return _formInstance;
            }
            set { _formInstance = value; }
        }
        public FrmWMSOutTask()
        {
            dbo.Open();
            InitializeComponent();
            _formInstance = this;
            DataSet ds = dbo.ExceSQL("select F_DeviceIndex,f_devicename from T_Base_Device where F_DeviceKindIndex=10");
            if (ds.Tables[0].DefaultView.Count > 0)
            {
                cbLaneway.ValueMember = "F_DeviceIndex";
                cbLaneway.DisplayMember = "f_devicename";
                cbLaneway.DataSource = ds.Tables[0].DefaultView;

            }

        }

        private void FrmAutoCommand_Load(object sender, EventArgs e)
        {
            DataSet ds = dbo.ExceSQL("select F_DeviceIndex,F_DeviceKindIndex from T_Base_Device where F_DeviceKindIndex=10 and  F_DeviceIndex=" + Convert.ToInt32(cbLaneway.SelectedValue) + "");
            if (ds.Tables[0].DefaultView.Count > 0)
            {

                cbLaneway.Enabled = true;

                plCell.Enabled = true;

                if (cellPanel == null)
                {
                    cellPanel = new CommonClassLib.UCellPanel(this, tbcellcode, null, string.Empty);
                    cellPanel.TaskType = "100";
                    cellPanel.GoodsName = "";
                    cellPanel.Dock = DockStyle.Fill;
                    cellPanel.AllowWareHouseChanged = false;
                    plCell.Controls.Add(cellPanel);

                }
                cellPanel.ControlCellCode = tbcellcode;
                cellPanel.Laneway = this.cbLaneway.SelectedValue.ToString();
                cellPanel.WareHouse = GetWarehouse(Convert.ToInt32(this.cbLaneway.SelectedValue));
                cellPanel.Visible = plCell.Visible;
            }
            else
            {
                plCell.Enabled = false;

            }


        }



        private void FrmAutoCommand_FormClosing(object sender, FormClosingEventArgs e)
        {
            _formInstance = null;
        }
        string GetWarehouse(int laneway)
        {
            DataView dv = dbo.ExceSQL(string.Format("SELECT DISTINCT FWAREHOUSE FROM ST_CELL WHERE (FLaneWay = {0})", laneway)).Tables[0].DefaultView;
            if (dv.Count > 0)
            {
                return dv[0][0].ToString();
            }
            else
            {
                return "01";
            }
        }

        private void cbLaneway_SelectedIndexChanged(object sender, EventArgs e)
        {//
            DataSet ds = dbo.ExceSQL(string.Format("SELECT F_EndDevice FROM T_Base_Route WHERE (F_StartDevice = {0})", Convert.ToInt32(cbLaneway.SelectedValue)));
            if (ds.Tables[0].DefaultView.Count > 0)
            {
                //cbTOStation.DataSource = null;
                //cbTOStation.ValueMember = "F_EndDevice";
                //cbTOStation.DisplayMember = "F_EndDevice";
                //cbTOStation.DataSource = ds.Tables[0].DefaultView;
                cbLaneway.Enabled = true;

                plCell.Enabled = true;

                if (cellPanel == null)
                {
                    cellPanel = new CommonClassLib.UCellPanel(this, tbcellcode, null, string.Empty);
                    cellPanel.TaskType = "100";
                    cellPanel.GoodsName = "";
                    cellPanel.Dock = DockStyle.Fill;
                    cellPanel.AllowWareHouseChanged = false;
                    plCell.Controls.Add(cellPanel);

                }
                cellPanel.ControlCellCode = tbcellcode;
                cellPanel.Laneway = this.cbLaneway.SelectedValue.ToString();
                cellPanel.WareHouse = GetWarehouse(Convert.ToInt32(this.cbLaneway.SelectedValue));
                cellPanel.Visible = plCell.Visible;

            }
        }

        private void lblPalletBarcode_TextChanged(object sender, EventArgs e)
        {
            DataView dv = dbo.ExceSQL(string.Format("SELECT FPALLETBARCODE FROM ST_CELL WHERE FPALLETBARCODE !='-' and (FCELLCODE = '{0}') AND (FLaneWay = {1})", tbcellcode.Text, Convert.ToInt32(cbLaneway.SelectedValue))).Tables[0].DefaultView;
            if (dv.Count > 0)
            {
                tbPalletBarcode.Text = dv[0][0].ToString();
            }
            else
            {
                tbPalletBarcode.Text = "";
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("您确认要更改该货位条码吗?", "操作提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
            {
                return;
            }
            if (tbcellcode.Text.Length < 6)
            {
                MessageBox.Show("货位尚未选择!", "误操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (tbnewPALLETBARCODE.Text == "")
            {
                MessageBox.Show("新条码未设定!", "误操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            dbo.ExecuteSql(string.Format("UPDATE ST_CELL SET FRUNSTATUS=0,FPALLETBARCODE = '{0}' WHERE (FCELLCODE = '{1}') AND (FLaneWay = {2})", tbnewPALLETBARCODE.Text, tbcellcode.Text, Convert.ToInt32(cbLaneway.SelectedValue)));
            MessageBox.Show("保存成功!", "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DataSet ds = dbo.ExceSQL(string.Format("SELECT F_EndDevice FROM T_Base_Route WHERE (F_StartDevice = {0})", Convert.ToInt32(cbLaneway.SelectedValue)));
            if (ds.Tables[0].DefaultView.Count > 0)
            {
                //cbTOStation.DataSource = null;
                //cbTOStation.ValueMember = "F_EndDevice";
                //cbTOStation.DisplayMember = "F_EndDevice";
                //cbTOStation.DataSource = ds.Tables[0].DefaultView;
                cbLaneway.Enabled = true;

                plCell.Enabled = true;

                if (cellPanel == null)
                {
                    cellPanel = new CommonClassLib.UCellPanel(this, tbcellcode, null, string.Empty);
                    cellPanel.TaskType = "100";
                    cellPanel.GoodsName = "";
                    cellPanel.Dock = DockStyle.Fill;
                    cellPanel.AllowWareHouseChanged = false;
                    plCell.Controls.Add(cellPanel);

                }
                cellPanel.ControlCellCode = tbcellcode;
                cellPanel.Laneway = this.cbLaneway.SelectedValue.ToString();
                cellPanel.WareHouse = GetWarehouse(Convert.ToInt32(this.cbLaneway.SelectedValue));
                cellPanel.Visible = plCell.Visible;

            }
        }
    }
}