<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <div class="filter-container">
      <span>图片标题:</span>
      <el-input
        v-model="listQuery.imageTitle"
        placeholder="图片标题"
        style="width: 200px"
        class="filter-item"
        @keyup.enter.native="getList"
      />
      <span>审核状态:</span>
      <el-select
        v-model="listQuery.auditStatus"
        placeholder="审核状态"
        clearable
        style="width: 200px"
        class="filter-item"
        @change="getList"
      >
        <el-option label="全部" value="" />
        <el-option label="待审核" :value="2" />
        <el-option label="审核通过" :value="1" />
        <el-option label="审核未通过" :value="0" />
      </el-select>
      <span>上线状态:</span>
      <el-select
        v-model="listQuery.status"
        placeholder="上线状态"
        clearable
        style="width: 200px"
        class="filter-item"
        @change="getList"
      >
        <el-option label="全部" value="" />
        <el-option label="已上线" :value="1" />
        <el-option label="待上线" :value="2" />
        <el-option label="未上线" :value="0" />
      </el-select>
      <el-button type="primary" icon="el-icon-search" @click="getList">查询</el-button>
      <el-button
        v-if="userIdentity === 2 || proxyInfo!==null"
        icon="el-icon-edit"
        type="primary"
        @click="$_navigationTo(`image-edit`)"
      >添加图片</el-button>
    </div>
    <!-- 搜索区域END -->
    <!-- 表格区域 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      style="width: 100%"
      border
      fit
      highlight-current-row
      cell-class-name="table-cell"
    >
      <el-table-column label="序号" :index="indexMethod" type="index" width="80" align="center" />
      <el-table-column label="图片标题" prop="imageTitle" align="center" />
      <el-table-column label="审核状态" width="180px" align="center">
        <template slot-scope="{ row }">
          <el-tag v-if="row.auditStatus === 2" size="small" type="warning">待审核</el-tag>
          <el-tag v-if="row.auditStatus === 1" size="small" type="success">审核通过</el-tag>
          <!-- 未通过原因展示 -->
          <template v-if="row.auditStatus === 0">
            <!-- <span class="status danger">审核未通过&nbsp;</span> -->
            <el-popover
              placement="top-start"
              title="审核说明"
              width="400"
              trigger="hover"
              :content="row.invalidReason"
            >
              <el-tag slot="reference" size="small" type="danger" class="reason">
                <span>审核未通过</span>
                <span class="el-icon-question status danger " />
              </el-tag>
            </el-popover>
            <!-- 未通过原因展示END -->
          </template>
        </template>
      </el-table-column>
      <el-table-column label="上线状态" width="180px" align="center">
        <template slot-scope="{row}">
          <!-- 只有审核通过了才能操作上下线 auditStatus ：审核状态 -->
          <template v-if="row.auditStatus === 1">
            <template v-if="row.status === 0">
              <span style="margin-right:10px;" class="status danger">已下线</span>
              <el-button v-if="userIdentity===2 || proxyInfo!==null" type="primary" size="mini" @click="handleChangeStatus(row)">上线</el-button>
            </template>
            <template v-else>
              <span style="margin-right:10px;" class="status success ">已上线</span>
              <el-button v-if="userIdentity===2 || proxyInfo!==null" type="info" size="mini" plain @click="handleChangeStatus(row)">下线</el-button>
            </template>
          </template>
          <template v-else>
            <!-- <el-tag type="warning">待上线</el-tag> -->
            <span style="margin-right:10px;" class="status warning">待上线</span>
          </template>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" width="240px" align="center">
        <template slot-scope="{row}">
          {{ row.createTime | formatTime }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="240px" align="center">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" style="margin-right:5px" icon="el-icon-edit" @click="$_navigationTo(`image-edit?imageId=${row.imageId}`)">编辑</el-button>
          <el-button type="danger" size="mini" style="margin-right:5px" icon="el-icon-s-check" @click="handleRemoveImage(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="getList(listQuery)" />
    <!-- 表格区域END -->
  </div>
</template>

<script>
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import { formatDate } from '@/utils'
import { mapGetters } from 'vuex'
import { changeImageStatus, getImageList, removeImage } from '@/api/doc'
export default {
  components: { Pagination },
  filters: {
    formatTime(time) {
      if (!time) {
        return ''
      }
      return formatDate(time, 'yyyy-MM-DD HH:mm:ss')
    }
  },
  data() {
    return {
      listLoading: false,
      total: 0,
      listQuery: {
        authUserId: '', // 机构id
        auditStatus: '', // 审核状态
        imageTitle: '', // 文章标题
        listType: 1, // 列表类型：1文章列表，2文章审核列表
        pageNum: 1, // 页码
        pageSize: 10, // 分页大小
        status: '' // 文章状态：0已下线，1已上线，2待上线
      },
      list: []
    }
  },
  computed: {
    ...mapGetters(['authUserId', 'userIdentity', 'proxyInfo'])
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取列表数据
    getList() {
      this.listLoading = true
      this.listQuery.authUserId = this.authUserId
      getImageList(this.listQuery)
        .then(res => {
          console.log(res)
          if (res.code !== 0) return
          this.list = res.data.list
          this.total = res.data.total
        })
        .finally(() => {
          this.listLoading = false
        })
    },

    // 删除图片
    async handleRemoveImage(row) {
      const text = await this.$confirm('确认删除该图片吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(() => {
        this.$message.info('已取消操作')
      })
      if (text !== 'confirm') return
      removeImage({ imageId: row.imageId })
        .then(res => {
          if (res.code !== 0) return
          this.$message.success(res.data)
          this.getList(this.listQuery)
        })
    },
    // 状态改变
    handleChangeStatus(item) {
      this.listLoading = true
      // const params = {
      //   authId: item.authId,
      //   status: item.status ? 1 : 0
      // }
      console.log(item)
      const params = {
        imageId: item.imageId,
        status: item.status === 1 ? 0 : 1
      }
      changeImageStatus(params).then(res => {
        // this.$message.success(res.data)
        this.$message({
          message: res.data,
          duration: 500,
          type: 'success'
        })
        this.getList()
      }).catch(err => {
        console.log(err)
      }).finally(() => {
        this.listLoading = false
      })
    },
    indexMethod(index) {
      return index + this.listQuery.pageSize * (this.listQuery.pageNum - 1) + 1
    }
  }
}
</script>

<style lang="scss" scoped>
.filter-container {
  span {
    display: inline-block;
    margin-bottom: 10px;
    vertical-align: middle;
    font-size: 14px;
  }
  .el-button {
    display: inline-block;
    margin-bottom: 10px;
    vertical-align: middle;
  }
  .el-input,
  .el-select {
    margin-right: 10px;
    margin-left: 10px;
  }
}
.el-table .cell {
  overflow: visible;
}
.el-badge{
  margin: 0 6px;
}
</style>
