using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DBFactory;
namespace wcfControlMonitorClient
{
    /// <summary>
    /// 20110906新增入库任务双叉关联设置
    /// </summary>
    public partial class FrmRelativeIDSet : Form
    {
        DBOperator dbo = CStaticClass.dbo;
        //DBOperator dboM = CStaticClass.dboM;
        private static FrmRelativeIDSet _formInstance;

        public static FrmRelativeIDSet FormInstance
        {
            get
            {
                if (_formInstance == null)
                {
                    _formInstance = new FrmRelativeIDSet();
                }
                return _formInstance;
            }
            set { _formInstance = value; }
        }
        public FrmRelativeIDSet()
        {
            InitializeComponent();

            _formInstance = this;

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            int flag = 0;//0表示双叉关联任务设置
            if(rbnNearForkSet.Checked)//获取radiobutton选择状态
            {
                flag = 1;

            }
            else
            if (rbnFarForkSet.Checked)
            {
                flag = 2;

            }

            if (cbBType.Text.Trim().Length <= 0)
            {
                MessageBox.Show("请选择任务类型!", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (cbBFloor.Text.Trim().Length <= 0)
            {
                MessageBox.Show("请选择任务所在楼层!", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (cbBLane.Text.Trim().Length <= 0)
            {
                MessageBox.Show("请选择任务所在巷道!", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if(flag!=1)
            {
                if (cbBFarID.Text.Trim().Length <= 0)
                {
                    MessageBox.Show("请选择远叉调度任务索引!", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }
            if (flag != 2)
            {
                if (cbBNearID.Text.Trim().Length <= 0)
                {
                    MessageBox.Show("请选择近叉调度任务索引!", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }

            //设置时1.检测任务是否存在,2.不能关联自己,3.提示设置错误的后果
            int FarID = 0, NearID = 0;
             int Type = cbBType.SelectedIndex + 1;
             int Floor = cbBFloor.SelectedIndex + 1;
             string Lane = string.Format("1800{0}", cbBLane.SelectedIndex + 1);

             string str1=string.Empty, str2=string.Empty;
            if(flag!=1)
            {

                FarID = Convert.ToInt32(cbBFarID.Text.Trim());//远叉任务号
                str1 = string.Format("调度任务: {0} 设置为远叉执行 ", FarID);
            }
            if (flag != 2)
            {
                NearID = Convert.ToInt32(cbBNearID.Text.Trim());//近叉任务号
                str2 = string.Format("调度任务: {0} 设置为近叉执行 ", NearID);
            }

            string info = string.Format("确认将{0}{1}吗?\r\n\r\n谨慎设置,设置错误将导致任务错乱!!!", str1, str2);


            //判断近叉任务号与远叉任务号是否相同
            if(FarID==NearID)
            {
                MessageBox.Show("远叉调度任务索引与近叉调度任务索引不能相同!", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            //判断远叉任务是否存在
            string sql = string.Format("select FID from T_Manage_Task where ( FCONTROLTASKTYPE = {0} ) And ((FCONTROLTASKTYPE=1 and FSTARTDEVICE like '{1}_%'   and FENDDEVICE like '{2}' ) or (FCONTROLTASKTYPE=2 and FENDDEVICE like '{3}_%' and FSTARTDEVICE like '{4}' ) And ( FID = {5}))", Type, Floor, Lane, Floor, Lane,FarID);

           // string sql = string.Format("select FID from T_Manage_Task where ( FCONTROLTASKTYPE = 1 ) and ( FSTARTDEVICE like '{0}_%' ) and ( FENDDEVICE like '{1}' ) and  FID = {2}", Floor, Lane, FarID);
            DataView dvs = dbo.ExceSQL(sql).Tables[0].DefaultView;

            if (dvs.Count <= 0)
            {
                MessageBox.Show("满足条件的远叉调度任务索引不存在!", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;

            }
            //判断近叉任务是否存在
             sql = string.Format("select FID from T_Manage_Task where ( FCONTROLTASKTYPE = {0} ) And ((FCONTROLTASKTYPE=1 and FSTARTDEVICE like '{1}_%'   and FENDDEVICE like '{2}' ) or (FCONTROLTASKTYPE=2 and FENDDEVICE like '{3}_%' and FSTARTDEVICE like '{4}' ) And ( FID = {5}))", Type, Floor, Lane, Floor, Lane, NearID);
            dvs = dbo.ExceSQL(sql).Tables[0].DefaultView;

            if (dvs.Count <= 0)
            {
                MessageBox.Show("满足条件的近叉调度任务索引不存在!", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;

            }
            //CStaticClass.WcfControl.BeginModifyManageTaskFork(mti, fid, Model.CGeneralFunction.TASKFINISH, new AsyncCallback(ModifyManageTaskCallBack), null);

             if (MessageBox.Show(info, "操作提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
             {
                 CStaticClass.WcfControl.BeginModifyManageTaskFork(flag, FarID, NearID, new AsyncCallback(ModifyManageTaskForkCallBack), null);
             }

           ////提示并设置关联任务
           // string info = string.Format("确认设置远叉任务: {0} 与 近叉任务: {1} 为双叉关联任务吗?\r\n\r\n谨慎设置,设置错误将导致任务错乱!!!",FarID,NearID);
           // if (MessageBox.Show(info, "操作提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
           // {
           //     //设置调度任务的关联任务,并指定货叉   
           //     //设置设备指令队列中的货叉
           //     //指定远叉
           //     string managesql = string.Format("update T_Manage_Task set FUseAwayFork='1',F_RELATIVECONTORLID={0} where FID ={1}",NearID,FarID);
           //     string monitorsql = string.Format("update T_Monitor_Task set F_UseAwayFork='1' where F_ManageTaskIndex= {0}", FarID);
           //     dbo.ExecuteSql(managesql);
           //     dbo.ExecuteSql(monitorsql);


           //     //指定近叉

           //     managesql = string.Format("update T_Manage_Task set FUseAwayFork='0',F_RELATIVECONTORLID={0} where FID = {1}",  FarID,NearID);
           //     monitorsql = string.Format("update T_Monitor_Task set F_UseAwayFork='0' where F_ManageTaskIndex= {0}", NearID);
           //     dbo.ExecuteSql(managesql);
           //     dbo.ExecuteSql(monitorsql);

           // }

                    
        }

        void ModifyManageTaskForkCallBack(IAsyncResult ar)
        {
            string errtext = string.Empty;
            if (CStaticClass.WcfControl.EndModifyManageTaskFork(out errtext, ar) == false)
            {
                MessageBox.Show(errtext, "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }
        private void FrmRelativeIDSet_Load(object sender, EventArgs e)
        {

        }

        private void cbBLane_SelectedIndexChanged(object sender, EventArgs e)
        {
            int Type = cbBType.SelectedIndex + 1;
            int Floor=cbBFloor.SelectedIndex+1;
            string Lane=string.Format("1800{0}",cbBLane.SelectedIndex+1);

            string sql = string.Format("select FID from T_Manage_Task where ( FCONTROLTASKTYPE = {0} ) And ((FCONTROLTASKTYPE=1 and FSTARTDEVICE like '{1}_%'   and FENDDEVICE like '{2}' ) or (FCONTROLTASKTYPE=2 and FENDDEVICE like '{3}_%' and FSTARTDEVICE like '{4}' ))", Type, Floor, Lane, Floor, Lane);
            DataView dvs = dbo.ExceSQL(sql).Tables[0].DefaultView;

            cbBFarID.ValueMember = "FID";
            cbBFarID.DisplayMember = "FID";
            cbBFarID.DataSource = dvs;


            DataView dvs1 = dvs.ToTable().DefaultView;

            cbBNearID.ValueMember = "FID";
            cbBNearID.DisplayMember = "FID";
            cbBNearID.DataSource = dvs1;

        }

        private void button2_Click(object sender, EventArgs e)
        {
            cbBLane_SelectedIndexChanged(null, EventArgs.Empty);
        }

        private void cbBFloor_SelectedIndexChanged(object sender, EventArgs e)
        {

            cbBLane.SelectedIndex = -1;
            //cbBLane.Text = "";
            //cbBFarID.Text = "";
            //cbBNearID.Text = "";
        }

        private void rbnOneForkSet_Click(object sender, EventArgs e)
        {
            gbFloor.Visible = false;
            gbLane.Visible = false;
        }

        private void rbnTwoForkSet_Click(object sender, EventArgs e)
        {
            gbNearFork.Visible = true;
            gbFarFork.Visible = true;
        }

        private void rbnOneForkSet_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void rbnFarForkSet_Click(object sender, EventArgs e)
        {
            gbNearFork.Visible = false;
            gbFarFork.Visible = true;
        }

        private void rbnNearForkSet_Click(object sender, EventArgs e)
        {
            gbNearFork.Visible = true;
            gbFarFork.Visible = false; ;
        }


    }
}