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 XR_inspection_item_edit : System.Web.UI.Page
    {

        [System.Web.Services.WebMethod]
        public static string save_xr_chart(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();
                    string weekno = strmt[18].ToString();
                    string dateno = strmt[19].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;



                        }


                    }

                    string chkfreq = string.Empty;
                    if (weekno.Length > 0)
                    {
                        chkfreq = string.Format("select freqid from mst_Frequency Where (freqtype='{0}' or freqid='{0}') and freqdate='{1}'", freqid, weekno, dateno);

                    }
                    else
                    {

                        chkfreq = string.Format("select freqid from mst_Frequency Where (freqtype='{0}' or freqid='{0}') and freqvalue='{2}' ", freqid, weekno, dateno);

                    }


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

                        if (freqname.IndexOf("1/W") != -1)
                        {

                            type = "2";
                        }
                        else if (freqname.IndexOf("1/M") != -1)
                        {
                            type = "3";

                        }
                        else
                        {
                            type = "1";

                        }

                        string inst_frq = string.Format("insert into mst_Frequency  (freqid, freqname, freqtype, freqdate, freqvalue, flagdel) values ('{0}','{1}','{2}','{3}','{4}',0) ", _no, freqname, type, weekno, dateno);
                        string cmd = fc.ExecSQLStatement(inst_frq);
                        if (cmd.Length == 0)
                        {

                            freqid = _no;

                        }
                    }


                    if (inspectid.IndexOf("\0\0") != -1 || 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 _itemno = string.Format("select convert(float,max(itemno))+1 as itemno from InspectionItem where inspection_type='2' and charindex('.',itemno)>0");
                        DataTable dtgenitemno = fc.SQL_DataTable(_itemno.ToString());
                        if(dtgenitemno.Rows.Count>0)
                        {
                            itemno = dtgenitemno.Rows[0]["itemno"].ToString();

                        }
                        



                    }



                    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 + "','2')");
                    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_xr_chart(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, a.freqid, a.frequency, howcheck, criteriatext, criteriamin, criteriamax, unitid, unit, a.flagdel, input_method, input_x, input_type,b.freqdate as dayweek,b.freqvalue as datemonth,report_type FROM  InspectionItem as a left outer join mst_Frequency as b on a.freqid=b.freqid 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");
        }
    }
}