<template>
  <!-- 执行详情 -->
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="会员ID" prop="fsUserId">
        <el-input
          v-model="queryParams.fsUserId"
          placeholder="请输入会员ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户昵称" prop="fsUserName">
        <el-input
          v-model="queryParams.fsUserName"
          placeholder="请输入用户昵称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="加密号码" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标签" prop="tagIds">
        <div @click="openTagSearchPage"
             style="cursor: pointer; border: 1px solid #e6e6e6; background-color: white; overflow: hidden; flex-grow: 1;width: 250px">
          <div style="min-height: 35px; max-height: 200px; overflow-y: auto;">
            <el-tag type="success"
                closable
                :disable-transitions="false"
                v-for="list in this.tagSearchConfig.selected"
                :key="list.tagId"
                @close="handleCloseTags(list)"
                style="margin: 3px;"
            >{{ list.tagName }}
            </el-tag>
          </div>
        </div>
      </el-form-item>
      <el-form-item label="客服" prop="customerRoles">
        <div @click="openCusSearchPage"
             style="cursor: pointer; border: 1px solid #e6e6e6; background-color: white; overflow: hidden; flex-grow: 1;width: 250px">
          <div style="min-height: 35px; max-height: 200px; overflow-y: auto;">
            <el-tag type="success"
                closable
                :disable-transitions="false"
                v-for="list in this.bindAISearch.selected"
                :key="list.id"
                @close="handleCloseCus(list)"
                style="margin: 3px;"
            >{{ list.roleName }}
            </el-tag>
          </div>
        </div>
      </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"
        >导出</el-button>
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >批量删除</el-button>

        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="multiple"
          @click="handleEditCourse"
        >再次发送记录</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sopLogsList"  @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="消息来源" align="center" prop="sourceFrom" min-width="120">
        <template slot-scope="scope">
          <dict-tag :options="appMessageSendSourceFromOptions" :value="scope.row.sourceFrom + ''"/>
        </template>
      </el-table-column>
      <el-table-column label="用户id" align="center" prop="fsUserId" min-width="170"/>
      <el-table-column label="用户昵称" align="center" prop="fsUserName"min-width="140"/>
      <el-table-column label="客服昵称" align="center" prop="customerName" min-width="150"/>
      <el-table-column label="发送的消息" align="center" prop="contentJson" >
        <template slot-scope="scope">
          <el-button type="text" @click="showContentDialog(scope.row)">
            查看详情
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="发送状态" align="center" prop="appSendStatus" min-width="120">
        <template slot-scope="scope">
          <dict-tag :options="sopLogsStatus" :value="scope.row.appSendStatus + ''"/>
        </template>
      </el-table-column>
      <el-table-column label="预计发送时间" align="center" prop="sendTime" min-width="180"/>
      <el-table-column label="实际发送时间" align="center" prop="realSendTime" min-width="180"/>
      <el-table-column label="生成时间" align="createTime" prop="createTime" min-width="180"/>
      <el-table-column label="备注" align="center" prop="appSendRemark" min-width="180"/>
    </el-table>

    <el-dialog :visible.sync="contentDialog.isDialogVisible" title="消息详情" width="30%" append-to-body>
      <div>
        <span>课节：{{contentDialog.videoId}}</span>
        <!-- 已发送 -->
        <template v-if="contentDialog.row.appSendStatus !== '0'">
          <div v-for="(item, index) in contentDialog.json || []" :key="index">
            <el-card class="box-card" style="margin-top: 2%">
              <div slot="header" class="clearfix" style="display: flex;justify-content: space-between;align-items: center;">
                <div>
                  <span>类型：</span>
                  <el-tag
                    type="primary"
                    v-for="op in contentTypeOptions"
                    :key="op.dictValue"
                    v-if="item.contentType == op.dictValue"
                  >
                    {{ op.dictLabel }}
                </el-tag>
                </div>
                <div>
                  发送状态：<el-tag :type="item.success ? 'success' : 'danger'" v-text="item.success ? '成功' : '失败'"/>
                </div>
              </div>
              <div v-if="!item.success">失败原因：<span style="color: #ff4949">{{item.message}}</span></div>

              <!-- 文本 -->
              <div v-if="item.contentType == 11" v-html="item.value"></div>

              <!-- 图片 -->
              <div v-else-if="item.contentType == 2" class="message-style">
                <div
                  style="background-color: white; padding: 10px; width: 100%">
                  <div style="display: flex; justify-content: end; width: 100%">
                    <ImageUpload
                      :disabled="true"
                      v-model="item.imgUrl"
                      type="image"
                      :num="1"
                      :file-size="1"
                      :width="80"
                      :height="80"
                      style="margin-top: 1%;"
                    />
                  </div>
                </div>
              </div>

              <!-- 视频 -->
              <div v-else-if="item.contentType == 6" class="message-style">
                <video
                  :src="item.videoUrl"
                  controls
                  style="width: 200px; height: 100px">
                </video>
              </div>

              <!-- 语音 -->
              <div v-else-if="item.contentType == 12" class="message-style">
                <span>
                  {{ item.value }}
                </span>
                <div v-if="!item.voiceUrl" style="margin-top: 3px"  >
                  <span style="color: red">无语音地址</span>
                </div>
              </div>

              <!-- 科普 -->
              <div v-else-if="item.contentType == 9" class="message-style">
                <div
                  style="background-color: white; padding: 10px; width: 100%"
                  @click="openLink(item.linkUrl)">
                  <div style="display: flex; justify-content: space-between; width: 100%">
                    <div style="font-size: 13px; flex: 1; display: flex; align-items: center;">
                      {{ item.linkTitle }}</br>
                      {{ item.linkDescribe }}
                    </div>
                    <!-- 点击图片时停止事件冒泡 -->
                    <el-image
                      style="width: 50px; height: 50px; flex-shrink: 0"
                      :src="item.linkImageUrl"
                      fit="contain"
                      @click.stop="openImageViewer(item.linkImageUrl)" />
                  </div>
                </div>
              </div>

              <!-- 疗法 -->
              <div v-else-if="item.contentType == 20" class="message-style">
                <div
                  style="background-color: white; padding: 10px; width: 100%">
                  <div style="display: flex; justify-content: space-between; width: 100%">
                    <div style="font-size: 13px; flex: 1; display: flex; align-items: center;">
                      {{ item.packageName }}
                    </div>
                    <ImageUpload
                      :disabled="true"
                      v-model="item.packageImgUrl"
                      type="image"
                      :num="1"
                      :file-size="1"
                      :width="80"
                      :height="80"
                      style="margin-top: 1%;"
                    />
                  </div>
                </div>
              </div>

              <!-- 直播 -->
              <div v-else-if="item.contentType == 21" class="message-style">
                <div
                  style="background-color: white; padding: 10px; width: 100%">
                  <div style="display: flex; justify-content: space-between; width: 100%">
                    <div style="font-size: 13px; flex: 1; display: flex; align-items: center;">
                      {{ item.liveTitle }}
                    </div>
                    <ImageUpload
                      :disabled="true"
                      v-model="item.liveImgUrl"
                      type="image"
                      :num="1"
                      :file-size="1"
                      :width="80"
                      :height="80"
                      style="margin-top: 1%;"
                    />
                  </div>
                </div>
              </div>

              <!-- 民品 -->
              <div v-else-if="item.contentType == 22" class="message-style">
                <div
                  style="background-color: white; padding: 10px; width: 100%">
                  <div style="display: flex; justify-content: space-between; width: 100%">
                    <div style="font-size: 13px; flex: 1; display: flex; align-items: center;">
                      {{ item.productName }}
                    </div>
                    <ImageUpload
                      :disabled="true"
                      v-model="item.productImgUrl"
                      type="image"
                      :num="1"
                      :file-size="1"
                      :width="80"
                      :height="80"
                      style="margin-top: 1%;"
                    />
                  </div>
                </div>
              </div>

              <!-- 药品 -->
              <div v-else-if="item.contentType == 23" class="message-style">
                <div
                  style="background-color: white; padding: 10px; width: 100%">
                  <div style="display: flex; justify-content: space-between; width: 100%">
                    <div style="font-size: 13px; flex: 1; display: flex; align-items: center;">
                      {{ item.medicinesName }}
                    </div>
                    <ImageUpload
                      :disabled="true"
                      v-model="item.medicinesImgUrl"
                      type="image"
                      :num="1"
                      :file-size="1"
                      :width="80"
                      :height="80"
                      style="margin-top: 1%;"
                    />
                  </div>
                </div>
              </div>

              <!-- 短视频 -->
              <div v-else-if="item.contentType == 24" class="message-style">
                <div
                  style="background-color: white; padding: 10px; width: 100%">
                  <div style="display: flex; justify-content: space-between; width: 100%">
                    <div style="font-size: 13px; flex: 1; display: flex; align-items: center;">
                      {{ item.shortVideoName }}
                    </div>
                    <ImageUpload
                      :disabled="true"
                      v-model="item.shortVideoImgUrl"
                      type="image"
                      :num="1"
                      :file-size="1"
                      :width="80"
                      :height="80"
                      style="margin-top: 1%;"
                    />
                  </div>
                </div>
              </div>

              <!-- 文章 -->
              <div v-else-if="item.contentType == 25" class="message-style">
                <div
                  style="background-color: white; padding: 10px; width: 100%">
                  <div style="display: flex; justify-content: space-between; width: 100%">
                    <div style="font-size: 13px; flex: 1; display: flex; align-items: center;">
                      {{ item.articleName }}
                    </div>
                    <ImageUpload
                      :disabled="true"
                      v-model="item.articleImgUrl"
                      type="image"
                      :num="1"
                      :file-size="1"
                      :width="80"
                      :height="80"
                      style="margin-top: 1%;"
                    />
                  </div>
                </div>
              </div>

              <!-- 公开课 -->
              <div v-else-if="item.contentType == 26" class="message-style">
                <div
                  style="background-color: white; padding: 10px; width: 100%">
                  <div style="display: flex; justify-content: space-between; width: 100%">
                    <div style="font-size: 13px; flex: 1; display: flex; align-items: center;">
                      {{ item.openClassVideoName }}
                    </div>
                    <ImageUpload
                      :disabled="true"
                      v-model="item.openClassVideoImgUrl"
                      type="image"
                      :num="1"
                      :file-size="1"
                      :width="80"
                      :height="80"
                      style="margin-top: 1%;"
                    />
                  </div>
                </div>
              </div>

            </el-card>
          </div>
        </template>
        <!-- 待发送 -->
        <template v-else>
          <div v-for="(item, index) in contentDialog.contentJson.setting || []" :key="index">
            <el-card class="box-card" style="margin-top: 2%">

              <div slot="header" class="clearfix" style="display: flex;justify-content: space-between; align-items: center;">
                <div>
                  <span>类型：</span>
                  <el-tag
                    type="primary"
                    v-for="op in contentTypeOptions"
                    :key="op.dictValue"
                    v-if="item.contentType == op.dictValue"
                  >
                    {{ op.dictLabel }}
                </el-tag>
                </div>
                <div>
                  发送状态：<el-tag type="primary" v-text="'待发送'"/>
                </div>
              </div>

              <!-- 文本 -->
              <div v-if="item.contentType == 11" v-html="item.value"></div>

              <!-- 图片 -->
              <div v-else-if="item.contentType == 2" class="message-style">
                <div
                  style="background-color: white; padding: 10px; width: 100%">
                  <div style="display: flex; justify-content: end; width: 100%">
                    <ImageUpload
                      :disabled="true"
                      v-model="item.imgUrl"
                      type="image"
                      :num="1"
                      :file-size="1"
                      :width="80"
                      :height="80"
                      style="margin-top: 1%;"
                    />
                  </div>
                </div>
              </div>

              <!-- 视频 -->
              <div v-else-if="item.contentType == 6" class="message-style">
                <video
                  :src="item.videoUrl"
                  controls
                  style="width: 200px; height: 100px">
                </video>
              </div>

              <!-- 语音 -->
              <div v-else-if="item.contentType == 12" class="message-style">
                <span>
                  {{ item.value }}
                </span>
                <div v-if="!item.voiceUrl" style="margin-top: 3px"  >
                  <span style="color: red">无语音地址</span>
                </div>
              </div>

              <!-- 科普 -->
              <div v-else-if="item.contentType == 9" class="message-style">
                <div
                  style="background-color: white; padding: 10px; width: 100%"
                  @click="openLink(item.linkUrl)">
                  <div style="display: flex; justify-content: space-between; width: 100%">
                    <div style="font-size: 13px; flex: 1; display: flex; align-items: center;">
                      {{ item.linkTitle }}</br>
                      {{ item.linkDescribe }}
                    </div>
                    <!-- 点击图片时停止事件冒泡 -->
                    <el-image
                      style="width: 50px; height: 50px; flex-shrink: 0"
                      :src="item.linkImageUrl"
                      fit="contain"
                      @click.stop="openImageViewer(item.linkImageUrl)" />
                  </div>
                </div>
              </div>

              <!-- 疗法 -->
              <div v-else-if="item.contentType == 20" class="message-style">
                <div
                  style="background-color: white; padding: 10px; width: 100%">
                  <div style="display: flex; justify-content: space-between; width: 100%">
                    <div style="font-size: 13px; flex: 1; display: flex; align-items: center;">
                      {{ item.packageName }}
                    </div>
                    <ImageUpload
                      :disabled="true"
                      v-model="item.packageImgUrl"
                      type="image"
                      :num="1"
                      :file-size="1"
                      :width="80"
                      :height="80"
                      style="margin-top: 1%;"
                    />
                  </div>
                </div>
              </div>

              <!-- 直播 -->
              <div v-else-if="item.contentType == 21" class="message-style">
                <div
                  style="background-color: white; padding: 10px; width: 100%">
                  <div style="display: flex; justify-content: space-between; width: 100%">
                    <div style="font-size: 13px; flex: 1; display: flex; align-items: center;">
                      {{ item.liveTitle }}
                    </div>
                    <ImageUpload
                      :disabled="true"
                      v-model="item.liveImgUrl"
                      type="image"
                      :num="1"
                      :file-size="1"
                      :width="80"
                      :height="80"
                      style="margin-top: 1%;"
                    />
                  </div>
                </div>
              </div>

              <!-- 民品 -->
              <div v-else-if="item.contentType == 22" class="message-style">
                <div
                  style="background-color: white; padding: 10px; width: 100%">
                  <div style="display: flex; justify-content: space-between; width: 100%">
                    <div style="font-size: 13px; flex: 1; display: flex; align-items: center;">
                      {{ item.productName }}
                    </div>
                    <ImageUpload
                      :disabled="true"
                      v-model="item.productImgUrl"
                      type="image"
                      :num="1"
                      :file-size="1"
                      :width="80"
                      :height="80"
                      style="margin-top: 1%;"
                    />
                  </div>
                </div>
              </div>

              <!-- 药品 -->
              <div v-else-if="item.contentType == 23" class="message-style">
                <div
                  style="background-color: white; padding: 10px; width: 100%">
                  <div style="display: flex; justify-content: space-between; width: 100%">
                    <div style="font-size: 13px; flex: 1; display: flex; align-items: center;">
                      {{ item.medicinesName }}
                    </div>
                    <ImageUpload
                      :disabled="true"
                      v-model="item.medicinesImgUrl"
                      type="image"
                      :num="1"
                      :file-size="1"
                      :width="80"
                      :height="80"
                      style="margin-top: 1%;"
                    />
                  </div>
                </div>
              </div>

              <!-- 短视频 -->
              <div v-else-if="item.contentType == 24" class="message-style">
                <div
                  style="background-color: white; padding: 10px; width: 100%">
                  <div style="display: flex; justify-content: space-between; width: 100%">
                    <div style="font-size: 13px; flex: 1; display: flex; align-items: center;">
                      {{ item.shortVideoName }}
                    </div>
                    <ImageUpload
                      :disabled="true"
                      v-model="item.shortVideoImgUrl"
                      type="image"
                      :num="1"
                      :file-size="1"
                      :width="80"
                      :height="80"
                      style="margin-top: 1%;"
                    />
                  </div>
                </div>
              </div>

              <!-- 文章 -->
              <div v-else-if="item.contentType == 25" class="message-style">
                <div
                  style="background-color: white; padding: 10px; width: 100%">
                  <div style="display: flex; justify-content: space-between; width: 100%">
                    <div style="font-size: 13px; flex: 1; display: flex; align-items: center;">
                      {{ item.articleName }}
                    </div>
                    <ImageUpload
                      :disabled="true"
                      v-model="item.articleImgUrl"
                      type="image"
                      :num="1"
                      :file-size="1"
                      :width="80"
                      :height="80"
                      style="margin-top: 1%;"
                    />
                  </div>
                </div>
              </div>

              <!-- 公开课 -->
              <div v-else-if="item.contentType == 26" class="message-style">
                <div
                  style="background-color: white; padding: 10px; width: 100%">
                  <div style="display: flex; justify-content: space-between; width: 100%">
                    <div style="font-size: 13px; flex: 1; display: flex; align-items: center;">
                      {{ item.openClassVideoName }}
                    </div>
                    <ImageUpload
                      :disabled="true"
                      v-model="item.openClassVideoImgUrl"
                      type="image"
                      :num="1"
                      :file-size="1"
                      :width="80"
                      :height="80"
                      style="margin-top: 1%;"
                    />
                  </div>
                </div>
              </div>

            </el-card>
          </div>
        </template>
      </div>
      <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"
    />

    <el-dialog
      title="标签搜索"
      :visible.sync="tagSearchConfig.visible"
      custom-class="dialog-search-tag"
      append-to-body
    >
      <div>搜索标签：
        <el-input v-model="tagSearchConfig.queryParams.groupName" placeholder="请输入标签名称" clearable size="small"
          style="width: 200px;margin-right: 10px" />
        <el-button type="primary" icon="el-icon-search" size="mini"
          @click="handleSearchTags()">
          搜索
        </el-button>
        <el-button type="primary" icon="el-icon-refresh" size="mini" @click="cancelSearchTags">重置</el-button>
      </div>
      <div class="dialog_body_tag">
        <div v-for="item in tagSearchConfig.tagGroupList" :key="item.id">
          <div style="font-size: 20px;margin-top: 20px;margin-bottom: 20px;">
            <span class="name-background">{{ item.name }}</span>
          </div>
          <div class="tag-container">
            <a v-for="tagItem in item.tags" class="tag-box" @click="tagSelection(tagItem)"
                :class="{ 'tag-selected': tagItem.isSelected || (tagSearchConfig.selectTags && tagSearchConfig.selectTags.some(tg => tg.id == tagItem.id)) }">
                {{ tagItem.name }}
            </a>
          </div>
        </div>
      </div>

      <pagination
        v-show="tagSearchConfig.total > 0"
        :total="tagSearchConfig.total"
        :page.sync="tagSearchConfig.queryParams.pageNum"
        :limit.sync="tagSearchConfig.queryParams.pageSize"
        @pagination="getSearchTagGroupList"
      />
      <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="tagSearchConfig.visible = false">确 定</el-button>
          <el-button @click="tagSearchConfig.visible = false">取消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="客服搜索" :visible.sync="bindAISearch.visible" width="1000px" append-to-body>
      <CusRoleList
        ref="QwUserList"
        @confirm="customerSelectedConfirmCallForBind"
        @selectionChange="customerSelectionChangeCallForBind"
        :selected="bindAISearch.selected"
        :ids="customerIds"
      />
    </el-dialog>

  </div>
