<template>
  <div class="app-container">
    <el-card>
      <div slot="header" class="clearfix">
        <span>会员注册记录管理</span>
      </div>

      <!-- 搜索表单 -->
      <el-form :inline="true" :model="queryParams" class="search-form">
        <el-form-item label="手机号">
          <el-input
            v-model="queryParams.phone"
            placeholder="请输入手机号"
            clearable
            style="width: 200px"
          />
        </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="dealerId" label="推荐人ID" width="100" />
        <el-table-column prop="phone" label="下级手机号" width="150" />
        <el-table-column prop="realName" label="下级姓名" width="120" />
        <el-table-column label="会员费" width="100">
          <template slot-scope="scope">
            ¥{{ scope.row.memberFee }}
          </template>
        </el-table-column>
        <el-table-column label="注册费" width="100">
          <template slot-scope="scope">
            ¥{{ scope.row.registerFee }}
          </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 label="操作" width="200">
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.memberFeeStatus === 0"
              type="text"
              @click="handleConfirmMemberFee(scope.row)"
            >
              确认收款
            </el-button>
            <el-button
              v-if="scope.row.registerFeeStatus === 0"
              type="text"
              @click="handleConfirmRegisterFee(scope.row)"
            >
              确认支付
            </el-button>
          </template>
        </el-table-column>
      </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 { getMemberRegisterList, confirmMemberFee, confirmRegisterFee } from '@/api/shop/shopMemberRegister';

export default {
  name: 'ShopMemberRegister',
  data() {
    return {
      loading: false,
      list: [],
      total: 0,
      queryParams: {
        page: 1,
        limit: 20,
        phone: '',
        status: ''
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    async getList() {
      this.loading = true;
      try {
        const res = await getMemberRegisterList(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,
        phone: '',
        status: ''
      };
      this.getList();
    },
    async handleConfirmMemberFee(row) {
      this.$confirm('确认已收到会员费?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        try {
          await confirmMemberFee(row.id);
          this.$message.success('确认成功');
          this.getList();
        } catch (error) {
          console.error('确认失败', error);
        }
      }).catch(() => {});
    },
    async handleConfirmRegisterFee(row) {
      try {
        await confirmRegisterFee(row.id);
        this.$message.success('确认成功');
        this.getList();
      } catch (error) {
        console.error('确认失败', error);
      }
    },
    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>
