import { Component, OnInit } from '@angular/core';
import {FormBuilder, FormGroup} from '@angular/forms';
import {HttpClient} from '@angular/common/http';
import {NzIconService, NzMessageService, NzModalService} from 'ng-zorro-antd';
import {Router} from '@angular/router';
import {environment} from '../../../../environments/environment';

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

  /* 请求参数-查询 */
  queryString = null;
  queryType = null;
  pageNum = 1;
  pageSize = 10;
  /* 查询结果数据 */
  requestData = [];
  total = 0;
  /* 权限 */
  WEB_SERVE_URL = environment.baseUrl;
  USER_INFO;    // 用户信息
  companyId;
  buttonList;    // 按钮权限列表
  FILE_URL = environment.imageUrl;
  /* 页面元素 */
  validateForm: FormGroup;
  isLoadingSearch = false;
  loading = true;

  resetForm(): void {
    this.validateForm.reset();
  }

  constructor(
    private fb: FormBuilder,
    private http: HttpClient, // http请求
    private message: NzMessageService, // 信息提示
    private router: Router,
    private modalService: NzModalService, // 对话框
    private iconService: NzIconService, // 引入 Iconfont.cn
  ) { }

  ngOnInit() {
    // 引入 Iconfont.cn
    this.iconService.fetchFromIconfont({
      scriptUrl: 'https://at.alicdn.com/t/font_1696267_usdl17u5ce.js'
    });

    this.validateForm = this.fb.group({
      queryString: [null],
      queryType: [null],
    });

    this.getSearch();
  }

  /* 查询列表 */
  getRequest(reset: boolean = false) {
    this.loading = false;
    if (reset) {
      this.pageNum = 1;
    }

    let permissionName = '';
    let type = '';
    if (this.queryString != null && this.queryString !== '') {
      permissionName = '&permissionName=' + this.queryString;
    }
    if (this.queryType != null && this.queryType !== '') {
      type = '&type=' + this.queryType;
    }
    const pageNum = '&pageNum=' + this.pageNum;
    const pageSize = '&pageSize=' + this.pageSize;

    this.http.get(this.WEB_SERVE_URL + '/secPermission/getPermissionList?' + pageNum + pageSize + permissionName + type).subscribe(data => {
      if (data['return_code'] === '000000') {
        this.requestData = data['return_data'].list;
        this.total = data['return_data'].total;
        this.isLoadingSearch = false;
      } else {
        this.message.create('error', data['return_msg']);
      }
    });
  }

  /* 搜索 */
  getSearch() {
    if (this.validateForm.value.queryString == null) {
      this.queryString = '';
    } else {
      this.queryString = this.validateForm.value.queryString;
    }
    if (this.validateForm.value.queryType == null) {
      this.queryType = '';
    } else {
      this.queryType = this.validateForm.value.queryType;
    }

    this.getRequest();
  }

}
