<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="addFiveA">新增五A</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="LocationName"
          label="名称">
        </el-table-column>
        <el-table-column
          prop=""
          label="排序">
          <template slot-scope="scope">
            <span>{{scope.$index + 1}}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="TypeImg"
          label="类型图片">
          <template slot-scope="scope">
            <a class="tableImg">
              <img :src="scope.row.TypeImg" class="centerLabel contain" alt="">
            </a>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="300">
          <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> -->
              <el-button
              size="mini"
              type="primary"
              v-if="scope.$index !== 0 && tableData.length > 1"
              @click="orderChange(scope.$index, scope.row, 'up')">上移</el-button>
              <el-button
              size="mini"
              type="success"
              v-if="tableData.length > 1 && scope.row.index !== (tableData.length - 1)"
              @click="orderChange(scope.$index, scope.row, 'down')">下移</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: 100,
        total: 1,
      },
      tableData: []
    }
  },
  computed: {
    ...mapState({
      positionList: x => x.cms.location
    })
  },
  components: {
    tableSearch,
  },
  created () {
    this.updateSystemInfo().then(() => {
      this.getList()
    })
    this.updateLocationInfo()
  },
  methods: {
    ...mapCmsActions(['updateLocationInfo']),
    ...mapActions(['updateSystemInfo']),
    handleCurrentChange (val) {
      this.postData.currentPage = val
      this.getList()
    },
    handleEdit (index, row) { // 编辑
      this.$router.push({ name: 'editFiveA', query: { id: row.LocationId } })
    },
    handleDelete (index, row) { // 删除
      this.$confirm('确认删除此消息?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then((res) => {
        this.deleteLocation(row.LocationId)
      }).catch((res) => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    addFiveA () {
      this.$router.push({ name: 'addFiveA' })
    },
    getList () {
      this.tableData = []
      this.$ajax(this.$api.cms.location.url, {
        method: this.$api.cms.location.method,
        queryData: {
          page: this.postData.currentPage,
          pagesize: this.postData.pageSize,
          issys: 0
        }
      }).then(res => {
        for (var n = 0; n < res.length; n++) {
          res[n].index = n
        }
        this.tableData = res
      })
    },
    deleteLocation (id) {
      this.$ajax(this.$api.cms.deleteLocation.url, {
        method: this.$api.cms.deleteLocation.method,
        urlData: {
          id: id
        }
      }).then(res => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.getList()
      }).catch(msg => {

      })
    },
    orderChange (index, row, direction) {
      let data
      if (direction === 'up') {
        let down = this.tableData[index - 1].LocationId
        data = {
          locations: row.LocationId + ':' + index + ',' + down + ':' + (index + 1)
        }
      } else {
        let down = this.tableData[index + 1].LocationId
        data = {
          locations: row.LocationId + ':' + (index + 2) + ',' + down + ':' + (index + 1)
        }
      }
      this.$ajax(this.$api.cms.orderChange.url, {
        method: this.$api.cms.orderChange.method,
        urlData: {
          id: row.LocationId
        },
        data: data
      }).then(res => {
        this.getList()
      })
    }
  }
}
</script>

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