<template>
    <div>
      <div v-show="$route.name === 'sendOrder'">
        <head-todo
        :selectedIds="selectedIds"
        :type="type"
        :tabIndex="tabIndex"
        :cacheQuery="cacheQuery"
        :rows="rows"
        @getList="getList"
        @handleOpenAddForm="handleOpenForm">
        </head-todo>

        <center-list
        :tableList="tableList"
        :currentPage="currentPage"
        :pageSize="pageSize"
        :total="total"
        :tabIndex="tabIndex"
        :tabsNum="tabsNum"
        :type="type"
        @getList="getList"
        @cacheSelectedIds="cacheSelectedIds"
        @handleClickTabs="handleClickTabs"
        @handleOpenEditForm="handleOpenEditForm"
        @handleOpenDetail="handleOpenDetail">
        </center-list>
      </div>
      <router-view></router-view>
    </div>
</template>
<script>
import HeadTodo from './components/head-todo'
import CenterList from './components/center-list'
export default {
  name: 'SendOrder',
  components: {
    HeadTodo,
    CenterList
  },
  data () {
    return {
      mode: 'create',
      selectedIds: '',
      // 查询条件
      query: {
        'page.currentPage': 1,
        'page.limitCount': 10
      },
      tabsNum: {},
      tableList: [],
      total: 0,
      currentPage: 1,
      pageSize: 10, // 每页显示多少条数据
      status: '',
      type: '',
      // todoBool: true,
      tabIndex: '0',
      cacheQuery: {
        'status': '00',
        'auditStatus': '90,00'
      },
      rows: []
    }
  },
  created () {
    const query = {
      'status': '00',
      'auditStatus': '90,00'
    }
    this.getList(query)
  },
  mounted () {

  },
  methods: {

    getData (url, param) {
      return this.$https.post(url, param)
    },

    // 查询列表
    getList (query, ...args) {
      let param = {}
      if (args && args[0] === 'quick') {
        param = this.query = query
      } else {
        param = this.query = { ...this.query, ...query }
      }
      const url = this.$API.stockOut.sendOrder.getList
      const tabUrl = this.$API.stockOut.sendOrder.getTabNum
      this.$https.all([this.getData(url, param), this.getData(tabUrl, param)]).then(res => {
        this.tableList = res[0].rows
        this.total = res[0].total
        this.currentPage = res[0].currentPage
        this.pageSize = res[0].limit
        this.tabsNum = res[1].data
      })
    },

    // 新增
    handleOpenForm (mode) {
      this.mode = mode
      this.$router.push({
        name: 'sendOrderAdd',
        params: {
          mode
        }
      })
    },
    //  编辑
    handleOpenEditForm (mode, headData, goodsData, carData) {
      this.mode = mode
      this.$router.push({
        name: 'sendOrderAdd',
        params: {
          headData,
          goodsData,
          carData,
          mode
        }
      })
    },

    // 详情
    handleOpenDetail (mode, index, ...arg) {
      this.mode = mode
      this.$router.push({
        name: 'sendOrderAdd',
        params: {
          mode,
          index,
          ...arg,
        }
      })
    },

    // 删除查询对象中的键值
    delQueryParam () {
      Object.keys(this.query).forEach((item, index) => {
        if (item === 'auditStatus') delete this.query[item]
      })
    },

    // 切换tab
    handleClickTabs (tab, event) {
      let query = {}
      switch (tab.paneName) {
        case '0':
          this.tabIndex = '0'
          query = {
            'status': '00',
            'auditStatus': '90,00' }
          this.cacheQuery = query
          this.getList(query)
          break
        case '1':
          // 待分配
          this.tabIndex = '1'
          query = {
            'status': '00,10,20,30',
            'auditStatus': '90,99' }
          this.cacheQuery = query
          this.getList(query)
          break
        case '2':
          // 已分配
          this.tabIndex = '2'
          this.delQueryParam()
          query = { 'status': '40' }
          this.cacheQuery = query
          this.getList(query)
          break
        case '3':
          // 出库中
          this.tabIndex = '3'
          this.delQueryParam()
          query = { 'status': '50,60,70' }
          this.cacheQuery = query
          this.getList(query)
          break
        case '4':
          // 已完成
          this.tabIndex = '4'
          this.delQueryParam()
          query = { 'status': '80' }
          this.cacheQuery = query
          this.getList(query)
          break
        case '5':
          // 已关闭
          this.tabIndex = '5'
          this.delQueryParam()
          query = { 'status': '99' }
          this.cacheQuery = query
          this.getList(query)
          break
        case '6':
          // 已取消
          this.tabIndex = '6'
          this.delQueryParam()
          query = { 'status': '90' }
          this.cacheQuery = query
          this.getList(query)
          break
        default:
          break
      }
    },

    // checkbox勾选的ID
    cacheSelectedIds (ids, rows) {
      this.selectedIds = ids
      this.rows = rows
    },

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

    next()
  },
  // beforeRouteLeave (to, from, next) {
  //   const answer = window.confirm('Do you really want to leave? you have unsaved changes!')
  //   if (answer) {
  //     next()
  //   } else {
  //     next(false)
  //   }
  // }
}
</script>
