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

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

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

const router = useRouter();
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 edit_id = ref(0)
const handleShowEdit = (type_id) => {
  showAddDrawer.value = true;
  edit_id.value = type_id
};

const handleUseList = (type_id) => {
  router.push({
    path: "/useList",
    query: {
      type_id: type_id,
    },
  });
};

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

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

const send_bonus_id = ref(null);

const handleSandBonus = (type_id) => {
  showSendBonus.value = true;
  send_bonus_id.value = type_id;
};
</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="bonus_number" label="發放數量" width="80" />
          <el-table-column prop="remain_number" label="剩餘數量" width="80" />
          <el-table-column label="操作" align="center" width="280">
            <template #default="scope">
              <el-button
                type="info"
                size="small"
                plain
                @click="handleSandBonus(scope.row.type_id)"
                >建立</el-button
              >
              <el-button
                type="info"
                size="small"
                plain
                @click="handleUseList(scope.row.type_id)"
                >查看</el-button
              >
              <el-button
                type="primary"
                size="small"
                plain
                @click="handleShowEdit(scope.row.type_id)"
                >編輯</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>
  <BonusForm v-model:show="showAddDrawer" v-model:edit_id="edit_id" @reload="reloadList" />
  <SendBonus v-model:show="showSendBonus" :bonus_id="send_bonus_id"/>
</template>

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