<template>
  <div class="table_page">
    <div style="margin-bottom: 20px">
      <el-input style="width: 240px; margin-right: 10px;" suffix-icon="Search" placeholder="资讯名称"  size="default"
                v-model="messageName"></el-input>
      <el-button type="primary" @click="onSearch(messageName)" style="margin-right: 10px;" size="default">
        搜索
      </el-button>
      <el-button type="warning" @click="reset" size="default">重置</el-button>
    </div>
    <!-- 数据展示 -->
    <el-table :data="tableData" border stripe header-cell-class-name="headerBg"
              :cell-style="{ 'text-align': 'center', 'font-size': '16px' }" @selection-change="handleSelectionChange"
              @select="selectChange"
              :header-cell-style="{ 'text-align': 'center' }">
      <el-table-column prop="id" label="编号" width="80">
        <template #default="{ $index }">
          {{ $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="title" label="标题"></el-table-column>
      <el-table-column prop="image" label="预览图" width="200">
        <template #default="scope">
          <div>
            <img :src="downloadUrl + scope.row.image" alt="" style="height: 50px;">
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="publishTime" label="发布时间" width="150"></el-table-column>
      <el-table-column label="操作" width="230px">
        <template #default="scope">
          <el-button size="small" @click="ReviseOrView(scope.row , 0)">
            详情
          </el-button>
          <el-button size="small" @click="ReviseOrView(scope.row, 1)">
            编辑
          </el-button>
          <el-popconfirm class="ml-5" confirm-button-text='确定' cancel-button-text='取消' icon="InfoFilled"
                         icon-color="red"
                         title="是否确认删除" @confirm="deleteMessage(scope.row.id)" width=180>
            <template #reference>
              <el-button class="ml-5" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <div style="padding: 10px 0">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                     :current-page="searchParams.current" :page-size="searchParams.pageSize" :page-sizes="[5, 10, 15, 20]"
                     :small="null" :disabled="null" :background="null" layout="total, sizes, prev, pager, next, jumper"
                     :total="total"/>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ElMessage } from 'element-plus';
import {ref, onMounted, inject} from 'vue';
import myAxios from "@/api/myAxios";
import { useRouter } from "vue-router";
import {downloadUrl} from '@/utils/formatImgUpload'

const router = useRouter();  //路由
const total = ref(0); //总页数
const idList = ref<Number[]>([]);
const searchParams: any = ref({ //封装分页
  current: 1, //当前页码
  pageSize: 5, //每页显示条数
  sortField: "id",  //根据ID分类
  sortOrder: "descend" //降序
})
const messageName = ref('');
const tableData: any = ref([]);  //实体类商品表格
const reload: any = inject("reload")  //页面重新刷新

onMounted(() => { //页面加载时获取商品列表和分类页表
  getMessageList()
})
const getMessageList = async () => {
  try {
    const res = await myAxios.post('/article/query/web', {...searchParams.value});
    console.log('资讯表格--->', res.data)
    if (res.data.code === 1) {
      tableData.value = res.data.data.records;
      total.value = parseInt(res.data.data.total)  //总数据量，用于分页
    } else {
      ElMessage({
        message: '获取数据失败',
      });
      return;
    }
  } catch (error) {
    ElMessage({
      message: '发生错误',
    });
  }
}

//删除活动信息
const deleteMessage = async (index: number) => {
  console.log(typeof index)
  const res = await myAxios.post('/article/delete', {id: index})
  console.log(res)
  if (res.data.code === 1) {
    ElMessage({
      type: 'success',
      message: '删除成功',
    })
    await getMessageList()
  }
}

//详情或编辑
const ReviseOrView = ( row: any, flag: number) => {
  router.push({
    name: '资讯详情',
    params: {
      id: row.id,
      flag: flag
    }
  })
}

//处理行数大小变化
const handleSizeChange = (newSize: any) => {
  searchParams.value.pageSize = newSize //新的页面数
  getMessageList()
}
//处理当前表格变化
const handleCurrentChange = (Current: any) => {
  searchParams.value.current = Current
  getMessageList()
}
//重置按钮
const reset = () => {
  reload()
};



const handleSelectionChange = () => {
}
const selectChange = (selection: any, row: any) => {   //selction是对象数组，row是当前行对象
  idList.value.splice(0, idList.value.length)   //删掉
  selection.forEach((item: any) => {
    idList.value.push(item.id)
  })
  // console.log('idList--->',idList.value);
}

//查询
const onSearch = (value: string) => {
  searchParams.value.title = value
  searchParams.value.current = 1
  getMessageList()
}
</script>

<style scoped>
.table_page {
  min-width: 800px;
}

</style>