//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using NopSolutions.NopCommerce.Common;
using NopSolutions.NopCommerce.Common.CustomerManagement;
using NopSolutions.NopCommerce.Common.ExportImport;
using NopSolutions.NopCommerce.Common.Utils;

namespace NopSolutions.NopCommerce.Web.Administration.Modules
{
    public partial class CustomersControl : BaseNopAdministrationUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                SetDefaultValues();
                phUsername.Visible = CustomerManager.UsernamesEnabled;
                gvCustomers.Columns[3].Visible = CustomerManager.UsernamesEnabled;
            }
        }

        protected void SetDefaultValues()
        {
            txtStartDate.Text = DateTime.Now.AddYears(-1).ToString(cStartDateButtonExtender.Format);
            txtEndDate.Text = DateTime.Now.AddMonths(1).ToString(cEndDateButtonExtender.Format);
        }

        protected CustomerCollection GetCustomers()
        {
            DateTime startDate = DateTime.MinValue;
            DateTime endDate = DateTime.MaxValue;
            if (!DateTime.TryParse(txtStartDate.Text, out startDate))
                throw new NopException("Start date is not set");
            if (!DateTime.TryParse(txtEndDate.Text, out endDate))
                throw new NopException("End date is not set");

            string email = txtEmail.Text;
            string username = txtUsername.Text;

            int totalRecords = 0;
            CustomerCollection customers = CustomerManager.GetAllCustomers(startDate,
                endDate, email, username, int.MaxValue, 0, out totalRecords);
            return customers;
        }

        protected void BindGrid()
        {
            CustomerCollection customers = GetCustomers();
            gvCustomers.DataSource = customers;
            gvCustomers.DataBind();
        }

        protected void gvCustomers_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvCustomers.PageIndex = e.NewPageIndex;
            BindGrid();
        }

        protected void SearchButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    BindGrid();
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
            }
        }

        protected void btnExportXML_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    string fileName = string.Format("customers_{0}.xml", DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss"));

                    CustomerCollection customers = GetCustomers();
                    string xml = ExportManager.ExportCustomersToXML(customers);
                    CommonHelper.WriteResponseXML(xml, fileName);
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
            }
        }
    }
}