<template>
  <div>
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="32">
              <a-form-item label="标题">
                <a-input v-model="queryParam.searchValue" allow-clear placeholder="请输入标题、内容" />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="32">
              <a-form-item label="类型">
                <a-select v-model="queryParam.type" placeholder="请选择类型" allow-clear>
                  <a-select-option
                    v-for="(item,index) in typeDictTypeDropDown"
                    :key="index"
                    :value="item.code"
                  >{{ item.value }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="32">
              <a-button type="primary" @click="$refs.table.refresh(true)">搜索</a-button>
              <a-button type="primary" style="margin-left: 8px" @click="$refs.detailsOplog.details()">新增</a-button>
            </a-col>
          </a-row>
        </a-form>
        <!-- 表格 -->
        <s-table
          ref="table"
          size="default"
          :pagination="false"
          :loading="loading"
          :columns="columns"
          :data="loadData"
          :rowKey="(record) => record.sessionId"
        >
          <span slot="lastLoginAddress" slot-scope="text">
            <ellipsis :length="20" tooltip>{{ text }}</ellipsis>
          </span>
          <span slot="lastLoginBrowser" slot-scope="text">
            <ellipsis :length="20" tooltip>{{ text }}</ellipsis>
          </span>
          <span slot="action" slot-scope="text, record">
            <a-popconfirm
              v-if="hasPerm('sysOnlineUser:forceExist')"
              placement="topRight"
              title="是否确认下线该条目？"
              @confirm="() => forceExist(record)"
            >
              <a class="mr5">下线</a>
            </a-popconfirm>
            <a class="mr5" @click="$refs.detailsOplog.details(record)">编辑</a>
            <a-popconfirm
              v-if="hasPerm('sysOnlineUser:forceExist')"
              placement="topRight"
              title="是否确认删除该条目？"
              @confirm="() => forceExist(record)"
            >
              <a class="mr5">删除</a>
            </a-popconfirm>
          </span>
        </s-table>
      </div>
      <details-oplog ref="detailsOplog" />
    </a-card>
  </div>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import { sysDictTypeDropDown } from '@/api/modular/system/dictManage'
import {
  findArticleContentById,
  addArticleContent,
  updateArticleContent,
  deleteArticleContent
} from '@/api/modular/system/contentControlApi'
import detailsOplog from './details'

export default {
  components: {
    STable,
    detailsOplog,
    Ellipsis
  },
  data () {
    return {
      // 查询参数
      queryParam: {},
      typeDictTypeDropDown: [],
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: 'setIndex'
        },
        {
          title: '标题',
          dataIndex: 'nickName'
        },
        {
          title: '封面',
          dataIndex: 'lastLoginIp'
        },
        {
          title: '摘要',
          dataIndex: 'lastLoginTime'
        },
        {
          title: '内容类型',
          dataIndex: 'lastLoginAddress',
          scopedSlots: { customRender: 'lastLoginAddress' }
        },
        {
          title: '分类',
          dataIndex: 'type'
        },
        {
          title: '子分类',
          dataIndex: 'childType'
        },
        {
          title: '排序',
          dataIndex: 'index'
        }
      ],
      loading: true,
      loadData: parameter => {
        const params = {
          articleContentTitle: '',
          id: ''
        }
        return findArticleContentById(Object.assign(parameter, params)).then(res => {
          if (this.hasPerm('sysOnlineUser:list')) {
            if (res.data.length > 0) {
              const list = res.data.rows
              list.map((item, index) => {
                item.setIndex = index + 1
                return item
              })
            }
            return res.data
          } else {
            return new Promise((resolve, reject) => {
              return resolve()
            })
          }
        })
      },
      selectedRows: []
    }
  },
  created () {
    this.sysDictTypeDropDown() // 先注释
    if (this.hasPerm('sysOnlineUser:forceExist')) {
      this.columns.push({
        title: '操作',
        width: '150px',
        dataIndex: 'action',
        scopedSlots: { customRender: 'action' }
      })
    }
  },
  methods: {
    forceExist (record) {
      const params = {
        id: '',
        isLine: 0 // 0是未上线，1是上线
      }
      updateArticleContent(params)
        .then(res => {
          if (res.success) {
            this.$message.success('强制下线成功')
            // 重新加载表格
            this.loadDataList()
          } else {
            this.$message.error('强制下线失败：' + res.message)
          }
        })
        .catch(err => {
          this.$message.error('强制下线错误：' + err.message)
        })
    },
    sysDictTypeDropDown () {
      sysDictTypeDropDown({ code: 'notice_type' }).then(res => {
        this.typeDictTypeDropDown = res.data
      })
    },
    addArticleContent () {
      addArticleContent({ code: 'notice_type' }).then(res => {
        this.typeDictTypeDropDown = res.data
      })
    },
    deleteArticleContent () {
      deleteArticleContent({ code: 'notice_type' }).then(res => {
        this.typeDictTypeDropDown = res.data
      })
    }
  }
}
</script>

<style lang="less">
.mr5 {
  margin-right: 5px;
}
</style>
