<template>
  <div style="background-color: #fff;margin: 15px">
    <el-card class="box-card">
      <div  class="desct">
        数据统计
      </div>
      <div class="statistics-container">
        <el-row :gutter="20" type="flex" justify="space-between">
          <el-col :span="6" v-for="(item, index) in CountGroupMsgUserList" :key="index">
            <el-card style="height: 100px">
              <div class="statistic-item">
                <h2>{{ item.count }} </h2>
                <p>{{ item.label }}</p>
              </div>
            </el-card>
          </el-col>
        </el-row>
      </div>
    </el-card>

    <el-card class="box-card" style="margin-top: 10px">
      <div style="margin-bottom: 2%">
            <div class="desct">
              发送接收详情
            </div>
            <el-alert
              title="注意事项"
              type="warning"
              description="1、因企业微信接口限制，提醒成员完成群发任务，24小时内每个群发最多触发三次提醒,多点无效噢。2、成员选择客户发送之后，未选择的客户不能再次选择发送了"
              :closable="false"
              show-icon>
            </el-alert>
          </div>
          <div>
            <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="120px">
              <el-form-item label="成员姓名" prop="nickName">
                <el-input
                  v-model="queryParams.nickName"
                  placeholder="请输入想查询的成员姓名"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="客户名称" prop="name">
                <el-input
                  v-model="queryParams.name"
                  placeholder="请输入想查询的客户姓名"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="成员发送状态" prop="status">
                 <el-select v-model="queryParams.status" placeholder="请选择员工发送状态" >
                   <el-option v-for="item in groupMsgStatusOptions"
                              :key="item.dictValue"
                              :label="item.dictLabel"
                              :value="item.dictValue">
                   </el-option>
                 </el-select>
              </el-form-item>

              <el-form-item label="客户接收状态" prop="sendStatus">
                  <el-select v-model="queryParams.sendStatus" placeholder="请选择客户接收状态">
                    <el-option v-for="item in groupMsgSendStatusOptions"
                               :key="item.dictValue"
                               :label="item.dictLabel"
                               :value="item.dictValue">

                    </el-option>
                  </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-button type="warning" icon="el-icon-message-solid" size="mini" @click="triggerRemindGroupMsg">提醒成员发送</el-button>
                <el-button type="primary" icon="el-icon-refresh" size="mini" plain @click="refreshResults">刷新/获取企微结果</el-button>
              </el-form-item>
            </el-form>

            <el-table v-loading="loading" :data="groupMsgUserDetailsList">
              <el-table-column label="成员" align="center" prop="nickName" />
              <el-table-column label="成员发送状态" align="center" prop="status">
                <template slot-scope="scope">
                  <dict-tag :options="groupMsgStatusOptions" :value="scope.row.status"/>
                </template>
              </el-table-column>
              <el-table-column label="客户" align="center" prop="name" >
                <template slot-scope="scope">
                  <el-image
                    v-if="scope.row.avatar"
                    style="width: 20px; height: 20px"
                    :src="scope.row.avatar"
                    fit="contain"
                    @click="openImageViewer(scope.row.avatar)"
                    />
                  <span>{{scope.row.name}}</span>
                </template>
              </el-table-column>
              <el-table-column label="客户接收状态" align="center" prop="sendStatus" >
                <template slot-scope="scope">
                  <dict-tag :options="groupMsgSendStatusOptions" :value="scope.row.sendStatus"/>
                </template>
              </el-table-column>
            </el-table>
          </div>

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

    </el-card>

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

import { countGroupMsgUser, CountGroupMsgUserDetails, remindGroupMsg } from '@/api/qw/groupMsg'
import { refreshResultsMsgUser } from '@/api/qw/groupMsgUser'

export default {
  name: "Customer-group-details",
  components:{},

  props:{
    rowDetailFrom:{},
  },

  watch:{
    rowDetailFrom:{
      handler(newVal){
        // 当formData变化时重新查询
        this.getUserList(newVal);
        this.getCountGroupMsgUser(newVal.groupMsgId)
      },
      deep: true
    }
  },
  data() {
    return {
      //数据统计
      CountGroupMsgUserList: [],

      //成员详情
      groupMsgUserDetailsList:[],

      //成员发送状态
      groupMsgStatusOptions:[],

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


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

      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,

      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,


      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        qwMsgId:null,
        msgId:null,
        nickName:null,
        name:null,
        status:null,
        sendStatus:null,
        chatType:"single",

      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },

    };
  },
  created() {
      this.getUserList(this.rowDetailFrom)
      this.getCountGroupMsgUser(this.rowDetailFrom.id)

    //成员发送状态
    this.getDicts("sys_qw_groupMsg_status").then(response => {
      this.groupMsgStatusOptions = response.data;
    });

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


  },


  methods: {
    //获取成员详细
    getUserList(val){
      this.queryParams.msgId = val.id || this.rowDetailFrom.id;

      this.loading=false;
      CountGroupMsgUserDetails(this.queryParams).then(res=>{
            this.groupMsgUserDetailsList=res.rows
            this.total = res.total;
            this.loading = false;
      })

    },

    /** 提醒成员群发 */
    triggerRemindGroupMsg(){
      this.queryParams.qwMsgId=this.rowDetailFrom.msgId
      remindGroupMsg(this.queryParams).then(res=>{
        if (res.code==200){

          if (res.data.errcode==0){
            return this.msgSuccess("提醒成员群发成功")
          }else if (res.data.errcode==41094){
            return this.msgWarning("此条记录的每日提醒次数超过限制了噢")
          }else {
            return this.msgError(res.data.errmsg)
          }

        }else {
          return this.msgError(res.msg)
        }

      })
    },

    /** 刷新获取结果 */
    refreshResults(){

      let params = [];
      this.loading = true;

      //搞定定时任务导致的问题
      let ResultsMsgUser={
          id:this.rowDetailFrom.id,
          msgId:this.rowDetailFrom.msgId,
          chatType:this.rowDetailFrom.chatType,
          timerResultType:1,
          companyId:this.rowDetailFrom.companyId,
          corpId:this.rowDetailFrom.corpId,
      }

      // 根据不同条件选择参数
      if (this.groupMsgUserDetailsList == 0) {
        params.push(ResultsMsgUser)
      } else {
        params = this.groupMsgUserDetailsList// 如果 `groupMsgUserDetailsList` 是一个对象
      }

      console.log("params",params)
      refreshResultsMsgUser(params).then(res=>{
        this.loading=false;
        if (res.code==200){
          this.handleQuery();
          return this.msgSuccess(res.msg)
        }else {
          this.handleQuery();
          return this.msgError(res.msg)
        }

      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getUserList(this.rowDetailFrom);
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    openImageViewer(url) {
      // 打开大图预览对话框
      this.dialogImageUrl=url
      this.dialogVisible = true;
    },
    //数据详情
    getCountGroupMsgUser(val){
      countGroupMsgUser(val).then(res=>{
        this.CountGroupMsgUserList=Object.keys(res.data).map(key=>{
          return {label: key,count:res.data[key]}
        })
      })
    },

  }
};
</script>
<style scoped>
.desct{
  padding-bottom: 20px;
  color: #524b4a;
  font-weight: bold;
}
.statistics-container {
  padding: 20px;
}
.statistic-item {
  text-align: center;
}
.statistic-item h2 {
  font-size: 24px;
  margin: 0;
  font-weight: normal;
}
.statistic-item p {
  margin: 5px 0 0;
  color: #666;
  font-weight: normal;
}
</style>
