<template>
  <div style="background-color: rgb(240 242 245);margin: 15px">
    <el-card class="box-card" style="margin-top: 10px">
      <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 CountGroupBaseMsgUserList" :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、发送给客户群的群结果，需要群主选择、确认发送后，才会有相关数据。2、提醒成员完成群发任务，24小时内每个群发最多触发三次提醒。3、成员选择群发送之后，未选择的群不能再次选择发送了"
                :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="chatName">
                <el-input
                  v-model="queryParams.chatName"
                  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>
                <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 icon="el-icon-refresh"  size="mini" type="warning" @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="chatName" >
              </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-column label="提醒群主群发" align="center"  >
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-message-solid"
                    @click="triggerRemindGroupMsg(scope.row)"
                  >提醒群主群发</el-button>
                </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="1200"
      append-to-body>
      <img
        :src="this.dialogImageUrl"
        style="display: block; max-width: 100%; margin: 0 auto"
      />
    </el-dialog>
  </div>
</template>
<script>

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

import {refreshResultsGroupMsgUser} from '@/api/qw/groupMsgUser'
export default {
  name: "customerBaseDetails",
  components:{},

  props:{
    rowDetailFrom:{},
  },

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

      //成员详情
      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,
        msgId:null,
        qwMsgId:null,
        nickName:null,
        chatName:null,
        name:null,
        status:null,
        sendStatus:null,
        chatType:"group",

      },
      // 表单参数
      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;
      console.log("rowDetailFrom,",this.rowDetailFrom)
      CountGroupMsgUserDetails(this.queryParams).then(res=>{
            this.groupMsgUserDetailsList=res.rows
            this.total = res.total;
            this.loading = false;
      })

    },

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

    /** 刷新/获取-结果 */
    refreshResults(){
      this.loading=true;
      refreshResultsGroupMsgUser(this.groupMsgUserDetailsList).then(res=>{
        this.loading=false;
        if (res.code==200){
          this.resetQuery
          this.msgSuccess(res.msg)
        }else {
          this.resetQuery
          this.msgError(res.msg)
        }

      })
    },
    openImageViewer(url) {
      // 打开大图预览对话框
      this.dialogImageUrl=url
      this.dialogVisible = true;
    },
    //数据详情
    getCountGroupMsgUser(val){

      CountGroupMsgBaseUser(val).then(res=>{
        this.CountGroupBaseMsgUserList=Object.keys(res.data).map(key=>{
          return {label: key,count:res.data[key]}
        })
      })
    },

    /** 提醒成员群发 */
    triggerRemindGroupMsg(row){
      this.queryParams.qwMsgId=row.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)
        }
      })
    },

  }
};
</script>
<style scoped>

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