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

import { getContactList, deleteContact } from '@/api/contact';

import Content from './components/content.vue';

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

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

const dialogTableVisible = ref(false);

const tableData = ref([]);

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

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

const handleDelete = (id) => {
  ElMessageBox.confirm('確認刪除此紀錄?')
    .then(async () => {
      let res = await deleteContact(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();
};

const formData = ref({});

const handleShowContent = (row) => {
  formData.value = row;
  dialogTableVisible.value = true;
};
</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>
      </div>
    </div>
    
    <el-card :body-style="{ padding: '0px' }">
      <el-table :data="tableData">
        <el-table-column fixed prop="id" label="#" width="50" />
        <el-table-column prop="company" label="公司名稱" width="180" />
        <el-table-column prop="name" label="姓名" width="180" />
        <el-table-column prop="email" label="E-mail" />
        <el-table-column prop="tel" label="聯絡電話" />
        <el-table-column prop="create_time" label="留言時間" />
        <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="Document" @click="handleShowContent(scope.row)">內容</el-dropdown-item>
                  <el-dropdown-item :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>
    </el-card>
  </div>
  <div>
    <el-dialog 
    v-model="dialogTableVisible" 
    title="留言內容"
    :destroy-on-close="true"
    >
      <content :data="formData"/>
    </el-dialog>
  </div>
</template>
