<template>
  <div class="percentage-content">
    <div class="content">
      <div class="h-layout space-between h-center">
        <div>
          <div class="h-layout">
            <div class="tool-item" @click="getList">
              <svg-icon name="refurbish" class="icon" />
              <span>刷新</span>
            </div>
            <div
              class="tool-item"
              @click="(dialogVisible = true), (form = {}), (readonly = false)"
            >
              <svg-icon name="add" class="icon" />
              <span>新增</span>
            </div>
          </div>
        </div>
        <div style="width: 30%;">
          <div>
            <el-input
              v-model="search"
              @keydown.enter.native="getList"
              placeholder="请输入关键字"
              class="search-input"
            >
              <template #append>
                <span @click="getList" class="search-bottom">
                  <svg-icon name="search" class="icon_class" />
                  <span>搜索</span>
                </span>
              </template>
            </el-input>
          </div>
        </div>
      </div>
      <el-table
        ref="loading"
        class="table"
        v-loading="tableDataloading"
        :data="tableData"
        header-row-class-name="el-one-header"
        border
      >
        <el-table-column
          align="center"
          :resizable="true"
          width="100"
          label="序列"
          type="index"
        >
          <template #default="scope">
            {{ scope.$index + 1 + (currentPage - 1) * pageSize }}
          </template>
        </el-table-column>
        <el-table-column
          prop="coverImg"
          :resizable="true"
          label="封面"
          width="100"
          align="center"
        >
        <template #default="scope">
          <img class="coverImg" :src="baseUrl+scope.row.coverPath" alt="">
          </template>
        </el-table-column>
        <el-table-column
          prop="title"
          :resizable="true"
          label="标题"
          align="center"
        >
        </el-table-column>
        <el-table-column
          :resizable="true"
          align="center"
          width="300"
          label="操作"
        >
          <template #default="scope">
            <span
              class="operate"
              @click="
                (dialogVisible = true), (form = scope.row), (readonly = false)
              "
            >
              <svg-icon name="edit" class="icon"></svg-icon>
              <span class="edit">编辑</span>
            </span>
            <span
              class="operate"
              @click="toDetail(scope.row.publicContentId)"
            >
              <svg-icon name="detail" class="icon"></svg-icon>
              <span class="detail">详情</span>
            </span>
            <span class="operate" @click="del(scope.row.publicContentId)">
              <svg-icon name="delete" class="icon"></svg-icon>
              <span class="delete">删除</span>
            </span>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        class="pagination"
        @size-change="getList"
        @current-change="getList"
        v-model:current-page.sync="currentPage"
        :page-sizes="[10, 15, 20, 30]"
        v-model:page-size.sync="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      >
      </el-pagination>
    </div>
    <!-- 新增/修改 -->
    <Add
      :visible="dialogVisible"
      :form="form"
      :readonly="readonly"
      @close="onclone"
    />
  </div>
</template>

<script lang='ts' setup>
import { onMounted, reactive, ref } from "vue";
import { publicContentPage, publicContentDelete } from "@/api/Sys";
import { ElMessage, ElMessageBox } from "element-plus";
import Add from "./add.vue";
import { useRouter } from "vue-router";
import useUserStore from "@/store/modules/user";
const router = useRouter();
const userStore = useUserStore();
const user = ref(JSON.parse(userStore.userInfo));
const userId = ref(user.value.userId);

const baseUrl = ref(import.meta.env.VITE_UPLOAD_URL);
const search = ref('');
const tableData = ref([]);
const currentPage = ref(1);
const pageSize = ref(10);
const total = ref(0);

const dialogVisible = ref(false);
const form = ref({});
const readonly = ref(false);

//获取列表
const tableDataloading = ref(false);
const getList = () => {
  tableDataloading.value = true;
  let params = {
    condition: search.value,
    limit: pageSize.value,
    page: currentPage.value,
  };
  publicContentPage(params).then((res: any) => {
    console.log(res,'res==>');
    tableData.value = res.data.list;
    total.value = res.data.total;
    tableDataloading.value = false;
  });
};
//删除列表
const del = (id) => {
  ElMessageBox.confirm("是否确认删除?", "提示", {
    confirmButtonText: "是",
    cancelButtonText: "否",
    type: "warning",
  }).then(() => {
    let params = {
      publicContentId:id
    }
    publicContentDelete(params).then((res: any) => {
      if (res.code == 1) {
        ElMessage.success({
          message: "删除成功",
          type: "success",
        });
      } else {
        ElMessage.error({
          message: res.message,
          type: "error",
        });
      }
      getList();
    });
  });
};
//公开内容详情
const toDetail = (id) => {
  router.push({
    name: "public-detail",
    query: { id: id },
  });
};
//关闭新增弹窗
const onclone = () => {
  dialogVisible.value = false;
  form.value = {};
  getList();
};

onMounted(() => {
  getList();
});
</script>

<style lang="scss" scoped>
.content {
    width: 100%;
    background: #fff;
    border-radius: 0.26042vw;
    box-shadow: 0 0 0.41667vw 0 rgb(8 33 85 / 10%);
    height: 100%;
    padding: 0.83333vw;
    overflow-y: auto;
    overflow-x: hidden !important;
    box-sizing: border-box;
    .table {
        margin-top: 16px;
    }
}
.coverImg {
    width: 50px;
    height: 50px;
}
</style>