 <!-- 知识审核 -->
<template>
  <app-container>
    <el-row type="flex">
      <el-col :span="5">
        <type-tree @change="changeType"/>
      </el-col>
      <el-col :span="19">
        <search-area :need-clear="false" :need-search-more="false" :size="size" type="default" search-more-type="default" @search="search">
          <!--一般查询条件-->
          <search-item>
            <el-input v-model.trim="listQuery.keywords" :size="size" placeholder="标题/内容" clearable/>
          </search-item>
          <search-item>
            <el-select v-model="listQuery.checkStatus" placeholder="审核状态" size="small" clearable>
              <el-option v-for="item in checkStateList" :key="'checkState_'+item.value" :label="item.name" :value="item.value"/>
            </el-select>
          </search-item>
          <search-item>
            <el-select v-model="listQuery.groundingStatus" placeholder="上架状态" size="small" clearable>
              <el-option v-for="item in groundStateList" :key="'ground_'+item.value" :label="item.name" :value="item.value"/>
            </el-select>
          </search-item>
          <search-item>
            <el-select v-model="listQuery.valid" placeholder="可用状态" size="small" clearable>
              <el-option v-for="item in validStateList" :key="'valid_'+item.value" :label="item.name" :value="item.value"/>
            </el-select>
          </search-item>
        </search-area>
        <div style="margin-bottom:10px;">
          <el-row>
            <el-button class="filter-item" style="" size="small" type="primary" icon="el-icon-upload2" @click="goGround">上架</el-button>
            <el-button class="filter-item" style="" size="small" type="primary" icon="el-icon-download" @click="ungrounding">下架</el-button>
          </el-row>
        </div>
        <normal-table
          :data="list"
          :head="tableOption.head"
          :size="size"
          :query="listQuery"
          :total="total"
          :columns="columns"
          :list-loading="listLoading"
          :options="tableOption.options"
          :tools-option="tableOption.toolsOption"
          @change="changePage"
          @select-change="handleSelectionChange">
          <template slot="columns">
            <el-table-column label="操作" align="center">
              <template slot-scope="scope">
                <el-button type="text" size="small" @click.stop="detail(scope.row)">详情</el-button>
                <el-button v-show="scope.row.checkStatus=='0'" type="text" size="small" @click.stop="check(scope.row)">审核</el-button>
              </template>
            </el-table-column>
          </template>
        </normal-table>
      </el-col>
    </el-row>
    <knowledge-detail ref="kdetail"/>
    <simple-dialog ref="simpledialog" title="审核意见" @report="handleCheck"/>
    <set-time-dialog ref="grounddialog" title="上架" @report="ground"/>
  </app-container>
</template>

<script>
import AppContainer from '@/components/layout/AppContainer'
import SearchArea from '@/components/SearchArea/SearchArea'
import SearchItem from '@/components/SearchArea/SearchItem'
import CaseListTable from '@/views/caseManage/caseCommon/caseListTable'
import NormalTable from '@/components/NomalTable'
import { knowledgeList, checkKnowledge, groundingKnowledge } from '@/api/knowledge'
import { getcheckStatus, getGroundStatus, getValidStatus } from '@/api/allDict'
import TypeTree from './components/TypeTree'
import KnowledgeDetail from '../caseManage/components/knowledgeDetail'
import SimpleDialog from '../../components/SimpleDialog/index'
import SetTimeDialog from './components/setTimeDialog'

