<template>
  <div class="app-container">
    <div class="box-container">
      <div class="contain-title">内容回收站列表</div>
      <div class="contian-operate">
        <el-button type="danger" @click.stop="deleteAll">全部彻底删除</el-button>
      </div>
    </div>
    <div class="content-container">
      <div class="contain-select">
        <span>栏目分类：</span>
        <el-select v-model="selectMode" placeholder="选择分类" @change="change">
          <el-option label="全部栏目" value=""></el-option>
          <el-option label="平台新闻" value="1"></el-option>
          <el-option label="采访专栏" value="2"></el-option>
          <el-option label="政策法规" value="3"></el-option>
          <el-option label="通知公告" value="4"></el-option>
        </el-select>
      </div>
      <complex-table :tableData="tableData" :tableItem="tableItem" :operatingList='operatingList' :total='total'
        v-on:deleteFun="deleteFun" :jump="'content'" v-on:current="current" v-on:restore="restore"></complex-table>
    </div>
  </div>
</template>

<script>
  import complexTable from '@/components/complexTable'
  import { thoroughDeleteAll, thoroughDeleteSingleUrl, recycleBinInquireUrl, restoreUrl, inquireContentUrl } from '@/api/content'
  import { parseTime, type } from '@/utils/index'
  import ret from '@/utils/comTable'
  import cacheModule from '@/utils/queryName'
  export default {
    data() {
      return {
        selectMode: '',
        info: {
          catalog: '',
          pageSize: 20,
          pageNo: 1
        },
        formData: {},
        operatingList: [ // 在子组件中遍历事件
          {
            text: '恢复',
            event: 'restore',
            icon: 'primary'
          },
          {
            text: '彻底删除',
            event: 'deleteFun',
            icon: 'danger'
          }
        ],
        tableData: [], //  每一页的存储的数据
        total: 0, //  页码总数
        tableItem: [ // 表头信息
          {
            prop: 'title',
            tit: '标题',
            active: 'active' // 点击加颜色，加此选项
          },
          {
            prop: 'catalog',
            tit: '栏目分类'
          },
          {
            prop: 'createTime',
            tit: '创建时间'
          },
          {
            prop: 'creator',
            tit: '创建人'
          },
          {
            prop: 'invalidTime',
            tit: '删除时间',
            width: '160'
          },
          {
            prop: 'invalidOperator',
            tit: '删除人',
            width: '160'
          },
          {
            operate: 'edit',
            width: '200'
          }
        ]
      }
    },
    created() {
      this.recycleBinList()
    },
    components: {
      complexTable //  表子页面
    },
    methods: {
      change(value) {
        this.info.catalog = value
        this.recycleBinList()
      },
      recycleBinList() {
        this.$http.get(recycleBinInquireUrl, this.info, (response) => { //  拉去回收站信息列表，tableData存每一页的信息
          if (response.success && response.data) {
            const res = response.data
            const epData = res.data
            let j = 0
            for (let i = 0; i < epData.length; i++) {
              epData[i].createTime = parseTime(epData[i].createTime).substr(0, 16)
              epData[i].invalidTime = parseTime(epData[i].invalidTime).substr(0, 16)
              epData[i].catalog = type[epData[i].catalog]
              j += 2
              cacheModule.Judge(epData[i].creator, info => {
                j--
                epData[i].creator = info.name
                if (j === 0) {
                  this.total = res.total
                  if (epData.length === 20) {
                    this.tableData = epData
                  } else {
                    const array = epData
                    ret.gapFilling(array)
                    this.tableData = array
                  }
                }
              })
              cacheModule.Judge(epData[i].invalidOperator, info => {
                j--
                epData[i].invalidOperator = info.name
                if (j === 0) {
                  this.total = res.total
                  if (epData.length === 20) {
                    this.tableData = epData
                  } else {
                    const array = epData
                    ret.gapFilling(array)
                    this.tableData = array
                  }
                }
              })
            }
            if (epData.length === 0) {
              this.total = 0
              this.tableData = []
            }
          } else {
            this.total = 0
            this.tableData = []
          }
        })
      },
      restore(obj) {
        this.$confirm(`确定恢复${obj.title}内容？`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          center: true
        }).then(() => {
          this.$http.get(restoreUrl, { id: obj.id }, response => {
            if (response.success) {
              if (response.data) {
                this.recycleBinList()
                this.$message({
                  message: '恢复成功',
                  type: 'success'
                })
              } else {
                this.$http.get(inquireContentUrl, { id: this.id }, (response) => {
                  if (response.success && response.data) {
                    const info = response.data
                    if (!info.actived) {
                      this.$message({
                        message: '状态改变',
                        type: 'warning'
                      })
                    }
                  }
                })
              }
            }
          })
        }).catch(() => {
        })
      },
      deleteAll() { //  删除全部回收站信息
        this.$confirm('确定全部彻底删除回收站内容？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          center: true
        }).then(() => {
          this.$http.get(thoroughDeleteAll, '', response => {
            if (response.success) {
              this.recycleBinList()
              this.$message({
                message: '全部删除成功',
                type: 'success'
              })
            }
          })
        }).catch(() => {
        })
      },
      deleteFun(obj) { //  彻底删除单个内容信息
        console.log(obj)
        this.$confirm('确定彻底删除该条回收站内容？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          center: true
        }).then(() => {
          this.$http.get(thoroughDeleteSingleUrl, { id: obj.id }, response => {
            if (response.success) {
              this.recycleBinList()
              this.$message({
                message: '删除成功',
                type: 'success'
              })
            }
          })
        }).catch(() => {
        })
      },
      jump(id) { // 点击超练级跳转
        this.$router.push({ path: 'viewRecycling', query: { id }})
      },
      current(val) { // 点击页码
        this.info.pageNo = val
        this.recycleBinList()
      }
    }
  }
</script>