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

namespace CheckSheet.checkinput
{
    public partial class Machine_input_check_status : System.Web.UI.Page
    {
        [System.Web.Services.WebMethod]
        public static string getdata(string parahd)
        {

            DataSet ds = new DataSet();
            string cmdhd = string.Empty;
            string inspidid = 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 equid = strmt[0].ToString();
                    string inspectid = strmt[1].ToString();


                    string str_getinput = string.Format("select inspidid,resultstatus,resultvalue, insp_piz,notice from InspectionInput Where equid='{0}' and inspectid='{1}' and inspectiontype='1'  and inspectiondate between '{2} 00:00:00' and '{2} 23:59:59' ", equid, inspectid, DateTime.Now.ToString("yyyy-MM-dd", new CultureInfo("en-US")));
                    ds = fc.SQL_DataSet(str_getinput);

                    fc.closeconnection();

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


        
        [System.Web.Services.WebMethod]
        public static string save(string parahd)
        {
            string cmdhd = string.Empty;
            string inspidid = 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 equid = strmt[1].ToString();
                    string inspectid = strmt[2].ToString();
                    string itemname = strmt[3].ToString();

                    string chkid = string.Format("select inspidid from InspectionInput Where equid='{0}' and inspectid='{1}' and itemname='{2}' and inspectiontype='1'  and inspectiondate between '{3} 00:00:00' and '{3} 23:59:59' ", equid, inspectid, itemname, DateTime.Now.ToString("yyyy-MM-dd", new CultureInfo("en-US")));
                    DataTable dtchkid = fc.SQL_DataTable(chkid.ToString());
                    if (dtchkid.Rows.Count > 0)
                    {
                        inspidid = dtchkid.Rows[0]["inspidid"].ToString();

                    }
                    
                    
                   

                    if (inspidid.Length == 0)
                    {

                        string _pv = string.Empty;
                        string _id = string.Empty;
                        string _q = string.Format(" select max(convert(real,inspidid)) as id from InspectionInput ");
                        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();
                                }
                            }
                        }

                        inspidid = _id;


                    }

                    string itemno = string.Empty;
                    string partnumber = string.Empty;
                    string str_inspect = string.Format("select itemno,partnumber from InspectionItem as a inner join  EquipmentList  as b on a.equid=b.equid Where a.itemname='{0}'", itemname);
                    DataTable dtinspect = fc.SQL_DataTable(str_inspect);
                    if (dtinspect.Rows.Count > 0)
                    {

                        itemno = dtinspect.Rows[0]["itemno"].ToString();
                        partnumber = dtinspect.Rows[0]["partnumber"].ToString();
                    }
                    string type = strmt[4].ToString();
                    string result = strmt[5].ToString();
                    string notic = strmt[6].ToString();
                    string user = strmt[7].ToString();
                    string piz = strmt[8].ToString();
                   

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

                    string inst_mt = string.Format(" insert into InspectionInput (inspidid, equid,inspectid, itemno,itemname, inspectiondate, inspectiontype, resultstatus, notice, flagdel, create_user, create_datetime,insp_piz,partnumber) VALUES ('" + inspidid + "','" + equid + "','"+inspectid+"','" + itemno + "','" + itemname + "','" + crdate + "','1','" + result + "','" + notic + "',0,'" + user + "','" + crdate + "','" + piz + "','"+partnumber+"')");
                    string up_mt = string.Format(" update InspectionInput SET equid= '" + equid + "',inspectid='"+inspectid+"', itemno= '" + itemno + "', itemname= '" + itemname + "',inspectiondate= '" + crdate + "',inspectiontype= '1',resultstatus= '" + result + "',notice= '" + notic + "',flagdel=0,Update_user= '" + user + "',Update_Datetime= '" + crdate + "',insp_piz='" + piz + "',partnumber='"+partnumber+"'  Where inspidid= '" + inspidid + "' ");

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

                }
                catch { };

            }

            //string cmdsendmail = string.Empty;
            //clsControlUI.sendemail("1", "Scrap document waiting for approval.", userapprove);

            //cmdhd = inspidid + "|" + cmdhd;

            return cmdhd;


        }



        [System.Web.Services.WebMethod]
        public static string getinspectiondetail(string equid, string inspectionid)
        {
            DataSet ds;
            using (CheckSheet.FcSql.Function fc = new CheckSheet.FcSql.Function())
            {
                string query = string.Empty;
                query = string.Format("select c.* from EquipmentList as a inner join MachineMaster as b on a.machineid=b.machineid inner join InspectionItem as c on a.equid=c.equid Where a.equid='{0}' and c.inspectid='{1}'", equid, inspectionid);
                ds = fc.SQL_DataSet(query.ToString());
                fc.closeconnection();
            }
            return ds.GetXml();


        }


        protected void Page_Load(object sender, EventArgs e)
        {

        }

       
    }
}