export default {
  name: 'KnowledgeCheck',
  components: { SetTimeDialog, SimpleDialog, KnowledgeDetail, TypeTree, AppContainer, SearchArea, SearchItem, CaseListTable, NormalTable },
  data() {
    return {
      list: [],
      total: 0,
      timeRange: [], // 申请时间范围
      listLoading: true, // 列表加载动画
      multipleSelection: [], // 多选选中项
      listQuery: {
        keywords: '', // 关键字
        type: '', // 类别
        checkStatus: '', // 审核状态
        groundingStatus: '', // 上架状态
        valid: '', // 可用状态
        offset: 1,
        limit: 20,
        sort: '',
        order: ''
      }, // 筛选条件
      columns: [
        {
          text: '标题',
          value: 'kName',
          align: 'center'
        },
        {
          text: '类别',
          value: 'kTypeName',
          align: 'center'
        },
        {
          text: '发布人',
          value: 'publisherName',
          align: 'center'
        },
        {
          text: '发布时间',
          value: 'publishTime',
          width: 90,
          align: 'center'
        },
        {
          text: '审核状态',
          value: 'checkStatusName',
          align: 'center'
        },
        {
          text: '上架状态',
          value: 'groundingStatusName',
          align: 'center'
        },
        {
          text: '可用状态',
          value: 'validStatus',
          align: 'center'
        },
        {
          text: '有效时限',
          value: 'validPeroid',
          align: 'center'
        }
      ], // 显示列
      checkStateList: [], // 审核状态
      groundStateList: [], // 上架状态
      validStateList: [], // 可用状态
      tableOption: {
        head: {
          show: false, // 是否需要标题栏，
          text: '数据列表' // 标题名称
        },
        options: {
          needIndex: true, // 是否需要序号列
          needMultipleSelection: true // 需要多选列
        },
        toolsOption: {
          selectColumns: false, // 是否需要筛选列
          refresh: false // 是否需要刷新按钮
        }
      },
      checkedId: '', // 被审核知识id
      size: 'small'
    }
  },
  created() {
    this.fetchCheckStateList()
    this.fetchGroundStateList()
    this.fetchValidList()
    this.fetchData()
  },
  methods: {
    search() {
      this.fetchData(false)
    },
    // 获取数据
    fetchData(isNowPage = true) {
      this.listLoading = true
      if (!isNowPage) { // 是否显示当前页，否则跳转第一页
        this.listQuery.offset = 1
      }
      this.listLoading = true
      knowledgeList(this.listQuery).then(response => {
        if (response.code === 200) {
          // response.data.rows = [
          //   {
          //     'kId': '1',
          //     'kName': '特种设备作业人员操作证怎样办理？',
          //     'kTypeName': '大类',
          //     'publisherName': '张三',
          //     'publishTime': '2020-04-12',
          //     'checkStatusName': '未审核',
          //     'groundingStatusName': '未上架',
          //     'validStatusName': '不可用',
          //     'validPeroid': '永久'
          //   }
          // ]
          this.list = response.data.rows
          this.total = response.data.total
          this.listLoading = false
        }
      })
    },
    detail(row) {
      this.$refs.kdetail.initDialog(row)
    },
    // 添加知识库
    add(row) {
      this.$refs.checkdialog.initDialog('create', true)
    },
    // 编辑
    check(row) {
      this.checkedId = row.kId
      this.$refs.simpledialog.initDialog()
    },
    // 点击上架，打开上架弹窗
    goGround() {
      if (this.checkSelection()) {
        this.$refs.grounddialog.initDialog()
      }
    },
    ground(type, val) {
      if (type === 'success') {
        this.grounding(val)
      } else {
        this.ungrounding(val)
      }
    },
    // 上架
    grounding(val) {
      const list = this.multipleSelection.map(item => item.kId)
      groundingKnowledge('1', list.join(','), val).then(response => {
        if (response.code === 200) {
          this.$message.success('上架成功')
          this.fetchData()
        }
      })
    },
    // 下架
    ungrounding() {
      if (this.checkSelection()) {
        this.$confirm(
          '确定要下架吗？',
          '确认操作',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }
        ).then(() => {
          const list = this.multipleSelection.map(item => item.kId)
          groundingKnowledge('2', list.join(',')).then(response => {
            if (response.code === 200) {
              this.$message.success('下架成功')
              this.fetchData()
            }
          })
        })
      }
    },
    // 处理审核结果
    handleCheck(type, val) {
      const params = {
        id: this.checkedId,
        checkStatus: '',
        notes: val
      }
      if (type === 'success') {
        params.checkStatus = '1'
      } else if (type === 'refuse') {
        params.checkStatus = '2'
      }
      checkKnowledge(params).then(response => {
        if (response.code === 200) {
          if (type === 'success') {
            this.$message.success('审核成功')
          } else if (type === 'refuse') {
            this.$message.success('驳回成功')
          }
          this.fetchData()
        }
      })
    },
    // 取审核状态列表
    fetchCheckStateList() {
      getcheckStatus().then(repsonse => {
        this.checkStateList = repsonse.data
      })
    },
    // 取上架状态列表
    fetchGroundStateList() {
      getGroundStatus().then(repsonse => {
        this.groundStateList = repsonse.data
      })
    },
    // 取可用状态列表
    fetchValidList() {
      getValidStatus().then(repsonse => {
        this.validStateList = repsonse.data
      })
    },
    // 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
    changePage(val) {
      if (val && val.size) {
        this.listQuery.limit = val.size
      }
      if (val && val.page) {
        this.listQuery.offset = val.page
      }
      this.fetchData()
    },
    // 更换类别
    changeType(typeid) {
      this.listQuery.type = typeid
      this.search()
    },
    // 多选选中项
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    checkSelection() {
      if (this.multipleSelection.length === 0) {
        this.$message.warning('请至少选择一项')
        return false
      } else {
        return true
      }
    }
  }
}
</script>
