<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.student__name" placeholder="学员姓名" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-input v-model="listQuery.coupon__title" placeholder="优惠券名称" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-select v-model="listQuery.status" placeholder="状态" clearable class="filter-item" style="width: 130px">
        <el-option v-for="item in statusOptions" :key="item.key" :label="item.label" :value="item.key" />
      </el-select>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
    </div>

    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%;">
      <el-table-column label="ID" align="center" width="80">
        <template slot-scope="{row}"><span>{{ row.id }}</span></template>
      </el-table-column>
      <el-table-column label="学员姓名" align="center">
        <template slot-scope="{row}"><span>{{ row.student_name }}</span></template>
      </el-table-column>
      <el-table-column label="电话" align="center">
        <template slot-scope="{row}"><span>{{ row.student_phone }}</span></template>
      </el-table-column>
      <el-table-column label="优惠券名称" align="center">
        <template slot-scope="{row}"><span>{{ row.coupon_title }}</span></template>
      </el-table-column>
      <el-table-column label="面额" align="center">
        <template slot-scope="{row}"><span>{{ row.coupon_detail.amount }} {{ row.coupon_detail.unit }}</span></template>
      </el-table-column>
      <el-table-column label="分配时间" align="center">
        <template slot-scope="{row}"><span>{{ row.assigned_at }}</span></template>
      </el-table-column>
      <el-table-column label="使用时间" align="center">
        <template slot-scope="{row}"><span>{{ row.used_at || '-' }}</span></template>
      </el-table-column>
      <el-table-column label="状态" align="center">
        <template slot-scope="{row}">
          <el-tag :type="statusTypeMap[row.status]">{{ row.status_display }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="180" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <template v-if="row.status === 'assigned'">
            <el-button type="primary" size="mini" @click="handleStatusChange(row, 'used')">
              核销
            </el-button>
            <el-dropdown trigger="click" style="margin-left: 10px;">
              <el-button size="mini">
                更多<i class="el-icon-arrow-down el-icon--right" />
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click.native="handleStatusChange(row, 'suspended')">暂停</el-dropdown-item>
                <el-dropdown-item @click.native="handleStatusChange(row, 'revoked')">回收</el-dropdown-item>
                <el-dropdown-item divided icon="el-icon-delete" style="color: #f56c6c;" @click.native="handleDelete(row)">删除</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>

          <template v-else-if="row.status === 'suspended'">
            <el-button type="success" size="mini" @click="handleStatusChange(row, 'assigned')">
              恢复
            </el-button>
            <el-dropdown trigger="click" style="margin-left: 10px;">
              <el-button size="mini">
                更多<i class="el-icon-arrow-down el-icon--right" />
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click.native="handleStatusChange(row, 'revoked')">回收</el-dropdown-item>
                <el-dropdown-item divided icon="el-icon-delete" style="color: #f56c6c;" @click.native="handleDelete(row)">删除</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>

          <template v-else>
            <el-button type="danger" size="mini" icon="el-icon-delete" @click="handleDelete(row)">
              删除
            </el-button>
          </template>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
  </div>
</template>

<script>
import { getStudentCoupons, updateStudentCoupon, deleteStudentCoupon } from '@/api/crm'
import Pagination from '@/components/Pagination'

export default {
  name: 'IssuedCoupons',
  components: { Pagination },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        student__name: undefined,
        coupon__title: undefined,
        status: undefined
      },
      statusOptions: [
        { key: 'assigned', label: '已分配' },
        { key: 'used', label: '已使用' },
        { key: 'expired', label: '已过期' },
        { key: 'revoked', label: '已回收' },
        { key: 'suspended', label: '暂停使用' }
      ],
      statusTypeMap: {
        assigned: 'success',
        used: 'info',
        expired: 'danger',
        revoked: 'info',
        suspended: 'warning'
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      getStudentCoupons(this.listQuery).then(response => {
        this.list = response.data.results
        this.total = response.data.count
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleStatusChange(row, status) {
        const actionText = {
            suspended: '暂停',
            assigned: '恢复',
            revoked: '回收',
            used: '核销'
        }[status]
        
        this.$confirm(`确认${actionText}该优惠券?`, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(() => {
            updateStudentCoupon(row.id, { status: status }).then(() => {
                this.$message({
                    message: '操作成功',
                    type: 'success'
                })
                this.getList()
            })
        })
    },
    handleDelete(row) {
        this.$confirm('确认删除该条记录?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(() => {
            deleteStudentCoupon(row.id).then(() => {
                this.$message({
                    message: '删除成功',
                    type: 'success'
                })
                this.getList()
            })
        })
    }
  }
}
</script>
