<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      ref="crud"
      v-model="form"
      v-model:page="page"
      @search-change="searchChange"
      @search-reset="searchReset"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @selection-change="selectionChange"
      @on-load="onLoad"
    >
      <template #status="{ row }">
        <el-tag v-if="row.status === 1" type="info">已读</el-tag>
        <el-tag v-if="row.status === 0" type="success">未读</el-tag>
      </template>
      <template #menu-left>
        <el-button type="primary" @click="handleRead">标记已读</el-button>
      </template>
      <template #content="{ row }">
        <span @click="hanleView(row)" style="color: #284c89; cursor: pointer">{{
          row.content
        }}</span>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import { getList, update } from '@/api/desk/myMessage';
import EventBus from '@/utils/event-bus';
export default {
  data() {
    return {
      form: {},
      selectionList: [],
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      option: {
        columnSort: true,
        tip: false,
        height: 'auto',
        align: 'center',
        calcHeight: 32,
        simplePage: false,
        searchShow: true,
        searchMenuSpan: 6,
        searchIcon: true,
        searchIndex: 3,
        tree: false,
        border: true,
        index: true,
        selection: true,
        viewBtn: false,
        delBtn: true,
        editBtn: true,
        editBtnText: '修改',
        editBtnIcon: ' ',
        delBtnIcon: ' ',
        addBtn: false,
        labelWidth: 120,
        searchLabelWidth: 120,
        menu: true,
        menuWidth: 200,
        // dialogWidth: 600,
        dialogClickModal: false,
        searchEnter: true,
        excelBtn: true,
        gridBtn: false,
        searchShowBtn: false,
        showOverflowTooltip: true,
        searchLabelPosition: 'left',
        searchLabelPosition: 'left',
        searchGutter: 24,
        searchSpan: 6,
        menuAlign: 'left',
        gridBtn: false,
        searchMenuPosition: 'right',
        addBtnIcon: ' ',
        viewBtnIcon: ' ',
        delBtnIcon: ' ',
        editBtnIcon: ' ',
        menu: false,

        column: [
          {
            label: '标题',
            prop: 'title',
            sortable: false,
            filter: true,
            span: 12,
            search: true,
            searchLabelWidth: 90,
            display:false,
          },
          {
            label: '内容',
            prop: 'content',
            sortable: false,
            filter: true,
            span: 12,
            width:600,
            search: false,
            overHidden:true,
            searchLabelWidth: 90,
            display:false,
          },
          {
            label: '消息类型',
            prop: 'bizType',
            sortable: false,
            type: 'select',
            filter: true,
            span: 24,
            search: true,
            searchLabelWidth: 90,
            dicData: [
              { label: '系统消息', value: 1 },
              { label: '业务消息', value: 2 },
            ],
          },
          {
            label: '状态',
            prop: 'status',
            sortable: false,
            type:"select",
            display:false,
            dicData: [
              {
                label: '已读',
                value: '1',
              },
              {
                label: '未读',
                value: '0',
              },
            ],
            filter: true,
            span: 12,
            search: true,
            searchLabelWidth: 90,
          },
          {
            label: '发送时间',
            prop: 'approvalTime',
            type: 'date',
            format: 'YYYY-MM-DD',
            valueFormat: 'YYYY-MM-DD',
            searchRange:true,
            sortable: false,
            filter: true,
            search: true,
            span:24,
            searchLabelWidth: 90,
          },
          {
            label: '内容',
            prop: 'content',
            component: 'avue-ueditor',
            action: '/blade-resource/oss/endpoint/put-file',
            propsHttp: {
              res: 'data',
              url: 'link',
            },
            hide: true,
            minRows: 4,
            span: 24,
          },
        ],
      },
      data: [],
    };
  },
  computed: {
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(',');
    },
  },
  created() {
    this.onLoad(this.page);
  },
  methods: {
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
       console.log('搜索参数:', params);
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad();
    },
    hanleView(row) {
      this.$refs.crud.rowView(row);
    },
    handleRead() {
      if (this.selectionList.length == 0) {
        this.$message.error('请至少选择一条数据！');
        return;
      }
      let tmp = this.selectionList.find(item => item.status == 1);
      console.log(tmp, '111');
      if (tmp) {
        this.$message.error('请选择状态为【未读】的数据！');
        return;
      } else {
        console.log(this.ids);
        this.$confirm('确定将选择消息标记为已读?', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {
          update(this.ids).then(() => {
            this.$message.success('操作成功！');
            this.onLoad(this.page);
            // 触发事件，通知顶部导航栏更新未读数量
            console.log('触发消息已读事件');
            EventBus.$emit('message-read');

            // 备用方案：直接更新 localStorage 通知
            localStorage.setItem('message-read-timestamp', Date.now());
          });
        });
      }
    },
    onLoad(page, params = {}) {
      // getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
      //   const data = res.data.data;
      //   this.page.total = data.total;
      //   this.data = data.records;
      //   this.loading = false;
      // });

        this.data = []
        this.loading = false;
    },
  },
};
</script>

<style></style>
