import { Component, OnInit, ViewChild } from '@angular/core';
import { STComponent, STColumn, STChange } from '@delon/abc/st';
import { SFComponent, SFSchema } from '@delon/form';
import { NzModalService } from 'ng-zorro-antd/modal';
import { SystemService } from '../../services/system.service';
import { BasicConfigActionModalComponent } from './basic-config-action-modal/basic-config-action-modal.component';

@Component({
  selector: 'app-basic-config',
  templateUrl: './basic-config.component.html',
  styleUrls: ['./basic-config.component.less']
})
export class BasicConfigComponent implements OnInit {
  @ViewChild('st', { static: true })
  st!: STComponent;
  @ViewChild('sf', { static: false })
  sf!: SFComponent;

  url = `/rule?_allow_anonymous=true`;

  searchSchema: SFSchema = {
    properties: {
      receiveName: {
        title: '配置类型',
        type: 'string',
        ui: {
          widget: 'select',
          placeholder: '请选择',
          // asyncData: () => {
          //   return this.service.request(this.service.$api_getAppRoleList).pipe(
          //     map((res: any) => {
          //       this.roleList = res;
          //       return res.map((item: any) => {
          //         return { label: item.roleName, value: item.id };
          //       });
          //     }),
          //   );
          // },
          change: (i: any) => {
            this.sf.value.receiveName = i;
            this.sf?.setValue('/receiveName', i);
          }
        }
      }
    }
  };

  columns: STColumn[] = [
    { title: '配置类型', index: 'no' },
    { title: '配置项', index: 'description' },
    {
      title: '启用状态',
      className: 'text-center',
      index: 'status',
      type: 'badge',
      badge: {
        0: { text: '启用', color: 'success' },
        2: { text: '停用', color: 'error' },
        3: { text: '停用', color: 'error' },
        1: { text: '停用', color: 'error' }
      }
    },
    {
      title: '创建时间',
      index: 'updatedAt',
      type: 'date'
    },
    {
      title: '操作',
      className: 'text-center',
      buttons: [
        {
          text: '编辑',
          click: item => this.configAction(item)
        },
        {
          text: '停用',
          click: item => this.deactivateConfig(item)
        }
      ]
    }
  ];

  selectedRows: any[] = [];

  reqParams = { pageIndex: 1, pageSize: 10 };

  constructor(public service: SystemService, private nzModalService: NzModalService) {}

  ngOnInit(): void {}

  stChange(e: STChange): void {
    switch (e.type) {
      case 'checkbox':
        this.selectedRows = e.checkbox!;
        break;
      case 'filter':
        this.st.load();
        break;
    }
  }

  configAction(item?: any) {
    const modal = this.nzModalService.create({
      nzContent: BasicConfigActionModalComponent,
      nzComponentParams: item ? { i: { ...item, roleId: 1, name: '车辆审核', phone: 18555555555 } } : { i: { id: 0 } },
      nzFooter: null
    });
    modal.afterClose.subscribe(res => {
      this.st.load();
    });
  }

  deactivateConfig(item?: any) {
    this.nzModalService.error({
      nzTitle: '确认停用？',
      nzContent: `<label class="error-color">停用后配置将无法使用，请谨慎操作！</label>`,
      nzClosable: false,
      nzCancelText: '取消',
      nzOnOk: () => {}
    });
  }
  /**
   * 重置表单
   */
  resetSF() {
    this.sf.reset();
  }
}
