using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DataLinkMesWork;
using System.Security.Cryptography;
//using SystemFramework;

namespace bizFacade
{
    public class ando_matertial
    {

        /// <summary>
        /// 物料ando明细查询 ANDON_类型_编号_查询
        /// </summary>
        /// <param name="tb"></param>
        /// <returns></returns>
        public static bool andoListSelect(string opname, string andoTypeCode, out DataTable tb)
        {
            string procedureName = "ANDON_类型_编号_查询";
            //string connectionString = ApplicationConfig.ConnectionString;
            DataSet ds;
            string errorMessage;
            bool isOK;
            tb = null;
            SqlParameter[] sqlParameters = new SqlParameter[2];
            sqlParameters[0] = new System.Data.SqlClient.SqlParameter("@安东类型代码", andoTypeCode);
            sqlParameters[1] = new System.Data.SqlClient.SqlParameter("@工位号", opname);
            isOK = DataAccess.ExecuteStoredProcedure(procedureName, ref sqlParameters, out  ds);
            if (isOK)
            {
                if (ds.Tables.Count > 0)
                {
                    tb = ds.Tables[0];
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// ANDON_呼叫状态_解除(返回table)
        /// </summary>
        /// <param name="opname">工位号</param>
        /// <param name="number">具体button编号</param>
        /// <param name="andoTypeCode">安东类型代码</param>
        /// <param name="andoListCode">安东明细代码(补料,错料,不合格)</param>
        /// <param name="callInfo">呼叫内容</param>
        /// <returns></returns>
        public static bool AndoMaterialResetOutTable(string opname, string number, string andoTypeCode, string andoListCode, string callInfo,out DataTable tb)
        {
            string procedureName = "ANDON_呼叫状态_解除";
            //string connectionString = ApplicationConfig.ConnectionString;
            DataSet ds;
            string errorMessage;
            bool isOK;
            tb = null;
            SqlParameter[] sqlParameters = new SqlParameter[5];
            sqlParameters[0] = new System.Data.SqlClient.SqlParameter("@工位号", opname);
            sqlParameters[0].Direction = ParameterDirection.Input;
            sqlParameters[1] = new System.Data.SqlClient.SqlParameter("@编号", number);
            sqlParameters[1].Direction = ParameterDirection.Input;
            sqlParameters[2] = new System.Data.SqlClient.SqlParameter("@安东类型代码", andoTypeCode);
            sqlParameters[2].Direction = ParameterDirection.Input;
            sqlParameters[3] = new System.Data.SqlClient.SqlParameter("@安东明细代码", andoListCode);
            sqlParameters[3].Direction = ParameterDirection.Input;
            sqlParameters[4] = new System.Data.SqlClient.SqlParameter("@解除内容", callInfo);
            sqlParameters[4].Direction = ParameterDirection.Input;
            isOK = DataAccess.ExecuteStoredProcedure(procedureName, ref sqlParameters, out  ds);
            if (isOK)
            {
                if (ds.Tables.Count > 0)
                {
                    tb = ds.Tables[0];
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// ANDON_呼叫状态_解除
        /// </summary>
        /// <param name="opname"></param>
        /// <param name="number"></param>
        /// <param name="andoTypeCode"></param>
        /// <param name="andoListCode"></param>
        /// <param name="callInfo"></param>
        /// <returns></returns>
        public static bool AndoMaterialReset(string opname, string number, string andoTypeCode, string andoListCode, string callInfo)
        {

            string procedureName = "ANDON_呼叫状态_解除";
            //string connectionString = ApplicationConfig.ConnectionString;
            string errorMessage;
            SqlParameter[] sqlParameters = new SqlParameter[5];
            sqlParameters[0] = new System.Data.SqlClient.SqlParameter("@工位号", opname);
            sqlParameters[0].Direction = ParameterDirection.Input;
            sqlParameters[1] = new System.Data.SqlClient.SqlParameter("@编号", number);
            sqlParameters[1].Direction = ParameterDirection.Input;
            sqlParameters[2] = new System.Data.SqlClient.SqlParameter("@安东类型代码", andoTypeCode);
            sqlParameters[2].Direction = ParameterDirection.Input;
            sqlParameters[3] = new System.Data.SqlClient.SqlParameter("@安东明细代码", andoListCode);
            sqlParameters[3].Direction = ParameterDirection.Input;
            sqlParameters[4] = new System.Data.SqlClient.SqlParameter("@解除内容", callInfo);
            sqlParameters[4].Direction = ParameterDirection.Input;
            bool isOK;
            isOK = DataAccess.ExecuteStoredProcedure(procedureName, ref sqlParameters);
            if (isOK)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// ANDON_呼叫状态_呼叫(物料ando插入)
        /// </summary>
        /// <param name="opname"></param>
        /// <param name="andoTypeCode"></param>
        /// <param name="andoListCode"></param>
        /// <param name="number"></param>
        /// <param name="callInfo"></param>
        /// <param name="callCount"></param>
        /// <param name="cardNumber"></param>
        /// <param name="noNumber"></param>
        /// <returns></returns>
        public static bool andoMaterialCall(string opname, string andoTypeCode, string andoListCode, string number, string callInfo, int callCount, string cardNumber, string noNumber)
        {

            string procedureName = "ANDON_呼叫状态_呼叫";
            //string connectionString = ApplicationConfig.ConnectionString;
            string errorMessage;
            SqlParameter[] sqlParameters = new SqlParameter[8];
            sqlParameters[0] = new System.Data.SqlClient.SqlParameter("@工位号", opname);
            sqlParameters[0].Direction = ParameterDirection.Input;
            sqlParameters[1] = new System.Data.SqlClient.SqlParameter("@安东类型代码", andoTypeCode);
            sqlParameters[1].Direction = ParameterDirection.Input;
            sqlParameters[2] = new System.Data.SqlClient.SqlParameter("@安东明细代码", andoListCode);
            sqlParameters[2].Direction = ParameterDirection.Input;
            sqlParameters[3] = new System.Data.SqlClient.SqlParameter("@编号", number);
            sqlParameters[3].Direction = ParameterDirection.Input;
            sqlParameters[4] = new System.Data.SqlClient.SqlParameter("@呼叫内容", callInfo);
            sqlParameters[4].Direction = ParameterDirection.Input;
            sqlParameters[5] = new System.Data.SqlClient.SqlParameter("@呼叫数量", callCount);
            sqlParameters[5].Direction = ParameterDirection.Input;
            sqlParameters[6] = new System.Data.SqlClient.SqlParameter("@卡片条码", cardNumber);
            sqlParameters[6].Direction = ParameterDirection.Input;
            sqlParameters[7] = new System.Data.SqlClient.SqlParameter("@不合格代码", noNumber);
            sqlParameters[7].Direction = ParameterDirection.Input;
            bool isOK;
            isOK = DataAccess.ExecuteStoredProcedure(procedureName, ref sqlParameters);
            if (isOK)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// ANDON_呼叫状态_呼叫(物料ando插入,返回table)
        /// </summary>
        /// <param name="opname">工位号</param>
        /// <param name="andoTypeCode">安东类型代码</param>
        /// <param name="andoListCode">安东明细代码(缺料,补料,不合格)</param>
        /// <param name="number">编号(具体物料信息)</param>
        /// <param name="callInfo"></param>
        /// <param name="callCount"></param>
        /// <param name="cardNumber"></param>
        /// <param name="noNumber"></param>
        /// <param name="tb"></param>
        /// <returns></returns>
        public static bool andoMaterialCallOutTable(string opname, string andoTypeCode,string andoListCode,string number,string callInfo,int callCount,string cardNumber,string noNumber,out DataTable tb)
        {
            string procedureName = "ANDON_呼叫状态_呼叫";
            //string connectionString = ApplicationConfig.ConnectionString;
            DataSet ds;
            string errorMessage;
            bool isOK;
            tb = null;
            SqlParameter[] sqlParameters = new SqlParameter[8];
            sqlParameters[0] = new System.Data.SqlClient.SqlParameter("@工位号", opname);
            sqlParameters[0].Direction = ParameterDirection.Input;
            sqlParameters[1] = new System.Data.SqlClient.SqlParameter("@安东类型代码", andoTypeCode);
            sqlParameters[1].Direction = ParameterDirection.Input;
            sqlParameters[2] = new System.Data.SqlClient.SqlParameter("@安东明细代码", andoListCode);
            sqlParameters[2].Direction = ParameterDirection.Input;
            sqlParameters[3] = new System.Data.SqlClient.SqlParameter("@编号", number);
            sqlParameters[3].Direction = ParameterDirection.Input;
            sqlParameters[4] = new System.Data.SqlClient.SqlParameter("@呼叫内容", callInfo);
            sqlParameters[4].Direction = ParameterDirection.Input;
            sqlParameters[5] = new System.Data.SqlClient.SqlParameter("@呼叫数量", callCount);
            sqlParameters[5].Direction = ParameterDirection.Input;
            sqlParameters[6] = new System.Data.SqlClient.SqlParameter("@卡片条码", cardNumber);
            sqlParameters[6].Direction = ParameterDirection.Input;
            sqlParameters[7] = new System.Data.SqlClient.SqlParameter("@不合格代码", noNumber);
            sqlParameters[7].Direction = ParameterDirection.Input;
            isOK = DataAccess.ExecuteStoredProcedure(procedureName, ref sqlParameters, out  ds);
            if (isOK)
            {
                if (ds.Tables.Count > 0)
                {
                    tb = ds.Tables[0];
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 创建物料andon
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string CreateAndo_Material(string opname,string AndoTypeCode)
        {
            System.Data.DataTable dt_list;
            //查询ando明细
            andoListSelect(opname, AndoTypeCode, out dt_list);


            StringBuilder sb = new StringBuilder();
            //if (dt_list != null)
            //{
            //    if (dt_list.Rows.Count > 0)
            //    {
                    sb.Append("<div id=\"div_ando_first_main\" class=\"div_ando_first_main\">");
                    sb.Append("<div id=\"div_ando_first\" class=\"div_ando_first\">");
                    sb.Append("<div style=\"width:96%;height:92%;margin-left:10px;margin-top:10px;\">");

                    #region 大图片的div
                    sb.Append("<div style=\"width:8%;height:100%;float:left;\">");
                    sb.Append("<img alt=\"loading....\" src=\"../images/materiracall2.png\" width=\"92%\" height=\"100%\" />");
                    sb.Append("</div>");
                    #endregion

                    #region 整个ANDON选项DIV
                    sb.Append("<div style=\"width:70%;height:100%;float:left;\">");

                    #region 左按钮DIV
                    sb.Append("<div style=\"width:10%;height:100%;float:left;\">");
                    sb.Append("<input type=\"button\" style=\"width:100%;height:100%;text-align:center;font-size:30px;\" value=\"<<\" onclick=\"RightMove()\" />");
                    sb.Append("</div>");
                    #endregion


                    #region 中间内容DIV
                    sb.Append("<div style=\"width:80%;height:100%;float:left;\">");

                    #region 可支持移动的div

                    sb.Append("<div style=\"width:100%;height:100%; overflow:hidden;position:relative;\" id=\"div_andon_table_material\">");

                    sb.Append("<div id=\"div_Scroll\" style=\"width:100%;height:100%; position:relative;\" >");

                    
                    //附加tabel
                    string tabelString=CreatAndo_Material_table(opname,AndoTypeCode);
                    sb.Append(tabelString);

                    sb.Append("</div>");
                    sb.Append("</div>");
                    #endregion

                    sb.Append("</div>");
                    #endregion

                    #region 右按钮div

                    sb.Append("<div style=\"width:10%;height:100%;float:left;\">");

                    sb.Append("<input type=\"button\" style=\"width:100%;height:100%;text-align:center;font-size:30px;\" value=\">>\"  onclick=\"leftMove()\" />");
                    sb.Append("</div>");

                    #endregion

                    sb.Append("</div>");
                    #endregion
                    #region ando按钮div

                    sb.Append("<div style=\"width:22%;height:100%;float:left;\">");

                    sb.Append("<div style=\"width: 100%; height: 100%; margin: 30px 0px 0px -5px;\">");

                    sb.Append("<table style=\"width: 100%;\">");
                    sb.Append("<tr>");
                    sb.Append("<td align=\"left\">");
                    sb.Append("<img onclick=\"andon_call_down('m_call')\" alt=\"loading...\" id='m_call' src=\"../images/call.png\" style=\"width: 100px; height: 80px\"/>");
                    sb.Append("</td>");
                    sb.Append("<td align=\"right\">");
                    sb.Append("<img onclick=\"andon_reset_down('m_reset')\" alt=\"loading...\" id='m_reset' src=\"../images/reset.png\" style=\"width: 100px;height: 80px\" />");
                    sb.Append("</td>");
                    sb.Append("</tr>");
                    sb.Append("<tr>");
                    sb.Append("<td style=\"padding-top: 20px\" align=\"center\">");
                    sb.Append("<label style=\"padding-top: 20px;font-size:18px;\">呼 叫</label>");
                    sb.Append("</td>");
                    sb.Append("<td style=\"padding-top: 20px\" align=\"center\">");
                    sb.Append("<label style=\"padding-top: 20px;font-size:18px;\">复 位</label>");
                    sb.Append("</td>");
                    sb.Append("</tr>");
                    sb.Append("</table>");

                    sb.Append("</div>");

                    sb.Append("</div>");

                    #endregion

                    sb.Append("</div></div></div>");

            //    }
              
            //}
          return sb.ToString();
        }
        /// <summary>
        /// 更新物料安东的中间的tabel
        /// </summary>
        /// <param name="opname">工位号</param>
        /// <param name="AndoTypeCode">物料ando类型明细</param>
        /// <returns></returns>
        public static string CreatAndo_Material_table(string opname, string AndoTypeCode)
        {
            System.Data.DataTable dt_list;
            //查询ando明细
            andoListSelect(opname, AndoTypeCode, out dt_list);
            StringBuilder sb = new StringBuilder();
            int tableCount = dt_list.Rows.Count;
            int td_count = (tableCount / 2) + (tableCount % 2);//获取每个tr中应该创建多少个td
            int tablewidth = 145 * td_count;
            if (tablewidth == 0)
            {
                tablewidth = 10;
            }
            string tablewidth2 = Convert.ToString(tablewidth) + "px";

            #region table

            sb.Append("<table id=\"table_Scroll\" style=\"width:" + tablewidth2 + ";height:100%;left:0px;\">");

            //创建第一行
            sb.Append("<tr>");
            for (int i = 0; i < td_count; i++)
            {
                #region 第一行按钮
                sb.Append("<td style=\"height:50%;width:145px;\">");
                //标签id
                string id = "button_ando_material_" + dt_list.Rows[i]["编号"].ToString();

                #region 颜色代码
                //标签值
                string value = dt_list.Rows[i]["编号描述2"].ToString();
                string color = dt_list.Rows[i]["颜色代码"].ToString();
                //string inputColor = "background-color:" + color + "";
                if (color == "0")
                {
                    sb.Append("<input type=\"button\" onclick=\"andon_material(id)\" id=\"" + id + "\" value=\"" + value + "\" style=\"width: 140px; height:100%;\"/>");
                }
                if (color == "1")
                {
                    sb.Append("<input type=\"button\" onclick=\"andon_material(id)\" id=\"" + id + "\" value=\"" + value + "\" style=\"width: 140px; height:100%;background-color:pink;\"/>");
                }
                if (color == "2")
                {
                    sb.Append("<input type=\"button\" onclick=\"andon_material(id)\" id=\"" + id + "\" value=\"" + value + "\" style=\"width: 140px; height:100%;background-color:yellow;\"/>");
                }
                #endregion

                sb.Append("</td>");

                #endregion
            }
            sb.Append("</tr>");


            //创建第二行
            sb.Append("<tr>");
            for (int i = td_count; i < tableCount; i++)
            {
                #region 第二行按钮
                sb.Append("<td style=\"height:50%;width:145px;\">");
                //标签id
                string id = "button_ando_material_" + dt_list.Rows[i]["编号"].ToString();

                #region 颜色代码
                //标签值
                string value = dt_list.Rows[i]["编号描述2"].ToString();
                string color = dt_list.Rows[i]["颜色代码"].ToString();
                //string inputColor = "background-color:" + color + "";
                if (color == "0")
                {
                    sb.Append("<input type=\"button\" onclick=\"andon_material(id)\" id=\"" + id + "\" value=\"" + value + "\" style=\"width: 140px; height:100%;\"/>");
                }
                if (color == "1")
                {
                    sb.Append("<input type=\"button\" onclick=\"andon_material(id)\" id=\"" + id + "\" value=\"" + value + "\" style=\"width: 140px; height:100%;background-color:pink;\"/>");
                }
                if (color == "2")
                {
                    sb.Append("<input type=\"button\" onclick=\"andon_material(id)\" id=\"" + id + "\" value=\"" + value + "\" style=\"width: 140px; height:100%;background-color:yellow;\"/>");
                }
                #endregion

                sb.Append("</td>");

                #endregion
            }

            //补充第二行的信息
            if ((tableCount % 2) > 0 && (tableCount > 0))
            {
                sb.Append("<td style=\"height:50%\">");
                sb.Append("</td>");
            }
            sb.Append("</tr>");

            //最后组合成table
            sb.Append("</table>");
            return sb.ToString();


            //    if (tableCount > 4)
            //    {
            //        sb.Append("<tr>");
            //        int i = 0;
            //        for (i = 0; i < dt_list.Rows.Count; i++)
            //        {
            //            //总条数
            //            int seecond = dt_list.Rows.Count;
            //            //总条数和2取余
            //            int seecond1 = seecond % 2;
            //            if ((seecond % 2) == 1)
            //            {
            //                seecond = seecond / 2 + 1;
            //            }
            //            else
            //            {
            //                seecond = dt_list.Rows.Count / 2;
            //            }
            //            if (i <= seecond)
            //            {
            //                #region 第一行按钮


            //                sb.Append("<td style=\"height:50%\">");
            //                //标签id
            //                string id = "button_ando_material_" + dt_list.Rows[i]["编号"].ToString();

            //                #region 颜色代码
            //                //标签值
            //                string value = dt_list.Rows[i]["编号描述2"].ToString();
            //                string color = dt_list.Rows[i]["颜色代码"].ToString();
            //                //string inputColor = "background-color:" + color + "";
            //                if (color == "0")
            //                {
            //                    sb.Append("<input type=\"button\" onclick=\"andon_material(id)\" id=\"" + id + "\" value=\"" + value + "\" style=\"width: 140px; height:100%;\"/>");
            //                }
            //                if (color == "1")
            //                {
            //                    sb.Append("<input type=\"button\" onclick=\"andon_material(id)\" id=\"" + id + "\" value=\"" + value + "\" style=\"width: 140px; height:100%;background-color:pink;\"/>");
            //                }
            //                if (color == "2")
            //                {
            //                    sb.Append("<input type=\"button\" onclick=\"andon_material(id)\" id=\"" + id + "\" value=\"" + value + "\" style=\"width: 140px; height:100%;background-color:yellow;\"/>");
            //                }
            //                #endregion

            //                sb.Append("</td>");

            //                #endregion

            //            }
            //        }
            //        sb.Append("</tr>");
            //        sb.Append("<tr>");
            //        for (i = 0; i < dt_list.Rows.Count; i++)
            //        {
            //            //如果第一行超过一半的按钮就重新放一行
            //            int second1 = dt_list.Rows.Count;
            //            if ((second1) % 2 == 1)
            //            {
            //                second1 = second1 / 2 + 1;

            //            }
            //            else
            //            {
            //                second1 = dt_list.Rows.Count / 2;
            //            }
            //            if (i > second1)
            //            {
            //                #region 第二行按钮


            //                sb.Append("<td style=\"height:50%\">");
            //                //标签id
            //                string id = "button_ando_material_" + dt_list.Rows[i]["编号"].ToString();

            //                #region 颜色代码

            //                //标签值
            //                string value = dt_list.Rows[i]["编号描述2"].ToString();
            //                string color = dt_list.Rows[i]["颜色代码"].ToString();
            //                //string inputColor = "background-color:" + color + "";
            //                if (color == "0")
            //                {
            //                    sb.Append("<input type=\"button\" onclick=\"andon_material(id)\" id=\"" + id + "\" value=\"" + value + "\" style=\"width: 140px; height:100%;\"/>");
            //                }
            //                if (color == "1")
            //                {
            //                    sb.Append("<input type=\"button\" onclick=\"andon_material(id)\" id=\"" + id + "\" value=\"" + value + "\" style=\"width: 140px; height:100%;background-color:pink;\"/>");
            //                }
            //                if (color == "2")
            //                {
            //                    sb.Append("<input type=\"button\" onclick=\"andon_material(id)\" id=\"" + id + "\" value=\"" + value + "\" style=\"width: 140px; height:100%;background-color:yellow;\"/>");
            //                }
            //                #endregion

            //                sb.Append("</td>");
            //                #endregion

            //            }
            //        }
            //        sb.Append("</tr>");
            //    }
            //    //小于4个物料ando按钮
            //    else
            //    {
            //        sb.Append("<tr>");
            //        int i = 0;
            //        for (i = 0; i < dt_list.Rows.Count; i++)
            //        {
            //            //如果按钮数小于一半,都放在第一行
            //            if (i < dt_list.Rows.Count)
            //            {
            //                #region 第一行按钮


            //                sb.Append("<td style=\"height:50%\">");
            //                //标签id
            //                string id = "button_ando_material_" + dt_list.Rows[i]["编号"].ToString();

            //                #region 颜色代码

            //                //标签值
            //                string value = dt_list.Rows[i]["编号描述2"].ToString();
            //                string color = dt_list.Rows[i]["颜色代码"].ToString();
            //                //string inputColor = "background-color:" + color + "";
            //                if (color == "0")
            //                {
            //                    sb.Append("<input type=\"button\" onclick=\"andon_material(id)\" id=\"" + id + "\" value=\"" + value + "\" style=\"width: 140px; height:100%;\"/>");
            //                }
            //                if (color == "1")
            //                {
            //                    sb.Append("<input type=\"button\" onclick=\"andon_material(id)\" id=\"" + id + "\" value=\"" + value + "\" style=\"width: 140px; height:100%;background-color:pink;\"/>");
            //                }
            //                if (color == "2")
            //                {
            //                    sb.Append("<input type=\"button\" onclick=\"andon_material(id)\" id=\"" + id + "\" value=\"" + value + "\" style=\"width: 140px; height:100%;background-color:yellow;\"/>");
            //                }
            //                #endregion

            //                sb.Append("</td>");

            //                #endregion

            //            }
            //        }
            //        sb.Append("</tr>");
            //    }
            //sb.Append("</table>");

            //return sb.ToString();

            #endregion
        }
    }
}
