{% extends "base.html" %}

{% block content %}
    <div class="row clearfix">
        <div class="col-md-3">
            <div class="panel panel-default">
                <div class="panel-heading">
                    选择操作
                </div>
                <div class="panel-body">
                    <div id="div-config" class="form-group">
                        <h5 class="control-label text-bold">配置项：</h5>
                        <div class="form-group">
                            <select id="config" name="confg"
                                    class="selectpicker show-tick form-control bs-select-hidden"
                                    data-name="配置项" data-placeholder="请选择配置项:" required>
                                <option value="is-empty" disabled="">请选择配置项:</option>
                                <option value="0" selected="selected">系统设置</option>
                                <option value="1">工单审核流配置</option>
                            </select>
                        </div>
                    </div>
                    <div id="div-workflow" class="form-group" style="display: none">
                        <h5 class="control-label text-bold">工单类型：</h5>
                        <div class="form-group">
                            <select id="workflow_type" name="group"
                                    class="selectpicker show-tick form-control bs-select-hidden"
                                    data-name="工单类型" data-placeholder="请选择工单类型:" required>
                                <option value="{{ WorkflowDict.workflow_type.sqlreview }}" selected="selected">
                                    {{ WorkflowDict.workflow_type.sqlreview_display }}</option>
                                <option value="{{ WorkflowDict.workflow_type.query }}">
                                    {{ WorkflowDict.workflow_type.query_display }}</option>
                                <option value="{{ WorkflowDict.workflow_type.archive }}">
                                    {{ WorkflowDict.workflow_type.archive_display }}</option>
                            </select>
                        </div>
                        <h5 class="control-label text-bold">组：</h5>
                        <div class="form-group">
                            <select id="group" name="group"
                                    class="selectpicker show-tick form-control bs-select-hidden"
                                    data-name="组" data-placeholder="请选择组:" data-live-search="true" required>
                                {% for group in group_list %}
                                    <option value="{{ group.group_name }}">{{ group.group_name }}</option>
                                {% endfor %}
                            </select>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-9 column">
            <div class="panel panel-default">
                <div class="panel-heading">
                    操作
                </div>
                <div class="panel-body">
                    <div id="div-system-config" class="form-group" style="display: none">
                        <h4 style="color: darkgrey; display: inline;"><b>Inception配置</b></h4>&nbsp;&nbsp;&nbsp;
                        <h6 style="color:red">注1：默认使用goInception进行MySQL的审核执行，Inception作为脱敏解析工具使用</h6>
                        <h6 style="color:red">注2：备份库的信息仅用于平台获取回滚语句，需要和审核工具自身的配置保持一致，此处配置变更不会修改审核工具自身的配置</h6>
                        <hr/>
                        <div class="form-horizontal">
                            <div class="form-group">
                                <label for="inception"
                                       class="col-sm-4 control-label">INCEPTION</label>
                                <div class="col-sm-8">
                                    <div class="switch switch-small">
                                        <label>
                                            <input id="inception"
                                                   key="inception"
                                                   value="{{ config.inception }}"
                                                   type="checkbox">
                                            是否切换为Inception审核
                                        </label>
                                    </div>
                                </div>
                            </div>

                            <div id="div-go-inception-config">
                                <div class="form-group">
                                    <label for="go_inception_host"
                                           class="col-sm-4 control-label">GO_INCEPTION_HOST</label>
                                    <div class="col-sm-5">
                                        <input type="text" class="form-control"
                                               id="go_inception_host"
                                               key="go_inception_host"
                                               value="{{ config.go_inception_host }}"
                                               placeholder="goInception地址">
                                    </div>
                                    <button id='check_goincption' class='btn-sm btn-info'>测试连接</button>&nbsp;
                                </div>
                                <div class="form-group">
                                    <label for="go_inception_port"
                                           class="col-sm-4 control-label">GO_INCEPTION_PORT</label>
                                    <div class="col-sm-5">
                                        <input type="number" class="form-control"
                                               id="go_inception_port"
                                               key="go_inception_port"
                                               value="{{ config.go_inception_port }}"
                                               placeholder="goInception端口">
                                    </div>
                                </div>
                            </div>
                            <div id="div-inception-config">
                                <div class="form-group">
                                    <label for="inception_host"
                                           class="col-sm-4 control-label">INCEPTION_HOST</label>
                                    <div class="col-sm-5">
                                        <input type="text" class="form-control"
                                               id="inception_host"
                                               key="inception_host"
                                               value="{{ config.inception_host }}"
                                               placeholder="Inception地址">
                                    </div>
                                    <button id='check_incption' class='btn-sm btn-info'>测试连接</button>
                                </div>
                                <div class="form-group">
                                    <label for="inception_port"
                                           class="col-sm-4 control-label">INCEPTION_PORT</label>
                                    <div class="col-sm-5">
                                        <input type="number" class="form-control"
                                               id="inception_port"
                                               key="inception_port"
                                               value="{{ config.inception_port }}"
                                               placeholder="Inception端口">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="inception_remote_backup_host"
                                           class="col-sm-4 control-label">REMOTE_BACKUP_HOST</label>
                                    <div class="col-sm-5">
                                        <input type="text" class="form-control"
                                               id="inception_remote_backup_host"
                                               key="inception_remote_backup_host"
                                               value="{{ config.inception_remote_backup_host }}"
                                               placeholder="备份库地址(Inception/goInception复用)">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="inception_remote_backup_port"
                                           class="col-sm-4 control-label">REMOTE_BACKUP_PORT</label>
                                    <div class="col-sm-5">
                                        <input type="number" class="form-control"
                                               id="inception_remote_backup_port"
                                               key="inception_remote_backup_port"
                                               value="{{ config.inception_remote_backup_port }}"
                                               placeholder="备份库端口(Inception/goInception复用)">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="inception_remote_backup_user"
                                           class="col-sm-4 control-label">REMOTE_BACKUP_USER</label>
                                    <div class="col-sm-5">
                                        <input type="text" autocomplete="new-password" class="form-control"
                                               id="inception_remote_backup_user"
                                               key="inception_remote_backup_user"
                                               value="{{ config.inception_remote_backup_user }}"
                                               placeholder="备份库用户(Inception/goInception复用)">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="inception_remote_backup_password"
                                           class="col-sm-4 control-label">REMOTE_BACKUP_PASSWORD</label>
                                    <div class="col-sm-5">
                                        <input type="password" autocomplete="new-password" class="form-control"
                                               id="inception_remote_backup_password"
                                               key="inception_remote_backup_password"
                                               value="{{ config.inception_remote_backup_password }}"
                                               placeholder="备份库用户密码(Inception/goInception复用)">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <br>
                        <h4 style="color: darkgrey"><b>功能模块配置</b></h4>
                        <hr/>
                        <h5 style="color: darkgrey"><b>SQL上线</b></h5>
                        <hr/>
                        <div class="form-horizontal">
                            <div class="form-group">
                                <label for="critical_ddl_regex"
                                       class="col-sm-4 control-label">CRITICAL_DDL_REGEX</label>
                                <div class="col-sm-5">
                                    <input type="text" class="form-control"
                                           id="critical_ddl_regex"
                                           key="critical_ddl_regex"
                                           value="{{ config.critical_ddl_regex }}"
                                           placeholder="正则条件，匹配的语句会禁止提交">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="auto_review_wrong"
                                       class="col-sm-4 control-label">AUTO_REVIEW_WRONG</label>
                                <div class="col-sm-5">
                                    <input type="number" class="form-control"
                                           id="auto_review_wrong"
                                           key="auto_review_wrong"
                                           value="{{ config.auto_review_wrong }}"
                                           placeholder="自动驳回的等级，1表示警告驳回，2和空表示错误才驳回，其他表示不驳回">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="enable_backup_switch"
                                       class="col-sm-4 control-label">ENABLE_BACKUP_SWITCH</label>
                                <div class="col-sm-8">
                                    <div class="switch switch-small">
                                        <label>
                                            <input id="enable_backup_switch"
                                                   key="enable_backup_switch"
                                                   value="{{ config.enable_backup_switch }}"
                                                   type="checkbox">
                                            是否开启备份选项(默认关闭, 强制要求备份)
                                        </label>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="auto_review"
                                       class="col-sm-4 control-label">AUTO_REVIEW</label>
                                <div class="col-sm-8">
                                    <div class="switch switch-small">
                                        <label>
                                            <input id="auto_review"
                                                   key="auto_review"
                                                   value="{{ config.auto_review }}"
                                                   type="checkbox">
                                            是否开启SQL上线自动审批
                                        </label>
                                    </div>
                                </div>
                            </div>
                            <div id="div-auto-review-config" style="display: none">
                                <div class="form-group">
                                    <label for="auto_review_tag"
                                           class="col-sm-4 control-label">AUTO_REVIEW_TAG</label>
                                    <div class="col-sm-5">
                                        <input type="text" class="form-control"
                                               id="auto_review_tag"
                                               key="auto_review_tag"
                                               value="{{ config.auto_review_tag }}"
                                               placeholder="自动审批的实例标签CODE，多个使用逗号分隔">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="auto_review_regex"
                                           class="col-sm-4 control-label">AUTO_REVIEW_REGEX</label>
                                    <div class="col-sm-5">
                                        <input type="text" class="form-control"
                                               id="auto_review_regex"
                                               key="auto_review_regex"
                                               value="{{ config.auto_review_regex }}"
                                               placeholder="正则条件，匹配的语句需要人工审批">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="auto_review_max_update_rows"
                                           class="col-sm-4 control-label">MAX_UPDATE_ROWS</label>
                                    <div class="col-sm-5">
                                        <input type="number" class="form-control"
                                               id="auto_review_max_update_rows"
                                               key="auto_review_max_update_rows"
                                               value="{{ config.auto_review_max_update_rows }}"
                                               placeholder="自动审批允许工单最大更新行数">
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="manual"
                                       class="col-sm-4 control-label">MANNUAL</label>
                                <div class="col-sm-8">
                                    <div class="switch switch-small">
                                        <label>
                                            <input id="manual"
                                                   key="manual"
                                                   value="{{ config.manual }}"
                                                   type="checkbox">
                                            是否开启SQL上线手工执行确认
                                        </label>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <h5 style="color: darkgrey"><b>SQL查询</b></h5>
                        <h6 style="color:red">注：开启脱敏功能必须要配置Inception信息，用于SQL语法解析</h6>
                        <hr/>
                        <div class="form-horizontal">
                            <div class="form-group">
                                <label for="data_masking"
                                       class="col-sm-4 control-label">DATA_MASKING</label>
                                <div class="col-sm-8">
                                    <div class="switch switch-small">
                                        <label>
                                            <input id="data_masking"
                                                   key="data_masking"
                                                   value="{{ config.data_masking }}" type="checkbox">
                                            是否开启动态脱敏
                                        </label>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="query_check"
                                       class="col-sm-4 control-label">QUERY_CHECK</label>
                                <div class="col-sm-8">
                                    <div class="switch switch-small">
                                        <label>
                                            <input id="query_check"
                                                   key="query_check"
                                                   value="{{ config.query_check }}" type="checkbox">
                                            是否开启脱敏校验(无法脱敏的语句会抛错)
                                        </label>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="disable_star"
                                       class="col-sm-4 control-label">DISABLE_STAR</label>
                                <div class="col-sm-8">
                                    <div class="switch switch-small">
                                        <label>
                                            <input id="disable_star"
                                                   key="disable_star"
                                                   value="{{ config.disable_star }}" type="checkbox">
                                            禁止在查询时使用 *
                                        </label>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="inception_remote_backup_port"
                                       class="col-sm-4 control-label">MAX_EXECUTION_TIME</label>
                                <div class="col-sm-5">
                                    <input type="number" class="form-control" id="max_execution_time"
                                           key="max_execution_time"
                                           value="{{ config.max_execution_time }}"
                                           placeholder="在线查询超时时间阈值，单位秒，默认60">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="admin_query_limit"
                                       class="col-sm-4 control-label">ADMIN_QUERY_LIMIT</label>
                                <div class="col-sm-5">
                                    <input type="number" class="form-control" id="admin_query_limit"
                                           key="admin_query_limit"
                                           value="{{ config.admin_query_limit }}"
                                           placeholder="管理员/DBA查询结果集限制">
                                </div>
                            </div>
                            <h5 style="color: darkgrey"><b>SQL优化</b></h5>
                            <hr/>
                            <div class="form-group">
                                <label for="sqladvisor"
                                       class="col-sm-4 control-label">SQLADVISOR_PATH</label>
                                <div class="col-sm-5">
                                    <div class="switch switch-small">
                                        <input type="text" class="form-control" id="sqladvisor" key="sqladvisor"
                                               value="{{ config.sqladvisor }}"
                                               placeholder="SQLADVISOR路径">
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="soar"
                                       class="col-sm-4 control-label">SOAR_PATH</label>
                                <div class="col-sm-5">
                                    <div class="switch switch-small">
                                        <input type="text" class="form-control" id="soar" key="soar"
                                               value="{{ config.soar }}"
                                               placeholder="SOAR路径">
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="soar_test_dsn"
                                       class="col-sm-4 control-label">SOAR_TEST_DSN</label>
                                <div class="col-sm-5">
                                    <div class="switch switch-small">
                                        <input type="text" class="form-control" id="soar_test_dsn" key="soar_test_dsn"
                                               value="{{ config.soar_test_dsn }}"
                                               placeholder="SOAR测试实例：user:pwd@host:port/db">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <br>
                        <h4 style="color: darkgrey;display: inline"><b>通知配置</b></h4>&nbsp;&nbsp;&nbsp;
                        <h6 style="color:red">注1：钉钉Webhook需要配置资源组Webhook地址，Webhook无法通知到指定用户</h6>
                        <h6 style="color:red">注2：企业微信自建应用开启后，若未配置企业微信UserID，则默认取用户的username作为UserId</h6>
                        <h6 style="color:red">注3：企业微信群机器人需要配置资源组webhook地址（企业微信群机器人是通知到群；企业微信自建应用是通过应用通知到指定人）</h6>
                        <hr/>
                        <div class="form-horizontal">
                            <div class="form-group">
                                <label for="archery_base_url"
                                       class="col-sm-4 control-label">ARCHERY_BASE_URL</label>
                                <div class="col-sm-5">
                                    <input type="text" class="form-control"
                                           id="archery_base_url"
                                           key="archery_base_url"
                                           value="{{ config.archery_base_url }}"
                                           placeholder="系统HOST地址, 用于通知链接，如https://archery.com">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="ddl_notify_auth_group"
                                       class="col-sm-4 control-label">DDL_NOTIFY_AUTH_GROUP</label>
                                <div class="col-sm-5">
                                    <input type="text" class="form-control"
                                           id="ddl_notify_auth_group"
                                           key="ddl_notify_auth_group"
                                           value="{{ config.ddl_notify_auth_group }}"
                                           placeholder="DDL工单通知权限组名，为空则不通知">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="mail"
                                       class="col-sm-4 control-label">MAIL</label>
                                <div class="col-sm-8">
                                    <div class="switch switch-small">
                                        <label>
                                            <input id="mail" key="mail" value="{{ config.mail }}"
                                                   type="checkbox"> 是否开启邮件通知
                                        </label>
                                        <button id='check_email' class='btn-sm btn-info'>测试连接</button>
                                    </div>
                                </div>
                            </div>
                            <div id="div-mail-config" style="display: none">
                                <div class="form-group">
                                    <label for="mail_ssl"
                                           class="col-sm-4 control-label">MAIL_SSL</label>
                                    <div class="col-sm-8">
                                        <div class="switch switch-small">
                                            <label>
                                                <input id="mail_ssl" key="mail_ssl"
                                                       value="{{ config.mail_ssl }}"
                                                       type="checkbox"> 是否使用SSL
                                            </label>
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="mail_smtp_server"
                                           class="col-sm-4 control-label">MAIL_SMTP_SERVER</label>
                                    <div class="col-sm-5">
                                        <input type="text" class="form-control"
                                               id="mail_smtp_server"
                                               key="mail_smtp_server"
                                               value="{{ config.mail_smtp_server }}"
                                               placeholder="邮件smtp地址">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="mail_smtp_port"
                                           class="col-sm-4 control-label">MAIL_SMTP_PORT</label>
                                    <div class="col-sm-5">
                                        <input type="number" class="form-control"
                                               id="mail_smtp_port"
                                               key="mail_smtp_port"
                                               value="{{ config.mail_smtp_port }}"
                                               placeholder="邮件smtp端口">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="mail_smtp_user"
                                           class="col-sm-4 control-label">MAIL_SMTP_USER</label>
                                    <div class="col-sm-5">
                                        <input type="text" class="form-control"
                                               id="mail_smtp_user"
                                               key="mail_smtp_user"
                                               value="{{ config.mail_smtp_user }}"
                                               placeholder="邮箱账号">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="mail_smtp_password"
                                           class="col-sm-4 control-label">MAIL_SMTP_PASSWORD</label>
                                    <div class="col-sm-5">
                                        <input type="password" class="form-control"
                                               id="mail_smtp_password"
                                               key="mail_smtp_password"
                                               value="{{ config.mail_smtp_password }}"
                                               placeholder="邮箱账号密码">
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="ding"
                                       class="col-sm-4 control-label">DING</label>
                                <div class="col-sm-8">
                                    <div class="switch switch-small">
                                        <label>
                                            <input id="ding" key="ding" value="{{ config.ding }}"
                                                   type="checkbox"> 是否开启钉钉Webhook通知
                                        </label>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="ding_to_person"
                                       class="col-sm-4 control-label">DING_TO_PERSON</label>
                                <div class="col-sm-8">
                                    <div class="switch switch-small">
                                        <label>
                                            <input id="ding_to_person" key="ding_to_person"
                                                   value="{{ config.ding_to_person }}"
                                                   type="checkbox"> 是否开启钉钉个人消息通知
                                        </label>
                                    </div>
                                </div>
                            </div>
                            <div id="div-ding-config" style="display: none">
                                <div class="form-group">
                                    <i class="ace-icon fa fa-hand-o-right"></i>
                                    创建钉钉Archery微应用参考：<a target="_blank"
                                                        href="https://ding-doc.dingtalk.com/doc#/bgb96b/aw3h75  ">钉钉企业接入开发文档</a>
                                    &nbsp;&nbsp;&nbsp;<button id='sync_ding_user' class='btn-xs btn-warning'>同步用户
                                </button>
                                </div>
                                <div class="form-group">
                                    <label for="ding_agent_id"
                                           class="col-sm-4 control-label">AgentId</label>
                                    <div class="col-sm-5">
                                        <input type="text" class="form-control"
                                               id="ding_agent_id"
                                               key="ding_agent_id"
                                               value="{{ config.ding_agent_id }}"
                                               placeholder="微应用Archery的AgentId">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="ding_app_key"
                                           class="col-sm-4 control-label">AppKey</label>
                                    <div class="col-sm-5">
                                        <input type="text" class="form-control"
                                               id="ding_app_key"
                                               key="ding_app_key"
                                               value="{{ config.ding_app_key }}"
                                               placeholder="微应用Archery的AppKey">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="ding_app_secret"
                                           class="col-sm-4 control-label">AppSecret</label>
                                    <div class="col-sm-5">
                                        <input type="password" class="form-control"
                                               id="ding_app_secret"
                                               key="ding_app_secret"
                                               value="{{ config.ding_app_secret }}"
                                               placeholder="微应用Archery的AppSecret">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="ding_archery_username"
                                           class="col-sm-4 control-label">Archery2Ding</label>
                                    <div class="col-sm-5">
                                        <input type="text" class="form-control"
                                               id="ding_archery_username"
                                               key="ding_archery_username"
                                               value="{{ config.ding_archery_username }}"
                                               placeholder="Archery.username对应钉钉里的字段">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="ding_dept_ids"
                                           class="col-sm-4 control-label">DingDeptIDs</label>
                                    <div class="col-sm-5">
                                        <textarea class="form-control"
                                                  id="ding_dept_ids"
                                                  key="ding_dept_ids"
                                                  placeholder="钉钉部门id，使用逗号分隔">{{ config.ding_dept_ids }}</textarea>
                                    </div>
                                </div>
                            </div>

                            <div class="form-group">
                                <label for="wx"
                                       class="col-sm-4 control-label">企业微信自建应用</label>
                                <div class="col-sm-8">
                                    <div class="switch switch-small">
                                        <label>
                                            <input id="wx" key="wx"
                                                   value="{{ config.wx }}"
                                                   type="checkbox"> 是否开启企业微信自建应用通知
                                        </label>
                                    </div>
                                </div>
                            </div>
                            <div id="div-wx-config" style="display: none">
                                <div class="form-group">
                                    <i class="ace-icon fa fa-hand-o-right"></i>
                                    创建企业微信应用参考：<a target="_blank"
                                                  href="https://work.weixin.qq.com/api/doc#90000/90135/90226">企业微信接入开发文档</a>
                                    {#                                    &nbsp;&nbsp;&nbsp;<button id='sync_ding_user' class='btn-xs btn-warning'>同步用户#}
                                    </button>
                                </div>
                                <div class="form-group">
                                    <label for="wx_corpid"
                                           class="col-sm-4 control-label">CorpId</label>
                                    <div class="col-sm-5">
                                        <input type="text" class="form-control"
                                               id="wx_corpid"
                                               key="wx_corpid"
                                               value="{{ config.wx_corpid }}"
                                               placeholder="企业微信的企业ID">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="wx_agent_id"
                                           class="col-sm-4 control-label">AgentId</label>
                                    <div class="col-sm-5">
                                        <input type="text" class="form-control"
                                               id="wx_agent_id"
                                               key="wx_agent_id"
                                               value="{{ config.wx_agent_id }}"
                                               placeholder="应用的AgentId">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="wx_app_secret"
                                           class="col-sm-4 control-label">Secret</label>
                                    <div class="col-sm-5">
                                        <input type="password" class="form-control"
                                               id="wx_app_secret"
                                               key="wx_app_secret"
                                               value="{{ config.wx_app_secret }}"
                                               placeholder="应用的Secret">
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="qywx_webhook"
                                       class="col-sm-4 control-label">企业微信群机器人</label>
                                <div class="col-sm-8">
                                    <div class="switch switch-small">
                                        <label>
                                            <input id="qywx_webhook" key="qywx_webhook"
                                                   value="{{ config.qywx_webhook }}"
                                                   type="checkbox"> 是否开启企业微信群机器人通知
                                        </label>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="feishu_webhook"
                                       class="col-sm-4 control-label">飞书Webhook</label>
                                <div class="col-sm-8">
                                    <div class="switch switch-small">
                                        <label>
                                            <input id="feishu_webhook" key="feishu_webhook"
                                                   value="{{ config.feishu_webhook }}"
                                                   type="checkbox"> 是否开启飞书Webhook通知
                                        </label>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="feishu"
                                       class="col-sm-4 control-label">飞书</label>
                                <div class="col-sm-8">
                                    <div class="switch switch-small">
                                        <label>
                                            <input id="feishu" key="feishu"
                                                   value="{{ config.feishu }}"
                                                   type="checkbox"> 是否开启飞书通知
                                        </label>
                                    </div>
                                </div>
                            </div>
                            <div id="div-feishu-config" style="display: none">
                                <div class="form-group">
                                    <i class="ace-icon fa fa-hand-o-right"></i>
                                    创建飞书应用参考：<a target="_blank"
                                                href="https://open.feishu.cn/document/uQjL04CN/ukzM04SOzQjL5MDN">飞书接入开发文档</a>
                                    {#                                    &nbsp;&nbsp;&nbsp;<button id='sync_ding_user' class='btn-xs btn-warning'>同步用户#}
                                    </button>
                                </div>
                                <div class="form-group">
                                    <label for="feishu_appid"
                                           class="col-sm-4 control-label">飞书应用AppID</label>
                                    <div class="col-sm-5">
                                        <input type="text" class="form-control"
                                               id="feishu_appid"
                                               key="feishu_appid"
                                               value="{{ config.feishu_appid }}"
                                               placeholder="飞书应用AppID">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="feishu_app_secret"
                                           class="col-sm-4 control-label">飞书AppSecret</label>
                                    <div class="col-sm-5">
                                        <input type="text" class="form-control"
                                               id="feishu_app_secret"
                                               key="feishu_app_secret"
                                               value="{{ config.feishu_app_secret }}"
                                               placeholder="飞书AppSecret">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <br>
                        <h4 style="color: darkgrey"><b>其他配置</b></h4>
                        <hr/>
                        <div class="form-horizontal">
                            <div class="form-group">
                                <label for="index_path_url"
                                       class="col-sm-4 control-label">INDEX_PATH_URL</label>
                                <div class="col-sm-5">
                                    <input type="text" class="form-control"
                                           id="index_path_url"
                                           key="index_path_url"
                                           value="{{ config.index_path_url }}"
                                           placeholder="系统首页路径，默认SQL工单页面：sqlworkflow">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="lock_time_threshold"
                                       class="col-sm-4 control-label">BINLOG2SQL</label>
                                <div class="col-sm-5">
                                    <input type="text" class="form-control"
                                           id="binlog2sql"
                                           key="binlog2sql"
                                           value="{{ config.binlog2sql }}"
                                           placeholder="binlog2sql调用路径，类似/opt/binlog2sql.py">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="default_auth_group"
                                       class="col-sm-4 control-label">DEFAULT_AUTH_GROUP</label>
                                <div class="col-sm-5">
                                    <input type="text" class="form-control"
                                           id="default_auth_group"
                                           key="default_auth_group"
                                           value="{{ config.default_auth_group }}"
                                           placeholder="默认权限组名，新用户首次登录自动关联, 老用户请手动配置">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="default_resource_group"
                                       class="col-sm-4 control-label">DEFAULT_RESOURCE_GROUP</label>
                                <div class="col-sm-5">
                                    <input type="text" class="form-control"
                                           id="default_resource_group"
                                           key="default_resource_group"
                                           value="{{ config.default_resource_group }}"
                                           placeholder="默认资源组名，新用户首次登录自动关联, 老用户请手动配置">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="lock_time_threshold"
                                       class="col-sm-4 control-label">LOCK_TIME_THRESHOLD</label>
                                <div class="col-sm-5">
                                    <input type="number" class="form-control"
                                           id="lock_time_threshold"
                                           key="lock_time_threshold"
                                           value="{{ config.lock_time_threshold }}"
                                           placeholder="账户登录失败锁定时间(秒)">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="lock_cnt_threshold"
                                       class="col-sm-4 control-label">LOCK_CNT_THRESHOLD</label>
                                <div class="col-sm-5">
                                    <input type="number" class="form-control"
                                           id="lock_cnt_threshold"
                                           key="lock_cnt_threshold"
                                           value="{{ config.lock_cnt_threshold }}"
                                           placeholder="账户登录失败几次锁账户">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="sign_up_enabled"
                                       class="col-sm-4 control-label">SIGN_UP_ENABLED</label>
                                <div class="col-sm-8">
                                    <div class="switch switch-small">
                                        <label>
                                            <input id="sign_up_enabled" key="sign_up_enabled"
                                                   value="{{ config.sign_up_enabled }}"
                                                   type="checkbox"> 是否开启注册功能
                                        </label>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-offset-4 col-sm-10">
                                <button id="saveconfig" type="button" class="btn btn-default">保存配置</button>
                            </div>
                        </div>
                    </div>
                    <div id="div-workflow-config" class="form-inline form-group" style="display: none">
                        <h5 class="control-label text-bold" style="color: red">
                            规则：<b>选择多个权限组即审批流程为多级审核，按照选择顺序进行流转，权限组内用户都可审核</b></h5>
                        <br>
                        <h5 class="control-label text-bold">当前审批流程：<b id="workflow_auditors"></b></h5>
                        <br>
                        <div><h5 style="float:left;" class="control-label text-bold">变更审批流程：</h5></div>
                        <div class="form-group form-inline">
                            <div class="form-group">
                                <input type="text" class="form-control" id="workflow_auditors_text"
                                       disabled="disabled" style="width: 283px">
                            </div>
                            <div class="form-group">
                                <select id="group_auditors" name="group" title="请选择审批权限组"
                                        class="selectpicker show-tick form-control bs-select-hidden"
                                        data-name="审批人" data-placeholder="审批人:" data-live-search="true"
                                        required>
                                    {% for auth_group in auth_group_list %}
                                        <option value="{{ auth_group.name }}">{{ auth_group.name }}
                                    {% endfor %}
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <button id="btn-workflow-config" class="btn btn-default">变更</button>
                            <button id="btn-workflow-config-clean" class="btn btn-default">刷新</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endblock content %}

{% block js %}
    {% load static %}
    <link href="{% static 'bootstrap-switch/css/bootstrap-switch.min.css' %}" rel="stylesheet" type="text/css"/>
    <script src="{% static 'bootstrap-switch/js/bootstrap-switch.min.js' %}"></script>
    <script>
        //配置项切换
        $("#config").change(function () {
            sessionStorage.setItem('config_type', $("#config").val());
            if ($("#config").val() === '0') {
                $("#div-system-config").show();
                $("#div-workflow").hide();
                $("#div-workflow-config").hide();
                $('input[type="checkbox"]').each(function (i) {
                    if ($(this).val() === 'true') {
                        $(this).bootstrapSwitch('state', true);
                    } else {
                        $(this).bootstrapSwitch('state', false);
                    }
                });
            } else if ($("#config").val() === '1') {
                $("#div-system-config").hide();
                $("#div-workflow").show();
                $("#div-workflow-config").show();
                $("#workflow_type").trigger("change");
            }
        });

        // 系统设置checkbox事件
        $('input[type="checkbox"]').on('switchChange.bootstrapSwitch', function (event, state) {
            if (state) {
                $(this).val(true);
                if ($(this).attr("id") === 'mail') {
                    $("#div-mail-config").show();
                } else if ($(this).attr("id") === 'ding_to_person') {
                    $("#div-ding-config").show();
                } else if ($(this).attr("id") === 'auto_review') {
                    $("#div-auto-review-config").show();
                } else if ($(this).attr("id") === 'wx') {
                    $("#div-wx-config").show();
                } else if ($(this).attr("id") === 'feishu') {
                    $("#div-feishu-config").show();
                }
            } else {
                $(this).val(false);
                if ($(this).attr("id") === 'mail') {
                    $("#mail_ssl").val(false);
                    $("#div-mail-config").hide();
                } else if ($(this).attr("id") === 'ding_to_person') {
                    $("#div-ding-config").hide();
                } else if ($(this).attr("id") === 'auto_review') {
                    $("#div-auto-review-config").hide();
                } else if ($(this).attr("id") === 'wx') {
                    $("#div-wx-config").hide();
                } else if ($(this).attr("id") === 'feishu') {
                    $("#div-feishu-config").hide();
                }
            }
        });

        // 修改系统设置
        $("#saveconfig").click(function () {
            // 当开启钉钉通知到个人时，企业id和secret等必须填写
            if ($("#ding_to_person").val() == 'true') {
                var v1 = $("#ding_agent_id").val();
                var v2 = $("#ding_app_key").val();
                var v3 = $("#ding_app_secret").val();
                var v4 = $("#ding_dept_ids").val();
                var v5 = $("#ding_archery_username").val();
                if (v1 == "" || v2 == "" || v3 == "" || v4 == "" | v5 == "") {
                    alert("当开启钉钉通知到个人时，企业id和secret等必须填写!");
                    return;
                }
            }
            var sys_config = $("#div-system-config");
            var configs = [];
            sys_config.find('[key]').each(
                function () {
                    var config_item = $(this).attr("key");
                    var config_value = $(this).val();
                    configs.push({
                        key: config_item,
                        value: config_value
                    });
                }
            );

            $.ajax({
                type: "post",
                url: "/config/change/",
                dataType: "json",
                data: {
                    configs: JSON.stringify(configs)
                },
                complete: function () {
                },
                success: function (data) {
                    if (data.status === 0) {
                        window.location.reload()
                    } else {
                        alert(data.msg);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(errorThrown);
                }
            });
        });

        // 切换组触发工单类型切换事件
        $("#group").change(function () {
            $("#workflow_type").trigger('change')
        });

        // 点击用户填充到workflow_auditors_text
        $("#group_auditors").change(function () {
            var auth_group = $(this).find(':selected').attr("disabled", "disabled").val();
            var auditors = $("#workflow_auditors_text").val();
            if (auditors) {
                $("#workflow_auditors_text").val(auditors + '->' + auth_group);
            } else {
                $("#workflow_auditors_text").val(auth_group)
            }
            $('#group_auditors').selectpicker('render');
            $('#group_auditors').selectpicker('refresh');
        });

        // 重置权限组选择框状态
        $("#group_auditors").on("reset", function () {
            $('#group_auditors').selectpicker('val', []);
            $(this).children().removeAttr("disabled");
            $(this).selectpicker("refresh");
        })

        // 清空审核人信息
        $("#btn-workflow-config-clean").click(function () {
            $('#group_auditors').trigger("reset");
            $("#workflow_auditors_text").val('');
        });

        // 切换工单类型获取对应组负责人
        $("#workflow_type").change(function () {
            $("#div-workflow-config").show();
            $('#group_auditors').trigger("reset");
            $("#workflow_auditors_text").val('');
            if ($("#group").val()) {
                $.ajax({
                    type: "post",
                    url: "/group/auditors/",
                    dataType: "json",
                    data: {
                        group_name: $("#group").val(),
                        workflow_type: $("#workflow_type").val()
                    },
                    complete: function () {

                    },
                    success: function (data) {
                        if (data.status === 0) {
                            var result = data.data;
                            $("#workflow_auditors").text(result['auditors_display']);
                        } else {
                            alert(data.msg);
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert(errorThrown);
                    }
                });
            }
        });

        // 变更组工单审批流程
        $("#btn-workflow-config").click(function () {
            if ($("#group").val() && $("#workflow_type").val() && $("#workflow_auditors_text").val()) {
                $(this).addClass('disabled');
                $(this).prop('disabled', true);
                var audit_auth_groups = $("#workflow_auditors_text").val().replace(/->/g, ",");
                $.ajax({
                    type: "post",
                    url: "/group/changeauditors/",
                    dataType: "json",
                    data: {
                        group_name: $("#group").val(),
                        audit_auth_groups: audit_auth_groups,
                        workflow_type: $("#workflow_type").val()
                    },
                    complete: function () {
                        $("#btn-workflow-config").removeClass('disabled');
                        $("#btn-workflow-config").prop('disabled', false);
                    },
                    success: function (data) {
                        if (data.status === 0) {
                            //alert('修改成功');
                            $("#workflow_type").trigger("change")
                        } else {
                            alert(data.msg);
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert(errorThrown);
                    }
                });
            } else {
                alert('请选择组、工单类型和审批流程！')
            }

        });

        // 检测inception
        $("#check_incption").click(function () {
            $(this).addClass('disabled');
            $(this).prop('disabled', true);
            $.ajax({
                type: "post",
                url: "/check/inception/",
                dataType: "json",
                data: {
                    inception_host: $("#inception_host").val(),
                    inception_port: $("#inception_port").val(),
                    inception_remote_backup_host: $("#inception_remote_backup_host").val(),
                    inception_remote_backup_port: $("#inception_remote_backup_port").val(),
                    inception_remote_backup_user: $("#inception_remote_backup_user").val(),
                    inception_remote_backup_password: $("#inception_remote_backup_password").val(),
                },
                complete: function () {
                    $("#check_incption").removeClass('disabled');
                    $("#check_incption").prop('disabled', false);
                },
                success: function (data) {
                    if (data.status === 0) {
                        alert('Inception和备份库测试连接成功，请点击保存生效！')
                    } else {
                        alert(data.msg);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(errorThrown);
                }
            });
        });

        // 检测goInception
        $("#check_goincption").click(function () {
            $(this).addClass('disabled');
            $(this).prop('disabled', true);
            $.ajax({
                type: "post",
                url: "/check/go_inception/",
                dataType: "json",
                data: {
                    go_inception_host: $("#go_inception_host").val(),
                    go_inception_port: $("#go_inception_port").val(),
                    inception_remote_backup_host: $("#inception_remote_backup_host").val(),
                    inception_remote_backup_port: $("#inception_remote_backup_port").val(),
                    inception_remote_backup_user: $("#inception_remote_backup_user").val(),
                    inception_remote_backup_password: $("#inception_remote_backup_password").val(),
                },
                complete: function () {
                    $("#check_goincption").removeClass('disabled');
                    $("#check_goincption").prop('disabled', false);
                },
                success: function (data) {
                    if (data.status === 0) {
                        alert('goInception和备份库测试连接成功，请点击保存生效！')
                    } else {
                        alert(data.msg);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(errorThrown);
                }
            });
        });

        // 检测邮件
        $("#check_email").click(function () {
            $(this).addClass('disabled');
            $(this).prop('disabled', true);
            $.ajax({
                type: "post",
                url: "/check/email/",
                dataType: "json",
                data: {
                    mail: $("#mail").val(),
                    mail_ssl: $("#mail_ssl").val(),
                    mail_smtp_server: $("#mail_smtp_server").val(),
                    mail_smtp_port: $("#mail_smtp_port").val(),
                    mail_smtp_user: $("#mail_smtp_user").val(),
                    mail_smtp_password: $("#mail_smtp_password").val(),
                },
                complete: function () {
                    $("#check_email").removeClass('disabled');
                    $("#check_email").prop('disabled', false);
                },
                success: function (data) {
                    if (data.status === 0) {
                        alert('测试邮件已成功发送给当前用户，注意查收，请点击保存生效！')
                    } else {
                        alert(data.msg);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(errorThrown);
                }
            });
        });

        // 同步钉钉用户信息
        $("#sync_ding_user").click(function () {
            // 先检查配置信息
            var v1 = $("#ding_agent_id").val();
            var v2 = $("#ding_app_key").val();
            var v3 = $("#ding_app_secret").val();
            var v4 = $("#ding_dept_ids").val();
            var v5 = $("#ding_archery_username").val();
            if (v1 == "" || v2 == "" || v3 == "" || v4 == "" | v5 == "") {
                alert("请先保存相关配置信息！");
                return;
            }
            $(this).addClass('disabled');
            $(this).prop('disabled', true);
            $.ajax({
                type: "post",
                url: "/4admin/sync_ding_user/",
                dataType: "json",
                data: {},
                complete: function () {
                    $("#sync_ding_user").removeClass('disabled');
                    $("#sync_ding_user").prop('disabled', false);
                },
                success: function (data) {
                    if (data.status === 0) {
                        alert('已添加定时同步任务并且触发同步，点击确认查看任务状态');
                        window.open('/admin/django_q/schedule/?q=sync_ding_user_id')
                    } else {
                        alert(data.msg);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(errorThrown);
                }
            });
        });

        //自动填充操作项
        $(document).ready(function () {
            if (sessionStorage.getItem('config_type')) {
                $("#config").val(sessionStorage.getItem('config_type')).trigger("change")
            } else if ($("#config").val()) {
                $("#config").trigger("change")
            }
        })
    </script>
{% endblock %}
