<template>
  <div>
    <div v-show="$route.name === 'contract'">
      <head-todo
        :selectIds="selectIds"
        :rows="rows"
        @getList="getList"
        @handleOpenForm="handleOpenForm">
        </head-todo>

        <center-list
            :tableData="tableList"
            :currentPage="currentPage"
            :pageSize="pageSize"
            :total="total"
            @handleSelectChange="cacheSelectedIds"
            @getList="getList"
            @handleOpenForm="handleOpenForm">
        </center-list>
    </div>
      <router-view></router-view>
  </div>
</template>
<script>
import HeadTodo from './components/head-todo'
import CenterList from './components/center-list'
import AddForm from './components/add-form'
export default {
  name: 'Contract',
  components: {
    HeadTodo,
    CenterList,
    AddForm
  },
  data () {
    return {
      tableList: [],
      total: 0,
      query: {
        'page.currentPage': 1,
        'page.limitCount': 10,
      },
      addFormVisible: false,
      selectIds: [],
      rowData: {},
      mode: 'create',
      rows: [],
      currentPage: 1,
      pageSize: 10
    }
  },
  created () {
    this.getList()
  },
  methods: {
    getList (query) {
      const url = this.$API.charging.contract.getList
      const param = { ...this.query, ...query }
      this.$https.post(url, param).then(res => {
        // console.log(res)
        this.tableList = res.rows
        this.total = res.total
        this.currentPage = res.currentPage
        this.pageSize = res.limit
      })
    },

    //  新增
    handleOpenForm (mode, ...args) {
      this.mode = mode
      this.$router.push({
        name: 'contractAdd',
        params: {
          mode,
          ...args
        }
      })
    },

    toGetList () {
      this.getList()
    },

    // 关闭新增弹窗
    handleFormClose () {
      this.addFormVisible = !this.addFormVisible
    },
    // checkbox勾选储存每一行数据对象
    cacheSelectedIds (ids, rows) {
      this.selectIds = ids
      this.rows = rows
    }

  },

  // 路由跳回来前判断是否需要刷新列表
  beforeRouteUpdate (to, from, next) {
    if (to.meta.needRefresh) {
      this.getList()
      delete to.meta.needRefresh
    }
    next()
  },
}

</script>
