<template>
  <div class="">
    <toolbar @on-filter="filterData" @on-reset="filterData" />

    <div class="m-10 bg-w p-20 br-10">
      <el-button type="primary" icon="el-icon-plus" @click="$PhotoUploadModal()"
        >新增</el-button
      >
      <el-button
        type="primary"
        icon="el-icon-plus"
        @click="$PhotoMultUploadModal()"
        >批量新增</el-button
      >

      <base-table
        :columns="columns"
        :items="items"
        :pagination="pagination"
        :page-change="pageChange"
      />
    </div>
  </div>
</template>

<script>
import toolbar from './toolbar';
import mxFilterList from '@/mixins/filterList';
import { getPage, delItem } from '@/api/photoWarehouse';

export default {
  name: 'PhotoVerify',

  components: { toolbar },

  mixins: [
    mxFilterList({
      fetchList: getPage // 在下方data再声明一个 fetchList: iGetList 同等效果
    })
  ],

  data() {
    return {
      columns: [
        {
          key: 'location',
          name: '上传位置',
          minWidth: this.$col.b,
          render: (h, { row }) =>
            h(
              'span',
              `${row.kindergartenName}${
                row.activityName ? '/' + row.activityName : ''
              }`
            )
        },

        {
          key: 'listPreview',
          name: '列表缩略图',
          minWidth: this.$col.b,
          render: (h, { row }) => {
            if (!row.listPreview) {
              return h('span', '图片正在处理中请稍等...');
            }
            return h('img', {
              style: {
                width: '160px',
                height: '90px'
              },
              class: 'pre-img',
              attrs: {
                src: row.listPreview
              },
              on: {
                click: () =>
                  this.$AdvanceViewImageModal({
                    items: [{ src: row.listPreview }]
                  })
              }
            });
          }
        },
        {
          key: 'detailPreview',
          name: '详情缩略图',
          minWidth: this.$col.b,
          render: (h, { row }) => {
            if (!row.detailPreview) {
              return h('span', '图片正在处理中请稍等...');
            }
            return h('img', {
              style: {
                width: '160px',
                height: '90px'
              },
              class: 'pre-img',
              attrs: {
                src: row.detailPreview
              },
              on: {
                click: () =>
                  this.$AdvanceViewImageModal({
                    items: [{ src: row.detailPreview }]
                  })
              }
            });
          }
        },
        {
          key: 'createAt',
          name: '上传时间',
          width: this.$col.b
        },
        {
          key: 'auditStatus',
          name: '状态',
          width: this.$col.s,
          type: 'tag',
          fetchTagType: val => {
            switch (val) {
              case 1:
                return 'success';
              case 0:
                return 'info';
              case -1:
                return 'error';
              default:
                return 'info';
            }
          },
          tagName: row => {
            switch (row.auditStatus) {
              case 1:
                return '通过';
              case 0:
                return '待审核';
              case -1:
                return '拒绝';
              default:
                return '-';
            }
          }
        },
        {
          key: 'action',
          name: '操作',
          width: '120',
          render: (h, { row }) => {
            const action = [];
            row.auditStatus !== 1 &&
              action.push(
                h(
                  'BaseBtn',
                  {
                    props: {
                      popip: true,
                      txt: '删除',
                      type: 'text'
                    },
                    class: 'ml-10',
                    on: {
                      ok: () => this.handleDelItem(row)
                    }
                  },
                  '删除'
                )
              );

            return h('div', action);
          }
        }
      ]
    };
  },

  created() {
    this.$g_on('photo_reload', this.reload);
  },

  beforeDestroy() {
    this.$g_off('photo_reload', this.reload);
  },

  methods: {
    async handleDelItem(item) {
      const { success, msg } = await delItem({
        id: item.id
      });
      if (success) {
        this.$success('删除成功！');
        this.$g_emit('photo_reload');
      }
    }
  }
};
</script>

<style type="scss" scoped></style>
