<template>
  <div>
    <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="type">
        <el-select v-model="queryParams.type" placeholder="请选择联系方式类型" clearable size="small">
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否免验证" prop="skipVerify">
        <el-select v-model="queryParams.skipVerify" placeholder="是否无需验证" clearable size="small">
          <el-option
            v-for="dict in isOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :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-form-item>
    </el-form>
    <el-table v-loading="loading" :data="contactWayList" @selection-change="handleSelectionChange" border>
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="二维码" align="center" prop="qrCode" width="150px" >
        <template slot-scope="scope">
          <el-popover
            placement="right"
            title=""
            trigger="hover">
            <img slot="reference" :src="scope.row.qrCode" width="100px">
            <img :src="scope.row.qrCode" style="max-width: 350px;">
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="typeOptions" :value="scope.row.type"/>
        </template>
      </el-table-column>

      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="是否免验证" align="center" prop="skipVerify">
        <template slot-scope="scope">
          <dict-tag :options="isOptions" :value="scope.row.skipVerify"/>
        </template>
      </el-table-column>
      <el-table-column label="state参数" align="center" prop="state" />
      <el-table-column label="用户" align="center" prop="userIds" width="200px">
        <template slot-scope="scope">
          <div v-for="i in JSON.parse(scope.row.userIds)" :key="i" style="display: inline;">
            <el-tag type="success" v-for="ii in companyUserList" :key="ii.id" style="margin: 3px;" v-if="ii.qwUserId==i">{{ii.qwUserName}}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="添加人数" align="center" prop="addNum" />
      <el-table-column label="删除人数" align="center" prop="deleteNum" />
      <el-table-column label="净人数" align="center" prop="num" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="170px">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleSelect(scope.row)"
          >选择</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination style="margin: 10px;"
                v-show="total>0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"
    />
   </div>
</template>

<script>
import { listContactWay } from "@/api/qw/contactWay";
import {getQwAllUserList } from "@/api/company/companyUser";
import { getMyQwCompanyList } from "@/api/qw/user";
export default {
  name: "ContactWay",
  data() {
    return {
      myQwCompanyList:[],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      userIds:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企微活码表格数据
      contactWayList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 联系方式类型,1-单人, 2-多人字典
      typeOptions: [],

      isOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        type: null,
        skipVerify: null,
        state: null,
        userIds: null,
        addNum: null,
        deleteNum: null,
        num: null,
        qrCode: null,
        configId: null,
        isDel: 0,
        companyId: null,
        groupId: null,
      },
      // 表单参数
      form: {

      },
      // 是否发送欢迎语字典
      companyUserList:[],
    };
  },
  created() {
    getMyQwCompanyList().then(response => {
        this.myQwCompanyList = response.data;
        if(this.myQwCompanyList!=null){
          this.queryParams.corpId=this.myQwCompanyList[0].dictValue;
          getQwAllUserList(this.queryParams.corpId).then(response => {
              this.companyUserList = response.data;
          });
          this.getList();
        }
    });
    this.getDicts("sys_qw_contact_way_type").then(response => {
      this.typeOptions = response.data;
    });
    this.getDicts("sys_company_or").then(response => {
      this.isOptions = response.data;
    });




  },
  methods: {
    handleSelect(row){
      this.$emit('selectWay',row);
    },
    /** 查询企微活码列表 */
    getList() {
      this.loading = true;
      listContactWay(this.queryParams).then(response => {
        this.contactWayList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
  }
};
</script>
<style scoped>
  .avatar-uploader .el-upload {
     border: 1px dashed #d9d9d9;
     border-radius: 6px;
     cursor: pointer;
     position: relative;
     overflow: hidden;
   }
   .avatar-uploader .el-upload:hover {
     border-color: #409EFF;
   }

   .avatar-uploader-icon {
     font-size: 28px;
     color: #8c939d;
     width: 150px;
     height: 150px;
     line-height: 150px;
     text-align: center;
   }
</style>
<style scoped>
/* CSS 样式 */
.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;
}



.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>
