<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='addNews'>新增资讯</el-button>
        </div>
        <ul>
          <li>
            <span>选择位置：</span>
            <el-select v-model="tableSearch.LocationId" placeholder="请选择">
              <el-option
                key=""
                label="全部"
                value="">
              </el-option>
              <el-option
                v-for="item in positionList"
                :key="item.LocationId"
                :label="item.LocationName"
                :value="item.LocationId">
              </el-option>
            </el-select>
          </li>
        </ul>
        <tableSearch @exportSearchKey="searchList" value="请输入标题"></tableSearch>
      </div>
      <div class="moreFilter"></div>
    </div>
    <div class="system-table-box">
      <el-table
        :data="tableData"
        stripe
        style="width: 100%">
        <el-table-column
          prop="img"
          label="图片">
          <template slot-scope="scope">
            <a class="tableImg">
              <img :src="scope.row.ImageUrl" class="centerLabel contain" alt="">
            </a>
          </template>
        </el-table-column>
        <el-table-column
          prop="Title"
          label="标题">
        </el-table-column>
        <el-table-column
          prop="LocationNames"
          label="位置">
        </el-table-column>
        <el-table-column
          prop="StatusShow"
          label="是否发布">
        </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.page"
      :page-size="postData.pagesize"
      layout="prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
import tableSearch from '@/components/tableSearch/index'
import { mapState, createNamespacedHelpers } from 'vuex'
const { mapActions: mapCmsActions } = createNamespacedHelpers('cms')
export default {
  name: '',
  data () {
    return {
      total: 0,
      postData: {
        page: 1, // 当前页码
        pagesize: 10,
      },
      tableSearch: { // 表格搜索条件
        key: '', // 搜索关键字
        LocationId: '',
      },
      tableData: []
    }
  },
  computed: {
    ...mapState({
      positionList: x => x.cms.location,
    })
  },
  components: {
    tableSearch,
  },
  created () {
    this.updateLocationInfo().then(() => {
      this.getList()
    })
  },
  methods: {
    ...mapCmsActions(['updateLocationInfo']),
    handleCurrentChange (val) {
      this.postData.page = val
      this.getList()
    },
    handleEdit (index, row) { // 编辑
      this.$router.push({ name: 'editNews', query: { id: row.NewsId } })
    },
    handleDelete (index, row) { // 删除
      this.$confirm('确认删除此资讯?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteNews(row.NewsId)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    searchList (key) { // 搜索列表
      this.postData.page = 1
      this.total = 1
      this.tableSearch.key = key
      this.getList()
    },
    addNews () {
      this.$router.push({ name: 'addNews' })
    },
    getList () {
      this.tableData = []
      this.$ajax(this.$api.cms.news.url, {
        method: this.$api.cms.news.method,
        queryData: {
          ...this.postData,
          locationid: this.tableSearch.LocationId,
          title: this.tableSearch.key
        }
      }).then(res => {
        for (let i = 0; i < res.list.length; i++) {
          res.list[i].Status === 1 ? res.list[i].StatusShow = '是' : res.list[i].StatusShow = '否'
        }
        this.tableData = res.list
        this.total = res.pagenum
        this.postData.page = res.page
      }).catch(msg => {

      })
    },
    deleteNews (id) {
      this.$ajax(this.$api.cms.deleteNews.url, {
        method: this.$api.cms.deleteNews.method,
        urlData: {
          id: id
        }
      }).then(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>
