<%--
  Created by IntelliJ IDEA.
  User: wuxuan
  Date: 2024/4/9
  Time: 16:10
  To change this template use File | Settings | File Templates.
--%>
<%@ page pageEncoding="UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ include file="../common/common.jsp" %>
<%
    String path = request.getContextPath();
%>
<c:set var="basePage">
    <%=request.getContextPath() %>
</c:set>
<html>
<head>
    <link type="text/css" rel="stylesheet" href="<%=path%>/css/base.css"/>
    <link type="text/css" rel="stylesheet" href="<%=path%>/css/layout.css"/>
    <link rel="stylesheet" type="text/css" href="<%=path%>/css/select2.min.css">
    <link href="<%=path%>/css/style.css" rel="stylesheet"/>
    <link href="<%=path%>/css/zTreeStyle.css" rel="stylesheet"/>
    <link href="<%=path%>/css/zoomify.min.css?a=2" rel="stylesheet" type="text/css"/>
    <script src="<%=path%>/js/jquery.js"></script>
    <script src="<%=path%>/js/jquery-1.10.2.min.js"></script>
    <script src="<%=path%>/js/ui.js"></script>
    <script type="text/javascript" src="<%=path%>/js/select2.min.js"></script>
    <script src="<%=path%>/js/provinceCitys.js" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript" src="<%=path%>/js/jquery.form.js"></script>
    <script type="text/javascript" src="<%=path%>/js/common.js"></script>
    <script type="text/javascript" src="<%=path%>/js/dialog/lhgdialog.min.js?self=true&skin=discuz"></script>
    <script type="text/javascript" src="<%=path%>/js/zoomify.min.js"></script>
    <script type="text/javascript" src="<%=path%>/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="<%=path%>/js/datePicker/WdatePicker.js"></script>
    <link rel="stylesheet" type="text/css" href="${basePage}/web/chatbot/static/css/noSignConfig.css">
    <title>代理商添加</title>
</head>
<body>

<div class="page_container" id="app">
    <div class="page_head">
        <h3>代理商配置详情</h3>
        <div class="breadcrumb">
            <a href="#">首页</a> > <a href="#">CSP业务</a> > 代理商配置详情
        </div>
    </div>
    <div class="row">
        <div class="col-xs-12">
            <div class="panel">
                <div class="container2">
                    <div class="main_p20">
                        <div>
                            <div class="infoShow">
                                <h3 class="info_tit">基本信息</h3>
                                <table width="100%" border="0" cellspacing="0" cellpadding="0" class="infotable">
                                    <tr>
                                        <th scope="row">
                                            <div class="floatLeft require">接口类型</div>
                                        </th>
                                        <td colspan="4">
                                            <label>
                                                <select name="apiType" class="form-control form_white"
                                                        v-model="config.apiType" @change="handleApiTypeChange" :disabled="isEditMode">
                                                    <option value="">请选择</option>
                                                    <option value="mobile5G">中国移动接口5G消息</option>
                                                    <option value="telecom5G">中国电信接口5G消息</option>
                                                    <option value="unicom5G">中国联通接口5G消息</option>
                                                </select>
                                            </label>
                                        </td>
