<template>
  <div class="user-message-container">
    <vab-query-form>
      <vab-query-form-right-panel :span="24">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.message_content"
              clearable
              :placeholder="__('请输入短消息内容')"
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              {{ __('查询') }}
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      :height="tableConfig.height"
    >
      <el-table-column
        align="center"
        :label="__('消息编号')"
        prop="user_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('短消相关用户')"
        prop="user_other_nickname"
        show-overflow-tooltip
      />

      <el-table-column
        align="center"
        :label="__('短消息内容')"
        prop="message_content"
      >
        <template slot-scope="scope">
          <div style="height: 30px" v-html="scope.row.message_content"></div>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('接收人姓名')"
        prop="user_nickname"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatTime"
        :label="__('短消息发送时间')"
        prop="message_time"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatRead"
        :label="__('是否读取')"
        prop="message_is_read"
        show-overflow-tooltip
      />

      <el-table-column
        align="center"
        :formatter="formatType"
        :label="__('消息类型')"
        prop="message_type"
        show-overflow-tooltip
      />
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'

  import { getList } from '@/api/user/message'
  import { renderTime } from '@/utils/date'

  export default {
    name: 'UserMessage',

    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 20,
          title: '',
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,

      formatRead(row) {
        return row.message_is_read ? this.__('已读') : this.__('未读')
      },
      formatType(row) {
        return row.message_type === 1
          ? this.__('系统消息')
          : row.message_type === 2
          ? this.__('用户消息')
          : this.__('私信')
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      formatTime(row) {
        const message_time = row.message_time
        return message_time ? renderTime(message_time) : ''
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
    },
  }
</script>
