<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="企业CorpID" prop="corpId">
        <el-input
          v-model="queryParams.corpId"
          placeholder="请输入企业CorpID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="企业名称" prop="corpName">
        <el-input
          v-model="queryParams.corpName"
          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
            v-for="dict in statusOptions"
            :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-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['qw:qwCompany:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['qw:qwCompany:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['qw:qwCompany:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['qw:qwCompany:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="qwCompanyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="企业CorpID" align="center" prop="corpId" />
      <el-table-column label="企业名称" align="center" prop="corpName" />
      <el-table-column label="关联公司" align="center" prop="companyIds" >
        <template slot-scope="scope">
          <div v-for="company in (scope.row.companyIds).split(',') ">
            <span v-for="com in companys" v-if="com.companyId==company">{{com.companyName }} </span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="statusOptions" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180"/>
      <el-table-column label="更改时间" align="center" prop="updateTime" width="180"/>
      <el-table-column label="创建人" align="center" prop="createBy" />
      <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-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['qw:qwCompany:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['qw:qwCompany:remove']"
          >删除</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-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="200px">
        <el-form-item label="企业CorpID" prop="corpId">
          <el-input v-model="form.corpId" placeholder="请输入企业CorpID" />
        </el-form-item>
        <el-form-item label="企业名称" prop="corpName">
          <el-input v-model="form.corpName" placeholder="请输入企业名称" />
        </el-form-item>
        <el-form-item label="服通讯录Secret" prop="serverBookSecret">
          <el-input v-model="form.serverBookSecret" placeholder="请输入服务商的通讯录Secret" />
        </el-form-item>
       <el-form-item label="应用Secret" prop="openSecret">
          <el-input v-model="form.openSecret" placeholder="请输入服务商应用的Secret" />
        </el-form-item>
        <!-- <el-form-item label="服务商应用的企业id" prop="openCorpId">
          <el-input v-model="form.openCorpId" placeholder="请输入服务商应用的企业id" />
        </el-form-item>
        <el-form-item label="服务商应用serverAgentId" prop="serverAgentId">
          <el-input v-model="form.serverAgentId" placeholder="请输入服务商应用serverAgentId" />
        </el-form-item>
        <el-form-item label="服务商通讯录企业id" prop="serverBookCorpId">
          <el-input v-model="form.serverBookCorpId" placeholder="请输入服务商通讯录企业id" />
        </el-form-item>

        <el-form-item label="通用开发参数" prop="providerSecret">
          <el-input v-model="form.providerSecret" placeholder="请输入通用开发参数" />
        </el-form-item> -->
        <el-form-item label="Token" prop="token" >
          <el-input v-model="form.token" placeholder="请输入Token" :readonly="true"/>
        </el-form-item>
        <el-form-item label="EncodingAESKey" prop="encodingAesKey" >
          <el-input v-model="form.encodingAesKey" placeholder="请输入EncodingAESKey" :readonly="true"/>
        </el-form-item>

        <el-form-item label="域名地址" prop="realmNameUrl">
          <el-input v-model="form.realmNameUrl" placeholder="请输入域名地址" />
        </el-form-item>

        <el-form-item label="应用回调地接" prop="notifyUrl" >
          <el-input v-model="form.notifyUrl" placeholder="请输入应用回调地接" :readonly="true"/>
        </el-form-item>
        <el-form-item label="应用id" prop="serverAgentId" >
          <el-input v-model="form.serverAgentId" placeholder="请输入serverAgentId" />
        </el-form-item>
        <el-form-item label="小程序id" prop="miniAppId" >
          <el-select
            v-model="form.miniAppId"
            placeholder="请选择"
            sizell"
          >
            <el-option
              v-for="item in miniAppList"
              :key="item.appId"
              :label="item.appName"
              :value="item.appId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="企业可信IP" >
          <div>42.194.245.189;119.29.195.254;129.204.130.233;43.138.187.210;129.204.76.229;159.75.239.132119.29.249.66;122.152.230.82</div>
        </el-form-item>
        <el-form-item label="聊天工具栏跳转地址" prop="chatToolbar">
          <el-input v-model="form.chatToolbar" placeholder="请输入聊天工具栏跳转地址" />
        </el-form-item>
        <el-form-item label="聊天工具栏实际运用地址" prop="chatToolbarOauth">
          <el-input v-model="form.chatToolbarOauth" placeholder="请输入聊天工具栏实际运用地址" />
        </el-form-item>
        <el-form-item label="关联公司" prop="companyIds">

          <el-select filterable  v-model="form.companyIds" remote multiple placeholder="请选择公司名"  clearable style="width: 50%;">
                <el-option
                  v-for="item in companys"
                  :key="item.companyId"
                  :label="item.companyName"
                  :value="item.companyId"
                />
          </el-select>


        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listQwCompany, getQwCompany, delQwCompany, addQwCompany, updateQwCompany, exportQwCompany } from "@/api/qw/qwCompany";
import { getCompanyList } from "@/api/company/company";
import { listAll } from '@/api/course/coursePlaySourceConfig'
export default {
  name: "QwCompany",
  watch: {
      'form.realmNameUrl': function(newVal) {
        if(newVal==null){
          this.form.notifyUrl="输入域名地址生成";
        }else{
           this.form.notifyUrl = "http://"+newVal+"/qw/data/"+this.form.corpId;
        }

      },
      'form.corpId': function(newVal) {
          if (this.form.realmNameUrl !== null) {
            this.form.notifyUrl = this.form.realmNameUrl + "/qw/data/" + newVal;
          }
        }
    },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企微主体表格数据
      qwCompanyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        corpId: null,
        corpName: null,
        openSecret: null,
        openCorpId: null,
        serverAgentId: null,
        serverBookCorpId: null,
        serverBookSecret: null,
        token: null,
        encodingAesKey: null,
        providerSecret: null,
        realmNameUrl: null,
        notifyUrl: null,
        chatToolbar: null,
        chatToolbarOauth: null,
        companyIds: null,
        status: null,
      },
      // 表单参数
      form: {},
      companys:[],
      maAppList:[],
      miniAppList:[],
      customAppList:[],
      // 表单校验
      rules: {
        corpId: [{ required: true, message: '请输入企业CorpID', trigger: 'blur' }],
        realmNameUrl: [{ required: true, message: '请输入域名地址', trigger: 'blur' }],
        corpName: [{ required: true, message: '请输入名称', trigger: 'blur' }],

      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_company_status").then(response => {
      this.statusOptions = response.data;
    });
    getCompanyList().then(response => {
        this.companys = response.data;

    });
  },
  methods: {
    /** 查询企微主体列表 */
    getList() {
      this.loading = true;
      listQwCompany(this.queryParams).then(response => {
        this.qwCompanyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        corpId: null,
        corpName: null,
        openSecret: null,
        openCorpId: null,
        serverAgentId: null,
        serverBookCorpId: null,
        serverBookSecret: null,
        token: null,
        encodingAesKey: null,
        providerSecret: null,
        realmNameUrl: null,
        notifyUrl: null,
        chatToolbar: null,
        chatToolbarOauth: null,
        companyIds: null,
        status: 0,
        createTime: null,
        updateTime: null,
        createBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    getAppList() {
      this.maAppList = []
      this.miniAppList = []
      this.customAppList = []
      listAll().then(response => {
        const { code, data } = response
        if (code === 200) {
          if (data) {
            this.maAppList = data.filter(v => v.type == 2).map(v => {
              return { appId: v.appid, appName: v.name }
            })
            this.miniAppList = data.filter(v => v.type == 1).map(v => {
              return { appId: v.appid, appName: v.name }
            })
            this.customAppList = data.filter(v => v.type == 3).map(v => {
              return { appId: v.appid, appName: v.name }
            })
          }
        }
      })
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getAppList()
      this.open = true;
      this.form.token="1o62d3YxvdHd4LEUiltnu7sK";
      this.form.encodingAesKey="UJfTQ5qKTKlegjkXtp1YuzJzxeHlUKvq5GyFbERN1iU";
      this.title = "添加企微主体";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getAppList()
      const id = row.id || this.ids
      getQwCompany(id).then(response => {
        this.form = response.data;
        this.form.companyIds=((this.form.companyIds).split(",")).map(Number)
        this.open = true;
        this.title = "修改企微主体";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          console.log(this.form.companyIds)
           this.form.companyIds = (this.form.companyIds).join(',');
          if (this.form.id != null) {
            updateQwCompany(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addQwCompany(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除企微主体编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delQwCompany(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有企微主体数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportQwCompany(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