<%--                                        <th scope="row">--%>
<%--                                            <div class="floatLeft require">状态</div>--%>
<%--                                        </th>--%>
<%--                                        <td colspan="2">--%>
<%--                                            <div class="checkbox-group">--%>
<%--                                                <label>--%>
<%--                                                    <input v-model="config.status" type="radio" name="switch"--%>
<%--                                                           value="1"> 开启--%>
<%--                                                </label>--%>
<%--                                                <label>--%>
<%--                                                    <input v-model="config.status" type="radio" name="switch"--%>
<%--                                                           value="0"> 关闭--%>
<%--                                                </label>--%>
<%--                                            </div>--%>
<%--                                        </td>--%>
                                    </tr>
                                    <template  v-if="!config.apiType || config.apiType === 'mobile5G'">
                                        <tr>
                                            <th scope="row">
                                                <div class="floatLeft require">CSP编码</div>
                                            </th>
                                            <td colspan="2">
                                                <label>
                                                    <input type="text" name="" class="form-control form_white"
                                                           :class="{'form_readonly': isEditMode}"
                                                           v-model="config.cspCode" :readonly="isEditMode">
                                                </label>
                                            </td>
                                            <th scope="row">
                                                <div class="floatLeft require">代理商编码</div>
                                            </th>
                                            <td colspan="2">
                                                <label>
                                                    <input type="text" name="" class="form-control form_white"
                                                           v-model="config.agentCode">
                                                </label>
                                            </td>
                                        </tr>


                                        <tr>
                                            <th scope="row">
                                                <div class="floatLeft require">代理商名称</div>
                                            </th>
                                            <td colspan="2">
                                                <label>
                                                    <input type="text" class="form-control form_white"
                                                           v-model="config.agentName">
                                                </label>
                                            </td>
                                            <th scope="row">
                                                <div class="floatLeft require">归属地区编码</div>
                                            </th>
                                            <td colspan="2">
                                                <div class="checkbox-group">
                                                    <input type="text" readonly class="form-control form_white"
                                                           v-model="config.agentRegion">
                                                    <span style="color: red;font-size: 12px;margin-left: 10px">只读输入框,请在下面选择正确地区后点击生成值</span>
                                                </div>
                                            </td>
                                        </tr>
                                        <tr>
                                            <th scope="row">
                                                <div class="floatLeft require">可用服务代码</div>
                                            </th>
                                            <td colspan="2">
                                                <label>
                                                    <textarea style="height: 100px" class="form-control form_white"
                                                              v-model="config.serviceCode"></textarea>
                                                </label>
                                            </td>
                                            <th scope="row">
                                                <div class="floatLeft">生成归属地区编码</div>
                                            </th>
                                            <td colspan="2">
                                                <div class="more-select">
                                                    <div class="more-select" style="width: 25%">
                                                        分区:
                                                        <select v-model="regionCodeObj.bigRegion" @change="provinceOnchange"
                                                                class="form-control form_white">
                                                            <option value="">请选择</option>
                                                            <option v-for="(item,index) in bigRegions" :value="item.value + '-' + item.name"
                                                                    :key="'province' + index">{{item.name}}</option>
                                                        </select>
                                                    </div>
                                                    <div class="more-select" style="width: 25%">
                                                        省份:
                                                        <select v-model="regionCodeObj.province" @change="cityOnchange"
                                                                class="form-control form_white">
                                                            <option value="">请选择</option>
                                                            <option v-for="(item,index) in provinceRegions" :value="item.value + '-' + item.name"
                                                                    :key="'province' + index">{{item.name}}</option>
                                                        </select>
                                                    </div>
                                                    <div class="more-select" style="width: 25%">
                                                        城市:
                                                        <select v-model="regionCodeObj.city" class="form-control form_white">
                                                            <option value="">请选择</option>
                                                            <option v-for="(item,index) in cityRegions" :value="item.value + '-' + item.name"
                                                                    :key="'city' + index">{{item.name}}</option>
                                                        </select>
                                                    </div>
                                                    <button @click="buildRegionCode" type="button"
                                                            class="btn btn_blue btn_small">生成编码
                                                    </button>
                                                </div>
                                            </td>
                                        </tr>
                                    </template>
                                    <template v-else>
                                        <tr>
                                            <th scope="row">
                                                <div class="floatLeft require">CSP编码</div>
                                            </th>
                                            <td colspan="2">
                                                <label>
                                                    <input type="text" name="" class="form-control form_white"
                                                           v-model="config.cspCode">
                                                </label>
                                            </td>
                                            <th scope="row">
                                                <div class="floatLeft require">代理商名称</div>
                                            </th>
                                            <td colspan="2">
                                                <label>
                                                    <input type="text" class="form-control form_white"
                                                           v-model="config.agentName">
                                                </label>
                                            </td>
                                        </tr>
                                        <tr>
                                            <th scope="row">
                                                <div class="floatLeft require">可用服务代码</div>
                                            </th>
                                            <td colspan="4">
                                                <label>
                                                    <textarea style="height: 100px" class="form-control form_white"
                                                              v-model="config.serviceCode"></textarea>
                                                </label>
                                            </td>
                                        </tr>
                                    </template>
                                </table>

                                <h3 class="info_tit">接口配置</h3>

                                <table width="100%" border="0" cellspacing="0" cellpadding="0" class="infotable">
                                    <template  v-if="!config.apiType || config.apiType === 'mobile5G'">
                                        <tr>
                                            <th scope="row">
                                                <div class="floatLeft require">代理商APPID</div>
                                            </th>
                                            <td colspan="2">
                                                <label>
                                                    <input type="text" name="" class="form-control form_white"
                                                           v-model="config.agentAppid">
                                                </label>
                                            </td>
                                            <th scope="row">
                                                <div class="floatLeft require">代理商Token</div>
                                            </th>
                                            <td colspan="2">
                                                <label>
                                                    <input type="text" name="" class="form-control form_white"
                                                           v-model="config.agentToken">
                                                </label>
                                            </td>
                                        </tr>

                                        <tr>
                                            <th scope="row">
                                                <div class="floatLeft require">代理商公钥</div>
                                            </th>
                                            <td colspan="5">
                                                <label>
                                                    <input type="text" name="" v-model="config.agentPublickey"
                                                           class="form-control form_white full_input_width">
                                                </label>
                                            </td>
                                        </tr>

                                        <tr>
                                            <th scope="row">
                                                <div class="floatLeft require">平台APPID</div>
                                            </th>
                                            <td colspan="2">
                                                <label>
                                                    <input type="text" name="" class="form-control form_white"
                                                           v-model="config.platAppid">
                                                </label>
                                            </td>
                                            <th scope="row">
                                                <div class="floatLeft require">平台Token</div>
                                            </th>
                                            <td colspan="2">
                                                <label>
                                                    <input type="text" name="" class="form-control form_white"
                                                           v-model="config.platToken">
                                                </label>
                                            </td>
                                        </tr>

                                        <tr>
                                            <th scope="row">
                                                <div class="floatLeft require">平台公钥</div>
                                            </th>
                                            <td colspan="5">
                                                <div class="more-select">
                                                    <input type="text" name="" v-model="config.platPublickey" readonly
                                                           class="form-control form_white full_input_width">
