using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using System.Data;

namespace CheckSheet.setting
{
    public partial class Machine_inspection_item_edit : System.Web.UI.Page
    {

        [System.Web.Services.WebMethod]
        public static string save(string parahd)
        {
            string cmdhd = string.Empty;
            string equid = string.Empty;
            string userapprove = string.Empty;
            using (FcSql.Function fc = new FcSql.Function())
            {
                byte[] datahd = System.Convert.FromBase64String(parahd);
                string base64Decoded = System.Text.ASCIIEncoding.UTF8.GetString(datahd);
                string[] strmt = base64Decoded.Split('|');
                try
                {
                    string inspectid = string.Empty;
                    string itemno = string.Empty;
                    string criteriatext = string.Empty;
                    string linegroup = strmt[0].ToString();
                    string machineno = strmt[1].ToString();
                    string partnumber = strmt[2].ToString();
                    string checkitem = strmt[3].ToString();
                    string whoid = strmt[4].ToString();
                    string whoname = strmt[5].ToString();
                    string freqid = strmt[6].ToString();
                    string freqname = strmt[7].ToString();
                    string how = strmt[8].ToString();
                    string inputid = strmt[9].ToString();
                    string inputname = strmt[10].ToString();
                    string unitid = strmt[11].ToString();
                    string unitname = strmt[12].ToString();
                    string x = strmt[13].ToString();
                    string upper = strmt[14].ToString();
                    string lower = strmt[15].ToString();
                    string userid = strmt[16].ToString();
                    inspectid = strmt[17].ToString();
                    if(upper.Length==0)
                    {
                        upper = "0";

                    }
                    if(lower.Length==0)
                    {

                        lower = "0";
                    }

                    if(inputid=="1")
                    {
                        criteriatext = "OK or Not Good";

                    }

                    string chkid = string.Format("select equid,linegroupid,partnumber from EquipmentList   Where machineid='{0}' and linegroupid='{1}' and partnumber='{2}' ", machineno, linegroup, partnumber);
                    DataTable dtchkid = fc.SQL_DataTable(chkid.ToString());
                    if (dtchkid.Rows.Count > 0)
                    {
                        equid = dtchkid.Rows[0]["equid"].ToString();
                        string chkidsub = string.Format("select inspectid,itemno from InspectionItem  Where equid='{0}' and inspectid='{1}' ", equid,inspectid);
                        DataTable dtchkidsub = fc.SQL_DataTable(chkidsub.ToString());
                        if (dtchkidsub.Rows.Count > 0)
                        {
                             inspectid = dtchkidsub.Rows[0]["inspectid"].ToString();
                             itemno = inspectid;
                   
                           

                        }


                    }
                                     




                    if (inspectid=="\0\0" || inspectid=="")
                    {

                        string _pv = string.Empty;
                        string _id = string.Empty;
                        string _q = string.Format(" select max(convert(real,inspectid)) as id from InspectionItem ");
                        DataTable dtgenid = fc.SQL_DataTable(_q.ToString());
                        if (dtgenid.Rows.Count == 0)
                        {
                            _id = "1";
                        }
                        else
                        {
                            foreach (DataRow dr in dtgenid.Rows)
                            {
                                if (dr[0].ToString() == "")
                                {
                                    _id = "1";
                                }
                                else
                                {
                                    _id = (int.Parse(dr[0].ToString()) + 1).ToString();
                                }
                            }
                        }

                        inspectid = _id;
                        itemno = inspectid;


                    }



                    string crdate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", new CultureInfo("en-US"));

                    string inst_mt = string.Format(" insert into InspectionItem ( inspectid, equid, itemno,itemname,whoid, whochecklist, freqid, frequency, howcheck, criteriatext, criteriamin, criteriamax,unitid, unit, flagdel, create_user, create_datetime, input_method, input_x, input_type,inspection_type) VALUES ('" + inspectid + "','" + equid + "','" + itemno + "','" + checkitem + "','" + whoid + "','" + whoname + "','" + freqid + "','" + freqname + "','" + how + "','" + criteriatext + "'," + lower.Replace(",", "") + "," + upper.Replace(",", "") + ",'" + unitid + "','" + unitname + "',0,'" + userid + "','" + crdate + "','" + inputname + "','" + x + "','" + inputid + "','1')");
                    string up_mt = string.Format(" update InspectionItem SET itemname= '" + checkitem + "',whoid='" + whoid + "',whochecklist='" + whoname + "',freqid='" + freqid + "',frequency='" + freqname + "',howcheck='" + how + "',criteriatext='" + criteriatext + "',criteriamin='" + lower.Replace(",", "") + "', criteriamax='" + upper.Replace(",", "") + "',unitid='" + unitid + "', unit='" + unitname + "',input_method='" + inputname + "', input_x='" + x + "', input_type='" + inputid + "', flagdel=0,   update_user='" + userid + "', update_datetime= '" + crdate + "' Where inspectid= '" + inspectid + "' ");

                    cmdhd = fc.ExecSQLStatement(inst_mt.ToString());
                    if (cmdhd.ToLower().IndexOf("duplicate") != -1)
                    {
                        cmdhd = fc.ExecSQLStatement(up_mt.ToString());
                    }

                   
                    fc.closeconnection();


                }
                catch { };

            }



            return cmdhd;


        }


        [System.Web.Services.WebMethod]
        public static string getdata(string value)
        {

            DataSet ds = new DataSet();
          
            using (FcSql.Function fc = new FcSql.Function())
            {
                
                try
                {

                    string query = string.Format("SELECT inspectid, equid, itemno, itemname, whoid, whochecklist, freqid, frequency, howcheck, criteriatext, criteriamin, criteriamax, unitid, unit, flagdel, input_method, input_x, input_type FROM  InspectionItem Where inspectid='{0}' ", value);
                    ds = fc.SQL_DataSet(query.ToString());



                    fc.closeconnection();

                }
                catch { }
            }
            return ds.GetXml();


        }


        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnback_Click(object sender, EventArgs e)
        {
            Response.Redirect("RegisterEquipment_inspection_item");
        }
    }
}