<template>
  <div class="subPage">
    <div class="system-table-search">
      <div class="flex-h">
        <div class="flex-item flex-h">
          <el-button size="mini" type="success" @click='addIndexMsg'>新增消息</el-button>
        </div>
        <tableSearch value="请输入标题" @exportSearchKey="searchList"></tableSearch>
      </div>
      <div class="moreFilter"></div>
    </div>
    <div class="system-table-box">
      <el-table
        :data="tableData"
        stripe
        style="width: 100%">
        <el-table-column
          prop="InfoName"
          label="消息标题">
        </el-table-column>
        <el-table-column
          prop="InfoUrl"
          label="跳转链接">
        </el-table-column>
        <el-table-column
          label="创建时间">
          <template slot-scope="scope">
            <label>{{FormatDate(scope.row.CreateDate)}}</label>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="warning"
              @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page.sync="postData.currentPage"
      :page-size="postData.pageSize"
      layout="prev, pager, next, jumper"
      :total="postData.total">
    </el-pagination>
  </div>
</template>

<script>
import tableSearch from '@/components/tableSearch/index'
import { createNamespacedHelpers } from 'vuex'

const { mapState: mapCaseState } = createNamespacedHelpers('case')

export default {
  name: '',
  data () {
    return {
      postData: {
        currentPage: 1, // 当前页码
        pageSize: 10,
        total: 0,
      },
      tableSearch: { // 表格搜索条件
        key: '', // 搜索关键字
        caseId: '', // 案场id
      },
      tableData: []
    }
  },
  computed: {
    ...mapCaseState({
      caseList: x => x.caseList,
    })
  },
  created () {
    this.getList()
  },
  components: {
    tableSearch,
  },
  methods: {
    FormatDate (date) {
      return this.toolClass.dateFormat(date)
    },
    handleCurrentChange (val) {
      this.postData.currentPage = val
      this.getList()
    },
    handleEdit (index, row) { // 编辑
      this.$router.push({ name: 'editIndexMsg', query: { id: row.InfoId } })
    },
    handleDelete (index, row) { // 删除
      this.$confirm('确认删除此消息?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteInfo(row.InfoId)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    searchList (key) { // 搜索列表
      this.tableSearch.key = key
      this.getList()
    },
    addIndexMsg () {
      this.$router.push({ name: 'addIndexMsg' })
    },
    getList () {
      this.tableData = []
      this.$ajax(this.$api.cms.info.url, {
        method: this.$api.cms.info.method,
        queryData: {
          page: this.postData.currentPage,
          pagesize: this.postData.pageSize,
          title: this.tableSearch.key
        }
      }).then(res => {
        this.tableData = res.list
        this.postData.total = res.pagenum
        this.postData.currentPage = res.page
      }).catch(msg => {

      })
    },
    deleteInfo (id) {
      this.$ajax(this.$api.cms.deleteInfo.url, {
        method: this.$api.cms.deleteInfo.method,
        urlData: {
          id: id
        }
      }).then(res => {
        console.log(res)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.getList()
      }).catch(msg => {

      })
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped>
</style>
