<%--
  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">
    <script type="text/javascript" src="${basePage}/web/chatbot/static/js/cspCommon.js"></script>
    <title>ChatBot添加</title>
    <style>
        .select2 {
            font-weight: bolder;
            border: 1px solid #ccc;
            margin: 5px 0;
            border-radius: 5px;
        }
    </style>
</head>
<body>

<div class="page_container" id="app">
    <div class="page_head">
        <h3>ChatBot添加</h3>
        <div class="breadcrumb">
            <a href="#">首页</a> > <a href="#">CSP业务</a> > ChatBot添加
        </div>
    </div>
    <div class="row">
        <div class="col-xs-12">
            <div class="panel">
                <div class="container2">
                    <div class="main_p20">
                        <div>
                            <div class="infoShow">
                                <input type="hidden" v-model="config.logo" />
                                <input type="hidden" v-model="config.accredit1" />
                                <input type="hidden" v-model="config.accredit2" />
                                <input type="hidden" v-model="config.accredit3" />
                                <input type="hidden" v-model="config.accredit4" />
                                <input type="hidden" v-model="config.accredit5" />
                                <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="saveType" class="form-control form_white singleList"
                                                        v-model="config.agentId" :disabled="isUpdateMode">
                                                    <option value="">请选择</option>
                                                    <option v-for="(item,index) in agentList" :key="'agentList' + index"
                                                            :value="item.agentId">{{item.agentId}} {{item.agentName}}
                                                    </option>
                                                </select>
                                            </label>
                                        </td>
                                    </tr>
                                    <tr>
                                        <th scope="row" v-if="config.apiType !== 'mobile5G'">
                                            <div class="floatLeft require">非直签客户</div>
                                        </th>
                                        <th scope="row" v-if="config.apiType === 'mobile5G'">
                                            <div class="floatLeft">非直签客户</div>
                                        </th>
                                        <td colspan="4">
                                            <div style="width: 100%; display: flex;align-items: center">
                                                <label>

                                                    <select name="saveType" class="form-control form_white" ref="nosignCustomerSelect"
                                                            @change="updateServiceCodeList"
                                                            v-model="config.nosignCustomerId"
                                                            :disabled="isUpdateMode">
                                                        <option value="">请选择</option>
                                                        <option v-for="(item,index) in noSignCustomerList"
                                                                :key="'noSignCustomerList' + index"
                                                                :value="item.customerId">
                                                            {{item.customerName}}
                                                        </option>
                                                    </select>
                                                </label>
                                                <input v-model="noSignCustomerKeyword" class="form-control form_white" placeholder="请输入下拉框搜索关键字"
                                                       style="margin-left: 5px;width: 150px">

                                                <button type="button" class="btn btn_green" style="margin-left: 5px;margin-right: 10px" @click="filterNoSignCustomerKeyword">过滤选项</button>
                                                <p>该客户所属服务代码:{{serviceCodeList}}</p>
                                            </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 require">ChatBot名称</div>
                                        </th>
                                        <td colspan="2">
                                            <label>
                                                <input type="text" name="" class="form-control form_white"
                                                       v-model="config.name">
                                            </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.serviceCode" :disabled="isUpdateMode">
                                            </label>
                                        </td>
                                    </tr>

                                    <tr>
                                        <th scope="row">
                                            <div class="floatLeft require">ChatBot ID</div>
                                        </th>
                                        <td colspan="2" v-if="config.apiType !== 'mobile5G'">
                                            <label style="display: flex; align-items: center; gap: 8px; width: 100%;">
                                                <input type="text" name="" class="form-control form_white"
                                                       v-model="config.chatbotId" :disabled="isUpdateMode"
                                                       style="flex: 0.5; max-width: 50%;">
                                                <span class="chatbot-id-suffix" style="color: #409EFF; font-weight: bold; font-size: 13px; white-space: nowrap;">
                                                    {{ config.apiType === 'telecom5G' ? '@botplatform.189.cn' : '@botplatform.189.cn' }}
                                                </span>
                                            </label>
                                        </td>
                                        <td colspan="2" v-if="config.apiType === 'mobile5G'">
                                            <label>
                                                <input type="text" name="" class="form-control form_white"
                                                       v-model="config.chatbotId" :disabled="isUpdateMode">
                                            </label>
                                            <button type="button" onclick="showIdCreateText()" class="btn btn_green">
                                                ID创建规则说明
                                            </button>
                                            <button type="button" onclick="addIdSuffix('chatbotId')"
                                                    class="btn btn_green">插入ID固定后缀
                                            </button>
                                        </td>
                                        <th scope="row">
                                            <div class="floatLeft require">行业类型</div>
                                        </th>
                                        <td colspan="2" >
                                            <label>
                                                <select name="" class="form-control form_white singleSelect"
                                                        onchange="updateVueData('serviceType',this.value)"
                                                        v-model="config.serviceType">
                                                    <option value="">请选择</option>
                                                    <option :value="item.code+'_'+item.name"
                                                            v-for="(item,index) in (config.apiType === 'mobile5G' ? industry : industry2)"
                                                            :key="'industry' + item.code">{{item.code}} {{item.name}}
                                                    </option>
                                                </select>
                                            </label>
                                        </td>
                                    </tr>
                                    <tr v-if="config.apiType !== 'mobile5G'">
                                        <th scope="row">
                                            <div class="floatLeft require">ChatBot 描述</div>
                                        </th>
                                        <td colspan="2">
                                            <label>
                                                <input type="text" name="" v-model="config.serviceDescription"
                                                       class="form-control form_white full_input_width">
                                            </label>
                                        </td>
                                        <th scope="row">
                                            <div class="floatLeft require">ChatBot 短信端口号</div>
                                        </th>
                                        <td colspan="2">
                                            <label>
                                                <input type="text" name="" v-model="config.smsNumber"
                                                       class="form-control form_white full_input_width">
                                            </label>
                                        </td>
                                    </tr>
                                    <tr>
                                        <th scope="row" v-if="config.apiType !== 'mobile5G'">
                                            <div class="floatLeft require">Logo图片</div>
                                        </th>
                                        <th scope="row" v-if="config.apiType === 'mobile5G'">
                                            <div class="floatLeft">Logo图片</div>
                                        </th>
                                        <td colspan="2" class="image-container" v-if="config.apiType === 'mobile5G'">
                                            <div style="display: flex; align-items: center; gap: 10px; width: 100%;">
                                                <div style="display: flex; justify-content: center;">
                                                    <button type="button" class="btn btn_small btn_blue"
                                                            @click="showFileSelect('logoName')">点击上传
                                                    </button>
                                                    <input type="file" name="logoName" style="display: none" id="logo"
                                                           @change="uploadFileHandler">
                                                </div>
                                                <img v-if="logoImagePath" :src="logoImagePath" class="inline-image"
                                                     style="max-width: 100px; max-height: 100px; border: 1px solid #eee; border-radius: 4px; object-fit: contain;" />
                                                <span v-if="!logoImagePath"
                                                      style="color: red">大小限50K,尺寸400*400,不选使用默认LOGO</span>
                                            </div>
                                        </td>
                                        <td colspan="2" class="image-container" v-if="config.apiType !== 'mobile5G'">
                                            <div style="display: flex; align-items: center; gap: 10px; width: 100%;">
                                                <div style="display: flex; justify-content: center;">
                                                    <button type="button" class="btn btn_small btn_blue"
                                                            @click="showFileSelect('logoName')">点击上传
                                                    </button>
                                                    <input type="file" name="logoName" style="display: none" id="logoLocal"
                                                           @change="uploadFileHandler">
                                                </div>
                                                <img v-if="logoImagePath" :src="logoImagePath" class="inline-image"
                                                     style="max-width: 100px; max-height: 100px; border: 1px solid #eee; border-radius: 4px; object-fit: contain;" />
                                                <span v-if="!logoImagePath"
                                                      style="color: red">大小限200K</span>
                                                <button type="button" class="btn btn_small btn_gray"
                                                        @click="useDefaultLogo()"
                                                        style="white-space: nowrap; margin-left: auto;">使用默认LOGO
                                                </button>
                                            </div>
                                        </td>
                                        <th scope="row">
                                            <div class="floatLeft require">签名</div>
                                        </th>
                                        <td colspan="2">
                                            <div class="checkbox-group"
                                                 style="width: 300px;justify-content: space-between">
                                                <input type="text" name="" class="form-control form_white"
                                                       ref="signInput"
                                                       v-model="config.sign" style="width: calc(100% - 100px)">
                                                <%--                                                <button @click="addSign" type="button" class="btn btn_small btn_green">--%>
                                                <%--                                                    插入签名符--%>
                                                <%--                                                </button>--%>
                                            </div>
                                        </td>
                                    </tr>

                                    <tr v-if="config.apiType === 'mobile5G'">
                                        <th scope="row">
                                            <div class="floatLeft">背景图片</div>
                                        </th>
                                        <td colspan="2" class="image-container">
                                            <button type="button" class="btn btn_small btn_blue"
                                                    @click="showFileSelect('bgName')">点击上传
                                            </button>
                                            <input type="file" name="bgName" style="display: none" id="backgroundImage"
                                                   @change="uploadFileHandler">
                                            <img v-if="bgImagePath" :src="bgImagePath" class="inline-image"/>
                                        </td>
                                        <th scope="row">
                                            <div class="floatLeft">气泡颜色</div>
                                        </th>
                                        <td colspan="2">
                                            <div class="checkbox-group"
                                                 style="width:300px;justify-content: space-between">
                                                <input v-model="config.bubbleColor" type="text" name=""
                                                       class="form-control form_white" style="width: calc(100% - 50px)">
                                                <input type="color" name="" @change="colorSelect"
                                                       class="form-control form_white"
                                                       style="width: 40px; border: inherit;padding: inherit">
                                            </div>
                                        </td>
                                    </tr>

                                    <tr v-if="config.apiType !== 'mobile5G'">
                                        <th scope="row">
                                            <div class="floatLeft require">提供者名称</div>
                                        </th>
                                        <td colspan="2">
                                            <div class="checkbox-group"
                                                 style="width: 300px;justify-content: space-between">
                                                <input type="text" name="" class="form-control form_white"
                                                       v-model="config.provider" style="width: calc(100% - 100px)">
                                            </div>
                                        </td>
                                        <th scope="row">
                                            <div class="floatLeft require">显示提供者</div>
                                        </th>
                                        <td colspan="2" >
                                            <label>
                                                <input type="radio" name="" value="1" v-model="config.showProvider">显示
                                            </label>
                                            <label>
                                                <input type="radio" name="" value="0" v-model="config.showProvider">不显示
                                            </label>
                                        </td>
                                    </tr>

                                    <tr>
                                        <th scope="row">
                                            <div class="floatLeft">服务电话</div>
                                        </th>
                                        <td colspan="2">
                                            <label>
                                                <input type="text" name="" v-model="config.servicePhone"
                                                       class="form-control form_white">
                                            </label>
                                        </td>
                                        <th scope="row" v-if="config.apiType === 'mobile5G'">
                                            <div class="floatLeft">实际下发行业</div>
                                        </th>
                                        <td colspan="2" v-if="config.apiType === 'mobile5G'">
                                            <select name="" class="form-control form_white"
                                                    v-model="config.actualSendIndustry">
                                                <option value="">请选择</option>
                                                <option :value="item.code" v-for="(item,index) in realy_industry"
                                                        :key="'realy_industry' + index">{{item.code}} {{item.name}}
                                                </option>
                                            </select>
                                        </td>
                                        <th scope="row" v-if="config.apiType !== 'mobile5G'">
                                            <div class="floatLeft require">IP地址白名单</div>
                                        </th>
                                        <td colspan="2" v-if="config.apiType !== 'mobile5G'">
                                            <label>
                                                <textarea name="remark" class="form-control form_white"
                                                          v-model="config.ipWhiteList"></textarea>
                                            </label>
                                        </td>
                                    </tr>
                                    <tr>
                                        <th scope="row">
                                            <div class="floatLeft">Chatbot邮箱</div>
                                        </th>
                                        <td colspan="2">
                                            <label>
                                                <input type="text" name="" class="form-control form_white"
                                                       v-model="config.chatbotEmail">
                                            </label>
                                        </td>
                                        <th scope="row">
                                            <div class="floatLeft">Chatbot官网</div>
                                        </th>
                                        <td colspan="2">
                                            <label>
                                                <input type="text" name="" class="form-control form_white"
                                                       v-model="config.chatbotOfficialWebsite">
                                            </label>
                                        </td>
                                    </tr>

                                    <tr>
                                        <th scope="row">
                                            <div class="floatLeft">办公地址</div>
                                        </th>
                                        <td colspan="4">
                                            <label>
                                                <input type="text" name="" v-model="config.officeAddress"
                                                       class="form-control form_white full_input_width">
                                            </label>
                                        </td>
                                    </tr>

                                    <tr>
                                        <th scope="row">
                                            <div class="floatLeft">服务条款地址</div>
                                        </th>
                                        <td colspan="4">
                                            <label>
                                                <input type="text" name="" v-model="config.termsServiceUrl"
                                                       class="form-control form_white full_input_width"
                                                       placeholder="包含条款和条件内容的网页地址">
                                            </label>
                                        </td>
                                    </tr>

                                    <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.longitude">
                                            </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.latitude">
                                            </label>
                                        </td>
                                    </tr>

                                    <tr v-if="config.apiType === 'mobile5G'">
                                        <th scope="row">
                                            <div class="floatLeft">实体附件</div>
                                        </th>
                                        <td colspan="4">
                                            <button type="button" class="btn btn_small btn_blue"
                                                    @click="showFileSelect('entityFile')">点击上传
                                            </button>
                                            <input type="file" name="entityFile" style="display: none"
                                                   id="entityAttachment" @change="uploadFileHandler">
                                            <span>{{config.entityAttachment ? config.entityAttachment : '未上传'}}</span>
                                        </td>
                                    </tr>
                                    <tr v-if="config.apiType !== 'mobile5G'">
                                        <th scope="row">
                                            <div class="floatLeft">Chatbot 授权书</div>
                                        </th>
                                        <td colspan="4">
                                            <div id="accreditContainer">
                                                <div class="accredit-item" v-for="(item, index) in accreditList" :key="index" style="margin-bottom: 8px; display: flex; align-items: center; gap: 8px;">
                                                    <button type="button" class="btn btn_small btn_blue" @click="showFileSelect('accreditLocal' + (index + 1))"
                                                            style="padding: 4px 12px; border-radius: 4px; transition: all 0.2s;">
                                                        上传授权书{{index + 1}}
                                                    </button>
                                                    <input
                                                            type="file"
                                                            :name="'accreditLocal' + (index + 1)"
                                                            :id="'accreditLocal' + (index + 1)"
                                                            style="display: none"
                                                            @change="handleAccreditUpload(index)"
                                                    >
                                                    <span style="margin: 0 8px; flex: 1; max-width: 350px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: #666;">
                                                        {{ config['accreditLocal' + (index + 1)] || '未上传' }}
                                                    </span>
                                                    <button
                                                            type="button"
                                                            class="btn btn_small btn_red"
                                                            @click="removeAccredit(index)"
                                                            v-if="accreditList.length > 1"
                                                            style="width: 22px; height: 22px; padding: 0; margin-left: -4px; border-radius: 50%; display: flex; align-items: center; justify-content: center; transition: all 0.2s; font-size: 12px;"
                                                            title="删除此授权书"
                                                    >×
                                                    </button>
                                                </div>
                                                <!-- 添加按钮（仅当数量<5时显示） -->
                                                <button
                                                        type="button"
                                                        class="btn btn_small btn_green"
                                                        @click="addAccredit"
                                                        v-if="accreditList.length < 5"
                                                        style="padding: 4px 12px; border-radius: 4px; transition: all 0.2s; display: flex; align-items: center; gap: 4px;"
                                                >
                                                    <span>+</span> 添加授权书
                                                </button>
                                            </div>
                                        </td>
                                    </tr>
                                    <tr v-if="config.apiType === 'mobile5G'">
                                        <th scope="row">
                                            <div class="floatLeft">端口服务类型</div>
                                        </th>
                                        <td colspan="4">
                                            <div style="display: flex; align-items: center; margin: 5px 0">
                                                <div style="display: flex; align-items: center;margin-left: 10px">
                                                    <input type="checkbox" value="01" class="portService portServiceType1"
                                                           onChange="portServiceTypeMapChangeHandler()">
                                                    01-账号注册
                                                    <input type="checkbox" value="02" class="portService portServiceType1"
                                                           onChange="portServiceTypeMapChangeHandler()">
                                                    02-账号登录
                                                    <input type="checkbox" value="03" class="portService portServiceType2"
                                                           onChange="portServiceTypeMapChangeHandler()">
                                                    03-广告促销
                                                    <input type="checkbox" value="04" class="portService portServiceType1"
                                                           onChange="portServiceTypeMapChangeHandler()">
                                                    04-通知提醒
                                                    <input type="checkbox" value="05" class="portService portServiceType3"
                                                           onChange="portServiceTypeMapChangeHandler()">
                                                    05-公共服务
                                                </div>
                                            </div>
                                            <span style="color: red;font-size: 12px">注:01,02和04可以同时选择,03、05不可多选</span>
                                        </td>

                                    </tr>
                                    <tr class="portServiceType2-1" style="display:none">
                                        <th scope="row" >
                                            <div class="floatLeft">广告营销行业类别</div>
                                        </th>
                                        <td colspan="1" >
                                            <label>
                                                <select name="" class="form-control form_white"
                                                        v-model="config.adMarketTndustryType">
                                                    <option value="">请选择</option>
                                                    <option v-for="(item,key) in adMarketTndustryTypeMap"
                                                            :key="'adMarketTndustryTypeMap' + item" :value="key">
                                                        {{key}}-{{item}}
                                                    </option>
                                                </select>
                                            </label>
                                        </td>
                                    </tr>
                                </table>

                                <h3 class="info_tit" v-if="config.apiType === 'mobile5G'">实名信息</h3>

                                <table width="100%" border="0" cellspacing="0" cellpadding="0" class="infotable" v-if="config.apiType === 'mobile5G'">
                                    <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.customerName">
                                            </label>
                                        </td>
                                        <td colspan="2">
                                            <span style="color: red">实名信息如填写，则chatbot以所填写实名信息为准，如不填写，则以所选非直签客户为准</span>
                                        </td>
                                    </tr>

                                    <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.remarkText">
                                            </label>
                                        </td>
                                        <td colspan="2">
                                        </td>
                                    </tr>

                                    <tr>
                                        <th scope="row">
                                            <div class="floatLeft">合同扫描件</div>
                                        </th>

                                        <td colspan="4">
                                            <button type="button" class="btn btn_small btn_blue"
                                                    @click="showFileSelect('customerContractFile1')">点击上传
                                            </button>
                                            <input type="file" name="customerContractFile1" style="display: none"
                                                   id="customerContractFile" @change="uploadFileHandler">
                                            <span>{{config.customerContractFile ? config.customerContractFile : '未上传'}}</span>
                                        </td>
                                    </tr>

                                    <tr>
                                        <th scope="row">
                                            <div class="floatLeft">客户附件</div>
                                        </th>

                                        <td colspan="4">
                                            <button type="button" class="btn btn_small btn_blue"
                                                    @click="showFileSelect('customerAttachment1')">点击上传
                                            </button>
                                            <input type="file" name="customerAttachment1" style="display: none"
                                                   id="customerAttachment" @change="uploadFileHandler">
                                            <span>{{config.customerAttachment ? config.customerAttachment : '未上传'}}</span>
                                        </td>
                                    </tr>

                                    <tr>
                                        <th scope="row">
                                            <div class="floatLeft require">统一社会信用代码</div>
                                        </th>
                                        <td colspan="2">
                                            <div class="checkbox-group"
                                                 style="width: 300px;justify-content: space-between">
                                                <input type="text" name="" class="form-control form_white"
                                                       ref="signInput"
                                                       v-model="config.creditCode">
                                            </div>
                                        </td>

                                        <th scope="row">
                                            <div class="floatLeft require">单位证件类型</div>
                                        </th>
                                        <td colspan="2">
                                            <select name="" class="form-control form_white"
                                                    v-model="config.institutionLicenceType">
                                                <option value="">请选择</option>
                                                <option value="1">营业执照</option>
                                                <option value="2">组织机构代码证</option>
                                                <option value="3">事业单位法人证书</option>
                                                <option value="4">社会团体法人登记证书</option>
                                                <option value="5">军队代码</option>
                                                <option value="6">个体户（注册号）</option>
