using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Aram_CRM_Project.BLL.Entity;

namespace Aram_CRM_Project.BLL
{
    class StuffCRUD : Aram_CRM_Project.DAL.DA
    {
        public int InsertStuff(Stuff StuffVar)
        {
            try
            {
                base.SQL = "SP_InsertStuff";
                base.InitializeCommand();
                base.AddParameter("@Name", StuffVar.Name);
                base.AddParameter("@StuffGroupId", StuffVar.StuffGroupId);
                base.AddParameter("@Feature", StuffVar.Feature);
                base.AddParameter("@Comment", StuffVar.Comment);
                return base.ExecuteStoredProcedure();
            }
            catch (Exception exp)
            {
                if (exp.Message.Contains("UNIQUE KEY") && exp.Message.Contains("duplicate"))
                    return -1;
                else
                    return 0;
            }
        }

        public int UpdateStuff(Stuff StuffVar)
        {
            try
            {
                base.SQL = "SP_UpdateStuff";
                base.InitializeCommand();
                base.AddParameter("@Id", StuffVar.Id);
                base.AddParameter("@Name", StuffVar.Name);
                base.AddParameter("@StuffGroupId", StuffVar.StuffGroupId);
                base.AddParameter("@Feature", StuffVar.Feature);
                base.AddParameter("@Comment", StuffVar.Comment);
                return base.ExecuteStoredProcedure();
            }
            catch (Exception exp)
            {
                if (exp.Message.Contains("UNIQUE KEY") && exp.Message.Contains("duplicate"))
                    return -1;
                else
                    return 0;
            }
        }

        public int DeleteStuff(string Id)
        {
            try
            {
                base.SQL = "SP_DeleteStuff";
                base.InitializeCommand();
                base.AddParameter("@Id", Id);
                return base.ExecuteStoredProcedure();
            }
            catch (Exception exp)
            {
                if (exp.Message.Contains("COLUMN REFERENCE"))
                    return -1;
                else
                    return 0;
            }
        }

        public Stuff ReadStuff(string Id)
        {
            try
            {
                Stuff StuffVar = new Stuff();
                DataTable dtTemp = new DataTable();
                base.SQL = "SP_ReadStuff";
                base.InitializeCommand();
                base.AddParameter("@Id", Id);
                base.FillDataTable(dtTemp);
                StuffVar.Id = dtTemp.Rows[0]["Id"].ToString();
                StuffVar.Name = dtTemp.Rows[0]["Name"].ToString();
                StuffVar.StuffGroupId = dtTemp.Rows[0]["StuffGroupId"].ToString();
                StuffVar.Feature = dtTemp.Rows[0]["Feature"].ToString();
                StuffVar.Comment = dtTemp.Rows[0]["Comment"].ToString();
                dtTemp.Dispose();
                return StuffVar;
            }
            catch (Exception exp)
            {
                return null;
            }
        }

        public DataTable SearchStuff(Stuff StuffVar)
        {
            try
            {
                DataTable dtTemp = new DataTable();
                base.SQL = "SP_SearchStuff";
                base.InitializeCommand();
                base.AddParameter("@Name", StuffVar.Name);
                base.AddParameter("@StuffGroupName", StuffVar.StuffGroupName);
                base.FillDataTable(dtTemp);
                return dtTemp;
            }
            catch (Exception exp)
            {
                return null;
            }
        }

        public DataTable ReportStuff(Stuff StuffVar)
        {
            try
            {
                DataTable dtTemp = new DataTable();
                base.SQL = "SP_ReportStuff";
                base.InitializeCommand();
                base.AddParameter("@Name", StuffVar.Name);
                base.AddParameter("@StuffGroupName", StuffVar.StuffGroupName);
                base.FillDataTable(dtTemp);
                return dtTemp;
            }
            catch (Exception exp)
            {
                return null;
            }
        }

        public DataTable GetStuffGroupForComboBox()
        {
            try
            {
                DataTable dtTemp = new DataTable();
                base.SQL = "SP_GetStuffGroupForComboBox";
                base.InitializeCommand();
                base.FillDataTable(dtTemp);
                return dtTemp;
            }
            catch (Exception exp)
            {
                return null;
            }
        }

        public DataTable GetStuffGroupForSearchComboBox()
        {
            try
            {
                DataTable dtTemp = new DataTable();
                DataRow dr = dtTemp.NewRow();

                base.SQL = "SP_GetStuffGroupForComboBox";
                base.InitializeCommand();
                base.FillDataTable(dtTemp);
                dr["Id"] = 0;
                dr["Name"] = "همه گروه ها";
                dtTemp.Rows.Add(dr);
                return dtTemp;
            }
            catch (Exception exp)
            {
                return null;
            }
        }

    }
}
