<template>
  <van-list
    class="folder-list"
    v-model="loading"
    :finished="isFinished"
    @load="onLoad"
  >
    <div
      v-for="(folder, index) in userFolderList"
      :key="index"
      class="item"
      @click="handleClickFolder(folder)"
    >
      <van-image
        class="thumb"
        :src="folder.folder_simg_url"
        fit="contain"
      ></van-image>
      <div class="title">{{ folder.name }}</div>
      <div class="desc" style="color: #aaaaaa">
        {{ folder.update_time | timeFormat("MM-dd hh:mm") }}
      </div>
      <common-svg
        v-if="isShowMode"
        name="jinru"
        class="right-icon"
      ></common-svg>
      <common-svg
        v-if="isSelectMode && selectedfolder.id === folder.id"
        name="xingzhuangjiehe"
        class="right-icon"
      ></common-svg>
      <common-svg
        v-if="isSelectMode && selectedfolder.id !== folder.id"
        name="tuoyuanxingbeifen3"
        class="right-icon"
      ></common-svg>
      <common-svg
        v-if="isActionMode"
        @click.stop="handleClickAction(folder)"
        name="sandian"
        class="right-icon"
      ></common-svg>
    </div>
  </van-list>
</template>

<script lang="ts">
import {
  Component,
  Prop,
  Vue,
  Emit,
  Inject,
  Mixins,
} from 'vue-property-decorator';
import { State, Action, namespace } from 'vuex-class';
import { IGetUserFoldersPayload } from '@/store/modules/types';
import { EFolderListMode } from './types';
const Template = namespace('template');

@Component
export default class FolderList extends Vue {
  @Prop() private mode!: EFolderListMode;
  @Prop() private selectedfolder!: any;

  @Template.Action('getUserFoldersFixed') private getUserFolders!: (
    payload?: IGetUserFoldersPayload
  ) => Promise<boolean>;
  @Template.State('userFolderList') private userFolderList!: any[];
  @Template.State('isFinished') private isFinished!: boolean;
  @Template.State('page') private page!: number;
  @Template.Mutation('setPage') private setPage!: (page: number) => void;

  @Prop() private handleClickFolder!: (id: number) => any;
  @Prop() private clickAction!:(folder: any) => void;

  get isSelectMode() {
    return this.mode === EFolderListMode.SELECT;
  }

  get isShowMode() {
    return this.mode === EFolderListMode.SHOW;
  }

  get isActionMode(){
    return this.mode === EFolderListMode.ACTION;
  }

  private finished = false;
  private loading = false;

  private async onLoad() {
    this.setPage(this.page + 1);
    await this.getUserFolders({ page: this.page });
    this.loading = false;
  }

  private handleClickItem(folder: any) {
    this.handleClickFolder && this.handleClickFolder(folder);
  }

  private handleClickAction(folder: any){
    this.clickAction && this.clickAction(folder);
  }
}
</script>

<style lang="scss" scoped>
.folder-list {
  .item {
    display: grid;
    grid-template-columns: 44px auto 30px;
    grid-template-rows: repeat(2, 20px);
    grid-auto-flow: column dense;
    place-items: center start;
    grid-gap: 0 17px;
    background: rgba(255, 255, 255, 1);
    border-radius: 5px;
    margin-bottom: 15px;
    .thumb,
    .right-icon {
      place-self: center;
      grid-row: span 2;
    }
    .thumb {
      width: 34px;
    }
    .title {
      font-size: 14px;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 500;
      color: #353535;
    }
    .desc {
      font-size: 12px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: #b6b5ba;
      line-height: 17px;
    }
  }
}
</style>
