import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { STColumn, STComponent, STData, STRequestOptions } from '@delon/abc/st';
import { SFComponent, SFSchema, SFUISchema } from '@delon/form';
import { processSingleSort } from '@shared';
import { NzModalService } from 'ng-zorro-antd/modal';
import { ChannelSalesService } from '../../services/level-config.service';
import { ParterLevelConfigEditComponent } from '../edit/edit.component';

@Component({
  selector: 'app-parter-LevelConfig-list',
  templateUrl: './list.component.html'
})
export class ParterLevelConfigListComponent implements OnInit {
  schema: SFSchema = {};
  columns!: STColumn[];
  ui!: SFUISchema;
  @ViewChild('st', { static: false })
  st!: STComponent;
  @ViewChild('sf', { static: false })
  sf!: SFComponent;
  spuStatus = '1';

  data=[{name1:1111}]
  constructor(
    public router: Router,
    public ar: ActivatedRoute,
    public service: ChannelSalesService,
    private modalService: NzModalService
  ) {}

  /**
   * 查询参数
   */
  get reqParams() {
    return { ...this.sf?.value };
  }
  ngOnInit() {
    this.initSF();
    this.initST();
  }

  initSF() {
    this.schema = {
      properties: {
        gradeName: {
          type: 'string',
          title: '等级姓名',
        },
        stateLocked: {
          type: 'string',
          title: '状态',
          enum:[{label:'启用',value:'1'},{label:'禁用',value:'0'}],
          ui:{
            widget:'select',
          }
        },
      }
    };
    this.ui = {
      '*': {
        width:300,
        grid: { span: 12, gutter: 4 }
      }
    };
  }

  initST() {
    this.columns = [
      {
        title: '等级姓名',
        index: 'gradeName'
      },
      {
        title: '备注',
        index: 'remark'
      },
      {
        title: '创建时间',
        index: 'createTime'
      },
      {
        title: '启用时间',
        index: 'enableTime'
      },
      {
        title: '状态',
        index: 'stateLocked',
        format: (item: any) => {
          return item.stateLocked ? '禁用':'启用'
        }
      },
      {
        title: '操作',
        className: 'text-center',
        buttons: [
          {
            text: '编辑',
            click: (_record, _modal, _instance) => this.edit(_record),
          },
          {
            text: '禁用',
            click: (_record, _modal, _instance) => this.stop(_record),
            iif:(item)=>!item.stateLocked
          },
          {
            text: '启用',
            click: (_record, _modal, _instance) => this.restart(_record),
            iif:(item)=>item.stateLocked
          }
        ]
      }
    ];
  }

  add() {
    const modalRef = this.modalService.create({
      nzWidth:500,
      nzTitle: '新增',
      nzContent: ParterLevelConfigEditComponent,
      nzComponentParams: { type: this.spuStatus }
    });
    modalRef.afterClose.subscribe(res => {
      if (res) {
        this.st.reload();
      }
    });
  }

  // 编辑
  edit(record: STData) {
    const modalRef = this.modalService.create({
      nzWidth:500,
      nzTitle: '编辑',
      nzContent: ParterLevelConfigEditComponent,
      nzComponentParams: { i: record, type: this.spuStatus }
    });
    modalRef.afterClose.subscribe(res => {
      if (res) {
        this.st.reload();
      }
    });
  }

   // 编辑
   view(record: STData) {
    const modalRef = this.modalService.create({
      nzTitle: '查看',
      nzContent: ParterLevelConfigEditComponent,
      nzComponentParams: { i: record }
    });
  }

  restart(item: any) {
    this.modalService.confirm({
      nzTitle: '<i>启用确认</i>',
      nzContent: `<b>确定启用该账号吗？</br>`,
      nzOnOk: () =>
        this.service.request(this.service.$api_updatePartnerGradeConfig, {id:item.id}).subscribe(res => {
          if (res) {
            this.service.msgSrv.success('启用成功！');
            this.st.reload();
          }
        })
    });
  }
  stop(item: any) {
    this.modalService.confirm({
      nzTitle: '<i>禁用确认</i>',
      nzContent: `<b>确定禁用该账号吗？</br>`,
      nzOnOk: () =>
        this.service.request(this.service.$api_updatePartnerGradeConfig, {id:item.id}).subscribe(res => {
          if (res) {
            this.service.msgSrv.success('禁用成功！');
            this.st.reload();
          }
        })
    });
  }

  /**
   * 重置表单
   */
  resetSF() {
    this.sf.reset();
    this.st.load(1);
  }


}
