using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace SSLMS.MobileUI.uc
{
    public partial class ucMANAGE_MOVE : ucBase
    {
        public ucMANAGE_MOVE()
        {
            this.InitializeComponent();

            this.Init();
        }

        private void Init()
        {
            this.cbWAREHOUSE_Bind();
        }

        private void cbWAREHOUSE_Bind()
        {
            try
            {
                cbWAREHOUSE.ValueMember = "WAREHOUSE_ID";

                cbWAREHOUSE.DisplayMember = "WAREHOUSE_NAME";

                cbWAREHOUSE.DataSource = this._i_st.WAREHOUSE_GetList();
            }
            catch (Exception ex)
            {
                MessageBox.Show("ϵͳÒì³££¡\n" + ex.Message);
            }
        }

        private void cbAREA_Bind(string WAREHOUSE_ID)
        {
            try
            {
                this.cbAREA.ValueMember = "AREA_ID";

                this.cbAREA.DisplayMember = "AREA_NAME";

                this.cbAREA.DataSource = this._i_st.AREA_GetList(WAREHOUSE_ID);
            }
            catch (Exception ex)
            {
                MessageBox.Show("ϵͳÒì³££¡\n" + ex.Message);
            }
        }

        private void STORAGE_LIST_Bind()
        {
            try
            {
                string AREA_ID = this.cbAREA.SelectedValue.ToString();

                string GOODS = this.txtGOODS.Text.Trim();

                string  STOCK_BARCODE= this.txtSTOCK_BARCODE.Text.Trim();

                this.uclSTORAGE_LIST.listXml = "V_ST_STORAGE_LIST";

                this.uclSTORAGE_LIST.listTable = "V_ST_STORAGE_LIST";

                string sComon = "  AND AREA_ID = {0} AND (STOCK_BARCODE IS NOT NULL OR STOCK_BARCODE<>'') AND STOCK_BARCODE = '{1}'" + string.Format(" AND RUN_STATUS='{0}'", SSLMS.Model.RUN_STATUS.enable.ToString());

                this.uclSTORAGE_LIST.listWhere = string.Format(sComon, AREA_ID, STOCK_BARCODE);

                this.uclSTORAGE_LIST.listWhere += string.IsNullOrEmpty(GOODS) ? string.Empty : string.Format(" AND (GOODS_CODE LIKE '%{0}%' OR GOODS_NAME LIKE '%{0}%' OR GOODS_REMARK LIKE '%{0}%') ", GOODS);

                this.uclSTORAGE_LIST.colGroup = "GOODS_TYPE_ID";

                this.uclSTORAGE_LIST.colSplit = "GOODS_PROPERTY";

                this.uclSTORAGE_LIST.bCheck = true;

                this.uclSTORAGE_LIST.Init();
            }
            catch (Exception ex)
            {
                MessageBox.Show("ϵͳÒì³££¡\n" + ex.Message);
            }
        }

        private void cbWAREHOUSE_SelectedValueChanged(object sender, EventArgs e)
        {
            if (this.cbWAREHOUSE.SelectedValue != null)
            {
                string WAREHOUSE_ID = this.cbWAREHOUSE.SelectedValue.ToString();

                this.cbAREA_Bind(WAREHOUSE_ID);
            }
        }

        private void cbAREA_SelectedValueChanged(object sender, EventArgs e)
        {
            if (null != this.cbAREA.SelectedValue)
            {
                this.STORAGE_LIST_Bind();
            }
        }

        public override void OnRead(object sender, Symbol.Barcode.ReaderData readerData)
        {
            if (this.txtGOODS.Focused)
            {
                this.txtGOODS.Text = readerData.Text;

                this.STORAGE_LIST_Bind();
            }

            if (this.txtSTOCK_BARCODE.Focused)
            {
                this.txtSTOCK_BARCODE.Text = readerData.Text;

                this.STORAGE_LIST_Bind();
            }

            if (this.txtCELL.Focused)
            {
                this.txtCELL.Text = readerData.Text;
            }
        }

        private void tsbOK_Click(object sender, EventArgs e)
        {
            bool bResult = true;

            string sResult = string.Empty;

            string[] aSTART_CELL_ID = this.uclSTORAGE_LIST.GetCheckColumns("CELL_ID", new char[] { ',' }).Split(',');

            if (aSTART_CELL_ID.Length <= 0 || string.IsNullOrEmpty(aSTART_CELL_ID[0]))
            {
                MessageBox.Show("ÇëÑ¡ÔñÒª³ö¿âµÄÎïÁÏ!");

                return;
            }

            if (string.IsNullOrEmpty(this.txtCELL.Text))
            {
                MessageBox.Show("ÇëÑ¡Ôñ³ö¿âվ̨!");

                return;
            }

            ST_CELL mST_CELL_END = this._i_st.CELL_GetModel_1(this.txtCELL.Text.Trim());

            if (null == mST_CELL_END)
            {
                MessageBox.Show("ÇëÑ¡ÔñÕýÈ·µÄ³ö¿âվ̨!");

                return;
            }

            if (MessageBox.Show("È·¶¨ÒÆ¿â?", "ϵͳÌáʾ", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button2) == DialogResult.Cancel)
            {
                return;
            }

            int START_CELL_ID = 0;

            int MANAGE_ID = 0;

            try
            {
                foreach (string sSTART_CELL_ID in aSTART_CELL_ID)
                {
                    START_CELL_ID = Convert.ToInt32(sSTART_CELL_ID);

                    ST_CELL mST_CELL_START = this._i_st.CELL_GetModel(START_CELL_ID);

                    if (null == mST_CELL_START || !SSLMS.Model.RUN_STATUS.enable.ToString().Equals(mST_CELL_START._run_status))
                    {
                        continue;
                    }

                    if (!this._i_io.MANAGE_Create_1(this._USER, START_CELL_ID, mST_CELL_END._cell_id, out sResult, out MANAGE_ID))
                    {
                        continue;
                    }

                    bResult = this._i_io.MANAGE_SendControl(MANAGE_ID, out sResult);
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("ϵͳÒì³££¡\n" + ex.Message);
            }

            this.STORAGE_LIST_Bind();
        }

        private void txt_KeyDown(object sender, KeyEventArgs e)
        {
            if (Keys.Enter != e.KeyCode)
            {
                return;
            }

            this.STORAGE_LIST_Bind();
        }       
    }
}