</template>

<script>
import { exportSopLogs, listSopLogsList, delSopLogs, editCourseSopLogs} from '@/api/app/sopLogs';

import { listTagGroupForUserBindTag } from "@/api/app/tag/tagGroup";
// import { getLiveOptions,} from "@/api/app/live";


import CusRoleList from '@/views/app/user/CusRoleList.vue';
import ImageUpload from "@/views/qw/sop/ImageUpload.vue";

export default {
  name: "sopLogsList",
  components: {CusRoleList, ImageUpload},
  props:{
    rowDetailFrom:{},
  },

  watch:{
    rowDetailFrom:{
      handler(newVal){
        // 当formData变化时重新查询
        this.getList(newVal);
      },
      deep: true
    }
  },
  computed: {
    customerIds() {
      return this.rowDetailFrom.qwUserIds ? this.rowDetailFrom.qwUserIds.split(',') : []
    },
  },
  data() {
    return {
      //图片放大
      dialogVisible: false,
      dialogImageUrl:null,
      //时间选择
      scheduleTime: null,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // SOP  定时任务表格数据
      sopLogsList: [],

      //成员状态
      sopLogsStatus:[],
      //消息来源
      appMessageSendSourceFromOptions: [],

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

      //发送的消息
      contentDialog:{
        isDialogVisible:false,
        json: [],
        videoId: null,
        row: {},
      },
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fsUserId: null,
        fsUserName: null,
        phone: null,
        tagIds: [],
        customerIds: [],
        sopId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      tagSearchConfig: {
        visible: false,//对话框是否展示
        queryParams: {
          groupName: null,
          pageNum: 1,
          pageSize: 10,
        },
        total: 0,
        tagGroupList: [],
        selected: [],
      },
      bindAISearch: {
        visible: false,
        selected: [],
      },
      contentTypeOptions: [],//发送消息的内容类型
    };
  },
  created() {
    this.getList(this.rowDetailFrom);

    //发送状态
    this.getDicts("app_sopLogs_status").then(response => {
      this.sopLogsStatus = response.data;
    });

    //消息来源
    this.getDicts("app_message_send_source_from").then(response => {
      this.appMessageSendSourceFromOptions = response.data;
    });

    this.getDicts("app_sop_plugin_settingType").then(response => {
      this.contentTypeOptions = response.data;
    });

  },
  methods: {
    // async getLiveList() {
    //   return await getLiveOptions();
    // },
    getList(val) {
      let qp = {...this.queryParams};
      qp['sopId'] = val.id || this.rowDetailFrom.id;
      qp['tagIds'] = this.tagSearchConfig.selected.map(tag => tag.tagId);
      qp['customerIds'] = this.bindAISearch.selected.map(s => s.id);
      this.loading = true;
      listSopLogsList(qp).then(response => {
        this.sopLogsList = 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;
    },
    openLink(url){
      // 使用 window.open 打开链接
      window.open(url, '_blank');
    },
    //查看发送的消息体
    showContentDialog(row) {
      this.contentDialog.row = row;
      const sanitizedJson = row.contentJson.replace(/[\u0000-\u001F\u007F]/g, '');
      const parsedData = JSON.parse(sanitizedJson);
      if (row.appSendStatus !== '0') {
        const remarkDetailStr = row.remarkDetail.replace(/[\u0000-\u001F\u007F]/g, '');
        const remarkDetailArray = JSON.parse(remarkDetailStr);
        remarkDetailArray.map(item => {
          if (item.meta) {
            let metaSource = item.meta.replace(/[\u0000-\u001F\u007F]/g, '');
            let targetMeta = JSON.parse(metaSource);
            parsedData.setting.map(async s => {
              if (targetMeta.contentType == s.contentType) {
                let flag = false;
                //科普
                if (s.contentType == 9 && s.appLinkUrl == targetMeta.appLinkUrl) {
                  flag = true;
                }
                //文本
                else if (s.contentType == 11 && s.value == targetMeta.value) {
                  flag = true;
                }
                //图片
                else if (s.contentType == 2 && s.imgUrl == targetMeta.imgUrl) {
                  flag = true;
                }
                //视频
                else if (s.contentType == 6 && s.videoUrl == targetMeta.videoUrl) {
                  flag = true;
                }
                //语音
                else if (s.contentType == 12 && s.voiceUrl == targetMeta.voiceUrl) {
                  flag = true;
                }
                //疗法
                else if (s.contentType == 20 && s.packageId == targetMeta.packageId) {
                  flag = true;
                }
                //直播
                else if (s.contentType == 21 && s.liveId == targetMeta.liveId) {
                  flag = true;
                  let defaultLive = await this.getLiveList();
                  let liveInfo = defaultLive.data.find(tmp => tmp.liveId == s.liveId);
                  if (liveInfo && liveInfo.liveName) {
                    s['liveTitle'] = liveInfo.liveName;
                  } else {
                    s['liveTitle'] = '--';
                  }
                }
                //民品
                else if (s.contentType == 22 && s.productId == targetMeta.productId) {
                  flag = true;
                }
                //药品
                else if (s.contentType == 23 && s.medicinesId == targetMeta.medicinesId) {
                  flag = true;
                }
                //短视频
                else if (s.contentType == 24 && s.shortVideoId == targetMeta.shortVideoId) {
                  flag = true;
                }
                //文章
                else if (s.contentType == 25 && s.articleId == targetMeta.articleId) {
                  flag = true;
                }
                //公开课
                else if (s.contentType == 26 && s.openClassVideoId == targetMeta.openClassVideoId) {
                  flag = true;
                }
                if (flag) {
                  // 把源对象所有key 赋值给目标响应式对象，不破坏代理
                  Object.keys(s).forEach(key => {
                    this.$set(item, key, s[key]);
                  })
                }
              }
            })
          }
        })
        this.contentDialog.json = remarkDetailArray;
      }
      this.contentDialog.contentJson = parsedData;
      this.contentDialog.videoId = parsedData.videoId;
      this.contentDialog.isDialogVisible = true;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList(this.rowDetailFrom);
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.tagSearchConfig.selected = [];
      this.bindAISearch.selected = [];
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        nickName: null,
        phone: null,
        tagsId: [],
        customerIds: [],
      };
      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('是否确认删除【执行详情】id为"【' + ids + '】"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delSopLogs(ids);
      }).then(() => {
        this.getList(this.rowDetailFrom);
        this.msgSuccess("删除成功");
      }).catch(() => {});
    },

    /**
    * 批量修改完课的发送
    */
    handleEditCourse(row){

      // 获取原始ids（确保是数组）
      let ids = row.id || this.ids;
      if (!Array.isArray(ids)) {
        ids = [ids];
      }

      // 过滤掉remark为'E级客户不发送'的项
      const filteredIds = ids.filter(id => {
        const item = this.sopLogsList.find(item => item.id === id);
        return item?.remark !== 'E级客户不发送';
      });

      // 如果没有可发送的记录
      if (filteredIds.length === 0) {
        this.msgWarning("没有可发送的记录（E级客户已自动排除）");
        return;
      }

      this.$confirm('确认要再次发送【执行详情】编号为"【' + filteredIds.join(',') + '】"的【完课】记录?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return editCourseSopLogs(filteredIds);
      }).then(() => {
        this.getList(this.rowDetailFrom);
        this.msgSuccess("已经 修改记录为 待发送~");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      let qp = {...this.queryParams};
      qp['sopId'] = val.id || this.rowDetailFrom.id;
      qp['tagIds'] = this.tagSearchConfig.selected.map(tag => tag.tagId);
      qp['customerIds'] = this.bindAISearch.selected;
      this.$confirm('是否确认导出所有自动化  定时任务数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportSopLogs(qp);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
    openTagSearchPage() {
      this.getSearchTagGroupList();
      this.tagSearchConfig.visible = true;
    },
    getSearchTagGroupList() {
      listTagGroupForUserBindTag(this.tagSearchConfig.queryParams).then(response => {
        let tRows = response.rows;
        if (tRows && tRows.length > 0) {
          tRows.forEach(group => {
            group.tags.forEach(tag => {
              if (this.tagSearchConfig.selected.some(item => item.tagId === tag.id)) {
                tag.isSelected = true;
              } else {
                tag.isSelected = false;
              }
            })
          });
        }
        this.tagSearchConfig.tagGroupList = tRows;
        this.tagSearchConfig.total = response.total;
      });
    },
    getTagGroupList() {
      listTagGroupForUserBindTag(this.tagSearchConfig.queryParams).then(response => {
        this.tagSearchConfig.tagGroupList = response.rows;
        this.tagSearchConfig.total = response.total;
      });
    },
    openCusSearchPage() {
      this.bindAISearch.visible = true;
    },
    cancelSearchTags() {
      this.tagSearchConfig.queryParams = {
        groupName: null,
        pageNum: 1,
        pageSize: 10,
      }
      this.getSearchTagGroupList();
    },
    handleSearchTags() {
      this.tagSearchConfig.queryParams.pageNum = 1;
      this.getSearchTagGroupList();
    },
    tagSelection(row) {
      row.isSelected = !row.isSelected;
      if (row.isSelected) {
        this.tagSearchConfig.selected.push({tagId: row.id, tagName: row.name});
      }
      //
      else {
        this.tagSearchConfig.selected = this.tagSearchConfig.selected.filter(item => item.tagId !== row.id);
      }
      this.$forceUpdate();
    },
    /**
     * 客服选中
     */
    customerSelectionChangeCallForBind(config) {
      let selected = config.selected;
      //单行操作
      if (config.mode === 0) {
        let row = config.row;
        if (selected) {
          if (!this.bindAISearch.selected.some(s => s.id == row.id)) {
            this.bindAISearch.selected.push(row)
          }
        } else {
          this.bindAISearch.selected = this.bindAISearch.selected.filter(m => m.id != row.id);
        }
      }
      //操作当前页所有行
      else {
        let list = config.rows;
        if (selected) {
          list.map(l => {
            if (!this.bindAISearch.selected.some(s => s.id == l.id)) {
              this.bindAISearch.selected.push(l)
            }
          });
        } else {
          list.map(l => {
            for (let i = 0; i < this.bindAISearch.selected.length; i++) {
              if (l.id == this.bindAISearch.selected[i].id) {
                this.bindAISearch.selected.splice(i, 1);
              }
            }
          })
        }
      }
    },
    /**
     * 确认客服
     * @param list
     */
    customerSelectedConfirmCallForBind() {
      this.bindAISearch.visible = false;
    },
    handleCloseCus(cus) {
      this.bindAISearch.selected = this.bindAISearch.selected.filter(item => item.id != cus.id);
    },
    handleCloseTags(tag) {
      this.tagSearchConfig.selected = this.tagSearchConfig.selected.filter(item => item.tagId != tag.tagId);
    },
  }
};
</script>
<style scoped>
.message-stayle{
  display: flex;
  justify-content: normal;
  align-items: center;
  margin-top: 10px;
}
.message-stayle .el-link {
  white-space: normal; /* 允许换行 */
  word-break: break-all; /* 单词中间断行 */
  overflow-wrap: break-word; /* 允许在单词内换行 */
}
.message-stayle span {
  word-break: break-all;
}

.tag-container {
  display: flex;
  flex-wrap: wrap; /* 超出宽度时自动换行 */
  gap: 8px; /* 设置标签之间的间距 */
}

.name-background {
  display: inline-block;
  background-color: #abece6; /* 背景颜色 */
  padding: 4px 8px; /* 调整内边距，让背景包裹文字 */
  border-radius: 4px; /* 可选：设置圆角 */
}

.tag-box {
  padding: 8px 12px;
  border: 1px solid #989797;
  border-radius: 4px;
  cursor: pointer;
  display: inline-block;
}

.tag-selected {
  background-color: #00bc98;
  color: #fff;
  border-color: #00bc98;
}

.el-tag + .el-tag {
  margin-left: 10px;
}

.dialog_body_tag {
  height: 450px;
  overflow: auto;
}
</style>
