<template>
  <el-card style="background-color: rgb(240 242 245)">
  <div class="app-container">
    <div style="display: flex; flex-wrap: nowrap; justify-content: space-between; align-items: stretch; width: 100%; padding: 0 20px;">
      <div style="background-color: #34bfa3; width: calc(25% - 40px); height: 130px; margin: 0 20px 20px;">
        <div class="topBlockStyle">
          <span>今日新增成员数</span>
        </div>
        <div class="topBlockStyle">
          <span>{{this.rowDetailFrom.todayJoinByChartUser}}</span>
        </div>
      </div>
      <div style="background-color: lightsalmon; width: calc(25% - 40px); height: 130px; margin: 0 20px 20px;">
        <div class="topBlockStyle">
          <span>今日退群成员数</span>
        </div>
        <div class="topBlockStyle">
          <span>{{this.rowDetailFrom.todayOutByChartUser}}</span>
        </div>
      </div>
      <div style="background-color: #ed5565; width: calc(25% - 40px); height: 130px; margin: 0 20px 20px;">
        <div class="topBlockStyle">
          <span>当前群成员总数</span>
        </div>
        <div class="topBlockStyle">
          <span>{{this.rowDetailFrom.groupSizeByChartUser}}</span>
        </div>
      </div>
      <div style="background-color: #7171C6; width: calc(25% - 40px); height: 130px; margin: 0 20px 20px;">
        <div class="topBlockStyle">
          <span>累计退群成员数</span>
        </div>
        <div class="topBlockStyle">
          <span>{{this.rowDetailFrom.allOutGroup}}</span>
        </div>
      </div>
    </div>

    <div style="margin: 20px">
      <el-radio-group v-model="queryParams.countType" @input="changeLineType">
        <el-radio-button :label="item.dictValue" v-for="item in groupChatUserDayType" style="padding-left:20px"  >{{item.dictLabel}}</el-radio-button>
      </el-radio-group>
      <div v-if="queryParams.countType==1" style="margin:20px 0px">
        <el-date-picker
          v-model="hospitalOrderScreenCharge"
          type="daterange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions"
          :default-time="['00:00:00', '23:59:59']"
          @change="changeLine">
        </el-date-picker>
      </div>
      <div></div>
      <div></div>
    </div>
    <div id="lineChart" style="width: 100%;height: 500px"/>

  </div >
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="120px">

      <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="isOut">
        <el-select v-model="queryParams.isOut" placeholder="请选择群员状态">
          <el-option v-for="item in groupChatUserIsOutType"
                     :key="item.dictValue"
                     :label="item.dictLabel"
                     :value="item.dictValue">

          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item style="float: right">
        <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-magic-stick" size="mini" @click="getCogradientGroupChat">同步此客户群</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="group_chat_userList">
      <el-table-column label="客户群ID" align="center" prop="chatId" />
      <el-table-column label="群成员账号" align="center" prop="userId" />
      <el-table-column label="成员类型" align="center" prop="type" >
        <template slot-scope="scope">
          <span v-for="(item,index) in  groupChatUserType" v-if="scope.row.type==item.dictValue">{{item.dictLabel}}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column label="外部联系人在微信开放平台的唯一身份标识" align="center" prop="unionid" />-->
      <el-table-column label="入群时间" align="center" prop="joinTime" width="180"/>
      <el-table-column label="入群方式" align="center" prop="joinScene">
        <template slot-scope="scope">
          <span v-for="(item,index) in groupChatUserScene" v-if="scope.row.joinScene==item.dictValue">{{item.dictLabel}}</span>
        </template>
      </el-table-column>
      <el-table-column label="邀请者" align="center" prop="invitorName" />
      <!--      <el-table-column label="客户在群里的昵称" align="center" prop="groupNickname" />-->
      <el-table-column label="名字" align="center" prop="name" />
      <el-table-column label="是否退群" align="center" prop="isOut" >
        <template slot-scope="scope">
          <dict-tag :options="groupChatUserIsOutType" :value="scope.row.isOut"/>
        </template>
      </el-table-column>
      <el-table-column label="退群时间" align="center" prop="outTime" />
      <el-table-column label="退群方式" align="center" prop="quitScene" >
        <template slot-scope="scope">
          <dict-tag :options="groupChatUserQuitSceneType" :value="scope.row.quitScene"/>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
  </el-card>
</template>

<script>
import {
  listGroup_chat_user,
  getGroup_chat_user,
  delGroup_chat_user,
  addGroup_chat_user,
  updateGroup_chat_user,
  getDataWeekMonthCount,
  cogradientGroupChatUser
} from '@/api/qw/group_chat_user'
import * as echarts from 'echarts'
import { cogradientGroupChat } from '@/api/qw/groupChat'

