<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="3" :xs="24">
        <div class="head-container">
          <el-input v-model="deptName" placeholder="请输入部门名称" clearable size="small" prefix-icon="el-icon-search" style="margin-bottom: 20px" />
        </div>
        <div class="head-container">
          <el-tree :data="deptOptions" :props="defaultProps" :expand-on-click-node="false" :filter-node-method="filterNode" ref="tree" @node-click="handleNodeClick" />
        </div>
      </el-col>
      <!--群聊数据-->
      <el-col :span="21" :xs="24">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="企微公司" prop="corpId">
          <el-select v-model="queryParams.corpId" placeholder="企微公司" size="small" @change="updateCorpId()">
            <el-option
              v-for="dict in myQwCompanyList"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
      </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="ownerName">
        <el-input
          v-model="queryParams.ownerName"
          placeholder="请输入群主名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="群主(企微)" prop="qwName">
        <el-input
          v-model="queryParams.qwName"
          placeholder="请输入群主名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="群公告" prop="notice">
        <el-input
          v-model="queryParams.notice"
          placeholder="请输入群公告"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="群跟进状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择客户群跟进状态" clearable size="small">
          <el-option label="请选择状态"
                     v-for="dict in groupChatStatusOptions"
                      :label="dict.dictLabel"
                      :key="dict.dictValue"
                      :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-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="groupChatList" border>
      <el-table-column label="群名" align="center" prop="name" >
        <template slot-scope="scope">
          <i class="el-icon-s-comment"></i>
          {{scope.row.name}}
        </template>
      </el-table-column>
      <el-table-column label="群主(后台)" align="center" prop="ownerName" />
      <el-table-column label="群主(企微)" align="center" prop="qwName" />
      <el-table-column label="群公告" align="center" prop="notice" />
      <el-table-column label="群跟进状态" align="center" prop="status" >
        <template slot-scope="scope">
          <span v-for="(item,index) in groupChatStatusOptions" v-if="scope.row.status==item.dictValue">{{item.dictLabel}}</span>
        </template>
      </el-table-column>
      <el-table-column label="群人数" align="center" prop="groupSizeByChartUser"></el-table-column>
      <el-table-column label="当日入群数" align="center" prop="todayJoinByChartUser"></el-table-column>
      <el-table-column label="群创建时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">

          <el-button
            size="mini"
            type="text"
            icon="el-icon-notebook-2"
            @click="hangdleDitels(scope.row)"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>

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

    <el-drawer :title="title" :visible.sync="open" width="100%" size="85%">
      <group_chat_user ref="GroupChatUser" :rowDetailFrom="this.form"></group_chat_user>
    </el-drawer>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import {
  listGroupChat,
  getGroupChat,
  cogradientGroupChat
} from '@/api/qw/groupChat'
import Group_chat_user from '@/views/qw/groupChatUser/groupChatUser'
import { getMyQwUserList,getMyQwCompanyListAll } from "@/api/qw/user";
import { treeselect } from "@/api/company/companyDept";

export default {
  name: "GroupChat",
  components: { Group_chat_user },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      myQwCompanyList:[],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户群详情表格数据
      groupChatList: [],
      //客户群跟进状态
      groupChatStatusOptions:[],
      // 部门树选项
      deptOptions: "",
      // 部门名称
      deptName: "",
      // 部门树props
      defaultProps: {
        children: "children",
        label: "label",
      },

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

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        owner: null,
        ownerName: null,
        qwName: null,
        notice: null,
        memberVersion: null,
        status: null,
        companyId: null,
        corpId: null,
        deptId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    },
  },
  created() {
    this.getTreeselect();
    getMyQwCompanyListAll().then(response => {
            this.myQwCompanyList = response.data;
            if(this.myQwCompanyList!=null){
              this.queryParams.corpId=this.myQwCompanyList[0].dictValue
              this.getList();
            }
     });
    //群跟进状态字典
    this.getDicts("sys_qw_groupChat_status").then(response => {
      this.groupChatStatusOptions = response.data;
    });
  },
  methods: {

     updateCorpId(){
         this.getList();
      },

    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then((response) => {
        this.deptOptions = response.data;
      });
    },

    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },

    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.deptId = data.id;
      this.queryParams.companyId = data.companyId;
      this.getList();
    },

    /** 查询客户群列表 */
    getList() {
      this.loading = true;
      listGroupChat(this.queryParams).then(response => {
        this.groupChatList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    //同步客户群（暂用）
    getCogradientGroupChat(){
      this.loading = true;
      cogradientGroupChat(this.queryParams.corpId).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 = {
        chatId: null,
        id: null,
        name: null,
        owner: null,
        notice: null,
        memberVersion: null,
        status: "0",
        companyId: null,
        corpId: null,
        createTime: null,
        updateTime: null,
        quitScene:null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    //详情
    hangdleDitels(row){

      this.reset();
      this.open = true;
      this.title = "客户群详情";
      this.form=row;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.corpId= this.myQwCompanyList[0].dictValue
      this.handleQuery();
    },

  }
};
</script>
