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

namespace DAL
{
    public sealed class CustomerMethods:Method
    {
        public bool Insert(string cid, string fname, string lname, string tel, string address, string bedehkar, string talabkar)
        {
            SqlParameter[] p = new SqlParameter[7];
            p[0] = new SqlParameter();
            p[0].ParameterName = "@cid";
            p[0].SqlDbType = System.Data.SqlDbType.BigInt;
            p[0].Value = cid;
            //
            p[1] = new SqlParameter();
            p[1].ParameterName = "@fname";
            p[1].SqlDbType = System.Data.SqlDbType.NChar;
            p[1].Value = fname;
            //
            p[2] = new SqlParameter();
            p[2].ParameterName = "@lname";
            p[2].SqlDbType = System.Data.SqlDbType.NChar;
            p[2].Value = lname;
            //
            p[3] = new SqlParameter();
            p[3].ParameterName = "@tel";
            p[3].SqlDbType = System.Data.SqlDbType.NChar;
            p[3].Value = tel;
            //
            p[4] = new SqlParameter();
            p[4].ParameterName = "@address";
            p[4].SqlDbType = System.Data.SqlDbType.NChar;
            p[4].Value = address;
            //
            p[5] = new SqlParameter();
            p[5].ParameterName = "@bedehkar";
            p[5].SqlDbType = System.Data.SqlDbType.Decimal;
            p[5].Value = bedehkar;
            //
            p[6] = new SqlParameter();
            p[6].ParameterName = "@talabkar";
            p[6].SqlDbType = System.Data.SqlDbType.Decimal;
            p[6].Value = talabkar;

            return this.ExecuteNonQuery("InsertCustomer", p, true);
        }

        public bool Update(string cid, string fname, string lname, string tel, string address, string bedehkar, string talabkar)
        {
            SqlParameter[] p = new SqlParameter[7];
            p[0] = new SqlParameter();
            p[0].ParameterName = "@cid";
            p[0].SqlDbType = System.Data.SqlDbType.BigInt;
            p[0].Value = cid;
            //
            p[1] = new SqlParameter();
            p[1].ParameterName = "@fname";
            p[1].SqlDbType = System.Data.SqlDbType.NChar;
            p[1].Value = fname;
            //
            p[2] = new SqlParameter();
            p[2].ParameterName = "@lname";
            p[2].SqlDbType = System.Data.SqlDbType.NChar;
            p[2].Value = lname;
            //
            p[3] = new SqlParameter();
            p[3].ParameterName = "@tel";
            p[3].SqlDbType = System.Data.SqlDbType.NChar;
            p[3].Value = tel;
            //
            p[4] = new SqlParameter();
            p[4].ParameterName = "@address";
            p[4].SqlDbType = System.Data.SqlDbType.NChar;
            p[4].Value = address;
            //
            p[5] = new SqlParameter();
            p[5].ParameterName = "@bedehkar";
            p[5].SqlDbType = System.Data.SqlDbType.Decimal;
            p[5].Value = bedehkar;
            //
            p[6] = new SqlParameter();
            p[6].ParameterName = "@talabkar";
            p[6].SqlDbType = System.Data.SqlDbType.Decimal;
            p[6].Value = talabkar;

            return this.ExecuteNonQuery("UpdateCustomer", p, true);
        }

        public bool Delete(string cid)
        {
            SqlParameter[] p = new SqlParameter[1];
            p[0] = new SqlParameter();
            p[0].ParameterName = "@cid";
            p[0].SqlDbType = System.Data.SqlDbType.BigInt;
            p[0].Value = cid;
            return this.ExecuteNonQuery("DELETE FROM Customers WHERE cid=@cid", p, false);
        }

        public bool AddToBedehkari(string cid, string mablagh)
        {
            SqlParameter[] p = new SqlParameter[2];
            p[0] = new SqlParameter();
            p[0].ParameterName = "@cid";
            p[0].SqlDbType = System.Data.SqlDbType.BigInt;
            p[0].Value = cid;

            p[1] = new SqlParameter();
            p[1].ParameterName = "@mablagh";
            p[1].SqlDbType = System.Data.SqlDbType.Decimal;
            p[1].Value = mablagh;

            return this.ExecuteNonQuery("UPDATE Customers SET bedehkar=bedehkar+@mablagh WHERE cid=@cid", p, false);
        }

        public object SelectAll()
        {
            return this.ExecuteReader("SELECT * FROM Customers", null, false);
        }

        public object Select(long cid)
        {
            SqlParameter[] p = new SqlParameter[1];
            p[0] = new SqlParameter();
            p[0].ParameterName = "@cid";
            p[0].SqlDbType = System.Data.SqlDbType.BigInt;
            p[0].Value = cid;
            return this.ExecuteReader("SELECT * FROM Customers WHERE cid=@cid", p, false);
        }

    }
}
