<template>
  <div>
    <div style="padding-left: 20px">
      <el-icon @click="handleRefresh" style="cursor: pointer">
        <Refresh />
      </el-icon>
    </div>
    <byTable
      :source="tableData.data"
      :pagination="tableData.pagination"
      :config="config"
      :loading="loading"
      :hideSearch="true"
      highlight-current-row
      :action-list="[]"
      :table-events="{
        //element talbe事件都能传
        'row-click': handleRowClick,
      }"
      @get-list="getList"
    >
      <template #icon="{ item }">
        <div>
          <span v-if="item.readStatus == 0">
            <img
              src="@/assets/images/mail/message.png"
              alt=""
              title="未读"
              class="messageImg"
            />
          </span>
          <span v-if="item.readStatus == 1">
            <img
              src="@/assets/images/mail/message-open.png"
              alt=""
              title="已读"
              class="messageImg"
            />
          </span>
        </div>
      </template>
    </byTable>
  </div>
</template>

<script setup>
import byTable from "@/components/byTable/index";
import useMailStore from "@/store/modules/mail";
const { proxy } = getCurrentInstance();
const mailStore = useMailStore();
let loading = ref(false);
const tableData = reactive({
  data: [],
  pagination: {
    pageNum: 1,
    pageSize: 10,
    total: 0,
  },
});
const config = computed(() => {
  return [
    {
      attrs: {
        label: "状态",
        slot: "icon",
        width: 70,
      },
    },
    {
      attrs: {
        label: "发件人",
        prop: "fromPersonalName",
      },
    },
    {
      attrs: {
        label: "发件人地址",
        prop: "fromEmail",
      },
    },
    {
      attrs: {
        label: "主题",
        prop: "subject",
      },
    },
    {
      attrs: {
        label: "时间",
        prop: "sendDate",
      },
    },
  ];
});

const getList = (req = {}) => {
  tableData.pagination = { ...tableData.pagination, ...req };
  loading.value = true;
  proxy
    .post("/mailService/getMessagePage", {
      ...mailStore.currentMenu,
      ...tableData.pagination,
      ...req,
    })
    .then((res) => {
      tableData.data = res.rows;
      tableData.pagination.total = res.total;
      loading.value = false;
    });
};

const handleRefresh = () => {
  tableData.pagination = {
    pageNum: 1,
    pageSize: 10,
  };
  getList();
};

const handleRowClick = (row) => {
  const menu = {
    title: row.subject.slice(0, 4) + "...",
    type: mailStore.selectMail.type,
    messageId: row.id,
    id: "detail" + "," + row.id,
  };
  const menuItem = mailStore.mailMenuList.find((x) => x.id === menu.id);
  if (menuItem === undefined) {
    mailStore.mailMenuList.push(menu);
  }
  mailStore.currentMenu = menu;
  mailStore.currentId = menu.id;
};
onMounted(() => {
  // getList();
});

const init = () => {
  getList();
};

defineExpose({
  initFn: init,
});
</script>

<style lang="scss" scoped>
.messageImg {
  width: 18px;
  height: 18px;
  transform: translateY(5px);
}
</style>