export default {
  name: "Group_chat_user",
  components:{},

  props:{
    rowDetailFrom:{},
  },

  watch:{
    rowDetailFrom:{
      handler(newVal){
        // 当formData变化时重新查询
        this.getList(newVal);
        this.lineChart(newVal);
      },
      deep: true
    }
  },
  data() {
    return {
      //折线图
      toDay:[],
      dailyJoinCount:[],
      dailyOutCount:[],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户群成员列表格数据
      group_chat_userList: [],
      //成员类型
      groupChatUserType:[],
      //是否退群
      groupChatUserIsOutType:[],

      //入群方式
      groupChatUserScene:[],

      //退群方式
      groupChatUserQuitSceneType:[],

      //日期查询类型
      groupChatUserDayType:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        chatId: null,
        userId: null,
        type: null,
        unionid: null,
        joinTime: null,
        joinScene: null,
        invitor: null,
        invitorUserid: null,
        groupNickname: null,
        name: null,
        companyId: null,
        corpId: null,
        isAdmin: null,
        beginTime:null,
        endTime:null,
        countType:null,
        isOut:null,
        quitScene:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      //日期快捷选择
      pickerOptions: {
        shortcuts: [
          {
            text: '最近三天',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 3);
              picker.$emit('pick', [start, end]);
            }
          },{
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }]
      },

      //日期选择
      hospitalOrderScreenCharge:[],
    };
  },
  created() {
    this.getList(this.rowDetailFrom)
    this.lineChart(this.rowDetailFrom)
    //成员类型字典
    this.getDicts("sys_qw_groupChat_user_type").then(response => {
      this.groupChatUserType = response.data;
    });

    //入群方式字典
    this.getDicts("sys_qw_groupChat_user_scene").then(response => {
      this.groupChatUserScene = response.data;
    });
    this.getDicts("sys_qw_groupChat_user_day_type").then(response => {
      this.groupChatUserDayType = response.data;
    });

    //是否退群
    this.getDicts("sys_qw_groupCharUser_isOut").then(response => {
      this.groupChatUserIsOutType = response.data;
    });

    //是否退群
    this.getDicts("sys_qw_groupChar_quitScene").then(response => {
      this.groupChatUserQuitSceneType = response.data;
    });

  },


  methods: {
    /** 查询客户群详细信息 */

    getList:function(newVal) {

      if (newVal && typeof newVal === 'object') {
        this.queryParams.chatId = newVal.chatId || this.rowDetailFrom.chatId;
      } else {
        this.queryParams.chatId = this.rowDetailFrom.chatId;
      }

      this.loading = true
      listGroup_chat_user(this.queryParams).then(response => {
        this.group_chat_userList = response.rows
        this.total = response.total
        this.loading = false
      })

    },
    //折线图初始化
    lineChart:function(newVal){
      this.resetLineChart();
      this.queryParams.chatId=newVal.chatId;
      //默认按星期
      this.queryParams.countType=2
      getDataWeekMonthCount(this.queryParams).then(response=>{
        for (const  key in response.data){
          this.toDay.push(response.data[key]["toDay"])
          this.dailyJoinCount.push(response.data[key]["dailyJoinCount"])
          this.dailyOutCount.push(response.data[key]["dailyOutCount"])
        }
        setTimeout(() => {
          this.initEchart();
        }, 100);
      })

    },

    initEchart(){
      const option = {
        title: {
          text: '时间段内新增成员，退群成员表'
        },
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          data: ['入群人员数', '退群成员数']
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        toolbox: {
          feature: {
            saveAsImage: {}
          }
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: this.toDay
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            name: '入群人员数',
            type: 'line',
            stack: 'Total',
            data: this.dailyJoinCount
          },
          {
            name: '退群成员数',
            type: 'line',
            stack: 'Total',
            data: this.dailyOutCount
          },
        ]
      }
      const lineChart = echarts.init(document.getElementById('lineChart'))
      lineChart.setOption(option,true);
    },

    //折线图
    changeLine(){
      this.resetLineChart();

      if (this.hospitalOrderScreenCharge!=null) {
        this.queryParams.beginTime=this.hospitalOrderScreenCharge[0]
        this.queryParams.endTime=this.hospitalOrderScreenCharge[1]
        getDataWeekMonthCount(this.queryParams).then(response=>{
          for (const  key in response.data){
            this.toDay.push(response.data[key]["toDay"])
            this.dailyJoinCount.push(response.data[key]["dailyJoinCount"])
            this.dailyOutCount.push(response.data[key]["dailyOutCount"])
          }
          setTimeout(() => {
            this.initEchart();
          }, 100);
        })
      }
    },
    changeLineType(){
      this.resetLineChart();
      if (this.queryParams.countType==2){
        this.queryParams.countType=2
        getDataWeekMonthCount(this.queryParams).then(response=>{
          for (const  key in response.data){
            this.toDay.push(response.data[key]["toDay"])
            this.dailyJoinCount.push(response.data[key]["dailyJoinCount"])
            this.dailyOutCount.push(response.data[key]["dailyOutCount"])
          }
          setTimeout(() => {
            this.initEchart();
          }, 100);
        })
      }
      if (this.queryParams.countType==3){
        this.queryParams.countType=3
        getDataWeekMonthCount(this.queryParams).then(response=>{
          for (const  key in response.data){
            this.toDay.push(response.data[key]["toDay"])
            this.dailyJoinCount.push(response.data[key]["dailyJoinCount"])
            this.dailyOutCount.push(response.data[key]["dailyOutCount"])
          }
          setTimeout(() => {
            this.initEchart();
          }, 100);
        })
      }
    },

    //同步某个客户群（暂用）
    getCogradientGroupChat(){
      this.loading = true;
      cogradientGroupChatUser(this.rowDetailFrom).then(response=>{
        if (response.code == 200) {
          this.msgSuccess("同步成功");
          this.getList();
        } else {
          this.msgError(response.msg);
        }
        this.loading = false;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        chatId: null,
        userId: null,
        type: null,
        unionid: null,
        joinTime: null,
        joinScene: null,
        invitor: null,
        invitorUserid: null,
        groupNickname: null,
        name: null,
        companyId: null,
        corpId: null,
        isAdmin: null
      };
      this.resetForm("form");
    },

    //折线图重置
    resetLineChart(){
      this.toDay=[];
      this.dailyJoinCount=[];
      this.dailyOutCount=[];
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

  }
};
</script>
<style>
  .topBlockStyle{
    margin: 30px;
    color: white;
    font-size: 20px
  }
</style>
