import {Component, OnInit} from '@angular/core';
import {environment} from "../../../../environments/environment";
import {ReactiveFormsModule, UntypedFormBuilder, UntypedFormGroup} from "@angular/forms";
import {HttpClient} from "@angular/common/http";
import {NzMessageService} from "ng-zorro-antd/message";
import {Router, RouterLink} from "@angular/router";
import {NzModalService} from "ng-zorro-antd/modal";
import {NzIconDirective, NzIconService} from "ng-zorro-antd/icon";
import {SalesmanStatusPipe} from "../../../pipes/salesman-status.pipe";
import {DatePipe, NgForOf, NgIf} from "@angular/common";
import {BreadcrumbComponent} from "../../common/breadcrumb/breadcrumb.component";
import {NzFormDirective} from "ng-zorro-antd/form";
import {NzColDirective, NzRowDirective} from "ng-zorro-antd/grid";
import {NzOptionComponent, NzSelectComponent} from "ng-zorro-antd/select";
import {NzInputDirective} from "ng-zorro-antd/input";
import {NzButtonComponent} from "ng-zorro-antd/button";
import {NzCellFixedDirective, NzTableComponent, NzThMeasureDirective} from "ng-zorro-antd/table";
import {AgentStatusPipe} from "../../../pipes/agent-status.pipe";
import {NzBreadCrumbComponent, NzBreadCrumbItemComponent} from "ng-zorro-antd/breadcrumb";
import {NzSiderComponent} from "ng-zorro-antd/layout";

@Component({
  selector: 'app-permission',
  standalone: true,
  imports: [
    RouterLink,
    SalesmanStatusPipe,
    DatePipe,
    BreadcrumbComponent,
    NzFormDirective,
    NzRowDirective,
    NzColDirective,
    NzSelectComponent,
    NzOptionComponent,
    ReactiveFormsModule,
    NgIf,
    NzInputDirective,
    NzButtonComponent,
    NzIconDirective,
    NzTableComponent,
    NzThMeasureDirective,
    NzCellFixedDirective,
    NgForOf,
    AgentStatusPipe,
    NzBreadCrumbItemComponent,
    NzBreadCrumbComponent,
    NzSiderComponent
  ],
  templateUrl: './permission.component.html',
  styleUrl: './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: UntypedFormGroup;
  isLoadingSearch = false;
  loading = true;

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

  constructor(
    private fb: UntypedFormBuilder,
    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();
  }
}
