<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="productName">
          <el-input
            v-model="queryParams.productName"
            placeholder="请输入商品名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="商品状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择商品状态" clearable size="small">
            <el-option
              v-for="item in statusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </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"
          v-hasPermi="['admin:product:list']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-document"
          size="mini"
          @click="handlePending"
        >待审核</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-bar-chart"
          size="mini"
          @click="showStatistics = true"
        >商品统计</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="productList">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="租户名称" align="center" prop="companyName" min-width="120" />
      <el-table-column label="商品名称" align="center" prop="productName" min-width="150" show-overflow-tooltip />
      <el-table-column label="商品图片" align="center" prop="productImage" min-width="100">
        <template slot-scope="scope">
          <el-image
            v-if="scope.row.productImage"
            style="width: 80px; height: 60px"
            :src="scope.row.productImage"
            fit="contain" />
        </template>
      </el-table-column>
      <el-table-column label="商品分类" align="center" prop="categoryName" min-width="100" />
      <el-table-column label="商品价格" align="center" prop="price" min-width="90" />
      <el-table-column label="库存数量" align="center" prop="stock" min-width="80" />
      <el-table-column label="创建时间" align="center" prop="createTime" min-width="150" />
      <el-table-column label="商品状态" align="center" prop="status" min-width="90">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 0" type="warning">待审核</el-tag>
          <el-tag v-else-if="scope.row.status == 1" type="success">已上架</el-tag>
          <el-tag v-else type="error">已拒绝</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            plain
            @click="handleView(scope.row)"
          >详情</el-button>
          <template v-if="scope.row.status == 0">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-check"
              plain
              @click="handleAudit(scope.row, 1)"
              v-hasPermi="['admin:product:audit']"
            >上架</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-close"
              plain
              @click="handleAudit(scope.row, 2)"
              v-hasPermi="['admin:product:audit']"
            >拒绝</el-button>
          </template>
        </template>
      </el-table-column>
    </el-table>

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

    <!-- 详情弹窗 -->
    <el-dialog title="商品详情" :visible.sync="viewOpen" width="600px" append-to-body>
      <el-form :model="viewForm" label-width="100px">
        <el-form-item label="租户名称">
          <span>{{ viewForm.companyName }}</span>
        </el-form-item>
        <el-form-item label="商品名称">
          <span>{{ viewForm.productName }}</span>
        </el-form-item>
        <el-form-item label="商品图片">
          <el-image v-if="viewForm.productImage" style="width: 120px; height: 90px" :src="viewForm.productImage" fit="contain" />
        </el-form-item>
        <el-form-item label="商品分类">
          <span>{{ viewForm.categoryName }}</span>
        </el-form-item>
        <el-form-item label="商品价格">
          <span>{{ viewForm.price }}</span>
        </el-form-item>
        <el-form-item label="库存数量">
          <span>{{ viewForm.stock }}</span>
        </el-form-item>
        <el-form-item label="商品描述">
          <span>{{ viewForm.description }}</span>
        </el-form-item>
        <el-form-item label="创建时间">
          <span>{{ viewForm.createTime }}</span>
        </el-form-item>
        <el-form-item label="商品状态">
          <el-tag v-if="viewForm.status == 0" type="warning">待审核</el-tag>
          <el-tag v-else-if="viewForm.status == 1" type="success">已上架</el-tag>
          <el-tag v-else type="error">已拒绝</el-tag>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 审核弹窗 -->
    <el-dialog :title="auditTitle" :visible.sync="auditOpen" width="400px" append-to-body>
      <el-form ref="auditForm" :model="auditForm" label-width="80px">
        <el-form-item label="审核备注">
          <el-textarea v-model="auditForm.auditRemark" placeholder="请输入审核备注" :rows="3" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitAudit">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 统计弹窗 -->
    <el-dialog title="商品统计" :visible.sync="showStatistics" width="600px" append-to-body>
      <el-card>
        <el-row :gutter="20">
          <el-col :span="12">
            <div class="stat-item">
              <span class="stat-value">{{ statistics.totalCount }}</span>
              <span class="stat-label">商品总数</span>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="stat-item">
              <span class="stat-value">{{ statistics.pendingCount }}</span>
              <span class="stat-label">待审核</span>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="stat-item">
              <span class="stat-value">{{ statistics.onlineCount }}</span>
              <span class="stat-label">已上架</span>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="stat-item">
              <span class="stat-value">{{ statistics.rejectedCount }}</span>
              <span class="stat-label">已拒绝</span>
            </div>
          </el-col>
        </el-row>
      </el-card>
    </el-dialog>
  </div>
