<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 { getActivityList, deleteActivity } from "@/api/activity";

const showAddNewsDrawer = ref(false);

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 getActivityList(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;
};

const handleEdit = (id) => {
  editid.value = id;
  showFormDraw.value = true;
};

const handleDelete = (id) => {
  ElMessageBox.confirm('確認刪除此紀錄?')
    .then(async () => {
      let res = await deleteActivity(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><a href="/">公司活動</a></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' }">
      <el-table :data="tableData" style="width: 100%">  
        <el-table-column type="expand" label="相簿" width="80">
          <template #default="props">
            <div class="imgGroup">
              <template v-for="(item,index) in props.row.imageList" :key="index">
                <el-image
                style="width: 100px; height: 60px"
                :src="item.url"
                :fit="cover"
              />
              </template>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="id" label="#" width="50" />
        <el-table-column prop="title1" label="標題(繁)" />
        <el-table-column prop="title2" label="標題(簡)" />
        <el-table-column prop="title3" 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="Edit" @click="handleEdit(scope.row.id)">修改</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-drawer
      ref="drawerRef"
      v-model="showAddNewsDrawer"
      title="新增動態"
      direction="rtl"
      size="550"
    >
      <el-card>
        <el-form :model="initform" label-width="120px">
          <el-form-item label="標題">
            <el-input v-model="initform.title" />
          </el-form-item>
          <el-form-item label="相簿上傳">
            <el-upload
              class="upload-demo"
              action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
              :on-preview="handlePreview"
              :on-remove="handleRemove"
              list-type="picture"
            >
              <el-button type="primary">上傳圖片</el-button>
              <template #tip>
                <small class="el-upload__tip"> ※ 請上傳500kb以內jpg檔案。</small>
              </template>
            </el-upload>
          </el-form-item>
        </el-form>
        
      </el-card>
      <div class="demo-drawer__footer" style="text-align: right">
        <el-button @click="showAddNewsDrawer = false">取消</el-button>
        <el-button type="primary" :loading="loading" @click="onClick">{{
          loading ? "Submitting ..." : "確認"
        }}</el-button>
      </div>
    </el-drawer>
  </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 lang="less" scoped>
  .imgGroup{
    margin: 10px;
    .el-image{
      margin: 4px;
    }
  }

</style>
