using System;
using System.Collections.Generic;
using System.Text;
using System.Data;


namespace Self_Project
{
    public class Reserve : IControlable
    {
        public Boolean Ch11 = true, Ch12 = true, Ch13 = true, Ch21 = true, Ch22 = true, Ch23 = true, Ch31 = true, Ch32 = true, Ch33 = true, Ch41 = true, Ch42 = true, Ch43 = true, Ch51 = true, Ch52 = true, Ch53 = true, Ch61 = true, Ch62 = true, Ch63 = true, Ch71 = true, Ch72 = true, Ch73 = true;
        public Int32 Price1 = 0, Price2 = 0, Price3 = 0;
        public System.Drawing.Color Color1 = System.Drawing.Color.DarkSlateGray, Color2 = System.Drawing.Color.White;
        //=======================================================
        private string _tarikh = "";

        public string Tarikh
        {
            get { return _tarikh; }
            set 
            {
                if (value.Length != 5)
                    throw new Reserve_Exceptions_Tarikh();
                else
                    _tarikh = value;
            }
        }
        //=======================================================
        private Int16 _cancelDay = 0;
        public Int16 CancelDay
        {
            get { return _cancelDay; }
            set 
            {
                if (value < 0 || value > 7)
                    throw new Reserve_Exceptions_CancelDay();
                else
                    _cancelDay = value;
            }
        }
        //=======================================================
        public void ControlInfo()
        {
            if (_tarikh.Length != 5)
                throw new Reserve_Exceptions_Tarikh();
            if (_cancelDay < 0 || _cancelDay > 7)
                throw new Reserve_Exceptions_CancelDay();
        }
        //=======================================================
        public void DeleteReserve(String FromWeek, String ToWeek)
        {
            Condition Cond = new Condition(2);
            Cond.AddCondition("Num", Operatotrs.بزرگتر_مساوی, FromWeek, Concat_Type.And);
            Cond.AddCondition("Num", Operatotrs.کوچکتر_مساوی, ToWeek, Concat_Type.None);
            DataBase_SQL.Action(DBOperation.Delete, Cond, "ReservedFood", "");
        }
        //=======================================================
        public void LoadLimits(Int16 StdType)
        {
            DataTable DT = new DataTable();
            Condition Cond = new Condition(1);
            Cond.AddCondition("Model", Operatotrs.مساوی, StdType, Concat_Type.None);
            DT = DataBase_SQL.Action(DBOperation.Select, Cond, "Limit", "Day11,Day12,Day13,Day21,Day22,Day23,Day31,Day32,Day33,Day41,Day42,Day43,Day51,Day52,Day53,Day61,Day62,Day63,Day71,Day72,Day73");
            //--------------------------------
            Ch11 = Convert.ToBoolean(DT.Rows[0][0]);
            Ch12 = Convert.ToBoolean(DT.Rows[0][1]);
            Ch13 = Convert.ToBoolean(DT.Rows[0][2]);
            Ch21 = Convert.ToBoolean(DT.Rows[0][3]);
            Ch22 = Convert.ToBoolean(DT.Rows[0][4]);
            Ch23 = Convert.ToBoolean(DT.Rows[0][5]);
            Ch31 = Convert.ToBoolean(DT.Rows[0][6]);
            Ch32 = Convert.ToBoolean(DT.Rows[0][7]);
            Ch33 = Convert.ToBoolean(DT.Rows[0][8]);
            Ch41 = Convert.ToBoolean(DT.Rows[0][9]);
            Ch42 = Convert.ToBoolean(DT.Rows[0][10]);
            Ch43 = Convert.ToBoolean(DT.Rows[0][11]);
            Ch51 = Convert.ToBoolean(DT.Rows[0][12]);
            Ch52 = Convert.ToBoolean(DT.Rows[0][13]);
            Ch53 = Convert.ToBoolean(DT.Rows[0][14]);
            Ch61 = Convert.ToBoolean(DT.Rows[0][15]);
            Ch62 = Convert.ToBoolean(DT.Rows[0][16]);
            Ch63 = Convert.ToBoolean(DT.Rows[0][17]);
            Ch71 = Convert.ToBoolean(DT.Rows[0][18]);
            Ch72 = Convert.ToBoolean(DT.Rows[0][19]);
            Ch73 = Convert.ToBoolean(DT.Rows[0][20]);
        }
        //=======================================================
        public void SetLimits(Int16 StdType)
        {
            Condition Cond = new Condition(1);
            Cond.AddCondition("Model", Operatotrs.مساوی, StdType, Concat_Type.None);
            DataBase_SQL.Action(DBOperation.Update, Cond, "Limit", "Day11,Day12,Day13,Day21,Day22,Day23,Day31,Day32,Day33,Day41,Day42,Day43,Day51,Day52,Day53,Day61,Day62,Day63,Day71,Day72,Day73", Convert.ToInt16(Ch11), Convert.ToInt16(Ch12), Convert.ToInt16(Ch13), Convert.ToInt16(Ch21), Convert.ToInt16(Ch22), Convert.ToInt16(Ch23), Convert.ToInt16(Ch31), Convert.ToInt16(Ch32), Convert.ToInt16(Ch33), Convert.ToInt16(Ch41), Convert.ToInt16(Ch42), Convert.ToInt16(Ch43), Convert.ToInt16(Ch51), Convert.ToInt16(Ch52), Convert.ToInt16(Ch53), Convert.ToInt16(Ch61), Convert.ToInt16(Ch62), Convert.ToInt16(Ch63), Convert.ToInt16(Ch71), Convert.ToInt16(Ch72), Convert.ToInt16(Ch73));
        }
        //=======================================================
        public void LoadPrices(Int16 StdType)
        {
            DataTable DT = new DataTable();
            Condition Cond = new Condition(1);
            Cond.AddCondition("Model", Operatotrs.مساوی, StdType, Concat_Type.None);
            DT = DataBase_SQL.Action(DBOperation.Select, Cond, "Prices", "Price1,Price2,Price3");
            try
            {
                Price1 = Convert.ToInt32(DT.Rows[0][0]);
                Price2 = Convert.ToInt32(DT.Rows[0][1]);
                Price3 = Convert.ToInt32(DT.Rows[0][2]);
            }
            catch
            {
                Price1 = Price2 = Price3 = 0;
            }
        }
        //=======================================================
        public void SetPrices(Int16 StdType)
        {
            Condition Cond = new Condition(1);
            Cond.AddCondition("Model", Operatotrs.مساوی, StdType, Concat_Type.None);
            if (DataBase_SQL.Action(DBOperation.Select, Cond, "Prices", "Price1,Price2,Price3").Rows.Count > 0)
                DataBase_SQL.Action(DBOperation.Update, Cond, "Prices", "Price1,Price2,Price3", Price1, Price2, Price3);
            else
                DataBase_SQL.Action(DBOperation.Insert, null, "Prices", "Model,Price1,Price2,Price3", StdType, Price1, Price2, Price3);
        }
        //=======================================================
        public void LoadCancelDay()
        {
            CancelDay = Convert.ToInt16(DataBase_SQL.Action(DBOperation.Select, null, "CancelReserve", "Num").Rows[0][0]);
        }
        //=======================================================
        public void SetCancelDay()
        {
            if (_cancelDay < 0 || _cancelDay > 7)
                throw new Reserve_Exceptions_CancelDay();
            DataBase_SQL.Action(DBOperation.Update, null, "CancelReserve", "Num", _cancelDay);
        }
        //=======================================================
        public void LoadDays(System.Windows.Forms.DataGridView DGV)
        {
            DGV.DataSource = DataBase_SQL.Action(DBOperation.Select, null, "DaysTable", "*");
            DGV.Columns[0].HeaderText = "شنبه";
            DGV.Columns[1].HeaderText = "یک شنبه";
            DGV.Columns[2].HeaderText = "دو شنبه";
            DGV.Columns[3].HeaderText = "سه شنبه";
            DGV.Columns[4].HeaderText = "چهار شنبه";
            DGV.Columns[5].HeaderText = "پنج شنبه";
            DGV.Columns[6].HeaderText = "جمعه";
            //-------------------------------------------------
            DGV.Columns[0].Width = DGV.Columns[1].Width = DGV.Columns[2].Width = DGV.Columns[3].Width = DGV.Columns[4].Width = DGV.Columns[5].Width = DGV.Columns[6].Width = 60;
        }
        //=======================================================
        public void ApplyLimit_Model(System.Windows.Forms.DataGridView DGV, String Model)
        {
            DataTable DT = new DataTable();
            Condition Cond = new Condition(1);
            Cond.AddCondition("Model", Operatotrs.مساوی, Model, Concat_Type.None);
            DT = DataBase_SQL.Action(DBOperation.Select, Cond, "Limit", "*");
            if (DT.Rows.Count > 0)
            {
                int i, Row = 0, Col = 0;
                for (i = 1; i <= 21; i++)
                {
                    DGV.Rows[Row].Cells[Col].ReadOnly = !Convert.ToBoolean(DT.Rows[0][i]);
                    //-------------------
                    if (Convert.ToBoolean(DT.Rows[0][i]) == false)
                        DGV.Rows[Row].Cells[Col].Style.BackColor = Color1;
                    else
                        DGV.Rows[Row].Cells[Col].Style.BackColor = Color2;
                    //--------------------
                    Row++;
                    if (Row >= 3)
                    {
                        Row = 0;
                        Col++;
                    }
                }
            }
        }
        //=======================================================
        public void ApplyLimit_CancelDay(System.Windows.Forms.DataGridView DGV, String WeekNum)
        {
            string Tarikh1, Tarikh2;
            short i, Rooz = 0;
            DataTable DT = new DataTable();
            //-------------------
            Condition Cond = new Condition(1);
            Cond.AddCondition("Num", Operatotrs.مساوی, WeekNum, Concat_Type.None);
            DT = DataBase_SQL.Action(DBOperation.Select, Cond, "HafteDetail", "HafteEsm");
            Tarikh1 = Functions.FarsiDate("/");
            Tarikh2 = Tarikh1.Substring(0, 5) + DT.Rows[0][0].ToString();
            //-------------------
            Rooz = Convert.ToInt16(Functions.Sub2Date(Tarikh1, Tarikh2));
            //-------------------
            LoadCancelDay();
            Rooz += _cancelDay;
            Rooz -= 1;
            if (Rooz > 6) Rooz = 6;
            //-------------------
            for (i = 0; i <= Rooz; i++)
            {
                DGV.Rows[0].Cells[i].Style.BackColor = DGV.Rows[1].Cells[i].Style.BackColor = DGV.Rows[2].Cells[i].Style.BackColor = Color1;
                DGV.Rows[0].Cells[i].ReadOnly = DGV.Rows[1].Cells[i].ReadOnly = DGV.Rows[2].Cells[i].ReadOnly = true;
            }
        }
        //=======================================================
        public void LoadReservedFood(System.Windows.Forms.DataGridView DGV, String WeekNum, String StdNum)
        {
            DataTable DT = new DataTable();
            Condition Cond = new Condition(2);
            Cond.AddCondition("Num", Operatotrs.مساوی, WeekNum, Concat_Type.And);
            Cond.AddCondition("ID", Operatotrs.مساوی, StdNum, Concat_Type.None);
            DT = DataBase_SQL.Action(DBOperation.Select, Cond, "ReservedFood", "*");
            if (DT.Rows.Count == 0)
            {
                LoadDays(DGV);
            }
            else
            {
                //---------------------------------آوردن غذاها
                int i, Row = 0, Col = 0;
                for (i = 2; i <= 22; i++)
                {
                    DGV.Rows[Row++].Cells[Col].Value = Convert.ToBoolean(DT.Rows[0][i]);
                    if (Row >= 3)
                    {
                        Row = 0;
                        Col++;
                    }
                }
            }
        }
        //=======================================================
        public void SaveReserve(System.Windows.Forms.DataGridView DGV, String WeekNum, String StdNum, Int32 Price)
        {
            //--------Delete
            Condition Cond = new Condition(2);
            Cond.AddCondition("Num", Operatotrs.مساوی, WeekNum, Concat_Type.And);
            Cond.AddCondition("ID", Operatotrs.مساوی, StdNum, Concat_Type.None);
            DataBase_SQL.Action(DBOperation.Delete, Cond, "ReservedFood", "");
            //---------------------------
            int Row = 0, Col = 0;
            string StrSql = "Insert Into ReservedFood Values (" + WeekNum + "," + StdNum;
            for (Col = 0; Col <= 6; Col++)
                for (Row = 0; Row <= 2; Row++)
                {
                    StrSql += "," + Convert.ToInt16(DGV.Rows[Row].Cells[Col].Value).ToString();
                }
            StrSql += ")";
            //---------------------------
            DataBase_SQL.Bank_Action(StrSql);
            //---------------------------
            Student Std = new Student();
            Std.ID = Convert.ToInt32(StdNum);
            Std.Price = Price;
            Std.ChangeInfo(Student.ChangeType.Price);          
        }
        //=======================================================
        public void LoadStatistics(out String BreakFast_Result, out String Launch_Result, out String Dinner_Result)
        {
            Condition Cond_BreakFast = new Condition(2);
            Condition Cond_Launch = new Condition(2);
            Condition Cond_Dinner = new Condition(2);
            DataTable DT = new DataTable();
            DT = DataBase_SQL.Bank_Select("Select Top 1 Num,HafteEsm From HafteDetail Where HafteEsm <= '" + _tarikh + "' Order By Num Desc");
            String BreakFast = "Day" + (Functions.Sub2Date("1388/" + _tarikh, "1388/" + DT.Rows[0][1].ToString()) + 1).ToString() + "1";
            String Launch = "Day" + (Functions.Sub2Date("1388/" + _tarikh, "1388/" + DT.Rows[0][1].ToString()) + 1).ToString() + "2";
            String Dinner = "Day" + (Functions.Sub2Date("1388/" + _tarikh, "1388/" + DT.Rows[0][1].ToString()) + 1).ToString() + "3";
            //----------------------------------------------------------------------------
            Cond_BreakFast.AddCondition("Num", Operatotrs.مساوی, DT.Rows[0][0], Concat_Type.And);
            Cond_BreakFast.AddCondition(BreakFast, Operatotrs.مساوی, 1, Concat_Type.None);
            BreakFast_Result = DataBase_SQL.Action(DBOperation.Select, Cond_BreakFast, "ReservedFood", "Count(*)").Rows[0][0].ToString();
            //----------------------------------------------------------------------------
            Cond_Launch.AddCondition("Num", Operatotrs.مساوی, DT.Rows[0][0], Concat_Type.And);
            Cond_Launch.AddCondition(Launch, Operatotrs.مساوی, 1, Concat_Type.None);
            Launch_Result = DataBase_SQL.Action(DBOperation.Select, Cond_Launch, "ReservedFood", "Count(*)").Rows[0][0].ToString();
            //----------------------------------------------------------------------------
            Cond_Dinner.AddCondition("Num", Operatotrs.مساوی, DT.Rows[0][0], Concat_Type.And);
            Cond_Dinner.AddCondition(Dinner, Operatotrs.مساوی, 1, Concat_Type.None);
            Dinner_Result = DataBase_SQL.Action(DBOperation.Select, Cond_Dinner, "ReservedFood", "Count(*)").Rows[0][0].ToString();
        }
        //=======================================================
        public void ApplyWeekVacation(System.Windows.Forms.DataGridView DGV, string WeekNum)
        {
            Condition Cond = new Condition(1);
            Cond.AddCondition("WeekNum", Operatotrs.مساوی, WeekNum, Concat_Type.None);
            if (DataBase_SQL.Action(DBOperation.Select, Cond, "VacationWeek", "WeekNum").Rows.Count > 0)
                for (int i = 0; i < DGV.Rows.Count; i++)
                    for (int j = 0; j < DGV.Columns.Count; j++)
                    {
                        DGV.Rows[i].Cells[j].ReadOnly = true;
                        DGV.Rows[i].Cells[j].Style.BackColor = Color1;
                    }
        }
        //=======================================================
        public void ApplyDayVacation(System.Windows.Forms.DataGridView DGV, String WeekNum)
        {
            Condition Cond1 = new Condition(1);
            DataTable DT = new DataTable();
            Cond1.AddCondition("Num", Operatotrs.مساوی, WeekNum, Concat_Type.None);
            DT = DataBase_SQL.Action(DBOperation.Select, Cond1, "HafteDetail", "HafteEsm");
            string Tarikh = Functions.AddDate(DT.Rows[0][0].ToString(), 0);
            for (int i = 0; i < 7; i++)
            {
                Tarikh = Functions.AddDate(Tarikh, 1);
                DT = DataBase_SQL.Bank_Select("Select Tarikh From VacationDay Where Tarikh = '" + Tarikh + "'");
                if (DT.Rows.Count > 0)
                {
                    DGV.Rows[0].Cells[i].ReadOnly = DGV.Rows[1].Cells[i].ReadOnly = DGV.Rows[2].Cells[i].ReadOnly = true;
                    DGV.Rows[0].Cells[i].Style.BackColor = DGV.Rows[1].Cells[i].Style.BackColor = DGV.Rows[2].Cells[i].Style.BackColor = Color1;
                }
            }
        }
        //=======================================================
        public Boolean HasReserve(String StdID, Int32 Place, out String Hafte, out Int32 Rooz)
        {
            String Tarikh1, Tarikh2;
            DataTable DT = new DataTable();
            //--------------
            DT = DataBase_SQL.Action(DBOperation.Select, null, "NumberOfWeek", "Num");
            Hafte = DT.Rows[0][0].ToString();
            //--------------
            Condition Cond1 = new Condition(1);
            Cond1.AddCondition("Num", Operatotrs.مساوی, Hafte, Concat_Type.None);
            DT = DataBase_SQL.Action(DBOperation.Select, Cond1, "HafteDetail", "HafteEsm");
            Tarikh1 = Functions.FarsiDate("/");
            Tarikh2 = Tarikh1.Substring(0, 5) + DT.Rows[0][0].ToString();
            //-------------------
            Rooz = Convert.ToInt16(Functions.Sub2Date(Tarikh1, Tarikh2));
            Rooz *= 3;
            Rooz += Place;
            String FName = "";
            if (Rooz == 1) FName = "Day11";
            if (Rooz == 2) FName = "Day12";
            if (Rooz == 3) FName = "Day13";
            if (Rooz == 4) FName = "Day21";
            if (Rooz == 5) FName = "Day22";
            if (Rooz == 6) FName = "Day23";
            if (Rooz == 7) FName = "Day31";
            if (Rooz == 8) FName = "Day32";
            if (Rooz == 9) FName = "Day33";
            if (Rooz == 10) FName = "Day41";
            if (Rooz == 11) FName = "Day42";
            if (Rooz == 12) FName = "Day43";
            if (Rooz == 13) FName = "Day51";
            if (Rooz == 14) FName = "Day52";
            if (Rooz == 15) FName = "Day53";
            if (Rooz == 16) FName = "Day61";
            if (Rooz == 17) FName = "Day62";
            if (Rooz == 18) FName = "Day63";
            if (Rooz == 19) FName = "Day71";
            if (Rooz == 20) FName = "Day72";
            if (Rooz == 21) FName = "Day73";
            //-------------------
            Condition Cond2 = new Condition(2);
            Cond2.AddCondition("Num", Operatotrs.مساوی, Hafte, Concat_Type.And);
            Cond2.AddCondition("ID", Operatotrs.مساوی, StdID, Concat_Type.None);
            DT = DataBase_SQL.Action(DBOperation.Select, Cond2, "ReservedFood", FName);
            if (DT.Rows.Count == 0)
                return false;
            else
            {
                if (Convert.ToBoolean(DT.Rows[0][0]) == false)
                    return false;
                else
                    return true;
            }
        }
        //=======================================================
        //=======================================================
    }
}
