<template>
  <div v-loading="loading">
    <div class="white-box">
      <div class="filter-box">
        <filter-user :filter-types="filterTypes" @select-value="handleFilterUser"></filter-user>
      </div>
      <el-table class="table-box" ref="multipleTable" :data="tableData" stripe style="width: 100%;" @selection-change="handleSelectionChange" :height="tool.getTableHeight()">
        <el-table-column type="selection" width="70" v-if="tableHeaders"></el-table-column>
        <el-table-column v-for="(tableHeader, key) in tableHeaders" :key="key" :label="tableHeader.header" :width="tableHeader.other.width ? tableHeader.other.width : ''" :prop="tableHeader.other.prop ? tableHeader.other.prop : null">
          <template slot-scope="scope">
            <template v-if="scope.row[tableHeader.index].other.tag" >
              <el-tag :type="scope.row[tableHeader.index].other.tag.type ? scope.row[tableHeader.index].other.tag.type : null" :size="scope.row[tableHeader.index].other.tag.size ? scope.row[tableHeader.index].other.tag.size : null" :class="scope.row[tableHeader.index].other.tag.class ? scope.row[tableHeader.index].other.tag.class : null" >{{scope.row[tableHeader.index].value}}</el-tag>
            </template>
            <template v-else>
              <div v-html="scope.row[tableHeader.index].value"></div>
            </template>
          </template>
        </el-table-column>
      </el-table>
      <div class="white-box-footer">
        <el-button type="success" size="small" @click="handleExport" v-show="permission.hasPermission(`shop/ba-order-list-export`)">Export Excel</el-button>
        <el-button type="primary" size="small" @click="handleExportPDF" v-show="permission.hasPermission(`shop/ba-order-list-export`)">Export PDF</el-button>
        <pagination :total="totalCount" :page_size="pageSize" @size-change="handleSizeChange" @current-change="handleCurrentChange"></pagination>
      </div>
    </div>
  </div>
</template>

<script>
import network from '@/utils/network'
import tool from '@/utils/tool'
import baseInfo from '@/utils/baseInfo'
import FilterUser from '@/components/FilterUser'
import permission from '@/utils/permission'
import Pagination from '@/components/Pagination'
import filterHelper from '@/utils/filterHelper'

export default {
  name: 'ba-order-list',
  components: {FilterUser, Pagination},
  mounted () {
    this.getData()
  },
  data () {
    return {
      tableHeaders: null,
      tableData: null,
      tableHeight: window.innerHeight - 310,
      loading: true,
      multipleSelection: [],
      currentPage: 1,
      totalPages: 1,
      totalCount: 1,
      pageSize: 20,
      tool: tool,
      permission: permission,
      filterTypes: null,
      filterModel: {},
    }
  },
  methods: {
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    handleCurrentChange (page) {
      this.getData(page, this.pageSize)
    },
    handleSizeChange (pageSize) {
      this.getData(this.currentPage, pageSize)
    },
    handleFilterUser (filterData) {
      filterHelper.handleFilterUser(this, filterData)
    },
    getData (page, pageSize) {
      let filterData = this.filterModel
      network.getPageData(this, 'shop/ba-order-list', page, pageSize, filterData, response => {
        this.filterTypes = response.filterTypes
        this.allData = response
      })
    },
    handleExport () {
      this.$confirm(`Are you sure you want to export the current data？`, 'Hint', { // `确定要导出当前数据吗？`, '提示'
        confirmButtonText: 'Confirm', // 确定
        cancelButtonText: 'Cancel', // 取消
        type: 'warning'
      }).then(() => {
        return network.getData(`shop/ba-order-list-export`, this.filterModel)
      }).then(response => {
        this.$message({
          message: response,
          type: 'success'
        })
      }).catch(response => {

      })
    },
    handleExportPDF () {
      if (this.multipleSelection.length === 0) {
        this.$message({
          message: 'Please select an order to export', // 请选择一条订单导出
          type: 'error'
        })
        return false
      }

      // 提取订单ID
      let orderSnList = this.multipleSelection.map((item) => item.SN.value || '')
      // 去重
      let orderSnSet = Array.from(new Set(orderSnList))
      if (orderSnSet.length !== 1) {
        this.$message({
          message: 'Only one order can be exported at a time', // 每次只能导出一条订单
          type: 'error'
        })
        return false
      }

      this.$confirm(`Are you sure you want to export the current data？`, 'Hint', { // `确定要导出当前数据吗？`, '提示'
        confirmButtonText: 'Confirm', // 确定
        cancelButtonText: 'Cancel', // 取消
        type: 'info'
      }).then(() => {
        // 导出时只需要订单ID即可
        let orderSn = orderSnSet[0]
        network.getData(`shop/ba-order-list-export-pdf/${orderSn}`).then(response => {
          this.$message({
            message: response,
            type: 'success'
          })
        })
      }).catch(response => {
        this.$message({
          message: response,
          type: 'error'
        })
      })
    },
  }
}

</script>

<style scoped>
</style>
