<template>
  <div class="app-container review-box">
    <!-- 搜索区域 -->
    <div class="filter-container">
      <div class="filter-control">
        <span>文件名称:</span>
        <el-input v-model="listQuery.fileTitle" 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">
        <el-button type="primary" @click="getList">查询</el-button>
      </div>
    </div>
    <!-- 搜索区域END -->
    <!-- 表格区域 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      style="width: 100%"
      border
      fit
      highlight-current-row
      header-row-class-name="tableHeader"
      cell-class-name="table-cell"
    >
      <el-table-column label="序号" :index="indexMethod" type="index" width="80" align="center" />
      <el-table-column label="文件名称" prop="fileTitle" align="center" />
      <el-table-column label="审核状态" width="120px" align="center">
        <template slot-scope="{ row }">
          <audit-status :status="row.auditStatus" :reason="row.invalidReason" />
        </template>
      </el-table-column>
      <el-table-column label="审核时间" width="160px" align="center">
        <template slot-scope="{ row }">
          <span v-if="row.auditStatus !== 2">{{ row.auditTime | formatTime }}</span>
          <span v-else>—</span>
        </template>
      </el-table-column>

      <el-table-column label="审核人" align="center" width="280px">
        <template slot-scope="{ row }">
          <span v-if="row.auditStatus !== 2">{{ row.auditBy }}</span>
          <span v-else>—</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150px" align="center">
        <template slot-scope="{ row }">
          <!-- <el-button
            v-if="row.auditStatus===2"
            type="primary"
            size="mini"
            @click="handleShowDialog(row)"
          >审核</el-button>
          <span v-else class="status success el-icon-check">&nbsp;已审核</span> -->
          <audit-button-group type="audit" :is-dot="true" :margin-right="false" @click="onClick(row, $event)" />
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格区域END -->
    <!-- 页码 -->
    <pagination
      :total="total"
      :page.sync="listQuery.pageNum"
      :limit.sync="listQuery.pageSize"
      @pagination="getList(listQuery)"
    />
    <!-- 视频预览对话框 -->
    <el-dialog :title="editTitle" :visible.sync="dialogVisible" width="40%" @closed="dialogColosed">
      <el-form ref="formRef" :model="dialogData" label-width="65px" :rules="dialogFormRules">
        <el-form-item label="标题：">
          {{ current.fileTitle }}
        </el-form-item>
        <el-form-item label="文件：">
          <img src="@/assets/img/pdf_cover.png" alt="文件封面" width="60" height="60">
          <span class="preview" @click.prevent="handlePreviewFile">预览</span>
        </el-form-item>
        <template v-if="type === 'audit'">
          <el-form-item label="审核：">
            <el-radio-group v-model="dialogData.auditStatus">
              <el-radio :label="1">通过</el-radio>
              <el-radio :label="0">不通过</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item v-if="dialogData.auditStatus === 0" label="原因：" prop="invalidReason">
            <el-input v-model="dialogData.invalidReason" type="textarea" placeholder="请说明原因" />
          </el-form-item>
        </template>
      </el-form>
      <template v-if="type === 'audit'">
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleAuditStatus">提 交</el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 视频预览对话框END -->
  </div>
</template>

<script>
import openWindow from '@/utils/open-window'
import { getFileList } from '@/api/doc'
import { auditfile } from '@/api/docReview'
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      type: 'review',
      dialogVisible: false, // 文件审核dialog是否显示
      listLoading: false, // 列表加载
      total: 0, // 列表数据条数
      listQuery: {
        authUserId: '', // 机构id
        auditStatus: '', // 审核状态
        fileTitle: '', // 文件标题
        listType: 2, // 列表类型
        pageNum: 1, // 页码
        pageSize: 10, // 分页大小
        status: '' // 文章状态：0已下线，1已上线，2待上线
      },
      list: [],
      // 当前审核选中的文件数据，需要传给预览窗口的数据
      current: {
        currentfileUrl: '',
        fileTitle: ''
      },
      // 审核提交数据
      dialogData: {
        fileId: '', // 文章id
        auditStatus: 1, // 审核状态
        invalidReason: '', // 不同过原因
        auditBy: '' // 审核人
      },
      dialogFormRules: {
        invalidReason: { required: true, message: '不通过原因不能为空', tigger: 'blur' }
      }
    }
  },
  computed: {
    ...mapGetters(['authUserId']),
    editTitle() {
      return this.type === 'review' ? '查看' : '审核'
    }
  },
  created() {
    this.listQuery.authUserId = this.$route.query.authUserId
    this.getList()
  },
  methods: {
    // 按钮组点击事件
    onClick(row, type) {
      const actions = {
        review: this.onReview,
        result: this.onResult,
        audit: this.onAudit
      }
      actions[type](row)
    },

    // 预览
    onReview(row) {
      console.log('预览')
      this.type = 'review'
      this.handleShowDialog(row)
    },
    // 驳回
    onResult(row) {
      console.log('驳回')
    },
    // 审核
    onAudit(row) {
      this.type = 'audit'
      this.handleShowDialog(row)
    },
    // 获取列表数据
    getList() {
      this.listLoading = true
      getFileList(this.listQuery)
        .then((res) => {
          if (res.code !== 0) return
          this.list = res.data.list
          this.total = res.data.total
          console.log(res)
        })
        .finally(() => {
          this.listLoading = false
        })
    },
    // 显示审核dialog对话框
    handleShowDialog(row) {
      console.log('------')
      console.log(row)
      this.current.currentfileUrl = row.filePreviewUrl
      this.current.fileTitle = row.fileTitle
      this.dialogData.fileId = row.fileId
      this.dialogVisible = true
    },
    // dialog关闭后的回调
    dialogColosed() {
      this.currentfileUrl = ''
      this.current.currentfileUrl = ''
      this.current.fileTitle = ''
      this.dialogData.auditStatus = 1
      this.dialogData.fileId = ''
    },
    // 审核操作
    handleAuditStatus() {
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          this.dialogData.auditBy = this.authUserId
          auditfile(this.dialogData).then((res) => {
            console.log(res)
            if (res.code !== 0) return
            this.$message({
              message: res.data,
              type: 'success',
              duration: 1000
            })
            this.dialogVisible = false
            this.getList()
          })
        }
      })
    },
    // 小窗口浏览pdf文件
    handlePreviewFile() {
      openWindow(this.current.currentfileUrl, this.current.fileTitle, this.current.fileTitle, 1200, 600)
    },
    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;
}
.preview {
  cursor: pointer;
  margin-left: 5px;
  color: #0eaae7;
}
</style>
