<template>
  <div class="app-container">
    <el-card shadow="never" class="mb16 filter-card">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px" size="small">
        <el-form-item label="租户名称" prop="companyName">
          <el-input v-model="queryParams.companyName" placeholder="请输入租户名称" clearable size="small" @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="账户名称" prop="accountName">
          <el-input v-model="queryParams.accountName" placeholder="请输入账户名称" clearable size="small" @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item>
          <inline-tenant-selector @change="onTenantChange" />
          <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="accountName" />
      <el-table-column label="平台" align="center" prop="platform" width="100" />
      <el-table-column label="状态" align="center" prop="status" width="100" />
      <el-table-column label="消耗金额" align="center" prop="costAmount" width="120" />
      <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 { listAdAccount, exportAdAccount } from '@/api/admin/ad'
import InlineTenantSelector from "@/components/InlineTenantSelector"

export default {
  name: 'AdminAd',
  components: { InlineTenantSelector },
  data() {
    return {
      loading: false, exportLoading: false, showSearch: true,
      list: [], total: 0,
      queryParams: { pageNum: 1, pageSize: 10, accountName: null, companyName: null, companyId: null }
    }
  },
  created() { this.getList() },
  methods: {
    getList() {
      this.loading = true
      listAdAccount(this.queryParams).then(res => {
        this.list = res.rows || []
        this.total = res.total || 0
        this.loading = false
      }).catch(() => { this.loading = false })
    },
    onTenantChange(companyId) {
      this.queryParams.companyId = companyId
      this.handleQuery()
    },
    handleQuery() { this.queryParams.pageNum = 1; this.getList() },
    resetQuery() { this.resetForm('queryForm'); this.queryParams.companyId = null; this.handleQuery() },
    handleExport() {
      this.exportLoading = true
      exportAdAccount(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>
