import { Component, OnInit } from '@angular/core';
import {LocalStorageService} from '../../services/local-storage.service';
import {DATA, ROUTENAME, MENUNAME, CHILDMENUNAME, INIT_FLAG, ADMIN_INFO_OBJECT, TOKEN} from '../../services/local-storage.namespace';
import {LoginService} from '../../services/login.service';
import {NzMessageService} from 'ng-zorro-antd';
import {Router} from '@angular/router';
import {IconService} from '../../services/icon.service';
import {RechargeService} from '../../services/recharge.service';
import {CommonsService} from '../../services/commons.service';

@Component({
  selector: 'app-navigation',
  templateUrl: './navigation.component.html',
  styleUrls: ['./navigation.component.less']
})
export class NavigationComponent implements OnInit {

  isCollapsed = false;
  userInfo: any;
  menuList: any;
  routeName: string;
  switchValue = false;
  constructor(
    private storage: LocalStorageService,
    private loginService: LoginService,
    private message: NzMessageService,
    private commonsService: CommonsService,
    private iconService: IconService,
    private router: Router,
  ) { }

  ngOnInit(): void {
    this.routeName = this.storage.get(ROUTENAME) == null ? 'index' : this.storage.get(ROUTENAME);
    this.userInfo = this.storage.getList(ADMIN_INFO_OBJECT);
    this.commonsService.mappingSysNameOl('RECHARGE', data => {
      this.switchValue = data['return_data']['codeValue'] === '1';
    });
    this.menuList = this.storage.getList(ADMIN_INFO_OBJECT)['menuList']; // 定义菜单数据
  }

  public activeLi(routeName): void {
    this.routeName = routeName;
    this.storage.set(ROUTENAME, routeName);
  }

  // 菜单姓名
  public getRouteName(menuName , childMenuName): void {
    this.storage.set(MENUNAME, menuName);
    this.storage.set(CHILDMENUNAME, childMenuName);
  }

  // 登出
  public loginOut(): void {
    this.loginService.loginOut(data => {
      if (data.return_code === '000000') {
        this.storage.set(ADMIN_INFO_OBJECT, null);
        this.storage.set(INIT_FLAG, false);
        this.storage.set(TOKEN, null);
        this.router.navigateByUrl('/adminLogin').then();
      } else {
        this.message.warning(data.return_msg);
      }
    });
  }

  // 返回
  public getBack(): void {
    history.back();
  }

  clickSwitch(): void {
    this.switchValue = !this.switchValue;
    this.commonsService.updateDictionary('RECHARGE' , this.switchValue === true ? '1' : '0' , data => {
      if (data['return_code'] === '000000') {
        this.message.success(data['return_data']);
      } else {
        this.message.error(data['return_msg']);
      }
    });
  }

}
