using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
namespace WpfApplication19
{
	/// <summary>
	/// Interaction logic for SearchAgreement.xaml
	/// </summary>
	public partial class SearchAgreement : Window
	{
		public SearchAgreement()
		{
			InitializeComponent();
		}

		private void Window_Loaded(object sender, RoutedEventArgs e)
		{

		}

		private void searchAgreementBtn_Click(object sender, RoutedEventArgs e)
		{
			var fileName = agreementInput.Text;
			try
			{
				//System.Diagnostics.Process.Start(@"agreements\" + fileName);

				var server = Utils.getSetting("webServerURL");
				string data = string.Format(@"title={0}&empid={1}&password={2}", fileName, GlobalVars.user.empid, GlobalVars.user.password);
				WebAppConnector wp = new WebAppConnector()
				{

					Uri = Utils.getSetting("webServerURL") + "/agreements/title.json",
					Method = "POST",
					PostData = data
				};
				Response r = null;
				try
				{
					r = wp.sendRequest();
				}
				catch
				{
					MessageBox.Show("Cannot connect to webserver :" + server);
					return;
				}
				string ans = (string)r.jsonBody["msg"];
				//Console.WriteLine((JArray)r.jsonBody["roles"]);

				if (r.status == "OK" && ans == "y")
				{
					System.Diagnostics.Process.Start(server + (string)r.jsonBody["url"]);
					
				}
				else
				{
					warning.Text = "Agreement: "+fileName+" Not Founded";
					warning.Visibility = Visibility.Visible;
				}
			}
			catch (Win32Exception ew)
			{
				MessageBox.Show(ew.Message);
			}
		}
	}
}