<%--                                                    <button type="button" class="btn btn_green" style="margin-left: 10px"--%>
<%--                                                            @click="copyText('platPublickey')">点击复制--%>
<%--                                                    </button>--%>
                                                </div>
                                            </td>
                                        </tr>

                                        <tr>
                                            <th scope="row">
                                                <div class="floatLeft require">平台私钥</div>
                                            </th>
                                            <td colspan="5">
                                                <div class="more-select">
                                                    <input type="text" name="" v-model="config.platPrivatekey" readonly
                                                           class="form-control form_white full_input_width">
<%--                                                    <button type="button" class="btn btn_green" style="margin-left: 10px"--%>
<%--                                                            @click="copyText('platPrivatekey')">点击复制--%>
<%--                                                    </button>--%>
                                                </div>
                                            </td>
                                        </tr>
                                    </template>

                                    <th scope="row" v-if="config.apiType === 'telecom5G' || config.apiType === 'unicom5G'">
                                        <div class="floatLeft require">CSP密钥</div>
                                    </th>
                                    <td colspan="2" v-if="config.apiType === 'telecom5G' || config.apiType === 'unicom5G'">
                                        <label>
                                            <input type="text" class="form-control form_white"
                                                   v-model="config.cspKey">
                                        </label>
                                    </td>
                                    <th scope="row" v-if="config.apiType === 'telecom5G' || config.apiType === 'unicom5G'">
                                        <div class="floatLeft require">CSPToken</div>
                                    </th>
                                    <td colspan="2" v-if="config.apiType === 'telecom5G' || config.apiType === 'unicom5G'">
                                        <label>
                                            <input type="text" class="form-control form_white"
                                                   v-model="config.cspToken">
                                        </label>
                                    </td>

                                    <tr>
                                        <th scope="row">
                                            <div class="floatLeft require">请求地址</div>
                                        </th>
                                        <td colspan="2">
                                            <label>
                                                <input type="text" name="" class="form-control form_white"
                                                       v-model="config.requestUrl">
                                            </label>
                                        </td>
                                        <th scope="row">
                                            <div class="floatLeft">发送地址</div>
                                        </th>
                                        <td colspan="2">
                                            <label>
                                                <input type="text" name="" class="form-control form_white"
                                                       v-model="config.sendUrl">
                                            </label>
                                        </td>
                                    </tr>
                                    <template v-if="config.apiType === 'telecom5G' || config.apiType === 'unicom5G'">
                                        <tr>
                                            <th scope="row">
                                                <div class="floatLeft">回落层地址</div>
                                            </th>
                                            <td colspan="2">
                                                <label>
                                                    <input type="text" name="" class="form-control form_white"
                                                           v-model="config.backAddress">
                                                </label>
                                            </td>
                                            <th scope="row">
                                                <div class="floatLeft">文件上传地址</div>
                                            </th>
                                            <td colspan="2">
                                                <label>
                                                    <input type="text" name="" class="form-control form_white"
                                                           v-model="config.fileUploadAddress">
                                                </label>
                                            </td>
                                        </tr>
                                    </template>
                                    <tr>
                                        <th scope="row">
                                            <div class="floatLeft">回落上行通道</div>
                                        </th>
                                        <td colspan="4">
                                            <div class="fallback_list">
                                                <select name="saveType" class="form-control form_white singleList">
                                                    <option value="">请选择</option>
                                                    <option v-for="(item,index) in channels" :value="item.channelId"
                                                            :key="'channel' + index">
                                                        {{item.channelId}} {{item.channelName}}
                                                    </option>
                                                </select>

                                                <button @click="addFallBackConfig" type="button"
                                                        class="btn btn_blue btn_small">添加
                                                </button>
                                            </div>

                                            <div class="fallback_list" v-for="(item,index) in fallbackChannelConfig"
                                                 :key="'channelConfig' + index">
                                                <select name="saveType" class="form-control form_white"
                                                        v-model="item.value">
                                                    <option value="">请选择</option>
                                                    <option v-for="(item,index) in channels" :value="item.channelId"
                                                            :key="'channelOption2' + index">
                                                        {{item.channelId}} {{item.channelName}}
                                                    </option>
                                                </select>

                                                <button @click="removeFallBackConfig(index)" type="button"
                                                        class="btn btn_red btn_small">删除
                                                </button>
                                            </div>
                                        </td>
                                    </tr>
                                </table>
                                <template  v-if="config.apiType === 'telecom5G' || config.apiType === 'unicom5G'">
                                    <h3 class="info_tit">开发者配置</h3>
                                    <table width="100%" border="0" cellspacing="0" cellpadding="0" class="infotable">
                                        <tr>
                                            <th scope="row">
                                                <div class="floatLeft">开发者协议</div>
                                            </th>
                                            <td colspan="4">
                                                <label>
                                                    <select name="agreement" class="form-control form_white"
                                                            v-model="config.agreement">
                                                        <option value="">请选择</option>
                                                        <option value="1">HTTP</option>
                                                        <option value="2">HTTPS</option>
                                                    </select>
                                                </label>
                                            </td>
                                        </tr>
                                        <tr>
                                            <th scope="row">
                                                <div class="floatLeft">回调URL地址根目录</div>
                                            </th>
                                            <td colspan="4">
                                                <label>
                                                    <input type="text" name="url" class="form-control form_white"
                                                           v-model="config.url">
                                                </label>
                                            </td>
                                        </tr>
                                    </table>
                                </template>
                                <template  v-if="!config.apiType || config.apiType === 'mobile5G'">
                                    <h3 class="info_tit">阅信配置(单点登录)</h3>
                                    <table width="100%" border="0" cellspacing="0" cellpadding="0" class="infotable">
                                        <tr>
                                            <th scope="row">
                                                <div class="floatLeft">阅信公钥</div>
                                            </th>
                                            <td colspan="4">
                                                <div class="more-select">
                                                    <input type="text" name="" class="form-control form_white"
                                                           v-model="config.aimPublickey"
                                                           style="width: 85%">
                                                    <button type="button" class="btn btn_green" style="margin-left: 10px"
                                                            @click="copyText('aimPublickey')">点击复制
                                                    </button>
                                                </div>
                                            </td>
                                        </tr>
                                        <tr>
                                            <th scope="row">
                                                <div class="floatLeft">阅信私钥</div>
                                            </th>
                                            <td colspan="4">
                                                <div class="more-select">
                                                    <input type="text" name="" class="form-control form_white"
                                                           v-model="config.aimPrivatekey"
                                                           style="width: 85%">
                                                    <button type="button" class="btn btn_green" style="margin-left: 10px"
                                                            @click="copyText('aimPrivatekey')">点击复制
                                                    </button>
                                                </div>
                                            </td>
                                        </tr>
                                    </table>

                                    <h3 class="info_tit">初始信息</h3>
                                    <table width="100%" border="0" cellspacing="0" cellpadding="0" class="infotable">
                                        <tr>
                                            <th scope="row">
                                                <div class="floatLeft">审核人员</div>
                                            </th>
                                            <td colspan="2">
                                                <label>
                                                    <input type="text" name="" class="form-control form_white"
                                                           v-model="config.auditer">
                                                </label>
                                            </td>
                                            <th scope="row">
                                                <div class="floatLeft">审核意见</div>
                                            </th>
                                            <td colspan="2">
                                                <label>
                                                    <input type="text" name="" class="form-control form_white"
                                                           v-model="config.auditerMsg">
                                                </label>
                                            </td>
                                        </tr>
                                        <tr>
                                            <th scope="row">
                                                <div class="floatLeft">调试白名单</div>
                                            </th>
                                            <td colspan="4">
                                                <label>
                                                    <input type="text" name="" class="form-control form_white"
                                                           v-model="config.debugPhone"
                                                           placeholder="多个手机号用英文逗号隔开"
                                                           style="width: 85%">
                                                </label>
                                            </td>
                                        </tr>
                                        <tr>
                                            <th scope="row">
                                                <div class="floatLeft">扩展默认值</div>
                                            </th>
                                            <td colspan="4">
                                                <div class="small-input-container">
                                                    非直签客户<input name="" class="form-control form_white"
                                                                     v-model="config.extendNosign">位,
                                                    ChatBot<input name="" class="form-control form_white"
                                                                  v-model="config.extendChatbot">位
                                                </div>
                                            </td>
                                        </tr>
                                    </table>
                                </template>

                                <h3 class="info_tit">备注</h3>
                                <table width="100%" border="0" cellspacing="0" cellpadding="0" class="infotable">
                                    <tr>
                                        <th scope="row">
                                            <div class="floatLeft">备注信息</div>
                                        </th>
                                        <td colspan="4">
                                            <label>
                                                <textarea name="" class="form-control form_white"
                                                          v-model="config.remark"
                                                          style="width: 85%;height: 200px"></textarea>
                                            </label>
                                        </td>
                                    </tr>
                                </table>
                            </div>

                            <div class="col-lg-12 my-col" style="height: 120px">
                                <div class="btn_save_group">
                                    <button @click="saveConfig" type="button" class="btn btn_blue">确认保存</button>
                                    <button @click="goList" type="button" class="btn btn_red">返回</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>


