<template>
  <div class="app-container">
    <el-card>
      <div slot="header" class="clearfix">
        <span>佣金记录管理</span>
        <el-button
          style="float: right"
          type="primary"
          @click="handleSettle"
        >
          手动结算
        </el-button>
      </div>

      <!-- 搜索表单 -->
      <el-form :inline="true" :model="queryParams" class="search-form">
        <el-form-item label="用户ID">
          <el-input
            v-model="queryParams.userId"
            placeholder="请输入用户ID"
            clearable
            style="width: 150px"
          />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="queryParams.status" placeholder="请选择" clearable>
            <el-option label="待结算" :value="0" />
            <el-option label="已结算" :value="1" />
            <el-option label="已失效" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">查询</el-button>
          <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 数据表格 -->
      <el-table v-loading="loading" :data="list" border style="width: 100%">
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="userId" label="会员ID" width="100" />
        <el-table-column prop="orderNo" label="订单号" width="200" />
        <el-table-column prop="subordinateId" label="下级ID" width="100" />
        <el-table-column label="佣金金额" width="120">
          <template slot-scope="scope">
            ¥{{ scope.row.commissionAmount }}
          </template>
        </el-table-column>
        <el-table-column label="订单金额" width="120">
          <template slot-scope="scope">
            ¥{{ scope.row.orderAmount }}
          </template>
        </el-table-column>
        <el-table-column label="状态" width="100">
          <template slot-scope="scope">
            <el-tag :type="getStatusType(scope.row.status)">
              {{ getStatusText(scope.row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="settleTime" label="结算时间" width="180" />
      </el-table>

      <!-- 分页 -->
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.page"
        :limit.sync="queryParams.limit"
        @pagination="getList"
      />
    </el-card>
  </div>
</template>

<script>
import { getCommissionRecordList, settleCommission } from '@/api/shop/shopCommissionRecord';

export default {
  name: 'ShopCommissionRecord',
  data() {
    return {
      loading: false,
      list: [],
      total: 0,
      queryParams: {
        page: 1,
        limit: 20,
        userId: '',
        status: ''
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    async getList() {
      this.loading = true;
      try {
        const res = await getCommissionRecordList(this.queryParams);
        this.list = res.data.list || [];
        this.total = res.data.total || 0;
      } catch (error) {
        console.error('查询失败', error);
      } finally {
        this.loading = false;
      }
    },
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    resetQuery() {
      this.queryParams = {
        page: 1,
        limit: 20,
        userId: '',
        status: ''
      };
      this.getList();
    },
    async handleSettle() {
      this.$confirm('确认要手动结算佣金吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        try {
          await settleCommission();
          this.$message.success('结算完成');
          this.getList();
        } catch (error) {
          console.error('结算失败', error);
        }
      }).catch(() => {});
    },
    getStatusType(status) {
      const map = { 0: 'warning', 1: 'success', 2: 'info' };
      return map[status] || 'info';
    },
    getStatusText(status) {
      const map = { 0: '待结算', 1: '已结算', 2: '已失效' };
      return map[status] || '未知';
    }
  }
};
</script>

<style scoped>
.search-form {
  margin-bottom: 20px;
}
</style>
