import { Component, Input, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { FilterItem, FilterType } from 'src/app/lib/pager/pager-filter.component';
import { SysService } from '../sys/sys.service';

@Component({
  selector: 'app-task',
  template: `
    
    <nz-page-header *ngIf="!embedMode" [nzGhost]="false">
      <nz-page-header-title i18n>{{data?.title || '任务列表'}}</nz-page-header-title>
    </nz-page-header>


  <cg-pager 
    [remoteUrl]="remoteUrl"
    remoteHeaderUrl="/napi/coloring/web/score/task/headers"
    [defaultOrder]="'desc'"
    [defaultOrderBy]="'time'"
    [listTemplate]="listTemplate"
    [pageSizeOptions]="[30, 60, 90, 120, 150, 180, 480]"
    [topPagination]="true"
    [bottomPagination]="true"
    [showOrder]="true"
    [showSearch]="true"
    [debug]="false"
    [refresh]="refreshTrigger"
    [filterConfig]="filterConfig"
    [inputFilters]="inputFilters"
    [orderColumns]="['time','lastMod' ]"
  ></cg-pager>

  <ng-template #listTemplate  let-list="list" >
    <div nz-row [nzGutter]="[20, 20]" class="pt-4 pb-4" 
      cdkDropList 
      id="list-1" 
      cdkDropListConnectedTo="list-2"
      [cdkDropListDisabled]="!embedMode" 
      [cdkDropListSortingDisabled]="true"
      [cdkDropListData]="list?.data"
       >
      <task-card   *ngFor="let item of list?.data" 
        nz-col  [nzSm]="8" [nzMd]="6" [nzLg]="6" [nzXl]="4" [nzXXl]="3"	
        [data]="item"  
        [hideAuthor]="hideAuthor"
        (itemClick)="onItemClick($event)"
        (userClick)="filter('user', [$event])"
        cdkDrag
         >
      </task-card>
    </div>
  </ng-template>

  `,
  styles: [
    `
    :host {
      padding-bottom:100px;
      display:block;
    }

    .cdk-drag-preview {
      box-sizing: border-box;
      border-radius: 4px;
      box-shadow: 0 5px 5px -3px rgba(0, 0, 0, 0.2),
                  0 8px 10px 1px rgba(0, 0, 0, 0.14),
                  0 3px 14px 2px rgba(0, 0, 0, 0.12);
    }

    .cdk-drag-placeholder {
      opacity: 0;
    }

    .cdk-drag-animating {
      transition: transform 250ms cubic-bezier(0, 0, 0.2, 1);
    }

    nz-select {
      width: 150px;
    }
    
    `
  ]
})
export class TaskComponent implements OnInit {
  @Input() remoteUrl = '/napi/coloring/web/score/my';
  @Input() hideAuthor: boolean = false;
  @Input() hideUpload: boolean = false;
  @Input() embedMode: boolean = false;
  @Input() refreshTrigger: any;

  @Input()
  filterConfig: FilterItem[] = [
    {
      data: 'done',
      title: $localize`状态`,
      filterType: FilterType.radio,
      options: [
        { label: $localize`已完成`, value: true },
        { label: $localize`未完成`, value: false },
      ],
      span: 2,
    },
    {
      data: 'user',
      title: $localize`分配给`,
      filterType: FilterType.select,
      mode: 'multiple',
      span: 2,
      fullWidth: true,
      optionsUrl: '/napi/web/user/select/options',
    },
    {
      data: 'time',
      title: $localize`分配时间`,
      filterType: FilterType.dateRange,
      span: 2,
    },
    {
      data: 'lastMod',
      title: $localize`修改时间`,
      filterType: FilterType.dateRange,
      span: 2,
    },
  ];


  inputFilters: any;

  constructor(
    private router: Router,
    private activatedRoute: ActivatedRoute,
  ) {
    this.data = this.activatedRoute.snapshot.data;
    let { remoteUrl, hideAuthor, hideUpload } = this.activatedRoute.snapshot.data;
    this.remoteUrl = remoteUrl || this.remoteUrl;
    this.hideAuthor = hideAuthor;
    this.hideUpload = hideUpload;
    if (this.hideAuthor) {
      this.filterConfig = this.filterConfig.filter(c => c.data != 'user');
    }
  }

  filter(data, value) {
    console.log('filter', data, value);
    let filter = {};
    filter[data] = value;
    this.inputFilters = filter;
  }

  data: any;

  onItemClick(data: any) {
    this.router.navigate(['/pages/detail', data.art._id]);
  }

  ngAfterViewInit() { }

  ngOnInit() {
  }

}
