using BPASmartClient.CustomResource.UserControls;
using BPASmartClient.CustomResource.UserControls.MessageShow;
using BPASmartClient.Helper;
using BPASmartClient.ViewModel;
using DataVAPI.Tool.IOT;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Navigation;
using System.Windows.Shapes;

namespace BPASmartClient.Control
{
    /// <summary>
    /// LogOrAlarmIOTView.xaml 的交互逻辑
    /// </summary>
    public partial class LogOrAlarmIOTView : UserControl
    {
        public LogOrAlarmIOTView()
        {
            InitializeComponent();
            this.DataContext = LogOrAlarmIOTViewModel.GetInstance();
        }
        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Button button = (Button)sender;
                if (button.Tag != null)
                {
                    string url=button.Tag.ToString();
                    SaveFileDialog openfile = new SaveFileDialog();
                    openfile.Filter = "log文件(*.log)|*.log";
                    openfile.FileName = button.ToolTip.ToString();
                    if (openfile.ShowDialog() == false)
                    {
                        return;
                    }
                    string path = openfile.FileName;
                    Tools.HttpDownloadFile(url,path);
                    NoticeDemoViewModel.OpenMsg(EnumPromptType.Info, MainViewModel.GetInstance().window, "提示", $"下载成功!");
                    string msg = string.Format("下载成功,是否打开！");
                    if (System.Windows.MessageBox.Show(msg, "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                    {
                        logHelper.GetLogConfigInstance().OpenFile(openfile.FileName);
                    }
                }
            }
            catch (Exception ex)
            {
                NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, MainViewModel.GetInstance().window, "提示", $"下载失败!");
            }
        }
    }
}
