<template>
  <div class="app-container">
    <el-card shadow="never" class="mb16 filter-card">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px" size="small">
      <el-form-item label="租户名称" prop="tenantId">
        <inline-tenant-selector :key="tenantSelectorKey" @change="handleTenantChange" />
      </el-form-item>
      <el-form-item label="文章标题" prop="articleTitle">
        <el-input
          v-model="queryParams.articleTitle"
          placeholder="请输入文章标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文章状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择文章状态" clearable size="small">
          <el-option
            v-for="item in statusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </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-card>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['admin:article:list']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-document"
          size="mini"
          @click="handlePending"
        >待审计</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-bar-chart"
          size="mini"
          @click="showStatistics = true"
        >文章统计</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border size="small" style="width:100%" v-loading="loading" :data="articleList">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="租户名称" align="center" prop="companyName" min-width="120" />
      <el-table-column label="文章标题" align="center" prop="articleTitle" min-width="160" show-overflow-tooltip />
      <el-table-column label="文章分类" align="center" prop="categoryName" min-width="100" />
      <el-table-column label="作者" align="center" prop="author" min-width="90" />
      <el-table-column label="创建时间" align="center" prop="createTime" min-width="150" />
      <el-table-column label="文章状态" align="center" prop="status" min-width="90">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 0" type="warning">待审计</el-tag>
          <el-tag v-else-if="scope.row.status == 1" type="success">已通过</el-tag>
          <el-tag v-else type="error">已拒绝</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            plain
            @click="handleView(scope.row)"
          >详情</el-button>
          <template v-if="scope.row.status == 0">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-check"
              plain
              @click="handleAudit(scope.row, 1)"
              v-hasPermi="['admin:article:audit']"
            >通过</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-close"
              plain
              @click="handleAudit(scope.row, 2)"
              v-hasPermi="['admin:article:audit']"
            >拒绝</el-button>
          </template>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            plain
            @click="handleDelete(scope.row)"
            v-hasPermi="['admin:article:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

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

    <el-dialog title="文章详情" :visible.sync="viewOpen" width="800px" append-to-body>
      <el-form :model="viewForm" label-width="100px">
        <el-form-item label="租户名称">
          <span>{{ viewForm.companyName }}</span>
        </el-form-item>
        <el-form-item label="文章标题">
          <span>{{ viewForm.articleTitle }}</span>
        </el-form-item>
        <el-form-item label="文章分类">
          <span>{{ viewForm.categoryName }}</span>
        </el-form-item>
        <el-form-item label="作者">
          <span>{{ viewForm.author }}</span>
        </el-form-item>
        <el-form-item label="文章内容">
          <div v-html="viewForm.content"></div>
        </el-form-item>
        <el-form-item label="创建时间">
          <span>{{ viewForm.createTime }}</span>
        </el-form-item>
        <el-form-item label="文章状态">
          <el-tag v-if="viewForm.status == 0" type="warning">待审计</el-tag>
          <el-tag v-else-if="viewForm.status == 1" type="success">已通过</el-tag>
          <el-tag v-else type="error">已拒绝</el-tag>
        </el-form-item>
      </el-form>
    </el-dialog>

    <el-dialog :title="auditTitle" :visible.sync="auditOpen" width="400px" append-to-body>
      <el-form ref="auditForm" :model="auditForm" label-width="80px">
        <el-form-item label="审计备注">
          <el-textarea v-model="auditForm.auditRemark" placeholder="请输入审计备注" :rows="3" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitAudit">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="文章统计" :visible.sync="showStatistics" width="600px" append-to-body>
      <el-card>
        <el-row :gutter="20">
          <el-col :span="12">
            <div class="stat-item">
              <span class="stat-value">{{ statistics.totalCount }}</span>
              <span class="stat-label">文章总数</span>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="stat-item">
              <span class="stat-value">{{ statistics.pendingCount }}</span>
              <span class="stat-label">待审计</span>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="stat-item">
              <span class="stat-value">{{ statistics.approvedCount }}</span>
              <span class="stat-label">已通过</span>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="stat-item">
              <span class="stat-value">{{ statistics.rejectedCount }}</span>
              <span class="stat-label">已拒绝</span>
            </div>
          </el-col>
        </el-row>
      </el-card>
    </el-dialog>
  </div>
