<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="发送人" prop="senderName">-->
<!--        <el-input-->
<!--          v-model="queryParams.senderName"-->
<!--          placeholder="请输入发送人名称"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="接收人" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入接收人名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否已读" prop="isRead">
        <el-select
          v-model="queryParams.isRead"
          placeholder="请选择是否已读"
          clearable
          @change="handleQuery"
        >
          <el-option
            v-for="dict in dict.type.is_read"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-has-permi="['bst:chatMsg:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="chatMsgList" @selection-change="handleSelectionChange" :default-sort="defaultSort"  @sort-change="onSortChange">
      <el-table-column type="selection" width="55" align="center" />
      <template v-for="column of showColumns">
        <el-table-column
            :key="column.key"
            :label="column.label"
            :prop="column.key"
            :align="column.align"
            :min-width="column.minWidth"
            :sort-orders="orderSorts"
            :sortable="column.sortable"
            :show-overflow-tooltip="column.overflow"
            :width="column.width"
        >
          <template slot-scope="d">
            <template v-if="column.key === 'id'">
              {{d.row[column.key]}}
            </template>
            <template v-else-if="column.key === 'isRead'">
              <dict-tag :options="dict.type.is_read" :value="d.row.isRead"/>
            </template>
            <template v-else>
              {{d.row[column.key]}}
            </template>
          </template>
        </el-table-column>
      </template>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改聊天消息列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <form-col :span="span"  label="聊天ID" prop="chatId">
            <el-input v-model="form.chatId" placeholder="请输入聊天ID" />
          </form-col>
          <form-col :span="span"  label="接收人ID" prop="userId">
            <el-input v-model="form.userId" placeholder="请输入接收人ID" />
          </form-col>
          <form-col :span="span"  label="是否已读 0-未读 1-已读" prop="isRead">
            <el-input v-model="form.isRead" placeholder="请输入是否已读 0-未读 1-已读" />
          </form-col>
          <form-col :span="span"  label="读取时间" prop="readTime">
            <el-date-picker clearable
              v-model="form.readTime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择读取时间">
            </el-date-picker>
          </form-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listChatMsg, getChatMsg, delChatMsg, addChatMsg, updateChatMsg } from "@/api/bst/chatMsg";
import { $showColumns } from '@/utils/mixins';
import FormCol from "@/components/FormCol/index.vue";

  // 默认排序字段
const defaultSort = {
  prop: "receiveTime",
  order: "descending"
}

export default {
  name: "ChatMsg",
  mixins: [$showColumns],
  dicts: ['is_read'],
  components: {FormCol},
  props: {
    query: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      span: 24,
      // 字段列表
      columns: [
        // {key: 'sender', visible: true, label: '发送人名称', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'userName', visible: true, label: '接收人名称名称', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'isRead', visible: true, label: '是否已读', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'receiveTime', visible: true, label: '接收时间', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'readTime', visible: true, label: '读取时间', minWidth: null, sortable: false, overflow: false, align: 'center', width: null},
      ],
      // 排序方式
      orderSorts: ['ascending', 'descending', null],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 聊天消息列表表格数据
      chatMsgList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      defaultSort,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: defaultSort.prop,
        isAsc: defaultSort.order,
        chatId: null,
        userId: null,
        isRead: null,
        readTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "接收人ID不能为空", trigger: "blur" }
        ],
        isRead: [
          { required: true, message: "是否已读 0-未读 1-已读不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.queryParams = {
      ...this.queryParams,
      ...this.query
    }
    this.getList();
  },
  methods: {
    /** 当排序按钮被点击时触发 **/
    onSortChange(column) {
      if (column.order == null) {
        this.queryParams.orderByColumn = defaultSort.prop;
        this.queryParams.isAsc = defaultSort.order;
      } else {
        this.queryParams.orderByColumn = column.prop;
        this.queryParams.isAsc = column.order;
      }
      this.getList();
    },
    /** 查询聊天消息列表列表 */
    getList() {
      this.loading = true;
      listChatMsg(this.queryParams).then(response => {
        this.chatMsgList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        chatId: null,
        userId: null,
        isRead: null,
        readTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加聊天消息列表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getChatMsg(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改聊天消息列表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateChatMsg(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addChatMsg(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除聊天消息列表编号为"' + ids + '"的数据项？').then(function() {
        return delChatMsg(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('bst/chatMsg/export', {
        ...this.queryParams
      }, `chatMsg_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
