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 CrCRUD : Aram_CRM_Project.DAL.DA
    {
        public int InsertCr(Cr CrVar)
        {
            try
            {
                base.SQL = "SP_InsertCr";
                base.InitializeCommand();
                base.AddParameter("@Date", CrVar.Date);
                base.AddParameter("@Subject", CrVar.Subject);
                base.AddParameter("@Comment", CrVar.Comment);
                base.AddParameter("@CustomerId", CrVar.CustomerId);
                base.AddParameter("@ResultCrId", CrVar.ResultCrId);
                base.AddParameter("@CrTypeId", CrVar.CrTypeId);
                return base.ExecuteStoredProcedure();
            }
            catch (Exception exp)
            {
                if (exp.Message.Contains("UNIQUE KEY") && exp.Message.Contains("duplicate"))
                    return -1;
                else
                    return 0;
            }
        }

        public int UpdateCr(Cr CrVar)
        {
            try
            {
                base.SQL = "SP_UpdateCr";
                base.InitializeCommand();
                base.AddParameter("@Id", CrVar.Id);
                base.AddParameter("@Date", CrVar.Date);
                base.AddParameter("@Subject", CrVar.Subject);
                base.AddParameter("@Comment", CrVar.Comment);
                base.AddParameter("@CustomerId", CrVar.CustomerId);
                base.AddParameter("@ResultCrId", CrVar.ResultCrId);
                base.AddParameter("@CrTypeId", CrVar.CrTypeId);
                return base.ExecuteStoredProcedure();
            }
            catch (Exception exp)
            {
                if (exp.Message.Contains("UNIQUE KEY") && exp.Message.Contains("duplicate"))
                    return -1;
                else
                    return 0;
            }
        }

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

        public Cr ReadCr(string Id)
        {
            try
            {
                Cr CrVar = new Cr();
                DataTable dtTemp = new DataTable();
                base.SQL = "SP_ReadCr";
                base.InitializeCommand();
                base.AddParameter("@Id", Id);
                base.FillDataTable(dtTemp);
                //dtTemp.Rows[0][""].ToString();
                CrVar.Id = dtTemp.Rows[0]["Id"].ToString();
                CrVar.Date = dtTemp.Rows[0]["Date"].ToString();
                CrVar.Subject = dtTemp.Rows[0]["Subject"].ToString();
                CrVar.Comment = dtTemp.Rows[0]["Comment"].ToString();
                CrVar.ResultCrId = dtTemp.Rows[0]["ResultCrId"].ToString();
                CrVar.CustomerId = dtTemp.Rows[0]["CustomerId"].ToString();
                CrVar.CustomerName = dtTemp.Rows[0]["Name"].ToString();
                CrVar.Tel = dtTemp.Rows[0]["Tel"].ToString();
                CrVar.CrTypeId = dtTemp.Rows[0]["CrTypeId"].ToString();
                dtTemp.Dispose();
                return CrVar;
            }
            catch (Exception exp)
            {
                return null;
            }
        }

        

        public DataTable SearchCr(Cr CrVar)
        {
            try
            {
                DataTable dtTemp = new DataTable();
                base.SQL = "SP_SearchCr";
                base.InitializeCommand();
                base.AddParameter("@Subject", CrVar.Subject);
                base.AddParameter("@ResultCrName", CrVar.ResultCrName);
                base.AddParameter("@CustomerName", CrVar.CustomerName);
                base.AddParameter("@CustomerGroupName", CrVar.CustomerGroupName);
                base.AddParameter("@StartDate", CrVar.StartDate);
                base.AddParameter("@EndDate", CrVar.EndDate);
                base.FillDataTable(dtTemp);
                return dtTemp;
            }
            catch (Exception exp)
            {
                return null;
            }
        }

        public DataTable SearchCrWithCrTypeId(Cr CrVar)
        {
            try
            {
                DataTable dtTemp = new DataTable();
                base.SQL = "SP_SearchCrWithCrTypeId";
                base.InitializeCommand();
                base.AddParameter("@Subject", CrVar.Subject);
                base.AddParameter("@ResultCrName", CrVar.ResultCrName);
                base.AddParameter("@CustomerName", CrVar.CustomerName);
                base.AddParameter("@CustomerGroupName", CrVar.CustomerGroupName);
                base.AddParameter("@StartDate", CrVar.StartDate);
                base.AddParameter("@EndDate", CrVar.EndDate);
                base.AddParameter("@CrTypeId", CrVar.CrTypeId);
                base.FillDataTable(dtTemp);
                return dtTemp;
            }
            catch (Exception exp)
            {
                return null;
            }
        }

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

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

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

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

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