<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item>
        <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-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="pushList" @selection-change="handleSelectionChange">
      <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="center" prop="userNumber" width="80"/>
      <el-table-column label="接收进度" align="left" prop="viewProgress>" width="200">
        <template slot-scope="scope">
          <el-progress :text-inside="true" :stroke-width="18" :percentage="scope.row.viewProgress"></el-progress>
        </template>
      </el-table-column>
      <el-table-column label="推送时间" align="left" prop="createTime" width="160" />
      <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="handleReview(scope.row)" v-hasPermi="['iit:push: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-table v-loading="reviewLoading" :data="messageList" >
        <el-table-column label="ID" align="center" prop="id" width="60" :show-overflow-tooltip="true"/>
        <el-table-column label="用户名称" align="left" prop="userName" />
        <el-table-column label="推送时间" align="left" prop="pushTime" width="180" />
        <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="180" :formatter="formatViewTime"/>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPush, getPush, delPush, addPush, updatePush } from "@/api/iit/push";
import {listMessage} from "@/api/iit/message";

export default {
  name: "Push",
  dicts: ['iit_file_type','iit_view_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      reviewLoading: true,
      messageList: [],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 推送管理表格数据
      pushList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        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;
      listPush(this.queryParams).then(response => {
        // this.pushList = response.rows;
        this.pushList = response.rows.map(item => {
          item.viewProgress = (item.viewNumber * 100 / item.userNumber).toFixed(0)
          return item;
        });
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        fileId: null,
        userNumber: null,
        createTime: null,
        userId: null,
        userName: null,
        deptId: 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 = "添加推送管理";
    },
    /** 修改按钮操作 */
    handleReview(row) {
      this.open = true;
      this.messageList = [];
      this.reviewLoading = true;
      const pushId = row.id;
      let queryParams = {
        pageNum: 1,
        pageSize: 200,
        pushId: pushId
      }
      listMessage(queryParams).then(response => {
        this.messageList = response.rows;
        this.title = "查看推送回执";
        this.reviewLoading = false;
      });
    },
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPush(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) {
            updatePush(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPush(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 delPush(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('iit/push/export', {
        ...this.queryParams
      }, `push_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