</template>

<script>
import { listAllArticles, listPendingArticles, getArticleInfo, auditArticle, deleteArticle, getArticleStatistics, exportAllArticles } from '@/api/admin/article'
import InlineTenantSelector from '@/components/InlineTenantSelector'

export default {
  name: 'ArticleAdmin',
  components: { InlineTenantSelector },
  data() {
    return {
      loading: true,
      exportLoading: false,
      showSearch: true,
      total: 0,
      articleList: [],
      tenantSelectorKey: 0,
      statistics: {
        totalCount: 0,
        pendingCount: 0,
        approvedCount: 0,
        rejectedCount: 0
      },
      statusOptions: [
        { value: 0, label: '待审计' },
        { value: 1, label: '已通过' },
        { value: 2, label: '已拒绝' }
      ],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tenantId: null,
        articleTitle: null,
        status: null
      },
      viewOpen: false,
      viewForm: {},
      auditOpen: false,
      auditTitle: '',
      auditForm: {
        articleId: null,
        status: null,
        auditRemark: ''
      },
      showStatistics: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      if (!this.queryParams.tenantId) {
        this.articleList = []
        this.total = 0
        this.loading = false
        return
      }
      this.loading = true
      listAllArticles(this.queryParams).then(response => {
        this.articleList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    handlePending() {
      this.loading = true
      listPendingArticles().then(response => {
        this.articleList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    handleQuery() {
      if (!this.queryParams.tenantId) {
        this.$message.warning('请先选择租户')
        return
      }
      this.queryParams.pageNum = 1
      this.getList()
    },
    handleTenantChange(val) {
      if (val) {
        this.queryParams.tenantId = val
        this.handleQuery()
      }
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.articleList = []
      this.total = 0
      this.queryParams.tenantId = null
      this.$store.dispatch('tenant/setTenantId', null)
      this.tenantSelectorKey++
    },
    handleView(row) {
      getArticleInfo(row.articleId).then(response => {
        this.viewForm = response.data
        this.viewOpen = true
      })
    },
    handleAudit(row, status) {
      this.auditTitle = status === 1 ? '通过文章' : '拒绝文章'
      this.auditForm = {
        articleId: row.articleId,
        status: status,
        auditRemark: ''
      }
      this.auditOpen = true
    },
    submitAudit() {
      auditArticle(this.auditForm.articleId, this.auditForm.status, this.auditForm.auditRemark).then(response => {
        this.$message.success(this.auditForm.status === 1 ? '通过成功' : '拒绝成功')
        this.auditOpen = false
        this.getList()
      })
    },
    handleDelete(row) {
      this.$confirm('确定删除文章 [' + row.articleTitle + '] 吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteArticle(row.articleId).then(response => {
          this.$message.success('删除成功')
          this.getList()
        })
      })
    },
    showStatisticsDialog() {
      getArticleStatistics().then(response => {
        this.statistics = response.data
        this.showStatistics = true
      })
    },
    handleExport() {
      this.exportLoading = true
      exportAllArticles(this.queryParams).then(response => {
        this.download(response.msg)
        this.exportLoading = false
      }).catch(() => { this.exportLoading = false })
    }
  }
}
</script>

<style scoped>
.stat-item {
  text-align: center;
  padding: 20px;
  background: #f5f7fa;
  border-radius: 8px;
}
.stat-value {
  font-size: 24px;
  font-weight: bold;
  color: #1890ff;
}
.stat-label {
  display: block;
  margin-top: 5px;
  font-size: 14px;
  color: #606266;
}
.mb16 { margin-bottom: 16px; }
.filter-card { padding-bottom: 0; }
</style>
