<script setup>
import { ref, reactive, watch, onMounted } from "vue";
import { useRoute, useRouter } from "vue-router";

import { getUseBonusList, getUseList,deleteBonus } from "@/api/bonus";

const router = useRouter();
const route  = useRoute();

const showAddDrawer = ref(false);
const showEditDrawer = ref(false);

const type_id = route.query.type_id;

const state = reactive({
  page: 1,
  pageSize: 10,
  total: 0,
  tableData: [],
});

const initData = async (page, pageSize) => {
  const res = await getUseBonusList({
    id: type_id,
    page: page,
    pageSize: pageSize,
  });

  state.tableData = res.data.data;
  state.total = res.data.total;
};

onMounted(() => {
  initData(state.page, state.pageSize);
});

watch(
  () => [state.page, state.pageSize],
  ([newPage, newPageSize]) => {
    initData(newPage, newPageSize);
  }
);

const onBack = () => {
  router.back();
};

const handleDelete = async (bonus) => {
  let res = await deleteBonus(bonus.bonus_id);
  if (res.code === 200) {
    initData(state.page, state.pageSize);
  }
};

const dialogTableVisible = ref(false)

const useData = ref([])

const handleShowUsed =async (row)=> {
  dialogTableVisible.value = true
  const res = await getUseList({
    bonus_id: row.bonus_id
  });
  useData.value = res.data
}


const reloadList = () => {
  initData(state.page, state.pageSize);
};
</script>

<template>
  <div class="main">
    <!-- Breadcrumb: Start -->
    <!-- <div class="breadcrumb-section"> -->
    <el-page-header @back="onBack">
      <template #breadcrumb>
        <el-breadcrumb separator="/">
          <el-breadcrumb-item> 電商管理中心 </el-breadcrumb-item>
          <el-breadcrumb-item>優惠劵</el-breadcrumb-item>
          <el-breadcrumb-item>優惠劵使用列表</el-breadcrumb-item>
        </el-breadcrumb>
      </template>
      <template #content>
        <span class="text-large font-600 mr-3"> 優惠劵使用列表 </span>
      </template>
    </el-page-header>
    <!-- </div> -->
    <!-- Breadcrumb: End -->
    <hr />
    <el-card>
      <div>
        <el-table :data="state.tableData" style="width: 100%">
          <el-table-column prop="bonus_id" label="編號" />
          <el-table-column prop="bonus_sn" label="優惠劵序號" />
          <el-table-column prop="bonus_number" label="優惠劵數量" align="right"/>
          <el-table-column prop="remain_number" label="剩餘數量" align="right"/>
          <!-- <el-table-column prop="send_type" label="類型" width="150" /> -->
          <el-table-column label="操作" align="center" width="180">
            <template #default="scope">
              <el-button
                type="danger"
                size="small"
                plain
                @click="handleShowUsed(scope.row)"
                v-if="scope.row.remain_number < scope.row.bonus_number"
                >查看</el-button
              >
              <el-button
                type="danger"
                size="small"
                plain
                @click="handleDelete(scope.row)"
                >刪除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination-block">
          <el-pagination
            v-model:current-page="state.page"
            v-model:page-size="state.pageSize"
            :page-sizes="[10, 20, 50, 100]"
            small="small"
            layout="->,total, sizes, prev, pager, next, jumper"
            :total="state.total"
          />
        </div>
      </div>
    </el-card>
  </div>

  <el-dialog v-model="dialogTableVisible" title="使用紀錄">
    <el-table :data="useData">
      <el-table-column prop="sso_user_id" label="使用會員" width="150" />
      <el-table-column prop="order_sn" label="訂單編號" width="200" />
      <el-table-column prop="used_time" label="使用時間" />
    </el-table>
  </el-dialog>
</template>

<style lang="less" scoped></style>
