

<title>DNS设置 - 线路分组</title>

<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a lay-href="">首页</a>
    <a><cite>线路分组</cite></a>
    <a><cite>DNS设置</cite></a>
  </div>
</div>

<style type="text/css">
.layui-elem-quote{padding: 6px;}
.thin-gray{font-weight: 400;color: #999;} 
.small-title{font-weight:700;font-size:14px}
hr {margin-bottom: 30px}
</style>
<div class="layui-fluid">
  <div class="layui-row layui-col-space15">
    <div class="layui-col-md12">
      <div class="layui-card">
        <div class="layui-card-body">
         <div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
          <div class="layui-tab-content">
            <div class="layui-tab-item layui-show">
              <div class="layui-row layui-col-space10">
                <div class="layui-col-lg2 layui-col-md4">DNS提供商：</div>
                <div class="layui-col-lg2 layui-col-md8 layui-form">
                  <select name="dns" lay-filter="dns" lay-verify="">
                    <option value="">请选择DNS</option>
                    <option value="aliyun">阿里云 (aliyun.com,alibabacloud.com)</option>
                    <option value="dnsdun">DNS盾 (dnsdun.com)</option>
                    <option value="dnspod_cn">DNSPod (dnspod.cn)</option>
                    <option value="dnspod_com">DNSPod国际版（dnspod.com）</option>
                    <option value="dnsdotcom">帝恩思 (dns.com)</option>
                    <option value="cloudflare">Cloudflare (cloudflare.com)</option>
                  </select>   
                </div>
              </div> 
              <div class="layui-row layui-col-space10">
                <div class="layui-col-md2 id">ID：</div>
                <div class="layui-col-lg2"><input type="text" name="id" autocomplete="off" class="layui-input"></div>
              </div> 
              <div class="layui-row layui-col-space10">
                <div class="layui-col-md2 token">Token/Secret：</div>
                <div class="layui-col-md2"><input type="text" name="token" autocomplete="off" class="layui-input"></div>
              </div>      

              <div class="layui-row layui-col-space10">
                <div class="layui-col-md2">主域名：</div>
                <div class="layui-col-md2"><input type="text" name="domain" autocomplete="off" class="layui-input"></div>
              </div>  

              <div class="layui-row layui-col-space10 layui-hide zone-id">
                <div class="layui-col-md2">zone id：</div>
                <div class="layui-col-md2"><input type="text" name="zone_id" autocomplete="off" class="layui-input"></div>
              </div> 

              <div class="layui-row layui-col-space10">
                <div class="layui-col-md2">TTL：</div>
                <div class="layui-col-md2"><input type="text" name="ttl"  value="600" autocomplete="off" class="layui-input"></div>
              </div> 

              <div class="layui-row layui-col-space10 dnsdun layui-hide">
                <div class="layui-col-md2">API地址：</div>
                <div class="layui-col-md2"><input type="text" name="gateway"  value="https://api.dnsdun.com" autocomplete="off" class="layui-input"></div>
              </div> 

              <div class="layui-row layui-col-space10" style="margin-top:20px;">
                <div class="layui-col-md2"></div>
                <div class="layui-col-md1"><input type="button" id="save" value="验证并保存" class="layui-btn layui-btn-sm"></div>
                <div class="layui-col-md1"><input type="button" id="repair" value="记录修复" class="layui-btn  layui-btn-normal layui-btn-sm"></div>
                
                
              </div>

            </div>
        </div>
      </div>                 

    </div>
  </div>
</div>
</div>
</div>

<script type="text/javascript">
  layui.use(['admin', 'table','form'], function(){
    var $ = layui.$
    ,admin = layui.admin
    ,view = layui.view
    ,table = layui.table
    ,form = layui.form;

    form.render()
    // 监听切换
    form.on('select(dns)', function(data){
      if (data.value == "huaweicloud") {
        $(".id").text("IAM用户名：")
        $(".token").text("IAM密码：")
        $(".zone-id").removeClass("layui-hide")
      }  
      else if (data.value == "cloudflare") {
        $(".id").text("Email：")
        $(".token").text("API Key：")
        $(".zone-id").addClass("layui-hide")

      } else {
        $(".id").text("ID：")
        $(".token").text("Token/Secret：")
        $(".zone-id").addClass("layui-hide")
      }

      if (data.value == "dnsdun") {
        $(".dnsdun").removeClass("layui-hide")
      } else {
        $(".dnsdun").addClass("layui-hide")
      }

    });  

    // 获取配置
    admin.req({
      url: '/configs/global-0-system-dns_config' //实际使用请改成服务端真实接口
      ,type: "get"
      ,contentType:"application/json"
      ,dataType: "json"        
      ,done: function(res){
        var data = JSON.parse(res.data.value)
        if (typeof(data.dns) != "undefined") {
          // dns
          $("select[name='dns']").val(data.dns)

          // id
          $("input[name='id']").val(data.id)

          // token
          $("input[name='token']").val(data.token)

          // domain
          $("input[name='domain']").val(data.domain)

          // zone_id
          $("input[name='zone_id']").val(data.zone_id)

          // ttl
          $("input[name='ttl']").val(data.ttl)

          if (data.dns == "huaweicloud") {
            $(".id").text("IAM用户名：")
            $(".token").text("IAM密码：")
            $(".zone-id").removeClass("layui-hide")
          } else {
            $(".id").text("ID：")
            $(".token").text("Token/Secret：")
            $(".zone-id").addClass("layui-hide")
          }

          form.render()

        }
      }
    });

    $("#save").click(function (argument) {
      var id = $("input[name='id']").val()
      var token = $("input[name='token']").val()
      var domain = $("input[name='domain']").val()
      var dns = $("select[name='dns']").val()
      var ttl = $("input[name='ttl']").val()
      var zone_id = $("input[name='zone_id']").val()
      var gateway = $("input[name='gateway']").val()
      var rnd = Math.ceil(Math.random()*100);
      var data = JSON.stringify({"id":id, "token":token,"gateway":gateway, "domain":domain,"dns":dns,"ttl":ttl,"rnd":rnd,"zone_id":zone_id})

      admin.req({
        url: '/configs/global-0-system-dns_config' //实际使用请改成服务端真实接口
        ,type: "put"
        ,data: JSON.stringify({"value": data})
        ,contentType:"application/json"
        ,dataType: "json"
        ,done: function(res){
          //登入成功的提示与跳转
          layer.msg('保存成功', {
            offset: '15px'
            ,icon: 1
            ,time: 1000
          });
        }
      });
    })

    $("#repair").click(function (argument) {
      admin.req({
        url: '/configs/global-0-system-record_repair' //实际使用请改成服务端真实接口
        ,type: "put"
        ,data: JSON.stringify({"value":"1"})
        ,contentType:"application/json"
        ,dataType: "json"        
        ,done: function(res){
          //登入成功的提示与跳转
          layer.msg('已发送修复请求，结果请到后台任务里查看', {
            offset: '15px'
            ,icon: 1
            ,time: 3000
          });
        }
      });
    })

  });  

</script>