import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { STColumn, STComponent, STData } from '@delon/abc/st';
import { SFComponent, SFSchema, SFSelectWidgetSchema, SFUISchema } from '@delon/form';
import { ModalHelper } from '@delon/theme';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalService } from 'ng-zorro-antd/modal';
import { ScrollImgService } from '../../services/scrollimg.service';

@Component({
  selector: 'app-scrollimg-components-list',
  templateUrl: './list.component.html',
  styleUrls: ['./list.component.less']
})

export class ScrollImgComponentsListComponent implements OnInit {
  schema: SFSchema = {};
  columns: STColumn[] = [];
  ui: SFUISchema = {};
  appList: any[] = [];
  _$expand = false;
  selectApp = {
    appName: '',
    appId: ''
  };
  isLoading: boolean = false;

  @ViewChild('st', { static: false }) st!: STComponent;
  @ViewChild('sf', { static: false }) sf!: SFComponent;
  constructor(public service: ScrollImgService, private modal: ModalHelper, private msg: NzMessageService, private router: Router, private modalSrv: NzModalService,  private ar: ActivatedRoute) { }

  /**
   * 查询字段个数
   */
  get queryFieldCount(): number {
    return Object.keys(this.schema?.properties || {}).length;
  }

  /**
   * 查询参数
   */
  get reqParams() {
    const params = Object.assign({}, this.sf?.value || {});
    delete params._$expand;
    if (params.status === '') {
      delete params.status;
    }
    if (params.style === '') {
      delete params.style;
    }
    if (params.navigationId === '') {
      delete params.navigationId;
    }
    return { ...params};
  }

  /**
   * 选中行
   */
  get selectedRows() {
    return this.st?.list.filter((item) => item.checked) || [];
  }
  ngOnInit() {
    this.initSF();
    this.initST();
  }
  selectAppFun(item: any) {
    this.selectApp = item;
    this.st.load(1);
  }
  dataProcess(data: STData[]): STData[] {
    return data.map((i, index) => {
      i.showSortFlag = false;
      return i;
    });
  }
  initSF() {
    this.schema = {
      properties: {
        _$expand: {
          type: 'boolean',
          ui: {
            hidden: true,
          },
        },
        name: {
          type: 'string',
          title: '轮播图名称',
          maxLength: 10,
          ui: {
            widget: '',
            placeholder: '请输入',
          }
        },
        status1: {
          type: 'string',
          title: '可见范围',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
          } as SFSelectWidgetSchema,
          enum: [
            { label: '全部', value: 1 },
            { label: '全部可见', value: 2 },
            { label: '渠道销售可见', value: 3 },
            { label: '合伙人可见', value: 4 },
          ]
        },
        status: {
          type: 'string',
          title: '状态',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
          } as SFSelectWidgetSchema,
          enum: [
            { label: '全部', value: 1 },
            { label: '正常', value: 2 },
            { label: '禁用', value: 3 }
          ]
        },
      },
    };
    this.ui = {
      '*': {
        spanLabelFixed: 110,
        grid: { span: 8 },
      },
    };
  }

  initST() {
    this.columns = [
      {
        title: '轮播图名称', // 位:px
        index: 'name',
        className: 'text-center'
      },
      {
        title: '轮播图',
        index: 'navigationName',
        className: 'text-center'
      },
      {
        title: '可见范围',
        index: 'navigationName',
        className: 'text-center'
      },
      {
        title: '排序', // 位 px
        index: 'sortId',
        className: 'text-center'
      },
      {
        title: '状态', // 位 px
        index: 'style',
        className: 'text-center',
        type: 'enum',
        enum: { 
          1: '正常',
          2: '禁用',
        }
      },
      {
        title: '最后修改时间', // 位 px
        index: 'createTime',
        className: 'text-center'
      },
      {
        title: '操作',
        fixed: 'right',
        className: 'text-center',
        width: 280,
        buttons: [
          {
            text: '修改',
            click: (item) => {
              this.router.navigate(['../detail'], { queryParams: { id: item.id, type: 'edit' }, relativeTo: this.ar });
            }
          },
          {
            text: '禁用',
            pop: {
              title: `是否确认禁用？`,
              okType: 'danger',
              icon: 'alert',
            },
            click: (item) => {
              this.changeStatus(item.id);
            },
            iif: (item) => item.status === 1
          },
          {
            text: '启用',
            pop: {
              title: `是否确认启用？`,
              okType: 'danger',
              icon: 'alert',
            },
            click: (item) => {
              this.changeStatus(item);
            },
            iif: (item) => item.status === 2
          },
          {
            text: '查看',
            click: (item) => {
              this.router.navigate(['../view'], { queryParams: { id: item.id, type: 'view' }, relativeTo: this.ar });
            }
          },
          {
            text: '删除',
            pop: {
              title: `确定删除吗？`,
              okType: 'danger',
              icon: 'alert',
            },
            click: (item) => {
              this.del(item);
            },
            iif: (item) => item.status === 2
          },
          {
            text: '操作记录',
            click: (item) => {
              this.router.navigate(['../operatordata'], { queryParams: { id: item.id }, relativeTo: this.ar });
            }
          },
        ],
      },
    ];
  }
  changeStatus(item: any) {
    const params = {
      status,
      idList: [item.id]
    };
    // this.service.request(this.service.$api_openOrClose, params).subscribe(res => {
    //   if (res) {
    //     this.st.reload();
    //   }
    // });
  }
  /**
   * 伸缩查询条件
   */
  expandToggle() {
    this._$expand = !this._$expand;
    this.sf?.setValue('/_$expand', this._$expand);
  }

  /**
   * 重置表单
   */
  resetSF() {
    this.sf.reset();
    this._$expand = false;
    this.isLoading = true
  }

  /**
   * 新增单个实例
   */
  add() {
    this.router.navigate(['../detail'], { queryParams: { id: 0, type: 'add' }, relativeTo: this.ar });
  }

  /**
   * 删除单个实例
   */
  del(item: any) {
    const ids = [];
    ids.push(item.id);
    this.service.request(this.service.$api_del_many, ids).subscribe(res => {
      if (res) {
        this.service.msgSrv.success('删除成功');
        this.st.reload();
      }
    });
  }
}
