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.approve
{
    public partial class XR_checklist_create : System.Web.UI.Page
    {

               [System.Web.Services.WebMethod]
        public static string save(string dayoff, string type,string month,string year,string lineid,string partnumber,string user)
        {
            string cmd_inst = string.Empty;
            string cmd_del = string.Empty;
            string equid = string.Empty;
            using (CheckSheet.FcSql.Function fc = new CheckSheet.FcSql.Function())
            {
                string str_equid = string.Format("select equid from EquipmentList Where linegroupid='{0}' and partnumber='{1}'", lineid, partnumber);
                DataTable dt_equid = fc.SQL_DataTable(str_equid);
                if (dt_equid.Rows.Count > 0)
                {
                    equid = dt_equid.Rows[0]["equid"].ToString();
                }


                string _id = string.Empty;
                string _q = string.Format(" select max(convert(real,calenid)) as id from CalendarPlan ");
                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();
                        }
                    }
                }
                if (type == "del")
                {
                    string str_del = string.Format("delete from  CalendarPlan Where check_type=2 and dayoff='{0}' and equid='{1}' ", dayoff,equid);
                    cmd_inst = fc.ExecSQLStatement(str_del);
                }
                else
                {
                    string str_del = string.Format("delete from  CalendarPlan Where check_type=2 and dayoff='{0}' and equid='{1}' ", dayoff,equid);
                    cmd_del = fc.ExecSQLStatement(str_del);
                  
                    string str_inst = string.Format("insert into CalendarPlan (calenid, monthno, yearno, check_type, equid, dayoff, flagdel, create_user, create_datetime) values ('{0}',{1},{2},{3},'{4}','{5}',0,'{6}','{7}')", _id, month, year, "2", equid, dayoff, user, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", new CultureInfo("en-US")));
                    cmd_inst = fc.ExecSQLStatement(str_inst);


                }

                fc.closeconnection();
            }
            return cmd_inst;


        }



        [System.Web.Services.WebMethod]
        public static string GetAllDay(string year, string month)
        {
            DataSet ds;
            using (CheckSheet.FcSql.Function fc = new CheckSheet.FcSql.Function())
            {
                string query = string.Empty;
                query = string.Format(" select Day(listday) as listnumday,convert(varchar,listday,120) as listday,listdayname from dbo.listdate_of_year('{0}')	Where month(listday)={1} and year(listday)={2}", year + "0101", month, year);
                ds = fc.SQL_DataSet(query.ToString());
                fc.closeconnection();
            }
            return ds.GetXml();


        }

        [System.Web.Services.WebMethod]
        public static string GetDayOff(string year, string month, string lineid, string partnumber, string user)
        {
            DataSet ds;
            string equid = string.Empty;
            using (CheckSheet.FcSql.Function fc = new CheckSheet.FcSql.Function())
            {
                string str_equid = string.Format("select equid from EquipmentList Where linegroupid='{0}' and partnumber='{1}'", lineid, partnumber);
                DataTable dt_equid = fc.SQL_DataTable(str_equid);
                if (dt_equid.Rows.Count > 0)
                {
                    equid = dt_equid.Rows[0]["equid"].ToString();
                }

                string query = string.Empty;
                query = string.Format(" select  Datename(dw,dayoff)+''+convert(varchar(3),Day(dayoff)) as listnumday, dayoff from CalendarPlan where check_type=2 and month(dayoff)={0} and year(dayoff)={1} and equid='{3}' ", month, year, year + "0101",equid);
                ds = fc.SQL_DataSet(query.ToString());
                if (ds.Tables[0].Rows.Count == 0)
                {
                    query = string.Format("select Datename(dw,listday)+''+convert(varchar(3),Day(listday)) as listnumday,listday as dayoff from dbo.listweekend_of_year('{2}')	Where month(listday)={0} and year(listday)={1} ", month, year, year + "0101");
                    ds = fc.SQL_DataSet(query.ToString());
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {

                        string _id = string.Empty;
                        string _q = string.Format(" select max(convert(real,calenid)) as id from CalendarPlan ");
                        DataTable dtgenid = fc.SQL_DataTable(_q.ToString());
                        if (dtgenid.Rows.Count == 0)
                        {
                            _id = "1";
                        }
                        else
                        {
                            foreach (DataRow drid in dtgenid.Rows)
                            {
                                if (drid[0].ToString() == "")
                                {
                                    _id = "1";
                                }
                                else
                                {
                                    _id = (int.Parse(drid[0].ToString()) + 1).ToString();
                                }
                            }
                        }
                        string str_inst = string.Format("insert into CalendarPlan (calenid, monthno, yearno, check_type, equid, dayoff, flagdel, create_user, create_datetime) values ('{0}',{1},{2},{3},'{4}','{5}',0,'{6}','{7}')", _id, month, year, "2", equid, dr["dayoff"].ToString(), user, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", new CultureInfo("en-US")));
                        string cmd_inst = fc.ExecSQLStatement(str_inst);




                    }


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


        }






        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {

                txt_checkyear.Items.Add(DateTime.Now.AddYears(-1).Year.ToString());
                txt_checkyear.Items.Add(DateTime.Now.Year.ToString());
                txt_checkyear.SelectedIndex = 1;


                txt_checkmonth.Items.Add("1");
                txt_checkmonth.Items.Add("2");
                txt_checkmonth.Items.Add("3");
                txt_checkmonth.Items.Add("4");
                txt_checkmonth.Items.Add("5");
                txt_checkmonth.Items.Add("6");
                txt_checkmonth.Items.Add("7");
                txt_checkmonth.Items.Add("8");
                txt_checkmonth.Items.Add("9");
                txt_checkmonth.Items.Add("10");
                txt_checkmonth.Items.Add("11");
                txt_checkmonth.Items.Add("12");

                txt_checkmonth.SelectedValue = DateTime.Now.Month.ToString();

            }

        }

        protected void btncreatenextmonth_Click(object sender, EventArgs e)
        {

        }

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