<template>
  <div class="app-container">
    <el-card shadow="never" class="mb16 filter-card">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" size="small" label-width="100px">
        <el-form-item label="租户名称" prop="tenantId">
          <inline-tenant-selector :key="tenantSelectorKey" @change="handleTenantChange" />
        </el-form-item>
        <el-form-item label="订单编号" prop="orderNo">
          <el-input v-model="queryParams.orderNo" placeholder="请输入订单编号" clearable size="small" @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" :loading="exportLoading" @click="handleExport">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border size="small" style="width:100%" v-loading="loading" :data="list">
      <el-table-column label="租户名称" align="center" prop="companyName" min-width="150" />
      <el-table-column label="订单编号" align="center" prop="orderNo" width="180" />
      <el-table-column label="订单金额" align="center" prop="orderAmount" width="120" />
      <el-table-column label="支付方式" align="center" prop="payType" width="100" />
      <el-table-column label="订单状态" align="center" prop="orderStatus" width="100" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="160" />
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
  </div>
</template>

<script>
import { listStoreOrder, exportStoreOrder } from '@/api/admin/storeOrder'
import InlineTenantSelector from '@/components/InlineTenantSelector'

export default {
  name: 'AdminStoreOrder',
  components: { InlineTenantSelector },
  data() {
    return {
      loading: false, exportLoading: false, showSearch: true,
      list: [], total: 0,
      tenantSelectorKey: 0,
      queryParams: { pageNum: 1, pageSize: 10, orderNo: null, tenantId: null }
    }
  },
  methods: {
    getList() {
      if (!this.queryParams.tenantId) {
        this.list = []
        this.total = 0
        this.loading = false
        return
      }
      this.loading = true
      listStoreOrder(this.queryParams).then(res => {
        this.list = res.rows || []
        this.total = res.total || 0
        this.loading = false
      }).catch(() => { this.loading = false })
    },
    handleTenantChange(val) {
      if (val) {
        this.queryParams.tenantId = val
        this.handleQuery()
      }
    },
    handleQuery() {
      if (!this.queryParams.tenantId) {
        this.$message.warning('请先选择租户')
        return
      }
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.list = []
      this.total = 0
      this.queryParams.tenantId = null
      this.$store.dispatch('tenant/setTenantId', null)
      this.tenantSelectorKey++
    },
    handleExport() {
      this.exportLoading = true
      exportStoreOrder(this.queryParams).then(response => {
        this.download(response.msg)
        this.exportLoading = false
      }).catch(() => { this.exportLoading = false })
    }
  }
}
</script>

<style scoped>
.mb16 { margin-bottom: 16px; }
.filter-card { padding-bottom: 0; }
</style>
