<template>
  <div class="event">
    <div ref="leftContainer" class="left">
      <el-form :inline="true">
        <el-form-item :label="$t('system:event:eventTypeName')">
          <el-select
            v-model="searchData.EventTypeName"
            clearable
            filterable
            placeholder=""
          >
            <el-option
              v-for="item in $d.EventTypeName"
              :key="item.id"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('system:event:eventState')">
          <el-select
            v-model="searchData.EventState"
            clearable
            filterable
            placeholder=""
          >
            <el-option
              v-for="item in $d.EventState"
              :key="item.id"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="getList"
          >
            {{ $t('common:button:search') }}
          </el-button>
          <el-button
            icon="el-icon-refresh-left"
            @click="handleReset"
          >
            {{ $t('common:button:reset') }}
          </el-button>
        </el-form-item>
      </el-form>
      <el-table
        v-loading="loading"
        v-adaptive="{ bottomOffset: 45 }"
        height="100"
        :data="list"
        class="table"
        @sort-change="handleSortByColumn"
        @selection-change="handleSelectionChange"
        ><el-table-column type="selection" width="50" />
        <el-table-column type="index" width="50" />
        <el-table-column
          :label="$t('system:event:eventTypeName')"
          prop="EventTypeName"
          show-overflow-tooltip
          sortable="custom"
        >
          <template slot-scope="scope">
            <span>{{ $fd('EventTypeName', scope.row.EventTypeName) }}</span>
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('system:event:eventState')"
          prop="EventState"
          show-overflow-tooltip
          sortable="custom"
        >
          <template slot-scope="scope">
            <el-tag
              :type="['info', 'success', 'primary'][scope.row.EventState]"
            >
              {{ $fd('EventState', scope.row.EventState) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('system:event:eventCreateTime')"
          prop="CreateTime"
          show-overflow-tooltip
          sortable="custom"
        />
        <el-table-column
          :label="$t('common:action:action')"
          fixed="right"
          prop=""
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <el-button type="text" @click="view(scope.row)">
              {{ $t('common:button:view') }}
            </el-button>
            <el-button type="text" @click="rePublish(scope.row)">
              {{ $t('system:event:button:rePublish') }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination" style="text-align: right; margin-top: 5px">
        <pagination
          :total="total"
          :page.sync="searchData.PageIndex"
          :limit.sync="searchData.PageSize"
          @pagination="getList"
        />
      </div>
    </div>
    <base-model v-if="model_cfg.visible" :config="model_cfg">
      <template slot="dialog-body">
        <div v-html="info"></div>
      </template>
    </base-model>
  </div>
</template>
<script>
import { getEventStoreRecordList, rePublishEvent } from '@/api/admin'
import Pagination from '@/components/Pagination'
import BaseModel from '@/components/BaseModel'
const searchDataDefault = () => {
  return {
    EventState: null,
    EventTypeName: null,
    Asc: false,
    SortField: 'CreateTime',
    PageIndex: 1,
    PageSize: 20,
  }
}
export default {
  name: 'event',
  components: { Pagination, BaseModel },
  data() {
    return {
      searchData: searchDataDefault(),
      list: [],
      total: 0,
      loading: false,
      PublishVersionList: [],
      selectTableList: [],
      model_cfg: { visible: false, title: '', width: '500px' },
      info: null,
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      getEventStoreRecordList(this.searchData)
        .then((res) => {
          this.loading = false
          this.list = res.Result.CurrentPageData
          this.total = res.Result.TotalCount
        })
        .catch(() => {
          this.loading = false
        })
    },
    async rePublish(row) {
      try {
        this.loading = true
        let res = await rePublishEvent({
          EventId: row.Id,
        })
        if (res.IsSuccess) {
          this.$message.success(
            this.$t('system:event:message:rePublishSuccess')
          )
          this.getList()
        }
      } catch (err) {
        this.loading = false
        console.log(err)
      }
    },
    // 重置列表查询
    handleReset() {
      this.searchData = searchDataDefault()
      this.getList()
    },
    // 排序
    handleSortByColumn(column) {
      if (column.order === 'ascending') {
        this.searchData.Asc = true
      } else {
        this.searchData.Asc = false
      }
      this.searchData.SortField = column.prop
      this.searchData.PageIndex = 1
      this.getList()
    },
    // 选择
    handleSelectionChange(val) {
      this.selectTableList = val
    },
    // 查看
    view(row) {
      let info = JSON.parse(row.EventData)
      let message = ''
      if (info) {
        Object.keys(info).forEach((key) => {
          message += `<div style="margin-bottom:10px">
            <span style="color:#F56C6C">${key}</span>：<span style="color:#67C23A">${info[key]}</span>
            </div>`
        })
      }
      this.info = message
      this.model_cfg.visible = true
    },
  },
}
</script>
<style lang="scss" scoped>
.event {
  height: 100%;
  box-sizing: border-box;
  display: flex;
  padding: 10px;
  border-radius: 5px;
  .left {
    display: flex;
    flex-direction: column;
    width: 0;
    flex-grow: 4;
    // border-right: 1px solid #ccc;
    .filter-container {
      display: flex;
      align-items: center;
      margin: 5px;
    }
    .data-table {
      flex: 1;
      padding: 5px 0px;
    }
    .pagination-container {
      text-align: right;
    }
  }
}
</style>
