<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="companyId">
          <el-select
            v-model="queryParams.companyId"
            placeholder="选择租户"
            clearable
            filterable
            size="small"
            style="width: 200px"
          >
            <el-option
              v-for="item in companyList"
              :key="item.companyId"
              :label="item.companyName"
              :value="item.companyId"
            />
          </el-select>
        </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 { listAllCompanies } from '@/api/admin/sysCompany'

export default {
  name: 'AdminStoreOrder',
  data() {
    return {
      loading: false, exportLoading: false, showSearch: true,
      list: [], total: 0, companyList: [],
      queryParams: { pageNum: 1, pageSize: 10, orderNo: null, companyId: null }
    }
  },
  created() { this.getCompanyList(); this.getList() },
  methods: {
    getCompanyList() {
      listAllCompanies({ pageSize: 9999 }).then(response => {
        this.companyList = response.rows || []
      })
    },
    getList() {
      this.loading = true
      listStoreOrder(this.queryParams).then(res => {
        this.list = res.rows || []
        this.total = res.total || 0
        this.loading = false
      }).catch(() => { this.loading = false })
    },
    handleQuery() { this.queryParams.pageNum = 1; this.getList() },
    resetQuery() { this.resetForm('queryForm'); this.queryParams.companyId = null; this.handleQuery() },
    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>
