using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Collections;
namespace DeiNiu.Wms.CE
{
    public partial class StockMaintain : basicForm
    {
        public StockMaintain()
        {
            InitializeComponent();
        }
      
        DataTable dt = null;
        string  djbh = "";
        int number = 0;
        string location = "";
        string loca = "";
        ArrayList ar = new ArrayList();
        int stkId = 0;
        decimal mcount = 0m;
        string goodsId = "";
        string batch = "";
        int skuId = 0;
        int ye = 0;
        int index = 0;
        private void StockMaintain_Load(object sender, EventArgs e)
        { 
            setTitle();
            showdata("");
          //  dataGrid1.DataSource = client.getIssueData("");
            DataTable dtx ;
            Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
            try
            {
                dtx =  client.getIssueData(0);
            }
            catch (Exception er)
            {
                Cursor.Current = System.Windows.Forms.Cursors.Default;
                MessageBox.Show("发生错误:" + er.Message);
                return;
            }
            Cursor.Current = System.Windows.Forms.Cursors.Default;
            bindDg(dataGrid1,dtx);
            focusOn(txtlocationId);
        }
        /// 
        /// 展示待养护信息
        /// 
        private void showdata(string locationId)
        {
            dg.DataSource = null;
            dt = new DataTable();
            DataSet ds;
            int cnt = 0;
            Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
            try
            {
                ds = client.getData4Maintain(locationId);
                cnt = Convert.ToInt16(ds.Tables[1].Rows[0][0].ToString());
            }
            catch (Exception er)
            {
                Cursor.Current = System.Windows.Forms.Cursors.Default;
                MessageBox.Show("发生错误:" + er.Message);
                return;
            }
            Cursor.Current = System.Windows.Forms.Cursors.Default;
            dt = ds.Tables[0];
         
            lbrecordcount.Text = string.Format("{0}/{1}条", dt.Rows.Count, cnt);
            if (dt.Rows.Count == 0)
                return;
          
            // dg.DataSource = dt;
           // dg1.DataSource = client.getIssueData("");
            bindDg(dg,dt);
            DataTable dtx; 
            Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
            try
            {
                dtx =client.getIssueData(0);
            }
            catch (Exception er)
            {
                Cursor.Current = System.Windows.Forms.Cursors.Default;
                MessageBox.Show("发生错误:" + er.Message);
                return;
            }
            Cursor.Current = System.Windows.Forms.Cursors.Default;
            bindDg(dg1, dtx);
            /*DataGridTableStyle ts = new DataGridTableStyle();
            ts.MappingName = dt.TableName;
            dg.TableStyles.Add(ts);
            dg.TableStyles[0].GridColumnStyles["货位"].Width = 65;
            dg.TableStyles[0].GridColumnStyles["数量"].Width = 40;
            dg.TableStyles[0].GridColumnStyles["商品"].Width = 100;
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                if (i >= 2) //如果某个条件满足就执行该列是否隐藏
                {
                    dg.TableStyles[0].GridColumnStyles[i].Width = 0;
                }
            }
            */
           
        }
        void bindDg(DataGrid dg,DataTable dt)
        {
            dg.DataSource = dt;
            DataGridTableStyle ts = new DataGridTableStyle();
            ts.MappingName = dt.TableName;
            dg.TableStyles.Clear();
            dg.TableStyles.Add(ts); 
            dg.TableStyles[0].GridColumnStyles["skuId"].Width = 50;
            dg.TableStyles[0].GridColumnStyles["货位"].Width = 70;
            dg.TableStyles[0].GridColumnStyles["数量"].Width = 40;
            dg.TableStyles[0].GridColumnStyles["商品"].Width = 100;
            dg.TableStyles[0].GridColumnStyles["厂家"].Width = 120;
            dg.TableStyles[0].GridColumnStyles["养护周期"].Width = 80;
            dg.TableStyles[0].GridColumnStyles["上次养护"].Width = 80;
            dg.TableStyles[0].GridColumnStyles["下次养护"].Width = 80;
           
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                if (i > 13) //如果某个条件满足就执行该列是否隐藏
                {
                    dg.TableStyles[0].GridColumnStyles[i].Width = 0;
                }
            }
        }
        private void dg_DoubleClick(object sender, EventArgs e)
        {
            if (dg.CurrentRowIndex > -1)
            {
                lbname.Text = dg[dg.CurrentRowIndex, 2].ToString().Trim();
                lbspec.Text = dg[dg.CurrentRowIndex, 9].ToString().Trim();
                lbbatch.Text = dg[dg.CurrentRowIndex, 3].ToString().Trim();
                lbcount.Text = dg[dg.CurrentRowIndex, 4].ToString().Trim();
                lbunit.Text = dg[dg.CurrentRowIndex, 5].ToString().Trim();
                txtlocationId.Text = dg[dg.CurrentRowIndex, 1].ToString().Trim();
                lbyxq.Text = dg[dg.CurrentRowIndex, 7].ToString().Trim();
                lbshccj.Text = dg[dg.CurrentRowIndex, 8].ToString().Trim();
                location = dg[dg.CurrentRowIndex, 1].ToString();
                stkId = Convert.ToInt32(dt.Rows[dg.CurrentRowIndex]["id"].ToString());
                mcount = Convert.ToDecimal(lbcount.Text);
                lbLoc.Text = location;
                //djbh = dg[dg.CurrentRowIndex, 10].ToString().Trim();
                //number = Convert.ToInt32(dg[dg.CurrentRowIndex, 11].ToString().Trim());
            }
        }
        /// 
        /// 养护措施
        /// 
        /// 
        private string get_yhcsh()
        {
            string yhcsh = "";
            if (cb1.Checked)
            {
                if (yhcsh == "")
                    yhcsh = cb1.Text.Trim();
                else
                    yhcsh += "," + cb1.Text.Trim();
            }
            if (cb2.Checked)
            {
                if (yhcsh == "")
                    yhcsh = cb2.Text.Trim();
                else
                    yhcsh += "," + cb2.Text.Trim();
            }
            if (cb3.Checked)
            {
                if (yhcsh == "")
                    yhcsh = cb3.Text.Trim();
                else
                    yhcsh += "," + cb3.Text.Trim();
            }
            if (cb4.Checked)
            {
                if (yhcsh == "")
                    yhcsh = cb4.Text.Trim();
                else
                    yhcsh += "," + cb4.Text.Trim();
            }
            if (cb5.Checked)
            {
                if (yhcsh == "")
                    yhcsh = cb5.Text.Trim();
                else
                    yhcsh += "," + cb5.Text.Trim();
            }
            if (cb6.Checked)
            {
                if (yhcsh == "")
                    yhcsh = cb6.Text.Trim();
                else
                    yhcsh += "," + cb6.Text.Trim();
            }
            return yhcsh;
        }
        private void clear()
        {
            row_lab.Text=lbname.Text = "";
            lbspec.Text = "";
            lbbatch.Text = "";
            lbcount.Text = "";
            lbunit.Text = "";
            txtlocationId.Text = "";
            txtycsl.Text = "0";
            lbyxq.Text = "";
            lbshccj.Text = "";
            cb1.Checked = cb2.Checked = cb3.Checked = cb4.Checked = cb5.Checked = cb6.Checked =cb7.Checked=cb8.Checked= false;
            djbh = "";
            number = 0;
            location = "";
            lbGoodsType.Text = "";
            lbbigcount.Text = "";
            lbLoc.Text = "";
            ye = 0;
            index = 0;
            up_bt.Enabled = down_bt.Enabled = false;
        }
        /// 
        /// 确认养护
        /// 
        /// 
        /// 
        private void btnOK_Click(object sender, EventArgs e)
        {
            newMaintain(); clear();
          
        }
        private void txtlocationId_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {   
              
                location = txtlocationId.Text.Trim();               
                showdata(location);  
                if (!String.IsNullOrEmpty(txtlocationId.Text))
                {   clear();
                    SetValue(0, dt);
                }
                else
                {
                    clear();
                    if (dt == null || dt.Rows.Count == 0)
                    {
                        return;
                    }
                    lbname.Text = dt.Rows[0]["商品"].ToString();
                    lbspec.Text = dt.Rows[0]["规格"].ToString();
                    lbbatch.Text = dt.Rows[0]["批号"].ToString();
                    lbcount.Text = dt.Rows[0]["数量"].ToString();
                    lbunit.Text = dt.Rows[0]["单位"].ToString();
                    txtlocationId.Text = dt.Rows[0]["货位"].ToString();
                    lbyxq.Text = dt.Rows[0]["有效期"].ToString();
                    lbshccj.Text = dt.Rows[0]["厂家"].ToString();
                    stkId = Convert.ToInt32(dt.Rows[0]["id"].ToString());
                    mcount = Convert.ToDecimal(lbcount.Text);
                    lbbigcount.Text = dt.Rows[0]["大包装"].ToString();
                    lbGoodsType.Text = dt.Rows[0]["goodstypename"].ToString();
                    txtycsl.SelectAll();
                    txtycsl.Focus();
                }
                //location = dt.Rows[0]["货位"].ToString();
                ////djbh = dt.Rows[0]["单据号"].ToString();
                //number =Convert.ToInt32(dt.Rows[0]["序号"].ToString());
            }
        }
        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)/////
        {
            Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
            try
            {
               // GetYcsl();
                //dataGrid1.DataSource = client.getIssueData("");
                if (tabControl1.SelectedIndex == 1)
                { 
                    bindDg(dg1, client.getIssueData(0));
                }
            }
            catch
            {
            }
            Cursor.Current = System.Windows.Forms.Cursors.Default;
        }
        /*
        public void GetYcsl()
        {
            double ycsl = 0;
            DataTable dt = (DataTable)dg1.DataSource;
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (dt.Rows[i][2].ToString() != null && dt.Rows[i][2].ToString() != "")
                    {
                        ycsl = ycsl + Convert.ToDouble(dt.Rows[i][2].ToString());
                    }
                }
            }
            if (Convert.ToDouble(lbcount.Text.ToString()) < ycsl)
            {
                MessageBox.Show("异常数量超过了需要养护的数量!");
                return;
            }
            txtycsl.Text = ycsl.ToString();
        }
        */
        public DataTable GetDatatable()
        {
            DataTable dt = (DataTable)dg1.DataSource;
           
            for (int i = 0; i < dt.Rows.Count;i++ )
            {
                if (dt.Rows[i][2].ToString() == "")
                {
                    dt.Rows.RemoveAt(i);
                    i--;
                }
              else  if (Convert.ToDouble(dt.Rows[i][2].ToString()) <= 0)
                {
                    dt.Rows.RemoveAt(i);
                    i--;
                }
                
            }
            return dt;
        }
        private void btn_Click(object sender, EventArgs e)///保存
        {
            //if()
            //{
            //}
           
        }
        /*
         [DllImport("Coredll.dll")]
        static extern int MapVirtualKey(uint uCode, uint uMapType);
        static char keycodetochar(Keys k)
        {
            int i = MapVirtualKey((uint)k, 2);
            char map = Convert.ToChar(i);
            return map;
        }
       
        public void KeyCodeTostrYhyc(Keys k)//捕捉不合格时键盘输入的值
        {
            int m = dg1.CurrentRowIndex;
            char ca = keycodetochar(k);
            string str = ca.ToString();
            if (Char.IsWhiteSpace(ca) || string.IsNullOrEmpty(str) || ca == '\r' || ca == '\n' || ca == keycodetochar(Keys.F1))
            {
                return;
            }
            else
            {
                if (dg1.CurrentCell.ColumnNumber == 2)
                {
                    try
                    {
                        if (ca == '\b')
                        {
                            dg1[m, 2] = "";
                            return;
                        }
                        else if (ca == '.')
                        {
                            str = ".";
                        }
                        else if (decimal.Parse(str) < 0)
                        {
                            str = "";
                            return;
                        }
                        
                    }
                    catch
                    {
                        str = "";
                        return;
                    }
                    dg1[m, 2] = dg1[m, 2] + str;
                }
               
            }
        }
        private void dg1_KeyDown(object sender, KeyEventArgs e)
        {
            Keys k = (Keys)e.KeyData;
            try
            {
                KeyCodeTostrYhyc(k);
            }
            catch
            {
            }
        }
         * */
        private void txtSkuId_KeyPress(object sender, KeyPressEventArgs e)
        {
            if(e.KeyChar==13)
            {
                try { skuId = Convert.ToInt32(txtSkuId.Text); }
                catch  
                {
                    focusOn(txtSkuId, "商品skuId");
                    return;
                }
                
              // dataGrid1.DataSource= client.getIssueData(loca);
               
                Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
                try
                {
                    bindDg(dataGrid1, client.getIssueData(skuId));
                }
                catch (Exception er)
                {
                    Cursor.Current = System.Windows.Forms.Cursors.Default;
                    MessageBox.Show("发生错误:" + er.Message);
                    
                }
                Cursor.Current = System.Windows.Forms.Cursors.Default;
                 
             
            }
        }
        private void btnUnlock_Click(object sender, EventArgs e)
        {
            bool b = false;
            Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
            try
            {
               b =  client.fixIssueData(skuId,LoginInfo.UserId,LoginInfo.Token);
            }
            catch (Exception er)
            {
                Cursor.Current = System.Windows.Forms.Cursors.Default;
                showErrorMsg(er);
            }
            Cursor.Current = System.Windows.Forms.Cursors.Default;
            if (b)
            {
                MessageBox.Show("解锁成功!!");
               
                //dataGrid1.DataSource = client.getIssueData("");
                bindDg(dataGrid1, client.getIssueData(skuId));
                skuId = 0;
                focusOn(txtSkuId);
            }
            else
            {
                MessageBox.Show("解锁失败!!");
            }
        }
        private void txtycsl_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                newMaintain();
            }
        }
        void newMaintain()
        {
            try
            {
                if (lbname.Text.Trim() == "")
                {
                    MessageBox.Show("无数据!");
                    return;
                }
                if (cb1.Checked || cb2.Checked || cb3.Checked || cb4.Checked || cb5.Checked || cb6.Checked || cb7.Checked || cb8.Checked)
                {
                    decimal ycsl = Convert.ToDecimal(txtycsl.Text.Trim());
                    if (ycsl < 0)
                    {
                        MessageBox.Show("异常数不能小于0!");
                        return;
                    }
                    if (ycsl > Convert.ToDecimal(lbcount.Text.Trim()))
                    {
                        MessageBox.Show("异常数不能大于养护数!");
                        return;
                    }
                    string yhcs = "";
                    //    string fd="否",wg="否",cs="否",js="否",sw="否",jw="否",tf="否",cc="否";
                    if (cb1.Checked)
                        yhcs = yhcs + "翻跺;";
                    if (cb2.Checked)
                        yhcs = yhcs + "外观;";
                    if (cb3.Checked)
                        yhcs = yhcs + "除湿;";
                    if (cb4.Checked)
                        yhcs = yhcs + "加湿;";
                    if (cb5.Checked)
                        yhcs = yhcs + "升温;";
                    if (cb6.Checked)
                        yhcs = yhcs + "降温;";
                    if (cb7.Checked)
                        yhcs = yhcs + "通风;";
                    if (cb8.Checked)
                        yhcs = yhcs + "除尘;";
                  //  DataTable dta = GetDatatable();
                    bool b_result; 
                    Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
                    try
                    {
                        b_result = client.newMaintain(stkId, mcount, ycsl, yhcs, LoginInfo.UserId, LoginInfo.Token);// bll.UpdateErpMaintain(location, txtycsl.Text, yhcs, Main.login_rolename, dta);
                    }
                    catch (Exception er)
                    {
                        Cursor.Current = System.Windows.Forms.Cursors.Default;
                        showErrorMsg(er);
                        //MessageBox.Show("发生错误:" + er.Message);
                        Cursor.Current = System.Windows.Forms.Cursors.Default;
                        return;
                    }
                    Cursor.Current = System.Windows.Forms.Cursors.Default;
                    clear();
                    
                    
                    if (b_result)
                    {
                        if (ye == 1 || index+1==ye)
                        {
                            showdata("");
                            clear();
                        }
                        MessageBox.Show("确认成功!");
                        txtlocationId.Focus();
                    }
                    else
                        MessageBox.Show("确认失败!");
                }
                else
                {
                    MessageBox.Show("请选择养护措施!");
                    return;
                }
            }
            catch (Exception)
            {
                MessageBox.Show("异常数格式有误!");
            }
           
        }
      /// 
        /// 上一页
        /// 
        /// 
        /// 
        private void up_bt_Click(object sender, EventArgs e)
        {
            if (index < ye)
            {
                index--;
                SetValue(index, dt);
            }
        }
        /// 
        /// 下一页
        /// 
        /// 
        /// 
        private void down_bt_Click(object sender, EventArgs e)
        {
            if (index < ye)
            {
                index++;
                SetValue(index, dt);
            }
        }
           /// 
        /// 绑定数据到界面
        /// 
        /// 
        /// 
        private void SetValue(int index0, DataTable dt)
        {
            index = index0;
            ye = dt.Rows.Count;
            row_lab.Text = String.Format("{0}/{1}页", ye > 0 ? index0 + 1 : index0, ye);
            down_bt.Enabled = up_bt.Enabled = true;
            if (ye < 2)
            {
                up_bt.Enabled = down_bt.Enabled = false;
            }
            else
            {
                if (index0 == 0)  //首页
                {
                    up_bt.Enabled = false;
                }
                else if ((index0 + 1) == ye) //最后一页
                {
                    down_bt.Enabled = false;
                }
            }
            int y = index0;
            if (dt == null || dt.Rows.Count == 0)
            {
                return;
            }
            lbname.Text = dt.Rows[y]["商品"].ToString();
            lbspec.Text = dt.Rows[y]["规格"].ToString();
            lbbatch.Text = dt.Rows[y]["批号"].ToString();
            lbcount.Text = dt.Rows[y]["数量"].ToString();
            lbunit.Text = dt.Rows[y]["单位"].ToString();
            txtlocationId.Text = dt.Rows[y]["货位"].ToString();
            lbyxq.Text = dt.Rows[y]["有效期"].ToString();
            lbshccj.Text = dt.Rows[y]["厂家"].ToString();
            stkId = Convert.ToInt32(dt.Rows[y]["id"].ToString());
            mcount = Convert.ToDecimal(lbcount.Text);
            lbbigcount.Text = dt.Rows[y]["大包装"].ToString();
            lbGoodsType.Text = dt.Rows[y]["goodstypename"].ToString();
            txtycsl.SelectAll();
            txtycsl.Focus();
            lbLoc.Text = txtlocationId.Text;
            txtlocationId.Text = "";
        }
    }
}