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 _4M1E_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 typeno = strmt[1].ToString();
                    string typename = strmt[2].ToString();

                    string str_getinput = string.Format("select inspidid, insp_piz,notice, detailchangepoint, result1, action1, result2, action2 from InspectionInput Where partnumber='{0}' and itemno='{1}' and inspectiontype='4' and itemname='{2}'  and inspectiondate between '{3} 00:00:00' and '{3} 23:59:59' ", partnumber,typeno,typename, 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;

            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 typeno = strmt[2].ToString();
                    string typename = strmt[3].ToString();
                    string detail = strmt[4].ToString();
                    string type = strmt[5].ToString();
                    string result1 = strmt[6].ToString();
                    string case1 = strmt[7].ToString();
                    string result2 = strmt[8].ToString();
                    string case2 = strmt[9].ToString();
                    string notice = strmt[10].ToString();
                    string user = strmt[11].ToString();
                    string piz = strmt[12].ToString();



                    string chkid = string.Format("select inspidid from InspectionInput Where partnumber='{0}' and itemno='{1}' and inspectiontype='4' and itemname='{2}' and inspectiondate between '{3} 00:00:00' and '{3} 23:59:59' ", partnumber, typeno, typename, 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 equid = string.Empty;
                    string inspectid = string.Empty;
                    string str_inspect = string.Format("select inspectid,equid from InspectionItem  Where itemno='{0}' and itemname='{1}'",typeno, typename);
                    DataTable dtinspect = fc.SQL_DataTable(str_inspect);
                    if (dtinspect.Rows.Count > 0)
                    {
                        inspectid = dtinspect.Rows[0]["inspectid"].ToString();
                        equid = dtinspect.Rows[0]["equid"].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,partnumber, inspectiondate, inspectiontype, result1,action1,result2,action2, notice, flagdel, create_user, create_datetime,insp_piz,detailchangepoint,insp_status) VALUES ('" + inspidid + "','" + equid + "','" + inspectid + "', '" + typeno + "','" + typename + "','" + partnumber + "','" + crdate + "','4','" + result1 + "','" + case1 + "','" + result2 + "','" + case2 + "','" + notice + "',0,'" + user + "','" + crdate + "','" + piz + "','" + detail + "','W')");
                    string up_mt = string.Format(" update InspectionInput SET equid='" + equid + "',inspectid='" + inspectid + "',itemno='" + typeno + "',itemname='" + typename + "', partnumber= '" + partnumber + "',inspectiondate= '" + crdate + "',inspectiontype= '4',result1= '" + result1 + "',action1='" + case1 + "',result2='" + result2 + "',action2='" + case2 + "',notice= '" + notice + "',flagdel=0,Update_user= '" + user + "',Update_Datetime= '" + crdate + "',insp_piz='" + piz + "',detailchangepoint='" + detail + "',insp_status='W' Where inspidid= '" + inspidid + "' ");

                    cmdhd = fc.ExecSQLStatement(inst_mt.ToString());
                    if(cmdhd.Length==0)
                    {

                        string userapprove = clsControlUI.getuserapprove("1", "4",partnumber);
                        clsControlUI.sendemail("1", "Check sheet document waiting for approval.", userapprove, "4");



                    }
                    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;


        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

       
        protected void btnrequestapprove_Click(object sender, EventArgs e)
        {
            Response.Redirect("4M1E_check");
        }

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