<%--                                                <option :value="item.code" v-for="(item,index) in realy_industry"--%>
<%--                                                        :key="'realy_industry' + index">{{item.code}} {{item.name}}--%>
<%--                                                </option>--%>
                                            </select>
                                        </td>
                                    </tr>

                                    <tr>
                                        <th scope="row">
                                            <div class="floatLeft require">责任人（含法人）姓名</div>
                                        </th>
                                        <td colspan="2">
                                            <label>
                                                <input type="text" name="" v-model="config.representativeName"
                                                       class="form-control form_white">
                                            </label>
                                        </td>
                                        <th scope="row">
                                            <div class="floatLeft">责任人手机号</div>
                                        </th>
                                        <td colspan="2">
                                            <label>
                                                <input type="text" name="" v-model="config.representativeTel"
                                                       class="form-control form_white">
                                            </label>
                                        </td>
                                    </tr>

                                    <tr>
                                        <th scope="row">
                                            <div class="floatLeft require">责任人（含法人）证件类型</div>
                                        </th>
                                        <td colspan="2">
                                            <select name="" class="form-control form_white"
                                                    v-model="config.representativeCertificateType">
                                                <option value="">请选择</option>
                                                <option value="1">居民身份证</option>
                                                <option value="2">中国人民解放军军人身份证件</option>
                                                <option value="3">中国人民武装警察身份证件</option>
                                            </select>
                                        </td>
                                        <th scope="row">
                                            <div class="floatLeft require">责任人（含法人）证件号码</div>
                                        </th>
                                        <td colspan="2">
                                            <label>
                                                <input type="text" name="" v-model="config.representativeCertificateNumber"
                                                       class="form-control form_white">
                                            </label>
                                        </td>
                                    </tr>

                                    <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.agentName">
                                            </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.agentTel">
                                            </label>
                                        </td>
                                    </tr>

                                    <tr>
                                        <th scope="row">
                                            <div class="floatLeft">经办人证件类型</div>
                                        </th>
                                        <td colspan="2">
                                            <select name="" class="form-control form_white"
                                                    v-model="config.agentCertificateType">
                                                <option value="">请选择</option>
                                                <option value="1">居民身份证</option>
                                                <option value="2">中国人民解放军军人身份证件</option>
                                                <option value="3">中国人民武装警察身份证件</option>
                                            </select>
                                        </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.agentCertificateNumber">
                                            </label>
                                        </td>
                                    </tr>
                                </table>



                                <h3 class="info_tit" v-if="config.apiType === 'mobile5G'">调试白名单</h3>
                                <table width="100%" border="0" cellspacing="0" cellpadding="0" class="infotable" v-if="config.apiType === 'mobile5G'">
                                    <tr v-if="!id">
                                        <th scope="row">
                                            <div class="floatLeft require">调试开关</div>
                                        </th>
                                        <td colspan="4" style="display: flex; align-items: center">
                                            <label>
                                                <input type="radio" name="" value="0" v-model="debugSwitch">开启
                                            </label>
                                            <label>
                                                <input type="radio" name="" value="1" v-model="debugSwitch">关闭
                                            </label>
                                        </td>
                                    </tr>
                                    <tr>
                                        <th scope="row">
                                            <div class="floatLeft require">调试白名单</div>
                                        </th>
                                        <td colspan="4">
                                            <label>
                                                <textarea type="text" name="" v-model="config.debugPhone"
                                                          class="form-control form_white"
                                                          style="width: 85%;height: 35px;"
                                                          placeholder="多个手机号用英文逗号隔开"></textarea>
                                            </label>
                                        </td>
                                    </tr>
                                </table>

                                <h3 class="info_tit" v-if="config.apiType === 'mobile5G'">审核信息</h3>
                                <table width="100%" border="0" cellspacing="0" cellpadding="0" class="infotable" v-if="config.apiType === 'mobile5G'">
                                    <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.auditer">
                                            </label>
                                        </td>
                                        <th scope="row">
                                            <div class="floatLeft require">审核日期</div>
                                        </th>
                                        <td colspan="2">
                                            <input type="text" name="" class="form-control form_white"
                                                   placeholder="点击选择日期" v-model="config.auditerDate"
                                                   onchange="updateVueData('auditerDate',this.value)"
                                                   onclick="dateTimeFocus('yyyy-MM-dd')">
                                        </td>
                                    </tr>
                                    <tr>
                                        <th scope="row">
                                            <div class="floatLeft require">审核意见</div>
                                        </th>
                                        <td colspan="4">
                                            <label>
                                                <input type="text" name="" v-model="config.auditerMsg"
                                                       class="form-control form_white full_input_width">
                                            </label>
                                        </td>
                                    </tr>
                                </table>

                                <h3 class="info_tit" v-if="config.apiType === 'mobile5G'">备注</h3>
                                <table width="100%" border="0" cellspacing="0" cellpadding="0" class="infotable" v-if="config.apiType === 'mobile5G'">
                                    <tr>
                                        <th scope="row">
                                            <div class="floatLeft">欢迎语</div>
                                        </th>
                                        <td colspan="4">
                                            <label>
                                                <textarea name="" class="form-control form_white"
                                                          v-model="config.welcome"
                                                          placeholder="收到上行'你好'时,自动回复此消息"
                                                          style="width: 85%;height: 35px;"></textarea>
                                            </label>
                                        </td>
                                    </tr>
                                    <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: 35px;"></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="backList" 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 type="text/javascript" src="${basePage}/web/chatbot/static/js/vanilla-picker.js"></script>
