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 Jig_input_check : 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 partnumber = strmt[0].ToString();
                    string serialnumber = strmt[1].ToString();
                    string toolname = strmt[2].ToString();

                    string str_getinput = string.Format("select inspidid, insp_piz,notice,partnumber, toolname, serialnumber, resultvalue, resultstatus from InspectionInput Where partnumber='{0}' and serialnumber='{1}' and inspectiontype='3' and toolname='{2}'  and inspectiondate between '{3} 00:00:00' and '{3} 23:59:59' ", partnumber, serialnumber, toolname, 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 partnumber = strmt[1].ToString();
                    string serialnumber = strmt[2].ToString();
                    string toolname = strmt[3].ToString();

                    string chkid = string.Format("select inspidid from InspectionInput Where partnumber='{0}' and serialnumber='{1}' and toolname='{2}' and inspectiontype='3' and inspectiondate between '{3} 00:00:00' and '{3} 23:59:59' ", partnumber, serialnumber, toolname, 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 jigid = string.Empty;
                    string str_inspect = string.Format("select jigid from JigMaster Where toolname='{0}' and serial='{1}'", toolname, serialnumber);
                    DataTable dtinspect = fc.SQL_DataTable(str_inspect);
                    if (dtinspect.Rows.Count > 0)
                    {

                        jigid = dtinspect.Rows[0]["jigid"].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, partnumber,serialnumber,jigid,toolname, inspectiondate, inspectiontype, resultstatus, notice, flagdel, create_user, create_datetime,insp_piz) VALUES ('" + inspidid + "','" + partnumber + "','" + serialnumber + "','" + jigid + "','" + toolname + "','" + crdate + "','3','" + result + "','" + notic + "',0,'" + user + "','" + crdate + "','" + piz + "')");
                    string up_mt = string.Format(" update InspectionInput SET partnumber= '" + partnumber + "',serialnumber='" + serialnumber + "', jigid= '" + jigid + "', toolname= '" + toolname + "',inspectiondate= '" + crdate + "',inspectiontype= '3',resultstatus= '" + result + "',notice= '" + notic + "',flagdel=0,Update_user= '" + user + "',Update_Datetime= '" + crdate + "',insp_piz='" + piz + "' 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 sendmail(string partnumber)
        {
            string userapprove = clsControlUI.getuserapprove("1", "3",partnumber);
            clsControlUI.sendemail("1", "Check sheet document waiting for approval.", userapprove, "3");

            return "";

        }

        [System.Web.Services.WebMethod]
        public static string saveall(string parahd)
        {


            string cmdhd = string.Empty;
            string inspidid = 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 partnumber = strmt[0].ToString();
                    string serialnumber = strmt[1].ToString();
                    string toolname = strmt[2].ToString();
                    string notice = strmt[3].ToString();
                    string piz = strmt[4].ToString();

                    string chkid = string.Format("select inspidid from InspectionInput Where partnumber='{0}' and serialnumber='{1}' and toolname='{2}' and inspectiontype='3' and inspectiondate between '{3} 00:00:00' and '{3} 23:59:59' ", partnumber, serialnumber, toolname, 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();

                    }



                    string up_mt = string.Format(" update InspectionInput SET notice= '" + notice + "',insp_piz='" + piz + "',insp_status='W'  Where inspidid= '" + inspidid + "' ");
                    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 getItem(string currentpage, string filterstring)
        {

            int pagesize = 10;

            String val_dayoff = string.Empty;


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

                string query = string.Empty;
                query = string.Format("select * from (Select  ROW_NUMBER() OVER (order by a.jigid)  as RowNum,  a.jigid, toolname, serial, expire_date,'' as expiredate from JigMaster as a inner join EquipmentList as b on a.jigid=b.jigid Where 1=1 and a.FlagDel=0 {0}  ) as temp WHERE RowNum BETWEEN ({1} - 1) * {2} + 1 AND {1} * {2}     ", filterstring, currentpage, pagesize);
                ds = fc.SQL_DataSet(query.ToString());
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    dr["expiredate"] = DateTime.Parse(dr["expire_date"].ToString()).ToString("yyyy/MM/dd", new CultureInfo("en-US"));

                }

                
                fc.closeconnection();
            }
            return ds.GetXml();


        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

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