<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="addBanner">新增轮播图</el-button>
        </div>
        <!-- <tableSearch @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="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="LocationShow"
          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.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 { mapState, createNamespacedHelpers, mapActions } from 'vuex'
const { mapActions: mapCmsActions } = createNamespacedHelpers('cms')
export default {
  name: '',
  data () {
    return {
      postData: {
        currentPage: 1, // 当前页码
        pageSize: 10,
        total: 0,
      },
      tableData: []
    }
  },
  computed: {
    ...mapState({
      positionList: x => x.cms.locationAll,
    })
  },
  components: {
    tableSearch,
  },
  created () {
    this.updateSystemInfo().then(() => {
      this.getList()
    })
    this.updateLocationAllInfo()
  },
  methods: {
    ...mapCmsActions(['updateLocationAllInfo']),
    ...mapActions(['updateSystemInfo']),
    handleCurrentChange (val) {
      this.postData.currentPage = val
      this.getList()
    },
    handleEdit (index, row) { // 编辑
      this.$router.push({ name: 'editBanner', query: { id: row.ImageId } })
    },
    handleDelete (index, row) { // 删除
      this.$confirm('确认删除此消息?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then((res) => {
        this.deleteImage(row.ImageId)
      }).catch((res) => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    addBanner () {
      this.$router.push({ name: 'addBanner' })
    },
    getList () {
      this.tableData = []
      this.$ajax(this.$api.cms.imageList.url, {
        method: this.$api.cms.imageList.method,
        queryData: {
          page: this.postData.currentPage,
          pagesize: this.postData.pageSize
        }
      }).then(res => {
        let length = res.list.length
        for (let i = 0; i < length; i++) {
          if (res.list[i].Status === 1) {
            res.list[i].StatusShow = '是'
          } else {
            res.list[i].StatusShow = '否'
          }
          for (let j = 0; j < this.positionList.length; j++) {
            if (res.list[i].LocationId === this.positionList[j].LocationId) {
              res.list[i].LocationShow = this.positionList[j].LocationName
            }
          }
        }
        this.tableData = res.list
        this.postData.total = res.pagenum
        this.postData.currentPage = res.page
      }).catch(msg => {

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