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

import { getGoodsList } from "@/api/goods";

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

const showGoodsInfo = 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 getGoodsList({
    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);
};

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

</script>

<template>
  <div class="main">
    <!-- Breadcrumb: Start -->
    <el-page-header :icon="null" title=" " @back="onBack">
      <template #breadcrumb>
        <el-breadcrumb separator="/">
          <el-breadcrumb-item> CRM系統 </el-breadcrumb-item>
          <el-breadcrumb-item>產品列表</el-breadcrumb-item>
        </el-breadcrumb>
      </template>
      <template #content>
        <span class="text-large font-600 mr-3"> 產品列表 </span>
      </template>
      <template #extra>
        <div class="flex items-center">
          <el-button type="primary" class="ml-2" size="small" :icon="Plus" @click="showGoodsInfo = true"
            >新增產品</el-button
          >
        </div>
      </template>
    </el-page-header>
    <!-- Breadcrumb: End -->
    <hr />
    <el-card>
      <div class="search-section">
        <el-row>
          <el-col :xs="12" :sm="12" :md="3" :lg="3" :xl="3">
            <div>
              <el-input/>
            </div>
          </el-col>
          <el-col :xs="12" :sm="12" :md="3" :lg="3" :xl="3">
            <el-button type="primary">搜尋</el-button>
          </el-col>
        </el-row>
      </div>

      <div v-loading="loading">
        <el-table :data="state.tableData" style="width: 100%">
          <el-table-column type="selection" width="55" />
          <el-table-column prop="goods_id" label="編號" sortable width="80" />
          <el-table-column prop="goods_name" label="產品名稱" />
          <el-table-column prop="goods_sn" label="貨號" width="100" />
          <el-table-column
            prop="shop_price"
            label="價格"
            align="right"
            width="100"
          />
          <el-table-column label="上架" width="80">
            <template #default="scope">
              <el-switch
                v-model="scope.row.is_on_sale"
                :active-value="1"
                :inactive-value="0"
              />
            </template>
          </el-table-column>
          <el-table-column prop="sort_order" label="排序" width="80" />
          <el-table-column
            prop="goods_number"
            label="庫存"
            align="right"
            width="80"
          />
          <el-table-column label="操作" align="center" width="280">
            <template #default="scope">
              <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="showGoodsInfo" @reload="reloadList" />
</template>

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