<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <div class="filter-container">
      <div class="filter-control">
        <span>文章标题:</span>
        <el-input v-model="listQuery.articleTitle" placeholder="文章标题" @keyup.enter.native="getList" />
      </div>
      <div class="filter-control">
        <span>审核状态:</span>
        <el-select v-model="listQuery.auditStatus" placeholder="审核状态" clearable @change="getList">
          <el-option label="全部" value="" />
          <el-option label="待审核" :value="2" />
          <el-option label="审核通过" :value="1" />
          <el-option label="审核未通过" :value="0" />
        </el-select>
      </div>
      <div class="filter-control">
        <span>上线状态:</span>
        <el-select v-model="listQuery.status" placeholder="上线状态" clearable @change="getList">
          <el-option label="全部" value="" />
          <el-option label="已上线" :value="1" />
          <el-option label="待上线" :value="2" />
          <el-option label="未上线" :value="0" />
        </el-select>
      </div>
      <div class="filter-control">
        <permission-button type="primary" @click="getList">查询</permission-button>
        <permission-button type="primary" @click="$_navigationTo(`article-edit`)">添加文章</permission-button>
      </div>
    </div>
    <!-- 搜索区域END -->
    <!-- 表格区域 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      style="width: 100%"
      border
      fit
      highlight-current-row
      cell-class-name="table-cell"
      header-row-class-name="tableHeader"
    >
      <el-table-column label="序号" :index="indexMethod" type="index" width="80" align="center" />
      <el-table-column label="文章标题" prop="articleTitle" align="center" />
      <el-table-column label="文章头图" width="100px" align="center">
        <template slot-scope="{ row }"> <el-image style="width: 50px; height: 50px" :src="row.articleImage" /></template>
      </el-table-column>
      <el-table-column label="审核状态" width="120px" 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="160px" 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>
              <permission-button type="primary" size="mini" @click="handleChangeStatus(row)">上线</permission-button>
            </template>
            <template v-else>
              <span style="margin-right:10px;" class="status success ">已上线</span>
              <permission-button type="info" size="mini" plain @click="handleChangeStatus(row)">下线</permission-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="160px" align="center">
        <template slot-scope="{ row }">
          {{ row.createTime | formatTime }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180px" align="center">
        <template slot-scope="{ row }">
          <permission-button
            type="primary"
            size="mini"
            style="margin-right:12px"
            @click="$_navigationTo(`article-edit?articleId=${row.articleId}`)"
          >编辑</permission-button>
          <permission-button
            type="danger"
            size="mini"
            style="margin-right:12px"
            @click="handleRemoveArticle(row)"
          >删除</permission-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格区域END -->
    <pagination
      :total="total"
      :page.sync="listQuery.pageNum"
      :limit.sync="listQuery.pageSize"
      @pagination="getList(listQuery)"
    />
  </div>
</template>

<script>
import PermissionButton from '@/views/components/PermissionButton'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import { mapGetters } from 'vuex'
import { changeArticleStatus, getArticleList, removeArticle } from '@/api/doc'
export default {
  components: { Pagination, PermissionButton },
  data() {
    return {
      listLoading: false,
      total: 0,
      listQuery: {
        authUserId: '', // 机构id
        auditStatus: '', // 审核状态
        articleTitle: '', // 文章标题
        listType: 1, // 列表类型：1文章列表，2文章审核列表
        pageNum: 1, // 页码
        pageSize: 10, // 分页大小
        status: '' // 文章状态：0已下线，1已上线，2待上线
      },
      list: []
    }
  },
  computed: {
    ...mapGetters(['authUserId', 'userIdentity'])
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取列表数据
    getList() {
      this.listLoading = true
      this.listQuery.authUserId = this.authUserId
      getArticleList(this.listQuery)
        .then(res => {
          if (res.code !== 0) return
          this.list = res.data.list
          console.log(res)
          this.total = res.data.total
        })
        .finally(() => {
          this.listLoading = false
        })
    },
    // 删除文章
    async handleRemoveArticle(row) {
      const text = await this.$confirm('确认删除该文章吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(() => {
        this.$message.info('已取消操作')
      })
      if (text !== 'confirm') return
      removeArticle({ articleId: row.articleId }).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 = {
        articleId: item.articleId,
        status: item.status === 1 ? 0 : 1
      }
      changeArticleStatus(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>
.el-table .cell {
  overflow: visible;
}
.el-badge {
  margin: 0 6px;
}
</style>
