<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="fileName">
        <el-input
          v-model="queryParams.fileName"
          placeholder="请输入文件名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文件类型" prop="fileType">
        <el-select v-model="queryParams.fileType" placeholder="请选择文件类型" clearable>
          <el-option
            v-for="dict in dict.type.iit_file_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="推送用户" prop="pushName">
        <el-input
          v-model="queryParams.pushName"
          placeholder="请输入文件名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </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-table v-loading="loading" :data="messageList" >
      <el-table-column label="主键ID" align="center" prop="id" width="60" :show-overflow-tooltip="true"/>
      <el-table-column label="文件名称" align="left" prop="fileName" :show-overflow-tooltip="true"/>
      <el-table-column label="文件类型" align="center" prop="fileType" width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.iit_file_type" :value="scope.row.fileType"/>
        </template>
      </el-table-column>
      <el-table-column label="文件备注" align="left" prop="fileRemark" :show-overflow-tooltip="true"/>
      <el-table-column label="接收用户" align="left" prop="userName" width="100" :show-overflow-tooltip="true"/>
      <el-table-column label="推送用户" align="left" prop="pushName" width="100" :show-overflow-tooltip="true"/>
      <el-table-column label="推送时间" align="left" prop="pushTime" width="160" />
      <el-table-column label="已读状态" align="center" prop="isView" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.iit_view_status" :value="scope.row.isView"/>
        </template>
      </el-table-column>
      <el-table-column label="已读时间" align="left" prop="viewTime" width="160" :formatter="formatViewTime"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleMessageInfo(scope.row)"
                     v-hasPermi="['iit:message:edit']">消息详情
          </el-button>
        </template>
      </el-table-column>
    </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="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="70px">
        <el-row>
          <el-col :span="11">
            <el-form-item label="文件名称" prop="fileName">
              <el-input v-model="form.fileName" :disabled="true"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="文件类型" prop="fileType">
              <el-select v-model="form.fileType" :disabled="true">
                <el-option
                  v-for="dict in dict.type.iit_file_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="已读状态" prop="viewStatus">
              <el-input v-model="form.viewStatus" :disabled="true"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="推送用户" prop="pushName">
              <el-input v-model="form.pushName" style="width: 200px" :disabled="true"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="推送时间" prop="pushTime">
              <el-input v-model="form.pushTime" style="width: 200px" :disabled="true"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="接收用户" prop="userName">
              <el-input v-model="form.userName" style="width: 200px" :disabled="true"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="接收时间" prop="viewTime">
              <el-input v-model="form.viewTime" style="width: 200px" :disabled="true"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="文件备注" prop="fileRemark">
          <el-input type="textarea" :rows="4" v-model="form.fileRemark" :disabled="true"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="downloadFile">文件下载</el-button>
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addMessage, delMessage, getMessage, getMessageInfo, listMessagePage, updateMessage} from "@/api/iit/message";

export default {
  name: "Message",
  dicts: ['iit_file_type','iit_view_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 消息管理表格数据
      messageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        pushName: null,
        fileName: null,
        fileType: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    formatViewTime(row, column) {
      if (row.viewTime === null) {
        return '- - - - - - -';
      } else {
        return row.viewTime;
      }
    },
    /** 查询消息管理列表 */
    getList() {
      this.loading = true;
      listMessagePage(this.queryParams).then(response => {
        this.messageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        pushId: null,
        userId: null,
        userName: null,
        pushTime: null,
        isView: null,
        viewStatus: null,
        viewTime: 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 = "添加消息管理";
    },
    handleMessageInfo(row) {
      getMessageInfo(row.id).then(response => {
        this.form = response.data;
        this.form.viewStatus = response.data.isView == 1 ? "已读" : "未读";
        this.open = true;
        this.title = "消息详情";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMessage(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改消息管理";
      });
    },
    downloadFile() {
      window.open( process.env.VUE_APP_BASE_API + this.form.fileUrl);
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMessage(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMessage(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 delMessage(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('iit/message/export', {
        ...this.queryParams
      }, `message_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
