<template>
  <div class="ele-body">
    <el-card shadow="never">
      <search ref="search" @search="search"></search>
      <ele-pro-table ref="table" :columns="columns" :datasource="datasource">
        <!-- 表头工具栏 -->
        <template v-slot:toolbar>
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit('add')"
          >
            添加
          </el-button>
        </template>
        <template v-slot:files="scope">
          <el-link
            v-for="link in scope.row.files"
            :key="link.id"
            type="primary"
            :underline="false"
            @click="downloadFile(link)"
          >
            {{ link.name }}
          </el-link>
        </template>

        <!-- 操作列 -->
        <template v-slot:action="{ row }">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit('edit', row)"
          >
            修改
          </el-link>
          <el-popconfirm
            class="ele-action"
            title="确定要删除吗？"
            @confirm="remove(row)"
          >
            <template v-slot:reference>
              <el-link type="danger" :underline="false" icon="el-icon-delete">
                删除
              </el-link>
            </template>
          </el-popconfirm>
        </template>
      </ele-pro-table>
    </el-card>
    <newEdit
      v-if="showEdit"
      :type="editType"
      :editObj="editObj"
      @close="close"
    ></newEdit>
  </div>
</template>
<script>
  import search from './components/search.vue';
  import newEdit from './components/newEdit.vue';
  import { getList, removeItem } from '@/api/inspectionPoint';
  import dictMixins from '@/mixins/dictMixins';
  import { getFile } from '@/api/system/file';

  export default {
    mixins: [dictMixins],
    components: {
      search,
      newEdit
    },
    data() {
      return {
        columns: [
          {
            width: 45,
            type: 'index',
            columnKey: 'index',
            align: 'center'
          },

          {
            prop: 'pointCode',
            label: '编码'
          },
          {
            label: '名称',
            prop: 'pointName'
          },
          {
            label: '区域',
            prop: 'areaName'
          },

          {
            label: '附件',
            slot: 'files'
          },
          {
            prop: 'createUserName',
            label: '创建人',
            align: 'center'
          },

          {
            prop: 'createTime',
            label: '创建时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            columnKey: 'action',
            label: '操作',
            width: 220,
            align: 'center',
            resizable: false,
            slot: 'action',
            showOverflowTooltip: true
          }
        ],

        editType: null,
        editObj: {},
        showEdit: false
      };
    },
    created() {
      
    },
    methods: {
      datasource({ page, where, limit }) {
        return getList({
          ...where,
          pageNum: page,
          size: limit
        });
      },
      search(where) {
        this.$refs.table.reload({
          where: where,
          page: 1
        });
      },
      openEdit(type, row) {
        this.editType = type;
        this.editObj = row || {};
        this.showEdit = true;
      },
      close() {
        this.showEdit = false;
      },

      downloadFile(file) {
        getFile({ objectName: file.storePath }, file.name);
      },
      remove(row) {
        removeItem([row.id])
          .then((message) => {
            this.$message.success(message);
            this.done();
          })
          .catch((e) => {});
      }
    }
  };
</script>
