<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="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="['course:sopLogs:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['qw:autoTagsLogs:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-tabs type="card" v-model="defaultWatchStatus" @tab-click="handleClickX">
      <el-tab-pane v-for="(item,index) in sysFsSopWatchStatus" :label="item.dictLabel" :name="item.dictValue"></el-tab-pane>
    </el-tabs>

    <el-table v-loading="loading" :data="qwSopLogsList"  @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <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="watchStatus">
        <template slot-scope="scope">
          <dict-tag :options="sysFsSopWatchStatus" :value="scope.row.watchStatus"/>
        </template>
      </el-table-column>
      <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>

    <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>消息{{index}}:</span>
        </div>
        <div v-html="item.value"></div>
        <el-link v-if="item.isBindUrl == 1" :href="item.url" target="_blank">{{ item.url }}</el-link>
      </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 { exportSopLogs, listSopLogsCVOList } from '@/api/course/sopLogs'
import { delAutoTagsLogs } from '@/api/qw/autoTagsLogs'

export default {
  name: "sopLogsList",
  data() {
    return {

      //图片放大
      dialogVisible: false,
      dialogImageUrl:null,
      //默认规则
      defaultWatchStatus:"0",
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业微信SOP  定时任务表格数据
      qwSopLogsList: [],

      //成员状态
      sysQwSopLogsStatus:[],

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

      //SOP课程观看状态
      sysFsSopWatchStatus:[],

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

    //成员状态
    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_fs_sop_watch_status").then(response => {
      this.sysFsSopWatchStatus = response.data;
    });
  },
  methods: {
    /** 查询企业微信SOP  定时任务列表 */
    getList() {
      this.loading = true;
      listSopLogsCVOList(this.queryParams).then(response => {
        this.qwSopLogsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /**
    *  查看规则下的规则详情
    */
    selectSopLogsDetails(val){
      this.queryParams.sopId= val;
      this.getList();
    },

    handleClickX(tab, event) {

      this.queryParams.watchStatus=tab.name;
      this.handleQuery();
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        qwUserName: null,
        externalUserName: null,
        contentJson: null,
        watchStatus:null,
        sendStatus: null,
        sendTime: null,
        companyId: null,
        receivingStatus: null,
        msgId: 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();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除自动打标签的日志编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delAutoTagsLogs(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有企业微信SOP  定时任务数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportSopLogs(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
