<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" 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>
        <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-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          :disabled="chatIds.length === 0"
          @click="handleTransferOnJob"
          v-hasPermi="['qw:groupChatTransfer:transferOnJob']"
        >分配客户群</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="groupChatList" border @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <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="qwUserName" />
      <el-table-column label="群公告" align="center" prop="notice" show-overflow-tooltip/>
      <el-table-column label="企微主体ID" align="center" prop="corpId" />
      <el-table-column label="群创建时间" align="center" prop="createTime" />
    </el-table>

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

    <!-- 分配客户群 -->
    <el-dialog :title="chatDialog.title" :visible.sync="chatDialog.open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <div style="background-color: rgb(239, 250, 255); margin: 10px;padding: 15px;">
          <div>可将选中的客户群转接给其他员工，进行后续服务</div>
          <div>继承给的新群主，必须是配置了客户联系功能的成员</div>
          <div>继承给的新群主，必须有设置实名</div>
          <div>继承给的新群主，必须有激活企业微信</div>
          <div>同一个人的群，限制每天最多分配300个给新群主</div>
          <div>继承给的新群主和旧的群主，需要在最近一年内至少登陆过一次企业微信</div>
          <div>90个自然日内，在职成员的每个客户群仅可被转接2次</div>
        </div>
        <el-form-item label="新群主" prop="qwId">
          <el-input style="width: 150px" disabled>
            <template slot="prefix">
              <el-button
                plain
                size="small"
                type="success"
                v-if="this.form.nickName">
                {{ form.nickName }}
              </el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-card>
          <qwUserSelectOne ref="qwUserSelectOne" @selectUser="selectUser"></qwUserSelectOne>
        </el-card>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :disabled="chatDialog.save" :loading="chatDialog.save" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {getMyQwCompanyList} from '@/api/qw/user'
import {listOnJob,transferOnJob} from '@/api/qw/groupChatTransfer.js'
import qwUserSelectOne from '@/views/qw/user/qwUserSelectOne.vue'

export default {
  name: 'groupChatTransferOnJob',
  components: { qwUserSelectOne },
  data() {
    return {
      loading: false,
      total: 0,
      chatIds: [],
      queryParams: {
        corpId: null,
        name: null,
        ownerName: null,
        qwName: null,
        pageNum: 1,
        pageSize: 10
      },
      myQwCompanyList: [],
      groupChatList: [],
      chatDialog: {
        open: false,
        title: '分配客户群',
        save: false
      },
      form: {
        nickName: null,
        qwId: null,
      },
      rules: {
        qwId: [{ required: true, message: '请选择新群主', trigger: 'blur' }],
      }
    }
  },
  created() {
    this.getMyQwCompanyList();
  },
  methods: {
    getMyQwCompanyList() {
      getMyQwCompanyList().then(response => {
        this.myQwCompanyList = response.data;
        if(this.myQwCompanyList!=null){
          this.queryParams.corpId=this.myQwCompanyList[0].dictValue
          this.getList();
        }
      });
    },
    updateCorpId() {
      this.handleQuery()
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    getList() {
      this.loading = true;
      listOnJob(this.queryParams).then(response => {
        this.groupChatList = response.rows;
        this.total = response.total;
        this.loading = false;
      })
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.corpId= this.myQwCompanyList[0].dictValue
      this.handleQuery();
    },
    handleSelectionChange(selection) {
      this.chatIds = selection.map(item => {
        return {
          chatId: item.chatId,
          owner: item.owner,
          qwId: item.qwId
        }
      })
    },
    handleTransferOnJob() {
      if(this.chatIds.length ===0){
        this.$message({
          message: '请选择需要分配的客户群',
          type: 'warning'
        });
        return;
      }
      setTimeout(() => {
        this.$refs.qwUserSelectOne.getDetails(this.queryParams.corpId);
      }, 1);
      this.reset();
      this.chatDialog.open = true;
    },
    selectUser(row) {
      this.form.nickName = row.qwUserName;
      this.form.qwId = row.id;
    },
    submitForm() {
      this.chatDialog.save = true;
      this.$refs["form"].validate(valid => {
        if (valid) {
          const form = {
            chatIds: this.chatIds,
            qwId: this.form.qwId,
            corpId: this.queryParams.corpId
          }
          transferOnJob(form).then(response => {
            this.$message.success({
              message: response.msg,
              dangerouslyUseHTMLString: true
            });
            this.chatDialog.open = false;
            this.getList();
          });
        }
        this.chatDialog.save = false;
      });
    },
    cancel() {
      this.chatDialog.open = false;
      this.reset();
    },
    reset() {
      this.form = {
        nickName: null,
        qwId: null,
      };
      this.resetForm("form");
    },
  }
}
</script>

<style scoped>

</style>
