<template>
  <div>
    <div style="padding-left: 20px">
      <el-icon @click="handleRefresh" style="cursor: pointer">
        <Refresh />
      </el-icon>
    </div>
    <div style="padding: 10px 15px; width: 100%">
      <el-table
        :data="tableData.data"
        style="width: 100%"
        :height="tableHeight"
        v-loading="loading"
      >
        <el-table-column label="状态" width="70">
          <template #default="{ row, $index }">
            <div style="cursor: pointer" @click="handleRowClick(row, $index)">
              <span v-if="row.flags && !row.flags.includes('6')">
                <img
                  src="@/assets/images/mail/message.png"
                  alt=""
                  title="未读"
                  class="messageImg"
                />
              </span>
              <span
                v-if="
                  row.flags !== undefined &&
                  row.flags !== null &&
                  row.flags === ''
                "
              >
                <img
                  src="@/assets/images/mail/message.png"
                  alt=""
                  title="未读"
                  class="messageImg"
                />
              </span>
              <span
                v-if="
                  row.flags &&
                  row.flags.includes('6') &&
                  row.flags.includes('1')
                "
              >
                <img
                  src="@/assets/images/mail/replied.png"
                  alt=""
                  title="已回复"
                  class="messageImg"
                />
              </span>
              <span v-if="row.flags && row.flags.includes('6')">
                <img
                  src="@/assets/images/mail/message-open.png"
                  alt=""
                  title="已读"
                  class="messageImg"
                />
              </span>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="fromPersonalName" label="发件人" width="150">
          <template #default="{ row, $index }">
            <div class="contentClass" @click="handleRowClick(row, $index)">
              {{ row.fromPersonalName }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="fromEmail" label="发件人地址" width="210">
          <template #default="{ row, $index }">
            <div class="contentClass" @click="handleRowClick(row, $index)">
              {{ row.fromEmail }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="subject" label="主题">
          <template #default="{ row, $index }">
            <div class="contentClass" @click="handleRowClick(row, $index)">
              {{ row.subject }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="sendDate" label="时间" width="160">
          <template #default="{ row, $index }">
            <div style="cursor: pointer" @click="handleRowClick(row, $index)">
              {{ row.sendDate }}
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        style="margin-top: 15px"
        v-model:current-page="tableData.pagination.pageNum"
        v-model:page-size="tableData.pagination.pageSize"
        :page-sizes="[15, 30, 50, 100]"
        layout="sizes, prev, pager, next"
        :total="tableData.pagination.total"
        prev-text="上一页"
        next-text="下一页"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      />
    </div>
  </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 tableHeight = ref(0);
tableHeight.value = window.innerHeight - 270;
const tableData = reactive({
  data: [],
  pagination: {
    pageNum: 1,
    pageSize: 15,
    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 = () => {
  if (mailStore.currentMenu.listPageType === "10") {
    loading.value = true;
    proxy
      .post("/mailService/getMessagePage", {
        ...mailStore.currentMenu,
        ...tableData.pagination,
      })
      .then((res) => {
        tableData.data = res.rows;
        mailStore.mailDataList = res.rows;
        tableData.pagination.total = res.total;
        loading.value = false;
      });
  } else if (mailStore.currentMenu.listPageType === "20") {
    loading.value = true;
    proxy
      .post("/myFolderMessage/page", {
        myFolderId: mailStore.currentMenu.folderId,
        type: mailStore.selectMail.type,
        ...tableData.pagination,
      })
      .then((res) => {
        tableData.data = res.rows;
        mailStore.mailDataList = res.rows;
        tableData.pagination.total = res.total;
        loading.value = false;
      });
  } else if (mailStore.currentMenu.listPageType === "30") {
    loading.value = true;
    proxy
      .post("/myTagMessage/page", {
        myTagId: mailStore.currentMenu.folderId,
        type: mailStore.selectMail.type,
        ...tableData.pagination,
      })
      .then((res) => {
        tableData.data = res.rows;
        mailStore.mailDataList = res.rows;
        tableData.pagination.total = res.total;
        loading.value = false;
      });
  }
};

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

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

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

const handleCurrentChange = (val) => {
  tableData.pagination.pageNum = val;
  getList();
};

const handleSizeChange = (val) => {
  tableData.pagination.pageSize = val;
  getList();
};

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

<style lang="scss" scoped>
.messageImg {
  width: 18px;
  height: 18px;
  transform: translateY(5px);
}
.el-pagination {
  padding-left: 30vw;
}
.contentClass {
  cursor: pointer;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
</style>