<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="企微员工昵称" prop="qwUserName">
        <el-input
          v-model="queryParams.qwUserName"
          placeholder="请输入企微员工昵称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户昵称" prop="externalUserName">
        <el-input
          v-model="queryParams.externalUserName"
          placeholder="请输入企微客户昵称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发送(成员)状态" prop="sendStatus">
        <el-select v-model="queryParams.sendStatus" placeholder="请选择发送(成员)状态" clearable size="small">
          <el-option
            v-for="dict in sysQwSopLogsStatus"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="预计发送时间" prop="sendTime">-->
<!--        <el-date-picker clearable size="small"-->
<!--          v-model="queryParams.sendTime"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="选择预计发送时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
      <el-form-item label="接收(客户)状态" prop="receivingStatus">
        <el-select v-model="queryParams.receivingStatus" placeholder="请选择接收(客户)状态" clearable size="small">
          <el-option
            v-for="dict in groupMsgSendStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </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"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['qw:qwSopLogs:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="qwSopLogsList">
      <el-table-column label="发送类型" align="center" prop="sendType" width="80">
        <template slot-scope="scope">
          <dict-tag :options="sysQwSopType" :value="scope.row.sendType"/>
        </template>
      </el-table-column>
      <el-table-column label="企微成员昵称" align="center" prop="qwUserName" />
      <el-table-column label="系统后台昵称" align="center" prop="userName" />
      <el-table-column label="客户昵称" align="center" prop="externalUserName" />
      <el-table-column label="发送的消息" align="center" prop="contentJson" >
        <template slot-scope="scope">
          <el-button type="text" @click="showContentDialog(scope.row.contentJson)">
            查看详情
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="成员状态" align="center" prop="sendStatus" >
        <template slot-scope="scope">
          <dict-tag :options="sysQwSopLogsStatus" :value="scope.row.sendStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="预计发送时间" align="center" prop="sendTime" width="180"/>
      <el-table-column label="客户" align="center" prop="receivingStatus" >
        <template slot-scope="scope">
          <dict-tag :options="groupMsgSendStatusOptions" :value="scope.row.receivingStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="消息ID" align="center" prop="msgId" />
    </el-table>

    <el-dialog :visible.sync="contentDialog.isDialogVisible" title="消息详情" width="30%" append-to-body>
      <el-card v-for="(item, index) in contentDialog.selectedContentJson" :key="index" class="box-card" style="margin-top: 2%">
        <div slot="header" class="clearfix">
          <span>类型:</span>
          <span v-if="item.type===1">文本</span>
          <span v-if="item.type===2">图片</span>
        </div>

        <div v-if="item.type===1" v-html="item.value"></div>
        <div v-if="item.type===2">
          <el-image style="width: 100px; height: 100px"  :src="item.value"
                    fit="contain" @click="openImageViewer(item.value)" />
        </div>

      </el-card>
      <span slot="footer" class="dialog-footer">
        <el-button @click="contentDialog.isDialogVisible = false">关闭</el-button>
      </span>
    </el-dialog>

    <!-- 大图预览对话框 -->
    <el-dialog
      :visible.sync="dialogVisible"
      :modal="false"
      width="500"
      append-to-body>
      <img
        :src="this.dialogImageUrl"
        style="display: block; max-width: 100%; margin: 0 auto"
      />
    </el-dialog>

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

<script>
import { listQwSopLogs, exportQwSopLogs, listQwSopLogsList } from '@/api/qw/sopLogs'

export default {
  name: "sopLogsList",
  props:{
    rowDetailFrom:{},
  },

  watch:{
    rowDetailFrom:{
      handler(newVal){
        // 当formData变化时重新查询
        this.getList(newVal);
      },
      deep: true
    }
  },
  data() {
    return {

      //图片放大
      dialogVisible: false,
      dialogImageUrl:null,

      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业微信SOP  定时任务表格数据
      qwSopLogsList: [],

      //成员状态
      sysQwSopLogsStatus:[],

      //客户接收状态
      groupMsgSendStatusOptions:[],

      //企微SOP发送类型
      sysQwSopType: [],

      //发送的消息
      contentDialog:{
        isDialogVisible:false,
        selectedContentJson: [],
      },
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        qwUserName: null,
        externalUserName: null,
        sendStatus: null,
        sendTime: null,
        corpId:null,
        receivingStatus: null,
        sendType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList(this.rowDetailFrom);

    //成员状态
    this.getDicts("sys_qw_sopLogs_status").then(response => {
      this.sysQwSopLogsStatus = response.data;
    });

    //客户接收状态
    this.getDicts("sys_qw_groupMsg_SendStatus").then(response => {
      this.groupMsgSendStatusOptions = response.data;
    });

    //发送消息类型
    this.getDicts("sys_qw_sop_type").then(response => {
      this.sysQwSopType = response.data;
    });


  },
  methods: {
    /** 查询企业微信SOP  定时任务列表 */
    getList(val) {



      this.queryParams.sopId = val.id || this.rowDetailFrom.id;
      this.queryParams.corpId= val.corpId || this.rowDetailFrom.corpId;
      this.loading = true;

      listQwSopLogsList(this.queryParams).then(response => {

        this.qwSopLogsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        qwUserName: null,
        externalUserName: null,
        logType: null,
        contentJson: null,
        sendStatus: null,
        sendTime: null,
        companyId: null,
        receivingStatus: null,
        msgId: null,
        sendType: null,
        sopId: null
      };
      this.resetForm("form");
    },

    openImageViewer(url) {
      // 打开大图预览对话框
      this.dialogImageUrl=url
      this.dialogVisible = true;
    },
    //查看发送的消息体
    showContentDialog(contentJson) {

      this.contentDialog.selectedContentJson = JSON.parse(contentJson);
      this.contentDialog.isDialogVisible = true;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList(this.rowDetailFrom);
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有企业微信SOP  定时任务数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportQwSopLogs(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
