{{include file="public/header" /}}

<!-- conntent start  -->
<div class="am-g my-content">
    <!-- form start -->
    <form class="am-form form-validation" method="post" action="{{:MyUrl('index/useraddress/save')}}" request-type="ajax-fun" request-value="ViewModalBack">
        <div class="am-form-group">
            <label>姓名</label>
            <input type="text" name="name" placeholder="姓名" minlength="2" maxlength="16" data-validation-message="姓名格式 2~16 个字符之间" class="am-radius" {{if !empty($data)}} value="{{$data.name}}"{{/if}} required />
        </div>
        <div class="am-form-group">
            <label>电话</label>
            <input type="text" name="tel" placeholder="电话" minlength="6" maxlength="30" data-validation-message="电话格式有误" class="am-radius" {{if !empty($data)}} value="{{$data.tel}}"{{/if}} required />
        </div>

        {{include file="lib/region_linkage" /}}
        <div class="am-form-group">
            <label>详细地址</label>
            <div class="am-form-group am-input-group am-input-group-sm">
                <input type="hidden" name="lng" id="form-lng" {{if !empty($data)}} value="{{$data.lng}}"{{/if}} />
                <input type="hidden" name="lat" id="form-lat" {{if !empty($data)}} value="{{$data.lat}}"{{/if}} />
                <input type="text" name="address" id="form-address" placeholder="详细地址" minlength="2" maxlength="80" data-validation-message="详细地址格式 1~80 个字符之间" class="am-radius" {{if !empty($data)}} value="{{$data.address}}"{{/if}} required />
                <span class="am-input-group-btn">
                    <button class="am-btn am-btn-default am-radius" type="button" id="map-location-submit"><span class="am-icon-map-marker"></span> 定位</button>
                </span>
            </div>
        </div>
        <div id="map" data-level="17" class="am-form-group"></div>

        <div class="am-form-group">
            <label>别名</label>
            <input type="text" name="alias" placeholder="别名" minlength="0" maxlength="16" data-validation-message="别名格式最多 16 个字符" class="am-radius" {{if !empty($data)}} value="{{$data.alias}}"{{/if}} />
        </div>
        <div class="am-form-group">
            <label class="block">是否默认</label>
            <input name="is_default" value="1" type="checkbox" data-off-text="否" data-on-text="是" data-size="xs" data-on-color="success" data-off-color="default" data-handle-width="49" data-am-switch {{if isset($data['is_default']) and $data['is_default'] eq 1}}checked="true"{{/if}} />
        </div>

        <div class="am-form-group">
            <input type="hidden" name="id" {{if !empty($data)}} value="{{$data.id}}"{{/if}} />
            <button type="submit" class="am-btn am-btn-primary am-radius btn-loading-example am-btn-sm am-btn-block" data-am-loading="{loadingText:'处理中...'}">保存</button>
        </div>
    </form>
    <!-- form end -->
</div>
<!-- conntent end  -->


<!-- footer start -->
{{include file="public/footer" /}}
<!-- footer end -->

<!-- map -->
<script type="text/javascript" src="{{$Think.__MY_HTTP__}}://api.map.baidu.com/api?v=2.0&ak={{:MyC('common_baidu_map_ak')}}"></script>
<script type="text/javascript">
$(function()
{
    // 百度地图API功能
    var map = new BMap.Map("map", {enableMapClick:false});
    var level = $('#map').data('level') || 16;
    var point = new BMap.Point({{if !empty($data['lng'])}}{{$data['lng']}}{{else /}}116.400244{{/if}},{{if !empty($data['lat'])}}{{$data['lat']}}{{else /}}39.92556{{/if}});
    map.centerAndZoom(point, level);

    /* 添加控件 */
    var navigationControl = new BMap.NavigationControl({
        // 靠左上角位置
        anchor: BMAP_ANCHOR_TOP_LEFT,
        // LARGE类型
        type: BMAP_NAVIGATION_CONTROL_LARGE,
    });
    map.addControl(navigationControl);

    /* 创建标注 */
    var marker = new BMap.Marker(point);  // 创建标注
    map.addOverlay(marker);              // 将标注添加到地图中
    marker.enableDragging();           // 可拖拽
    marker.addEventListener("dragend", function(e) {
        map.panTo(e.point);
        $('#form-lng').val(e.point.lng);
        $('#form-lat').val(e.point.lat);
    });

    /* 设置版权控件位置 */
    var cr = new BMap.CopyrightControl({anchor:BMAP_ANCHOR_BOTTOM_RIGHT});
    map.addControl(cr); //添加版权控件
    var bs = map.getBounds();   //返回地图可视区域
    cr.addCopyright({id: 1, content: "<div class='map-copy'><span>拖动红色图标直接定位</span></div>", bounds:bs});
});

// 返回处理
function ViewModalBack(e)
{
    if(e.code == 0)
    {
        $.AMUI.progress.done();
        Prompt(e.msg, 'success');
        setTimeout(function()
        {
            parent.location.reload();
        }, 1500);
    } else {
        $('form.form-validation').find('button[type="submit"]').button('reset');
        $.AMUI.progress.done();
        Prompt(e.msg);
    }
}
</script>