<template>
  <div class="app-container">
    <el-form :model="queryParams" class="main-search" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-form-item label="作品标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入作品标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="状态" clearable>
          <el-option key="A" label="启用" value="A" />
          <el-option key="N" label="禁用" value="N" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="lookList">
      <el-table-column label="ID" prop="id" width="70" />
      <el-table-column label="封面" align="center" width="120">
        <template slot-scope="scope">
          <img class="list-img" :src="scope.row.cover" />
        </template>
      </el-table-column>
      <el-table-column label="标题" prop="title" :show-overflow-tooltip="true" min-width="180" />
      <el-table-column label="作者" prop="author" align="center" width="120" />
      <el-table-column label="点赞" prop="likes" align="center" width="80" />
      <el-table-column label="评论" prop="comments" align="center" width="80" />
      <el-table-column label="状态" align="center" prop="status" width="120">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="A"
            inactive-value="N"
            @change="handleStatusChange(scope.row)"
            v-hasPermi="['content:look:edit']"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="170">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['content:look:edit']"
          >下架</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { getLookList, updateLookStatus } from '@/api/look'

export default {
  name: 'ContentLookIndex',
  data() {
    return {
      loading: true,
      total: 0,
      lookList: [],
      queryParams: {
        page: 1,
        pageSize: 10,
        title: '',
        status: '',
      },
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      getLookList(this.queryParams).then((response) => {
        this.lookList = response.data.dataList.content
        this.total = response.data.dataList.totalElements
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    handleQuery() {
      this.queryParams.page = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    handleStatusChange(row) {
      updateLookStatus(row.id, row.status).then(() => {
        this.$modal.msgSuccess('操作成功')
      }).catch(() => {
        row.status = row.status === 'A' ? 'N' : 'A'
      })
    },
    handleDelete(row) {
      this.$modal.confirm('确认下架该作品吗？').then(() => {
        return updateLookStatus(row.id, 'D')
      }).then(() => {
        this.$modal.msgSuccess('下架成功')
        this.getList()
      })
    },
  },
}
</script>

<style scoped>
.list-img {
  width: 72px;
  height: 72px;
  border-radius: 6px;
  object-fit: cover;
}
</style>
