using RGD.DataService;
using RGD.DBUtility;
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace RGD.WCS
{
    public partial class FrmHandRepickCommand : Form
    {
        private CCommonFunction ccf = new CCommonFunction();

        public FrmHandRepickCommand()
        {
            InitializeComponent();
        }

        private void btn_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            if (button.BackColor == SystemColors.ControlLight)
            {
                button.BackColor = SystemColors.ActiveCaption;
            }
            else
            {
                button.BackColor = SystemColors.ControlLight;
            }
            compute();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (cbDeviceName.SelectedValue == null || this.cbDeviceName.Text.Trim() == "")
            {
                MessageBox.Show("请选择设备！", "操作提示：", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.cbDeviceName.Focus();
                return;
            }

            int routeIDsub = GetRouteIDsub(Convert.ToInt32(cbDeviceName.SelectedValue));

            if (routeIDsub == -1)
            {
                MessageBox.Show("您选择的设备错误！", "操作提示：", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            DataSet dss = DbHelperSQL.Query("SELECT T_Base_Device.F_DeviceKindIndex, F_DeviceCommandName,F_DeviceCommandIndex FROM " +
            "T_Base_Device , T_Base_Device_Command WHERE T_Base_Device.F_DeviceKindIndex" +
            "= T_Base_Device_Command.F_DeviceKindIndex and F_DeviceIndex=" +
            Convert.ToInt32(cbDeviceName.SelectedValue) + " and F_DeviceCommandIndex=" + (tabControl1.SelectedIndex + 1) + " and F_DeviceCommandIndex <> -1");
            if (dss.Tables[0].DefaultView.Count <= 0)
            {
                MessageBox.Show("请选择设备和对应的有效命令！", "操作提示：", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            //20101028
            string dtime = DateTime.Now.ToString("u");
            dtime = dtime.Substring(0, dtime.Length - 1);
            string qcell = string.Empty, scell = string.Empty;
            int hidx = 0;
            int hmindx = 0; int rehidx = 0;
            //20101028
            int z = 0, x = 0, y = 0, z1 = 0, x1 = 0, y1 = 0;
            //2	将取,3	将送,4	取货,5	送货
            int devicekind = BaseDeviceService.GetDeviceKindIdx(Convert.ToInt32(cbDeviceName.SelectedValue));

            //20101028
            hidx = GetManageHandIdx();
            hmindx = ccf.GetMonitorIndex(hidx, 4);
            string Sql = "insert into T_Monitor_Task(F_RouteID,F_ManageTaskIndex,F_ManageTaskKindIndex,F_MonitorIndex," +
                    "F_DeviceIndex,F_DeviceCommandIndex,F_NumParam1,F_NumParam4,F_TxtParam,F_PickUpLocation) " +
                    "values(" + routeIDsub + "," + hidx + ",4," + hmindx + "," + Convert.ToInt32(cbDeviceName.SelectedValue)
                    + "," + (tabControl1.SelectedIndex + 1) + ",0,0,'-'," + tbNUM.Text + ")";
            DbHelperSQL.ExecuteSql(Sql);

            DbHelperSQL.ExecuteSql("insert into T_Manage_Task(FID,F_ManageTaskKindIndex,F_RELATIVECONTORLID,FIntoStepOK,FSTARTDEVICE,FREMARK,FSTARTCELL,FENDDEVICE,FENDCELL,FBEGTIME) values(" +
                hidx + ",4,-1,1," + Convert.ToInt32(cbDeviceName.SelectedValue) + ",'" + (tabControl1.SelectedIndex + 1) + "','" + z.ToString() + "-" + x.ToString() + "-" + y.ToString()
                + "'," + Convert.ToInt32(cbDeviceName.SelectedValue) + ",'" + z1.ToString() + "-" + x1.ToString() + "-" + y1.ToString() + "','" + dtime + "')");

            if (rehidx > 0)
            {
                DbHelperSQL.ExecuteSql("update T_Manage_Task set F_RELATIVECONTORLID=" + rehidx + " where F_ManageTaskKindIndex=4 and FID=" + hidx);
                DbHelperSQL.ExecuteSql("update T_Manage_Task set F_RELATIVECONTORLID=" + hidx + " where F_ManageTaskKindIndex=4 and FID=" + rehidx);
            }
            RecordMaxHandTaskFID(hidx);
        }

        private int GetRouteIDsub(int device)
        {
            DataView dv = DbHelperSQL.Query("SELECT F_RouteIDSub, F_DeviceIndex, F_RouteID FROM T_Base_Route_Device where F_DeviceIndex=" + device + "").Tables[0].DefaultView;
            if (dv.Count > 0)
            {
                return Convert.ToInt32(dv[0]["F_RouteIDSub"]);
            }
            else
            {
                return -1;
            }
        }

        private int GetManageHandIdx()
        {
            DataSet ds = DbHelperSQL.Query("SELECT F_ManageTaskKindIndex, max(FID) as mFID FROM T_Manage_Task Where F_ManageTaskKindIndex=4 group by F_ManageTaskKindIndex");
            if (ds.Tables[0].DefaultView.Count > 0)
            {
                return (Convert.ToInt32(ds.Tables[0].DefaultView[0]["mFID"]) + 1);
            }
            else
            {
                DataSet dss = DbHelperSQL.Query("SELECT F_ManageTaskIndex FROM T_Base_Manage_Task_Index_Hand_Task");
                if (dss.Tables[0].DefaultView.Count > 0)
                {
                    if ((Convert.ToInt32(dss.Tables[0].DefaultView[0]["F_ManageTaskIndex"]) + 1) >= 29998)
                    {
                        return 20001;
                    }
                    else
                    {
                        return (Convert.ToInt32(dss.Tables[0].DefaultView[0]["F_ManageTaskIndex"]) + 1);
                    }
                }
                else
                {
                    return 20001;
                }
            }
        }

        private void RecordMaxHandTaskFID(int fid)
        {
            DataSet ds = DbHelperSQL.Query("select F_ManageTaskIndex from T_Base_Manage_Task_Index_Hand_Task");
            DataView dv = ds.Tables[0].DefaultView;
            if (dv.Count > 0)
            {
                if (fid == 29998)
                {
                    DbHelperSQL.ExecuteSql("UPDATE T_Base_Manage_Task_Index_Hand_Task SET F_ManageTaskIndex =20001");
                    return;
                }
                if (fid > Convert.ToInt32(dv[0]["F_ManageTaskIndex"]))
                {
                    DbHelperSQL.ExecuteSql("UPDATE T_Base_Manage_Task_Index_Hand_Task SET F_ManageTaskIndex =" + fid);
                    return;
                }
            }
            else
            {
                DbHelperSQL.ExecuteSql("INSERT INTO T_Base_Manage_Task_Index_Hand_Task (F_ManageTaskIndex)VALUES (" + fid + ")");
                return;
            }
        }

        private void FrmHandRepickCommand_Load(object sender, EventArgs e)
        {
            DataView dv = DbHelperSQL.Query("select F_DeviceIndex,F_DeviceName from T_Base_Device where F_DeviceKindIndex=32").Tables[0].DefaultView;
            if (dv.Count > 0)
            {
                this.cbDeviceName.DisplayMember = "F_DeviceName";
                this.cbDeviceName.ValueMember = "F_DeviceIndex";
                this.cbDeviceName.DataSource = dv;

                this.cbDeviceName.Text = "";
                this.cbDeviceName.SelectedIndex = 0;
            }
        }

        private void compute()
        {
            int H, L;
            if (tabControl1.SelectedIndex == 0)
            {
                L = I(D1) + I(D2) * 2 + I(D3) * 4 + I(D4) * 8 + I(D5) * 16 + I(D6) * 32 + I(D7) * 64 + I(D8) * 128;
                H = I(D9) + I(D10) * 2 + I(D11) * 4 + I(D12) * 8;
            }
            else
            {
                L = I(S1) + I(S2) * 2 + I(S3) * 4 + I(S4) * 8;
                H = 0;
            }
            tbDEC.Text = H + " " + L;
            tbNUM.Text = (H * 256 + L).ToString();
            tbBIN.Text = Convert.ToString(H, 2).PadLeft(8, '0') + " " + Convert.ToString(L, 2).PadLeft(8, '0');
        }

        private int I(Button button)
        {
            if (button.BackColor == SystemColors.ControlLight)
                return 0;
            else
                return 1;
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Clear();
            compute();
        }

        private void Clear()
        {
            D1.BackColor = D2.BackColor = D3.BackColor = D4.BackColor
                = D5.BackColor = D6.BackColor = D7.BackColor = D8.BackColor
                = D9.BackColor = D10.BackColor = D11.BackColor = D12.BackColor
                = S1.BackColor = S2.BackColor = S3.BackColor = S4.BackColor = SystemColors.ControlLight;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Clear();
        }
    }
}