<script setup>
import { ref, reactive, watch, onMounted } from "vue";
import { Plus } from "@element-plus/icons-vue";
import AddForm from "./components/AddForm.vue";
import EditForm from "./components/EditForm.vue";

import SendBonus from "./components/SendBonus.vue";

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

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

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

const initData = async (page, pageSize) => {
  loading.value = true
  const res = await getBonusList({
    page: page,
    pageSize: pageSize,
  });
  
  if(res.code === 200){
    state.tableData = res.data.data;
    state.total = res.data.total;
  } 
  //顯示錯誤訊息
  else {
    ElMessage.error(res.message)
  }

  loading.value = false
};

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

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

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

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

<template>
  <div class="main">
    <!-- Breadcrumb: Start -->
    <div class="breadcrumb-section">
      <div>
        <el-breadcrumb separator="/">
          <el-breadcrumb-item> 電商管理中心 </el-breadcrumb-item>
          <el-breadcrumb-item><a href="/">優惠劵</a></el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <div>
        <el-button
          type="primary"
          :icon="Plus"
          size="small"
          @click="showAddDrawer = true"
          >新增優惠劵</el-button
        >
      </div>
    </div>
    <!-- Breadcrumb: End -->
    <hr />
    <el-card>
      <div  v-loading="loading">
        <el-table :data="state.tableData" style="width: 100%">
          <el-table-column prop="type_name" label="優惠劵名稱" />
          <el-table-column prop="send_type" label="類型" width="150" />
          <el-table-column
            prop="type_money"
            label="金額"
            align="right"
            width="100"
          />
          <el-table-column
            prop="min_amount"
            label="訂單下限"
            align="right"
            width="100"
          />
          <el-table-column prop="send_count" label="發放數量" width="80" />
          <el-table-column prop="use_count" label="使用數量" width="80" />
          <el-table-column label="操作" align="center" width="280">
            <template #default="scope">
              <el-button
                type="info"
                size="small"
                plain
                @click="showSendBonus = true"
                >發放</el-button
              >
              <el-button
                type="info"
                size="small"
                plain
                @click="$router.push('/useList')"
                >查看</el-button
              >
              <el-button
                type="primary"
                size="small"
                plain
                @click="showAddDrawer = true"
                >編輯</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>
  <AddForm v-model:show="showAddDrawer" @reload="reloadList" />
  <EditForm v-model:show="showEditDrawer" />
  <SendBonus v-model:show="showSendBonus" />
</template>

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