using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DBFactory;
using Microsoft.VisualBasic;
using System.ServiceModel;

namespace wcfControlMonitorClient
{
    public partial class FrmAutoOutTasks : Form
    {
        DBOperator dbo = CStaticClass.dbo;

        private static FrmAutoOutTasks _formInstance;

        public static FrmAutoOutTasks FormInstance
        {
            get
            {
                if (_formInstance == null)
                {
                    _formInstance = new FrmAutoOutTasks();
                }
                return _formInstance;
            }
            set { _formInstance = value; }
        }
        public FrmAutoOutTasks()
        {
            InitializeComponent();
            DataView dvs1 = dbo.ExceSQL("SELECT DISTINCT F_EndDevice,F_DeviceName FROM T_Base_Route,T_Base_Device where F_EndDevice = F_DeviceIndex and T_Base_Route.F_RouteKind = 2 order by F_EndDevice").Tables[0].DefaultView;
            //cbendposition
            cbendposition.ValueMember = "F_EndDevice";
            cbendposition.DisplayMember = "F_DeviceName";
            cbendposition.DataSource = dvs1;
        }

        private void buttonouttask_Click(object sender, EventArgs e)
        {
            int i = 0;
            if (MessageBox.Show("您确认要增加自动任务吗?", "操作提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
            {
                return;
            }

            if (this.cbendposition.Text.Trim().Length == 0)
            {
                MessageBox.Show("终点位置不允许保持空值!", "误操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (this.tbcellcodes.Text.Trim().Length == 0)
            {
                MessageBox.Show("货位编码不允许保持空值!", "误操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            string[] xy = this.tbcellcodes.Text.Trim().Split(';');
            string[] barcodes = new string[xy.Length];
            string[] startdevices = new string[xy.Length];
             
            for (i = 0; i < xy.Length; i++)
            {
                DataView dv = dbo.ExceSQL("select * from ST_CELL t where fcellcode = '" + xy[i] + "'").Tables[0].DefaultView;
                if (dv.Count == 0)
                {
                    MessageBox.Show("无法找到起点货位" + xy[i], "误操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                else
                {
                    startdevices[i] = dv[0]["flaneway"].ToString();
                    if ((dv[0]["fpalletbarcode"].ToString() != "-") && (dv[0]["fpalletbarcode"].ToString() != ""))
                    {
                        barcodes[i] = dv[0]["fpalletbarcode"].ToString();
                    }
                    else
                    {
                        barcodes[i] = "999" + xy[i].Substring(0,2) + xy[i].Substring(3, 2) + xy[i].Substring(6, 2);
                    }
                }
            }
            //调用服务
            for (i = 0; i < xy.Length; i++)
            {
                Model.AutoTask at = new Model.AutoTask(2, barcodes[i], startdevices[i], xy[i], cbendposition.SelectedValue.ToString(), "", true, false, 0,0,0);
                CStaticClass.WcfControl.BeginBuildAutoTask(at, null, at);
            }
            MessageBox.Show("下发成功!", "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

        private void FrmAutoOutTasks_FormClosing(object sender, FormClosingEventArgs e)
        {
            _formInstance = null;
        }
    }
}