</template>

<script>
import { listAllProducts, listPendingProducts, getProductInfo, auditProduct, getProductStatistics, exportAllProducts } from '@/api/admin/product'
import { listAllCompanies } from '@/api/admin/sysCompany'

export default {
  name: 'ProductAdmin',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品列表
      productList: [],
      // 租户列表
      companyList: [],
      // 统计数据
      statistics: {
        totalCount: 0,
        pendingCount: 0,
        onlineCount: 0,
        rejectedCount: 0
      },
      // 状态选项
      statusOptions: [
        { value: 0, label: '待审核' },
        { value: 1, label: '已上架' },
        { value: 2, label: '已拒绝' }
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyId: null,
        productName: null,
        status: null
      },
      // 详情弹窗
      viewOpen: false,
      viewForm: {},
      // 审核弹窗
      auditOpen: false,
      auditTitle: '',
      auditForm: {
        productId: null,
        status: null,
        auditRemark: ''
      },
      // 统计弹窗
      showStatistics: false
    }
  },
  created() {
    this.getCompanyList()
    this.getList()
  },
  methods: {
    /** 获取租户列表 */
    getCompanyList() {
      listAllCompanies({ pageSize: 9999 }).then(response => {
        this.companyList = response.rows || []
      })
    },
    /** 查询列表 */
    getList() {
      this.loading = true
      listAllProducts(this.queryParams).then(response => {
        this.productList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 查询待审核列表 */
    handlePending() {
      this.loading = true
      listPendingProducts().then(response => {
        this.productList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.queryParams.companyId = null
      this.handleQuery()
    },
    /** 查看详情 */
    handleView(row) {
      getProductInfo(row.productId).then(response => {
        this.viewForm = response.data
        this.viewOpen = true
      })
    },
    /** 审核商品 */
    handleAudit(row, status) {
      this.auditTitle = status === 1 ? '上架商品' : '拒绝商品'
      this.auditForm = {
        productId: row.productId,
        status: status,
        auditRemark: ''
      }
      this.auditOpen = true
    },
    /** 提交审核 */
    submitAudit() {
      auditProduct(this.auditForm.productId, this.auditForm.status, this.auditForm.auditRemark).then(response => {
        this.$message.success(this.auditForm.status === 1 ? '上架成功' : '拒绝成功')
        this.auditOpen = false
        this.getList()
      })
    },
    /** 查看统计 */
    showStatisticsDialog() {
      getProductStatistics().then(response => {
        this.statistics = response.data
        this.showStatistics = true
      })
    },
    /** 导出按钒操作 */
    handleExport() {
      this.exportLoading = true
      exportAllProducts(this.queryParams).then(response => {
        this.download(response.msg)
        this.exportLoading = false
      }).catch(() => { this.exportLoading = false })
    }
  }
}
</script>

<style scoped>
.stat-item {
  text-align: center;
  padding: 20px;
  background: #f5f7fa;
  border-radius: 8px;
}
.stat-value {
  font-size: 24px;
  font-weight: bold;
  color: #1890ff;
}
.stat-label {
  display: block;
  margin-top: 5px;
  font-size: 14px;
  color: #666;
}
.mb16 { margin-bottom: 16px; }
.filter-card { padding-bottom: 0; }
</style>