import { Component, HostBinding, OnInit } from '@angular/core';
declare var $: any;

import { SettingsService } from './core/settings/settings.service';
import {ToasterConfig, ToasterService} from 'angular2-toaster';

const swal = require('sweetalert');
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {

    @HostBinding('class.layout-fixed') get isFixed() { return this.settings.getLayoutSetting('isFixed'); };
    @HostBinding('class.aside-collapsed') get isCollapsed() { return this.settings.getLayoutSetting('isCollapsed'); };
    @HostBinding('class.layout-boxed') get isBoxed() { return this.settings.getLayoutSetting('isBoxed'); };
    @HostBinding('class.layout-fs') get useFullLayout() { return this.settings.getLayoutSetting('useFullLayout'); };
    @HostBinding('class.hidden-footer') get hiddenFooter() { return this.settings.getLayoutSetting('hiddenFooter'); };
    @HostBinding('class.layout-h') get horizontal() { return this.settings.getLayoutSetting('horizontal'); };
    @HostBinding('class.aside-float') get isFloat() { return this.settings.getLayoutSetting('isFloat'); };
    @HostBinding('class.offsidebar-open') get offsidebarOpen() { return this.settings.getLayoutSetting('offsidebarOpen'); };
    @HostBinding('class.aside-toggled') get asideToggled() { return this.settings.getLayoutSetting('asideToggled'); };
    @HostBinding('class.aside-collapsed-text') get isCollapsedText() { return this.settings.getLayoutSetting('isCollapsedText'); };

   public static GobalComponent: AppComponent;
   // 是否显示loading
   private loading ;

    // 右下角提示框
    toasterconfig: ToasterConfig = new ToasterConfig({
      positionClass: 'toast-bottom-right',
      showCloseButton: true,
      mouseoverTimerStop: true,
      limit: 3
    });


    constructor(public settings: SettingsService, private toasterService: ToasterService) {
       AppComponent.GobalComponent = this;
    }

    ngOnInit() {
      $(document).on('click', '[href="#"]', e => e.preventDefault());
    }

    // 获取右下角提示框
    public getToaster() {
        return this.toasterService;
    }

    // 设置是否显示Loading
    public  setLoading(flag: boolean) {
      this.loading = Boolean(flag);
    }

    public successMsg(msg) {
      AppComponent.GobalComponent.getToaster().pop('success', '', msg);
    }

    public errorMsg(msg) {
      AppComponent.GobalComponent.getToaster().pop('error', '', msg);
    }

    public warnMsg(msg) {
      AppComponent.GobalComponent.getToaster().pop('warn', '', msg);
    }

    public static confirm(msg, callback) {
      swal({
        title: '操作确认',
        text: msg,
        // type: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#17a2b8',
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        closeOnConfirm: false,
        closeOnCancel: true
      }, (isConfirm) => {
        if (isConfirm) {
          swal.close();
          callback();
        }
      });
    }

    public static confirmForTitle(title,msg, callback) {
      swal({
        title: title,
        text: msg,
        // type: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#17a2b8',
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        closeOnConfirm: false,
        closeOnCancel: true
      }, (isConfirm) => {
        if (isConfirm) {
          swal.close();
          callback();
        }
      });
    }

    public static alert(conf, callback?){
      swal({
        title: conf && conf.title?conf.title:'信息提示',
        text: conf && conf.text?conf.text:'请选择数据!',
        type: 'warning',
        // showCancelButton: true,
        confirmButtonColor: '#17a2b8',
        confirmButtonText: '确定',
        // cancelButtonText: '取消操作',
        // closeOnConfirm: false,
        // closeOnCancel: true
      }, (isConfirm) => {
        if (isConfirm) {
          if(callback){
            callback();
          }else{
            swal.close();
          }
        }
      });
    }
}
