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.Collections;
using System.Globalization;

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

        [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} 08:00:00' and '{4} 07:59:59' ", equid, inspectid, itemname, DateTime.Now.ToString("yyyy-MM-dd", new CultureInfo("en-US")), DateTime.Now.AddDays(1).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 shift_id = string.Empty;
                    string shift_name = string.Empty;

                    string str_shift = string.Format("select  shift_id,shift_name from Work_shift as a inner join EquipmentList as b on a.linegroupid=b.linegroupid  Where b.equid='{0}'	and '{1}' between shift_timestart and shift_timeend", equid, DateTime.Now.ToString("HH:mm:ss", new CultureInfo("en-US")));
                    DataTable dtshift = fc.SQL_DataTable(str_shift);
                    if (dtshift.Rows.Count > 0)
                    {
                        shift_id = dtshift.Rows[0]["shift_id"].ToString();
                        shift_name = dtshift.Rows[0]["shift_name"].ToString();

                    }

                    if (shift_id.Length == 0)
                    {
                        shift_id = "1";
                        shift_name = "Day";

                    }

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


                    if (piz.Length == 0)
                    {
                        string str_lastpiz = string.Format("select top 1 insp_piz from InspectionInput Where inspectiontype='1' and equid='{0}' and inspectid='{1}' order by create_datetime desc", equid, inspectid);
                        DataTable dtlastpiz = fc.SQL_DataTable(str_lastpiz);
                        if (dtlastpiz.Rows.Count > 0)
                        {
                            piz = dtlastpiz.Rows[0][0].ToString();

                        }

                    }

                    string inspection_date = string.Empty;
                    int chktime = DateTime.Now.Hour;
                    if (chktime >= 0 && chktime < 8)
                    {
                        inspection_date = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd", new CultureInfo("en-US")) + " " + DateTime.Now.ToString("HH:mm:ss", new CultureInfo("en-US"));



                    }
                    else
                    {


                        inspection_date = 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, resultvalue, notice, flagdel, create_user, create_datetime,insp_piz,partnumber,shift_id,shift_name,insp_status) VALUES ('" + inspidid + "','" + equid + "','" + inspectid + "','" + itemno + "','" + itemname + "','" + inspection_date + "','1'," + result + ",'" + notic + "',0,'" + user + "','" + crdate + "','" + piz + "','" + partnumber + "'," + shift_id + ",'" + shift_name + "','W')");
                    string up_mt = string.Format(" update InspectionInput SET equid= '" + equid + "',inspectid='" + inspectid + "', itemno= '" + itemno + "', itemname= '" + itemname + "',inspectiondate= '" + inspection_date + "',inspectiontype= '1',resultvalue= " + result + ",notice= '" + notic + "',flagdel=0,Update_user= '" + user + "',Update_Datetime= '" + crdate + "',insp_piz='" + piz + "',partnumber='" + partnumber + "',shift_id=" + shift_id + ",shift_name='" + shift_name + "' 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)
        {

        }

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