<script>
    const layer = layui.layer;

    $(function () {
        $(".singleSelect").select2()

        $(".image-container").on('click', '.inline-image', function () {
            showBigImage($(this).attr('src'))
        })
    })

    function showBigImage(url) {
        if (!url) {
            return
        }
        layer.open({
            type: 1,
            title: '预览大图',
            area: '600px', // 宽高
            shadeClose: true, // 点击遮罩关闭
            content: `<div style="width: 100%;height: 500px;display: flex;justify-content: center; align-items: center;background-color: transparent">
                        <img src="\${url}" alt="大图" style="max-width: 100%;"></div>`
        });
    }

    function updateVueData(key, value) {
        console.log(key + "," + value)
        app.$data.config[key] = value
    }

    let app = new Vue({
        el: '#app',
        data: function () {
            return {
                id: '',
                isUpdateMode: false,
                accreditList: [1],
                config: {
                    id: "",
                    actualSendIndustry: "",
                    agentId: "",
                    auditer: "",
                    auditerDate: "",
                    auditerMsg: "",
                    backgroundImage: "",
                    bubbleColor: "",
                    chatbotEmail: "",
                    chatbotId: "",
                    chatbotOfficialWebsite: "",
                    debugPhone: "",
                    entityAttachment: "",
                    latitude: "",
                    logo: "",
                    logoLocal: "",
                    longitude: "",
                    name: "",
                    nosignCustomerId: "",
                    officeAddress: "",
                    remark: "",
                    serviceCode: "",
                    servicePhone: "",
                    serviceType: "",
                    sign: "",
                    status: 0,
                    termsServiceUrl: "",
                    welcome: "",
                    eTag: "",
                    portServiceType: "",
                    apiType: "mobile5G",
                    serviceDescription: "",
                    smsNumber: "",
                    provider: "",
                    showProvider: "",
                    ipWhiteList: "",
                    accredit1: "",
                    accredit2: "",
                    accredit3: "",
                    accredit4: "",
                    accredit5: "",
                    accreditLocal1: "",
                    accreditLocal2: "",
                    accreditLocal3: "",
                    accreditLocal4: "",
                    accreditLocal5: "",
                    adMarketTndustryType: "",
                    customerName:"",
                    customerContractFile:"",
                    customerAttachment:"",
                    remarkText:"",
                    creditCode:"",
                    representativeName:"",
                    representativeCertificateType:"",
                    representativeCertificateNumber:"",
                    representativeTel:"",
                    agentName:"",
                    agentCertificateType:"",
                    agentCertificateNumber:"",
                    agentTel:"",
                    institutionLicenceType:""
                },
                industry: mainIndustry,
                industry2: industry2,
                realy_industry: realyIndustry,
                agentList: [],
                noSignCustomerList: [],
                logoImagePath: '',
                bgImagePath: '',
                serviceCodeList: '',
                debugSwitch: '1',
                portServiceTypeArray: [],
                portServiceTypeMap: {
                    "01": "账号注册",
                    "02": "账号登录",
                    "03": "广告促销",
                    "04": "通知提醒",
                    "05": "公共服务"
                },
                adMarketTndustryTypeMap: {
                    "01": "金融推销",
                    "02": "零售业推销",
                    "03": "电信业务宜传",
                    "04": "网络游戏推广",
                    "05": "教育培训推销",
                    "06": "房产中介推销",
                    "07": "股票证券推销",
                    "08": "医疗卫生推销",
                    "09": "猎头招聘推广",
                    "10": "旅游推广",
                    "11": "其他营销类别"
                },
                noSignCustomerKeyword: '',
                noSignCustomerListFull: []
            }
        },
        watch: {
            'config.logo': function (val) {
                if (val) {
                    this.logoImagePath = '${basePage}/chatbot/downloadCspResource.html?filePath=' + val
                }
            },
            'config.logoLocal': function (val) {
                if (val) {
                    this.logoImagePath = '${basePage}/chatbot/downloadCspResource.html?filePath=' + val
                }
            },
            'config.backgroundImage': function (val) {
                if (val) {
                    this.bgImagePath = '${basePage}/chatbot/downloadCspResource.html?filePath=' + val
                }
            },
            'config.agentId': function (val) {
                this.loadNoSignCustomerList()
            },
        },
        beforeMount() {
            this.loadAgentList()
        },
        mounted() {
            const index = layer.load(1, {shade: [0.8, '#393D49'], scrollbar: false})
            const paramsMap = new URLSearchParams(location.search)
            if (paramsMap.has('id')) {
                this.id = paramsMap.get('id')
                this.isUpdateMode = true
                this.initConfig()
            } else {
                this.config.auditer = '运营'
                this.config.auditerMsg = '通过'
                const currentDay = new Date()
                let year = currentDay.getFullYear()
                let month = currentDay.getMonth() + 1
                if (month < 10) {
                    month = '0' + month
                }
                let day = currentDay.getDate()
                if (day < 10) {
                    day = '0' + day
                }
                this.config.auditerDate = year + '-' + month + '-' + day
            }
            layer.close(index)
        },
        methods: {
            initConfig() {
                if (!this.id) {
                    return
                }
                const that = this;
                $.ajax({
                    url: '${basePage}/chatbot/getChatBotConfig.html?chatBotId=' + that.id,
                    type: 'get',
                    async: false,
                    success: function (res) {
                        if (res.code === 0) {
                            that.config = res.data;
                            const chatbotId = that.config.chatbotId || ''; // 获取带后缀的ID
                            const apiType = that.config.apiType || ''; // 获取当前apiType
                            if (apiType === 'telecom5G' && chatbotId.endsWith('@botplatform.189.cn')) {
                                that.config.chatbotId = chatbotId.slice(0, chatbotId.length - '@botplatform.189.cn'.length);
                            }
                            else if (apiType === 'unicom5G' && chatbotId.endsWith('@test')) {
                                that.config.chatbotId = chatbotId.slice(0, chatbotId.length - '@btest'.length);
                            }
                            if (that.config.apiType !== 'mobile5G') {
                                let accreditCount = 0;
                                // 检查accreditLocal1~5是否有值，统计非空数量
                                for (let i = 1; i <= 5; i++) {
                                    if (that.config['accreditLocal' + i]) {
                                        accreditCount = i; // 取最大的非空序号（如3个非空则count=3）
                                    }
                                }
                                // 更新accreditList长度（确保上传框数量匹配非空数据）
                                that.accreditList = accreditCount > 0 ? Array(accreditCount).fill(1) : [1];
                            }
                            if (that.config.agentId) {
                                that.loadNoSignCustomerList()
                                setTimeout(() => {
                                    that.setPortServiceType()
                                    that.portServiceTypeMapChangeHandler2()
                                    const selectedCustomer = that.noSignCustomerList.find(
                                        c => c.customerId === that.config.nosignCustomerId
                                    );
                                    if (selectedCustomer) {
                                        that.config.apiType = selectedCustomer.apiType;
                                        const $serviceTypeSelect = $("select[name='serviceType']");

                                        // 彻底销毁旧select2和旧选项
                                        if ($serviceTypeSelect.data('select2')) {
                                            $serviceTypeSelect.select2('destroy');
                                        }
                                        $serviceTypeSelect.empty(); // 清空旧option

                                        // 等待Vue渲染新数据源（industry/industry2）
                                        that.$nextTick(() => {
                                            $serviceTypeSelect.append('<option value="">请选择</option>');
                                            $serviceTypeSelect.select2({
                                                placeholder: "请选择行业类型",
                                                allowClear: true
                                            });
                                            // 选中历史行业值（此时是正确的code：移动A/电信0）
                                            if (that.config.serviceType) {
                                                $serviceTypeSelect.val(that.config.serviceType).trigger('change');
                                            }
                                        });
                                    }
                                }, 200);
                            }
                        } else {
                            layer.msg(res.msg, {icon: 2})
                        }
                    },
                    error: function () {
                        layer.msg('请求ChatBot配置信息失败', {icon: 2})
                    }
                })
            },
            saveConfig() {
                if (!this.validParams()) {
                    return
                }
                const submitData = JSON.parse(JSON.stringify(this.config));
                const apiType = submitData.apiType;
                const chatbotId = submitData.chatbotId || "";
                if (apiType === "telecom5G") {
                    submitData.chatbotId = chatbotId.trim() + "@botplatform.189.cn";

                } else if (apiType === "unicom5G") {
                    submitData.chatbotId = chatbotId.trim() + "@botplatform.189.cn";

                }
                for (const key in submitData) {
                    // 将所有空字符串字段转为null
                    if (submitData[key] === "") {
                        submitData[key] = null;
                    }
                }
                const index = layer.load(1, {shade: [0.8, '#393D49'], scrollbar: false})
                const params = Object.assign({}, this.config, {needDirectActivate: this.debugSwitch})
                let uploadUrl = '${basePage}/chatbot/saveChatBotConfig.html';
                if (this.config.apiType !== 'mobile5G') {
                    uploadUrl = '${basePage}/chatbot/saveTelecomChatBotConfig.html';
                }
                $.ajax({
                    url: uploadUrl,
                    type: 'post',
                    contentType: 'application/json; charset=utf-8',
                    data: JSON.stringify(submitData),
                    success: function (res) {
                        layer.close(index)
                        if (res.code === 0) {
                            layer.msg('保存成功', {icon: 1})
                        } else {
                            layer.msg(res.msg, {icon: 2})
                        }
                    },
                    error: function () {
                        layer.close(index)
                        layer.msg('保存ChatBot配置信息失败', {icon: 2})
                    }
                })
            },
            colorSelect(e) {
                this.config.color = e.target.value
            },
            showFileSelect(name) {
                $('input[name=' + name + ']').click()
            },
            loadAgentList() {
                const that = this;
                $.ajax({
                    url: '${basePage}/chatbot/getListCspAgent.html',
                    type: 'get',
                    success: function (res) {
                        if (res.code === 0) {
                            that.agentList = res.data
                            if (that.config.agentId) {
                                that.loadAgentServiceCode(that.config.agentId);
                            }
                        } else {
                            layer.msg(res.msg, {icon: 2})
                        }
                    },
                    error: function () {
                        layer.msg('请求代理商列表失败', {icon: 2})
                    }
                })
            },
            loadNoSignCustomerList() {
                const that = this;
                $.ajax({
                    url: '${basePage}/chatbot/getListCspNoSignCustomer.html',
                    type: 'post',
                    data: {agentId: that.config.agentId},
                    success: function (res) {
                        if (res.code === 0) {
                            that.noSignCustomerList = res.data
                            that.noSignCustomerListFull = res.data
                        } else {
                            layer.msg(res.msg, {icon: 2})
                        }
                    },
                    error: function () {
                        layer.msg('请求非直签客户列表失败', {icon: 2})
                    }
                })
            },
            setPortServiceType(){
                let portServiceType = this.config.portServiceType
                if(portServiceType&&portServiceType.length>0){
                    portServiceType = portServiceType.split("&")
                    for(let item of portServiceType){
                        $(".portService").each((index,el) => {
                            let tmp = $(el).val()
                            if(tmp == item){
                                $(el).attr("checked",true)
                            }
                        })
                    }
                }
            },
            portServiceTypeMapChangeHandler2() {
                const arr = []
                $(".portService:checked").each((index,el) => {
                    arr.push($(el).val())
                })

                if (arr.includes("01") || arr.includes("02") || arr.includes("04")) {
                    $(".portServiceType2").attr("disabled",true)
                    $(".portServiceType3").attr("disabled",true)
                    $(".portServiceType1").attr("disabled",false)
                    $(".portServiceType2-1").css("display","none")
                    this.config.adMarketTndustryType = "";
                }
                if (arr.includes("03") ) {
                    $(".portServiceType3").attr("disabled",true)
                    $(".portServiceType1").attr("disabled",true)
                    $(".portServiceType2").attr("disabled",false)
                    $(".portServiceType2-1").css("display","")
                }
                if (arr.includes("05")) {
                    $(".portServiceType2").attr("disabled",true)
                    $(".portServiceType1").attr("disabled",true)
                    $(".portServiceType3").attr("disabled",false)
                    $(".portServiceType2-1").css("display","none")
                    this.config.adMarketTndustryType = "";
                }
                if(arr.length===0){
                    $(".portServiceType2").attr("disabled",false)
                    $(".portServiceType1").attr("disabled",false)
                    $(".portServiceType3").attr("disabled",false)
                    $(".portServiceType2-1").css("display","none")
                    this.config.adMarketTndustryType = "";
                }
                this.config.portServiceType = arr.join("&")

            },
            uploadFileHandler(e) {
                const files = e.target.files;
                if (files.length === 0) {
                    layer.msg('未选择文件或取消选择', {icon: 2})
                    return
                }
                const key = e.target.id
                const that = this;
                const file = files[0]
                const fileName = file.name
                layer.confirm('确定上传文件' + fileName + '吗？', {
                    btn: ['确定', '取消'],
                    title: '提示',
                    icon: 3
                }, function () {
                    that.executeUpload(file, key, that)
                }, function () {
                    layer.msg('取消上传文件', {icon: 2})
                })
            },
            executeUpload(file, key, obj) {
                const index = layer.load(1, {shade: [0.8, '#393D49'], scrollbar: false})
                const formData = new FormData;
                formData.append('file', file, file.name)
                formData.append('agentId', obj.config.agentId);
                const isNonMobile5G = obj.config.apiType !== 'mobile5G';
                let uploadUrl = '${basePage}/chatbot/uploadCspResource.html';
                if (key === 'logoLocal' && obj.config.apiType !== 'mobile5G') {
                    formData.append('type', '0');
                    uploadUrl = '${basePage}/chatbot/uploadTelecomChatBotResource.html';
                }
                if (key.includes('accreditLocal') && obj.config.apiType !== 'mobile5G') {
                    formData.append('nosignCustomerId', obj.config.nosignCustomerId);
                    uploadUrl = '${basePage}/chatbot/uploadTelecomChatBotAccredit.html';
                }
                $.ajax({
                    url: uploadUrl,
                    type: 'post',
                    data: formData,
                    contentType: false,
                    processData: false,
                    success: function (res) {
                        layer.close(index)
                        if (res.code === 0) {
                            layer.msg('上传成功', {icon: 1})
                            if (isNonMobile5G) {
                                let value = res.data;
                                console.log("结果" + value)
                                const [remoteUrl, localName] = String(value).split(',', 2);
                                let baseKey = key;
                                if (key.includes('accreditLocal')) {
                                    // 提取数字后缀：accreditLocal1 → 1
                                    const num = key.replace('accreditLocal', '');
                                    baseKey = 'accredit' + num; // 生成 baseKey：accredit1
                                }
                                // 2. 处理Logo上传（logoLocal → logo）（保留原有逻辑）
                                else if (key === 'logoLocal') {
                                    baseKey = key.replace('Local', ''); // logoLocal → logo
                                }
                                if (baseKey) {
                                    obj.config[baseKey] = remoteUrl; // 如：config.accredit1 = "远程文件路径.pdf"
                                    obj.config[key] = localName;     // 如：config.accreditLocal1 = "本地文件名.pdf"
                                }
                            } else {
                                obj.config[key] = res.data
                            }
                        } else {
                            layer.msg(res.msg, {icon: 2})
                        }
                    },
                    error: function () {
                        layer.close(index)
                        layer.msg('上传文件失败', {icon: 2})
                    }
                })
            },
            addSign() {
                this.config.sign = '【】'
                const el = this.$refs.signInput;
                el.focus()
                this.$nextTick(() => {
                    el.selectionStart = 1
                    el.selectionEnd = 1
                })
            },
            validParams() {
                if (!this.config.agentId) {
                    layer.msg('归属代理商ID参数不能为空', {icon: 2})
                    return false
                }
                if (!this.config.name) {
                    layer.msg('Chatbot名称参数不能为空', {icon: 2})
                    return false
                }
                if (!this.config.serviceCode) {
                    layer.msg('服务代码参数不能为空', {icon: 2})
                    return false
                }
                if (!this.config.chatbotId) {
                    layer.msg('chatbotId参数不能为空', {icon: 2})
                    return false
                }
                if (!this.config.serviceType) {
                    layer.msg('行业类型参数不能为空', {icon: 2})
                    return false
                }
                if (!this.config.sign) {
                    layer.msg('签名参数不能为空', {icon: 2})
                    return false
                }
                if (this.config.apiType === "mobile5G") {
                    if (this.debugSwitch === '0') {
                        if (!this.config.debugPhone) {
                            layer.msg('调试白名单参数不能为空', {icon: 2})
                            return false
                        }
                    }
                    if (!this.config.auditer) {
                        layer.msg('审核人员参数不能为空', {icon: 2})
                        return false
                    }
                    if (!this.config.auditerMsg) {
                        layer.msg('审核意见参数不能为空', {icon: 2})
                        return false
                    }
                    if (!this.config.auditerDate) {
                        layer.msg('审核日期参数不能为空', {icon: 2})
                        return false
                    }

                    if (!this.config.nosignCustomerId&&!this.config.customerName) {
                        layer.msg('非直签客户未选择，客户名称不能为空', {icon: 2})
                        return false
                    }

                    if (!this.config.nosignCustomerId&&!this.config.creditCode) {
                        layer.msg('非直签客户未选择，统一社会信用代码不能为空', {icon: 2})
                        return false
                    }

                    if (!this.config.nosignCustomerId&&!this.config.representativeName) {
                        layer.msg('非直签客户未选择，责任人（含法人）姓名不能为空', {icon: 2})
                        return false
                    }

                    if (!this.config.nosignCustomerId&&!this.config.representativeCertificateType) {
                        layer.msg('非直签客户未选择，责任人（含法人）证件类型必选', {icon: 2})
                        return false
                    }

                    if (!this.config.nosignCustomerId&&!this.config.representativeCertificateNumber) {
                        layer.msg('非直签客户未选择，责任人（含法人）证件号码不能为空', {icon: 2})
                        return false
                    }

                    if (!this.config.nosignCustomerId&&!this.config.institutionLicenceType) {
                        layer.msg('非直签客户未选择，单位证件类型不能为空', {icon: 2})
                        return false
                    }
                }
                if (this.config.apiType !== "mobile5G") {
                    if (!this.config.nosignCustomerId) {
                        layer.msg('非直签客户ID参数不能为空', {icon: 2})
                        return false
                    }
                    if (!this.config.smsNumber) {
                        layer.msg('短信端口号不能为空', {icon: 2})
                        return false
                    }
                    if (!this.config.logo) {
                        layer.msg('logo参数不能为空', {icon: 2})
                        return false
                    }
                    if (!this.config.serviceDescription) {
                        layer.msg('Chatbot描述信息不能为空', {icon: 2})
                        return false
                    }
                    if (!this.config.provider) {
                        layer.msg('提供者名称不能为空', {icon: 2})
                        return false
                    }
                    if (!this.config.showProvider) {
                        layer.msg('是否显示Chatbot提供者名称不能为空', {icon: 2})
                        return false
                    }
                    if (!this.config.ipWhiteList) {
                        layer.msg('IP白名单不能为空', {icon: 2})
                        return false
                    }
                }

                return true
            },
            copyText(text) {
                let input = document.createElement("input");
                input.value = text;
                document.body.appendChild(input);
                input.select();
                document.execCommand("Copy");
                document.body.removeChild(input);
                layer.msg('复制成功');
            },
            updateServiceCodeList() {
                let that = this;
                const selectedCustomerId = that.config.nosignCustomerId;
                if (!selectedCustomerId) {
                    that.serviceCodeList = '';
                    that.config.apiType = "mobile5G";
                    that.config.serviceType = '';
                    const $serviceTypeSelect = $("select[name='serviceType']");
                    if ($serviceTypeSelect.data('select2')) {
                        $serviceTypeSelect.select2('destroy');
                    }
                    $serviceTypeSelect.empty();
                    that.$nextTick(() => {
                        $serviceTypeSelect.append('<option value="">请选择</option>');
                        // 重新初始化select2
                        $serviceTypeSelect.select2({
                            placeholder: "请选择行业类型",
                            allowClear: true
                        });
                    });
                    return;
                }
                const selectedCustomer = that.noSignCustomerList.find(
                    customer => customer.customerId === selectedCustomerId
                );
                if (!selectedCustomer) {
                    layer.msg('未找到选中的非直签客户', {icon: 2});
                    return;
                }
                that.config.serviceType = '';
                const oldApiType = that.config.apiType;
                that.config.apiType = selectedCustomer.apiType;
                if (that.config.apiType !== 'mobile5G') {
                    that.config.actualSendIndustry = ''; // 非移动时清空值
                }
                const $serviceTypeSelect = $("select[name='serviceType']");
                if ($serviceTypeSelect.data('select2')) {
                    $serviceTypeSelect.select2('destroy'); // 销毁旧select2
                }
                $serviceTypeSelect.empty();
                that.config.apiType = selectedCustomer.apiType;
                console.log("当前apiType（后端返回）：", that.config.apiType);
                that.$nextTick(() => {
                    // 重新初始化select2
                    $serviceTypeSelect.append('<option value="">请选择</option>');
                    $serviceTypeSelect.select2({
                        placeholder: "请选择行业类型",
                        allowClear: true
                    });
                });

                $.ajax({
                    url: '${basePage}/chatbot/getServiceCodeList.html',
                    type: 'post',
                    data: {
                        customerId: that.config.nosignCustomerId
                    },
                    success: function (res) {
                        if (res.code === 0) {
                            that.serviceCodeList = res.data
                        } else {
                            // layer.msg(res.msg, {icon: 2})
                        }
                    },
                    error: function () {
                        layer.msg('获取该客户服务代码列表失败', {icon: 2})
                    }
                })
            },
            backList() {
                window.location.href = "${basePage}/chatbot/chatbotList.html"
            },
            filterNoSignCustomerKeyword() {
                // customerName
                if (!this.noSignCustomerKeyword) {
                    this.noSignCustomerList = this.noSignCustomerListFull
                } else {
                    const temp = []
                    for (const item of this.noSignCustomerListFull) {
                        if (item.customerName.includes(this.noSignCustomerKeyword)) {
                            temp.push(item)
                        }
                    }
                    this.noSignCustomerList = temp
                    const el = this.$refs.nosignCustomerSelect;
                    el.focus()
                }
            },
            addAccredit() {
                if (this.accreditList.length < 5) {
                    this.accreditList.push(1); // 数组长度+1，触发新上传框渲染
                } else {
                    layer.msg('最多只能添加5个授权书', {icon: 2});
                }
            },
            removeAccredit(index) {
                // 删除对应索引的上传框
                this.accreditList.splice(index, 1);
                // 清空已上传的文件数据（避免残留）
                const accreditKey = 'accreditLocal' + (index + 1);
                this.config[accreditKey] = "";
            },
            handleAccreditUpload(index) {
                const accreditIndex = index + 1; // 上传框序号（1~5）
                const fileInput = document.getElementById('accreditLocal' + accreditIndex);
                const files = fileInput.files;

                if (files.length === 0) {
                    layer.msg('未选择文件', {icon: 2});
                    return;
                }
                const file = files[0];
                const fileName = file.name;
                const that = this;
                // 确认上传弹窗
                layer.confirm(`确定上传授权书` + fileName + `吗？`, {
                    btn: ['确定', '取消'],
                    title: '提示',
                    icon: 3
                }, function () {
                    that.executeUpload(file, 'accreditLocal' + accreditIndex, that);;
                }, function () {
                    layer.msg('取消上传', {icon: 2});
                    // 重置文件选择框（避免重复上传同一文件）
                    fileInput.value = "";
                });
            },
            useDefaultLogo() {
                const that = this;
                layer.confirm('确定使用默认LOGO吗？', {
                    btn: ['确定', '取消'],
                    title: '提示',
                    icon: 3
                }, function () {
                    const index = layer.load(1, {shade: [0.8, '#393D49'], scrollbar: false});
                    const formData = new FormData();
                    // 关键：传递默认流标记（让后端识别用默认流，而非真实文件）
                    formData.append('agentId', that.config.agentId);
                    formData.append('type', '0'); // 保持与原LOGO上传一致的类型参数

                    // 调用与原上传相同的接口
                    $.ajax({
                        url: '${basePage}/chatbot/uploadDefaultLogo.html',
                        type: 'post',
                        data: formData,
                        contentType: false,
                        processData: false,
                        success: function (res) {
                            layer.close(index);
                            if (res.code === 0) {
                                layer.msg('默认LOGO设置成功', {icon: 1});
                                // 解析后端返回的默认LOGO路径（与原上传逻辑一致）
                                const [remoteUrl, localName] = String(res.data).split(',', 2);
                                that.config.logo = remoteUrl; // 赋值远程路径（用于提交）
                                that.config.logoLocal = localName; // 赋值本地名称（用于预览）
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        },
                        error: function () {
                            layer.close(index);
                            layer.msg('默认LOGO请求失败', {icon: 2});
                        }
                    });
                });
            }
        }
    })

    function portServiceTypeMapChangeHandler() {
       const arr = []
       $(".portService:checked").each((index,el) => {
           arr.push($(el).val())
       })

        if (arr.includes("01") || arr.includes("02") || arr.includes("04")) {
            $(".portServiceType2").attr("disabled",true)
            $(".portServiceType3").attr("disabled",true)
            $(".portServiceType1").attr("disabled",false)
            $(".portServiceType2-1").css("display","none")
            app.$data.config.adMarketTndustryType = "";
        }
        if (arr.includes("03") ) {
            $(".portServiceType3").attr("disabled",true)
            $(".portServiceType1").attr("disabled",true)
            $(".portServiceType2").attr("disabled",false)
            $(".portServiceType2-1").css("display","")
        }
        if (arr.includes("05")) {
            $(".portServiceType2").attr("disabled",true)
            $(".portServiceType1").attr("disabled",true)
            $(".portServiceType3").attr("disabled",false)
            $(".portServiceType2-1").css("display","none")
            app.$data.config.adMarketTndustryType = "";
        }
        if(arr.length===0){
            $(".portServiceType2").attr("disabled",false)
            $(".portServiceType1").attr("disabled",false)
            $(".portServiceType3").attr("disabled",false)
            $(".portServiceType2-1").css("display","none")
            app.$data.config.adMarketTndustryType = "";
        }
        app.$data.config.portServiceType = arr.join("&")

    }

    function showIdCreateText() {
        layer.open({
            type: 1,
            title: 'ChatBotID创建规则说明',
            area: ['400px', '300px'],
            closeBtn: 0,
            shadeClose: true,
            content: `<div style="width: calc(100% - 20px);margin: 10px auto;font-size: 14px"> 1. chatbotid 由数字部分+域名部分组成，数字部分建议不超过15位，最长不能超过20位<br>2.数字部分包含服务代码+非直签客户扩展码+chatbot扩展部分<br>3.域名部分固定@botplatform.rcs.chinamobile.com
<br>4.非直签用户服务代码包含服务代码+扩展码，建议不超过15位，最长不能超过20位，需预留出chatbot的扩展位</div>`,
        });
    }

    function addIdSuffix(key) {
        let val = app.$data.config[key]
        if (val) {
            val = val + "@botplatform.rcs.chinamobile.com"
        } else {
            val = "@botplatform.rcs.chinamobile.com"
        }
        app.$data.config[key] = val
    }
</script>
</html>