</body>
<script type="text/javascript" src="${basePage}/js/layui/layui.js"></script>
<script type="text/javascript" src="${basePage}/js/vue.min.js"></script>
<script type="text/javascript" src="${basePage}/web/chatbot/static/js/csp_region.js"></script>
<script>
    const layer = layui.layer;

    $(function () {
        $(".singleList").select()
    })

    new Vue({
        el: '#app',
        data: function () {
            return {
                id: '',
                isEditMode: false,
                config: {
                    id: "",
                    agentAppid: "",
                    agentCode: "",
                    agentName: "",
                    agentPublickey: "",
                    agentRegion: "",
                    regionName: "",
                    agentToken: "",
                    aimPrivatekey: "",
                    aimPublickey: "",
                    apiType: "mobile5G",
                    auditer: "",
                    auditerMsg: "",
                    createTime: "",
                    cspCode: "",
                    debugPhone: "",
                    extendChatbot: "",
                    extendNosign: "",
                    platAppid: "",
                    platPrivatekey: "",
                    platPublickey: "",
                    platToken: "",
                    remark: "",
                    replyChannel: "",
                    requestUrl: "",
                    sendUrl: "",
                    serviceCode: "",
                    agreement: "",
                    url: "",
                    status: 0
                },
                fallbackChannelConfig: [],
                channels: [],
                regionCode: '',
                bigRegions: big_region,
                provinceRegions: [],
                cityRegions: [],
                regionCodeObj: {
                    bigRegion: '',
                    province: '',
                    city: ''
                }
            }
        },
        beforeMount() {
            const paramsMap = new URLSearchParams(location.search);
            this.getFallBackChannels();
            if (paramsMap.has('id')) {
                this.id = paramsMap.get('id')
                this.isEditMode = true
                this.loadConfig()
            } else {
                this.isEditMode = false
                this.initConfig()
            }
        },
        methods: {
            handleApiTypeChange() {
                const currentType = this.config.apiType;
                if (currentType === 'telecom5G' || currentType === 'unicom5G') {
                    this.config.agentCode = '';         // 代理商编码
                    this.config.agentRegion = '';       // 归属地区编码
                    this.config.regionName = '';        // 归属地区名称
                    this.config.agentAppid = '';        // 代理商APPID
                    this.config.agentToken = '';        // 代理商Token
                    this.config.agentPublickey = '';    // 代理商公钥
                    this.config.platAppid = '';         // 平台APPID
                    this.config.platToken = '';         // 平台Token
                    this.regionCodeObj = {
                        bigRegion: '',
                        province: '',
                        city: ''
                    };
                    this.provinceRegions = [];
                    this.cityRegions = [];
                }
                // 如果切换回【移动5G】类型
                else if (currentType === 'mobile5G') {
                    this.config.cspKey = '';            // CSP密钥
                    this.config.cspToken = '';          // CSPToken
                    this.config.backAddress = '';       // 回落层地址
                    this.config.fileUploadAddress = ''; // 文件上传地址
                }
            },
            addFallBackConfig() {
                this.fallbackChannelConfig.push('')
            },
            removeFallBackConfig(index) {
                this.fallbackChannelConfig.splice(index, 1)
            },
            provinceOnchange() {
                const bigRegionId = this.regionCodeObj.bigRegion.split("-")[0]
                this.provinceRegions = region_province[bigRegionId]
            },
            cityOnchange() {
                if (!this.regionCodeObj.province) {
                    layer.msg('请选择省份', {icon: 2})
                    return
                }
                const provinceId = this.regionCodeObj.province.split("-")[0]
                this.cityRegions = province_city[provinceId]
            },
            buildRegionCode() {
                if (!this.regionCodeObj.bigRegion || !this.regionCodeObj.province || !this.regionCodeObj.city) {
                    layer.msg('请选择完整的地区信息', {icon: 2})
                    return
                }
                const bigRegionArr = this.regionCodeObj.bigRegion.split("-")
                const provinceArr = this.regionCodeObj.province.split("-")
                const cityArr = this.regionCodeObj.city.split("-")
                this.config.agentRegion = bigRegionArr[0] + "," + provinceArr[0] + "," + cityArr[0]
                this.config.regionName = bigRegionArr[1] + "-" + provinceArr[1] + "-" + cityArr[1]
            },
            initConfig() {
                const that = this;
                $.ajax({
                    url: '${basePage}/chatbot/initPlatformSecretKey.html',
                    type: 'get',
                    success: function (res) {
                        if (res.code === 0) {
                            const data = res.data;
                            that.config.platPublickey = data.publicKey
                            that.config.platPrivatekey = data.privateKey
                        } else {
                            layer.msg(res.msg, {icon: 2})
                        }
                    },
                    error: function () {
                        layer.msg('初始化平台公私钥失败,请退出重新进入初始化', {icon: 2})
                    }
                })
            },
            loadConfig() {
                if (!this.id) {
                    return
                }
                const that = this;
                $.ajax({
                    url: '${basePage}/chatbot/getAgentConfig.html?cspAgentId=' + this.id,
                    type: 'get',
                    success: function (res) {
                        if (res.code === 0) {
                            that.config = res.data;
                            layer.msg('加载配置成功', {icon: 1})
                        } else {
                            layer.msg(res.msg, {icon: 2})
                        }
                    },
                    error: function () {
                        layer.msg('请求代理商客户配置信息失败', {icon: 2})
                    }
                })
            },
            goList() {
                window.location.href = '${basePage}/chatbot/agentList.html'
            },
            saveConfig() {
                if (!this.validParams()) {
                    return
                }
                const that = this;
                const index = layer.load(1, {shade: [0.8, '#393D49'], scrollbar: false})
                $.ajax({
                    url: '${basePage}/chatbot/saveAgentConfig.html',
                    type: 'post',
                    contentType: 'application/json; charset=utf-8',
                    data: JSON.stringify(this.config),
                    success: function (res) {
                        layer.close(index)
                        if (res.code === 0) {
                            layer.msg('保存成功', {icon: 1})
                            setTimeout(() => {
                                that.loadConfig()
                            }, 1000)
                        } else {
                            layer.msg(res.msg, {icon: 2})
                        }
                    },
                    error: function () {
                        layer.close(index)
                        layer.msg('保存代理商客户配置信息失败', {icon: 2})
                    }
                })
            },
            getFallBackChannels() {
                const that = this;
                $.ajax({
                    url: '${basePage}/chatbot/getFallBackChannels.html',
                    type: 'get',
                    success: function (res) {
                        if (res.code === 0) {
                            that.channels = res.data
                        } else {
                            layer.msg(res.msg, {icon: 2})
                        }
                    },
                    error: function () {
                        layer.msg('请求服务端失败,无法获取回落通道', {icon: 2})
                    }
                })
            },
            validParams() {
                const { apiType, cspCode, agentCode, agentName, agentRegion, agentAppid, agentToken, agentPublickey, platAppid, platToken, platPublickey, platPrivatekey, requestUrl, sendUrl, serviceCode, cspKey, cspToken, backAddress } = this.config;
                // 所有类型都需校验
                if (!apiType) {
                    layer.msg("接口类型不能为空", { icon: 2 });
                    return false;
                }
                if (!serviceCode) {
                    layer.msg("可用服务代码不能为空", { icon: 2 });
                    return false;
                }
                if (!cspCode) {
                    layer.msg("CSP编码不能为空", { icon: 2 });
                    return false;
                }
                if (!agentName) {
                    layer.msg("代理商名称不能为空", { icon: 2 });
                    return false;
                }
                if (!requestUrl) {
                    layer.msg("请求地址不能为空", { icon: 2 });
                    return false;
                }
                // 移动5G专属校验
                if (apiType === "mobile5G") {
                    if (!sendUrl) {
                        layer.msg("发送地址不能为空", { icon: 2 });
                        return false;
                    }
                    if (!agentCode) {
                        layer.msg("代理商编码不能为空", { icon: 2 });
                        return false;
                    }
                    if (!agentRegion) {
                        layer.msg("归属地区编码不能为空", { icon: 2 });
                        return false;
                    }
                    if (!agentAppid) {
                        layer.msg("代理商AppId不能为空", { icon: 2 });
                        return false;
                    }
                    if (!agentToken) {
                        layer.msg("代理商Token不能为空", { icon: 2 });
                        return false;
                    }
                    if (!agentPublickey) {
                        layer.msg("代理商公钥不能为空", { icon: 2 });
                        return false;
                    }
                    if (!platAppid) {
                        layer.msg("平台appid不能为空", { icon: 2 });
                        return false;
                    }
                    if (!platToken) {
                        layer.msg("平台token不能为空", { icon: 2 });
                        return false;
                    }
                    if (!platPublickey) {
                        layer.msg("平台公钥不能为空", { icon: 2 });
                        return false;
                    }
                    if (!platPrivatekey) {
                        layer.msg("平台私钥不能为空", { icon: 2 });
                        return false;
                    }
                }
                //电信/联通5G专属校验
                else if (["telecom5G", "unicom5G"].includes(apiType)) {
                    if (!cspKey) {
                        layer.msg("CSP密钥不能为空", { icon: 2 });
                        return false;
                    }
                    if (!cspToken) {
                        layer.msg("CSPToken不能为空", { icon: 2 });
                        return false;
                    }
                }
                // 所有校验通过
                return true;
            },
            copyText(key) {
                const value = this.config[key]
                if (!value) {
                    layer.msg('复制失败,请检查数据是否为空')
                    return
                }
                let input = document.createElement("input");
                input.value = value;
                document.body.appendChild(input);
                input.select();
                document.execCommand("Copy");
                document.body.removeChild(input);
                layer.msg('复制成功');
            }
        }
    })
</script>
</html>
