// 袁野
// 2019-05-21
import {Component, OnInit} from '@angular/core';
import {PlatformLocation} from '@angular/common';
import {HttpClient} from '@angular/common/http';
import {NzFormatEmitEvent, NzMessageService, NzModalService} from 'ng-zorro-antd';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {ActivatedRoute} from '@angular/router';
import {LocalStorageService} from '../../../services/local-storage.service';
import {ADMIN_INFO_OBJECT} from '../../../services/local-storage.namespace';
import {environment} from '../../../../environments/environment';

declare var $: any;
@Component({
  selector: 'app-system-role-show',
  templateUrl: './system-role-show.component.html',
  styleUrls: ['./system-role-show.component.less', '../../../../assets/css/bootstrap-treeview.min.css']
})
export class SystemRoleShowComponent implements OnInit {
  WEB_SERVE_URL = environment.baseUrl;
  role: any = {
    companyId: '',
    roleType: '',
    roleName: '',
    roleDesc: '',
    enableStatus: '',
    sId: '',
    createTime: '',
    updateTime: '',
    templetType : 0
  };
  rolePermission: any = {
    roleId: '',
    pmsIds: new Array()
  };
  test = [];
  dataTree;
  private locationUrl: any;
  constructor(
    private location: PlatformLocation, // 路径
    private http: HttpClient, // http请求
    private message: NzMessageService, // 信息提示
    private fb: FormBuilder, // 表单
    private modalService: NzModalService, // 对话框
    private store: LocalStorageService ,
    private activatedRoute: ActivatedRoute,
  ) {
  }


  ngOnInit() {

    // 查询详情
    this.activatedRoute.queryParams.subscribe(queryParams => {
      this.role = queryParams;

      let roleId = this.store.get(ADMIN_INFO_OBJECT)['secRole'].id;
      let roleType = this.store.get(ADMIN_INFO_OBJECT)['secRole'].roleType;
      const allotRoleId = this.role.id;
      if (this.role.templetType === 1) {
        // 如果是模板
        roleId = '';
        roleType = 0;
      }

      this.http.get(this.WEB_SERVE_URL + '/secMenu/findMenuTree?roleId=' + roleId + '&roleType=' + roleType + '&allotRoleId=' + allotRoleId).subscribe( data => {
        // this.goInformation(data['return_data']);
        this.goTree(data['return_data'] );
      });

    });
  }

  // 循环树数据
  goInformation(data: any) {

     this.dataTree = data;
    // 请求查询留言
     this.goTree(data);
  }
  goTree(res) {
    const that = this;
    $('#tree').treeview({
      showCheckbox: true,
      levels: 0,
      data: res,
      onNodeChecked: function getArray(event, data) {
        if (data.parentId != null) {
          const parentNode = $('#tree').treeview('getNode', data.parentId);
          $('#tree').treeview('checkNode', parentNode.nodeId);
          if (parentNode.parentId != null) {
            getArray(parentNode, parentNode);
          }
        }
        that.test = $('#tree').treeview('getChecked');
      },
      onNodeUnchecked: function getArray(event, data) {
        if (data.nodes != null) {
          // tslint:disable-next-line:prefer-for-of
          for (let i = 0; i < data.nodes.length; i++) {
            $('#tree').treeview('uncheckNode', [data.nodes[i].nodeId, {silent: true}]);
            getArray(data.nodes[i], data.nodes[i]);
          }
        }
        that.test = $('#tree').treeview('getChecked');
      }
    });
  }

  // 保存分配
  savePermission() {
    // tslint:disable-next-line:prefer-for-of
    for (let i = 0 ; i < this.test.length ; i++) {
      this.rolePermission.pmsIds.push(this.test[i].id);
    }

    let requestUrl = null;
    if (this.role.templetType === '1') {
      requestUrl = '/secPermission/allotTemplatePermission';
    } else if (this.role.templetType === '0') {
      requestUrl = '/secPermission/allotPermission';
    } else {
      this.message.create('warning', '角色信息错误');
      return;
    }

    this.rolePermission.roleId = this.role.id;
    this.http.post(this.WEB_SERVE_URL + requestUrl, this.rolePermission).subscribe(data => {
      if (data['return_code'] === '000000') {
        history.back();
        this.message.create('success', '提交成功');
      } else {
        this.message.create('warning', data['return_msg']);
      }
    });
  }

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

}
