<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="groupId">
        <el-select v-model="queryParams.groupId" clearable size="small">
          <el-option
              v-for="dict in systemPushGroupIdList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
              @keyup.enter.native="handleQuery"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" clearable size="small">
          <el-option
              v-for="dict in systemPushStatusList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
              @keyup.enter.native="handleQuery"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          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-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['cai:sysPush:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sysPushList" @selection-change="handleSelectionChange">
      <el-table-column label="id" align="center" prop="id" width="50"/>
      <el-table-column label="群体类型" align="center" prop="groupId" >
        <template v-slot="scope">
          <cai-dict-tag :options="systemPushGroupIdList" :value="scope.row.groupId"/>
        </template>
      </el-table-column>
      <el-table-column label="消息类型" align="center" prop="type" >
        <template v-slot="scope">
          <cai-dict-tag :options="systemPushTypeList" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="用户" align="center" prop="usercode" />
      <el-table-column label="标题" align="center" prop="title" show-overflow-tooltip/>
<!--      <el-table-column label="内容" align="center" prop="content" />-->
<!--      <el-table-column label="前端参数" align="center" prop="params" />-->
      <el-table-column label="备注说明" align="center" prop="remark" show-overflow-tooltip/>
      <el-table-column label="发送类型" align="center" prop="sendTimeType" >
        <template v-slot="scope">
          <cai-dict-tag :options="systemPushSendTimeTypeList" :value="scope.row.sendTimeType"/>
        </template>
      </el-table-column>
      <el-table-column label="发送时间" align="center" prop="sendTime" width="180" />
<!--      <el-table-column label="已推送人数" align="center" prop="num" />-->
      <el-table-column label="状态" align="center" prop="status">
        <template v-slot="scope">
          <cai-dict-tag :options="systemPushStatusList" :value="scope.row.status"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="完成时间" align="center" prop="endtime" width="180" />-->
      <el-table-column label="创建时间" align="center" prop="createTime" width="160" />
      <el-table-column label="更新时间" align="center" prop="updateTime" width="160" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="100">
        <template v-slot="scope">
          <el-button
              size="mini"
              type="text"
              v-if="scope.row.status === 0"
              @click="handleRun(scope.row)"
              v-hasPermi="['cai:sysPush:run']"
          >执行</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleInfo(scope.row)"
            v-hasPermi="['cai:sysPush:edit']"
          >预览</el-button>
          <el-button
              size="mini"
              type="text"
              v-if="scope.row.status === 0 && scope.row.sendTimeType === 1"
              @click="handleClose(scope.row)"
              v-hasPermi="['cai:sysPush:close']"
          >取消</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['cai:sysPush:remove']"
          >删除</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"
    />

    <add-sys-push-dialog v-if="addSysPushDialogVisible" ref="addSysPushDialog" @refreshDataList="getList" />

    <el-dialog title="系统消息查看" :close-on-click-modal="false" :visible.sync="infoOpen" width="350px" append-to-body>
      <preview-client :data="infoData.form" :textContext="infoData.textContext" :imageContext="infoData.imageContext"></preview-client>
      <div slot="footer" class="dialog-footer">
        <el-button @click="infoOpen = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { closeSysPush, delSysPush, listSysPush, runSysPush } from '@/api/cai/sysPush'
import {
  systemPushGroupIdList,
  systemPushLogStatus,
  systemPushSendTimeTypeList,
  systemPushStatusList,
  systemPushTypeList
} from '@/constant/statusMap'
import AddSysPushDialog from '@/views/cai/sysPush/add-sys-push-dialog.vue'
import PreviewClient from '@/components/PreviewInMobileClient/index.vue'

export default {
  name: "SysPush",
  components: {
    AddSysPushDialog,PreviewClient
  },
  data() {
    return {
      systemPushGroupIdList,systemPushLogStatus,systemPushSendTimeTypeList,systemPushStatusList,systemPushTypeList,
      addSysPushDialogVisible: false,
      infoOpen: false,
      infoData:{
        form:{},
        textContext:[],
        imageContext:[]
      },
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 推送系统消息表格数据
      sysPushList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        groupId: undefined,
        type: undefined,
        title: undefined,
        content: undefined,
        params: undefined,
        sendTimeType: undefined,
        status: undefined,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleInfo(row){
      let parse = JSON.parse(row.params);
      this.$set(this.infoData,'form',parse.master)
      this.$set(this.infoData,'textContext',parse.textContext)
      this.$set(this.infoData,'imageContext',parse.imageContext)
      console.log(this.infoData)
      this.infoOpen = true;
    },
    /** 查询推送系统消息列表 */
    getList() {
      this.loading = true;
      listSysPush(this.queryParams).then(response => {
        this.sysPushList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    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.addSysPushDialogVisible = true
      this.$nextTick(() => {
        this.$refs.addSysPushDialog.init()
      })
    },
    handleClose(row){
      this.$modal.confirm('是否确定取消定时推送系统【"' + row.title + '"】的数据项？').then(() => {
        this.loading = true;
        return closeSysPush(row.id);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("取消成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    handleRun(row){
      this.$modal.confirm('是否确认执行推送系统【"' + row.title + '"】的数据项？').then(() => {
        this.loading = true;
        return runSysPush(row.id);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("执行成功，请等待执行结果");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除推送系统消息编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delSysPush(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
  }
};
</script>
