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

import AscForm from "./components/Form.vue";

import { getNewsList, deleteNews } from "@/api/news";
import router from "../../../router";

const state = reactive({
  page: 1,
  pageSize: 10,
});

const { page, pageSize } = toRefs(state);
const total = ref(0);
const loading = ref(false);

const showFormDraw = ref(false);

const tableData = ref([]);

onMounted(() => {
  getTableData();
});

const getTableData = async () => {
  loading.value = true;
  let res = await getNewsList(state);
  if (res.code === 200) {
    tableData.value = res.data.list;
    total.value = res.data.total;
  }
  loading.value = false;
};

const handleAdd = () => {
  editid.value = 0;
  // showFormDraw.value = true;
  router.push({
    path: '/news/list/form',
  });
};

const handleEdit = (id) => {
  editid.value = id;
  // showFormDraw.value = true;
  router.push({
    path: '/news/list/form',
    query: {
      id: id
    }  
  });
};

const handleDelete = (id) => {
  ElMessageBox.confirm('確認刪除此紀錄?')
    .then(async () => {
      let res = await deleteNews(id);
      if (res.code === 200) {
        ElMessage({
          message: '刪除成功.',
          type: 'success',
        });
        getTableData();

      }else{
        ElMessage({
          message: '刪除失敗.',
          type: 'error',
        });
      }
    })
    .catch(() => {
    })
};

// Pagination
const handleSizeChange = (val) => {
  state.pageSize = val;
  getTableData();
};

const handleCurrentChange = (val) => {
  state.page = val;
  getTableData();
};

const handlePrevClick = (val) => {
  state.page = val;
  getTableData();
};

const handleNextClick = (val) => {
  state.page = val;
  getTableData();
};

// Drawer

const editid = ref(0);

const handleCloseFormDraw = (val) => {

  showFormDraw.value = false;
  if(val){
    getTableData();
  }
};
</script>

<template>
  <div class="main">
    <!-- Breadcrumb: Start -->
    <div class="breadcrumb-section">
      <div>
        <el-breadcrumb separator="/">
          <el-breadcrumb-item> 最新消息 </el-breadcrumb-item>
          <el-breadcrumb-item> 產業動態 </el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <div>
        <el-button type="primary" :icon="Plus" @click="handleAdd">新增產業動態</el-button>
      </div>
    </div>
    <!-- Breadcrumb: End -->

    <el-card :body-style="{ padding: '0px' }">
      <div v-loading="loading">
        <el-table :data="tableData" style="width: 100%">
          <el-table-column label="#" prop="id" width="50" />
          <el-table-column label="標題" prop="title" />
          <el-table-column label="建立時間" prop="create_time" />
          <el-table-column label="操作" width="105" fixed="right">
            <template #default="scope">
              <el-dropdown>
                <el-button type="danger">
                  操作<el-icon class="el-icon--right"><arrow-down /></el-icon>
                </el-button>
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item :icon="Edit" @click="handleEdit(scope.row.id)">編輯</el-dropdown-item>
                    <el-dropdown-item v-if="scope.row.is_main !== 1" :icon="Delete" @click="handleDelete(scope.row.id)">刪除</el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination-block">
          <el-pagination 
          v-model:current-page="page" 
          v-model:page-size="pageSize" 
          :page-sizes="[10, 20, 50, 100]"
          layout="->,total, sizes, prev, pager, next, jumper" 
          :total="total" 
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          @prev-click="handlePrevClick"
          @next-click="handleNextClick"
          />
        </div>
      </div>
    </el-card>
  </div>
  <div>
    <el-drawer ref="drawerRef" v-model="showFormDraw" :destroy-on-close="true" title="新增管理員" direction="rtl" size="70%">
      <asc-form :editid="editid" @close="handleCloseFormDraw"/>
    </el-drawer>
  </div>
</template>

<style scoped>
</style>