@extends('admin.layout')

@section('content')

    <?php
    if (!isset($data)) $data = array();
    if (!$data && session("data")) {
        $data = session("data");
    }
    if (!$data && session('_old_input')) {
        $data = session("_old_input");
    }
    ?>
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>导航栏设置</h5>
                        <div class="ibox-tools">
                            <a class="collapse-link"> <i class="fa fa-chevron-up"></i>
                            </a>
                        </div>
                    </div>
                    <div class="ibox-content">
                        @if(role('Album/Nav/index'))
                            <div class="row">
                                <div class="col-sm-3 pull-right">
                                    <a href="{{ U('Album/Nav/index')}}"
                                       class="btn btn-sm btn-primary pull-right">返回列表</a>
                                </div>
                            </div>
                        @endif

                        <div class="row">
                            <div class="col-lg-10">
                                <form name="form_product" id="form-validation" action=""
                                      class="form-horizontal form-validation" accept-charset="UTF-8" method="post">
                                    <div class="form-group">

                                        <label class="control-label col-sm-3">图标名称</label>

                                        <div class="col-sm-5">
                                            <input id="data_name" name="data[name]" class="form-control"
                                                   value="{{ $data['name'] or ''}}" required="" aria-required="true"
                                                   placeholder="">
                                        </div>

                                    </div>

                                    <div class="form-group">
                                        <label class="control-label col-sm-3">位置</label>
                                        <div class="col-sm-5">
                                            <select name="data[state]" id="data_state" class="form-control" required=""
                                                    aria-required="true">
                                                @foreach(dict()->get('album','top_buttom') as $key=>$val)
                                                    <option value="{{$key}}"
                                                            @if($key == (isset($data['state'])? $data['state']: ''))selected @endif>{{$val}}</option>
                                                @endforeach
                                            </select>
                                        </div>
                                    </div>

                                    <div class="form-group">

                                        <label class="control-label col-sm-3">默认图标</label>

                                        <div class="col-sm-5">
                                            {!!  widget('Tools.ImgUpload')->single('pic_url',"data[pic_url]", isset($data['pic_url'])? $data['pic_url'] : "") !!}
                                        </div>

                                    </div>
                                    <div class="form-group">

                                        <label class="control-label col-sm-3">选中图标</label>

                                        <div class="col-sm-5">
                                            {!!  widget('Tools.ImgUpload')->single('pic_url_active',"data[pic_url_active]", isset($data['pic_url_active'])? $data['pic_url_active'] : "") !!}
                                        </div>

                                    </div>

                                    <div class="form-group">
                                        <label class="control-label col-sm-3">跳转类型</label>
                                        <div class="col-sm-5">
                                            <select name="data[open_type]" id="data_open_type" class="form-control"
                                                    required="" aria-required="true">
                                                @foreach(dict()->get('album','nav') as $key=>$val)
                                                    <option value="{{$key}}"
                                                            @if($key == (isset($data['open_type'])? $data['open_type']: '' ))selected @endif>{{$val}}</option>
                                                @endforeach
                                            </select>
                                        </div>
                                    </div>





                                    <div class="form-group re_xcx" style="display: none">

                                        <label class="control-label col-sm-3">App_ID</label>

                                        <div class="col-sm-5">
                                            <input id="data_app_id" name="data[app_id]" class="form-control re_xcx"
                                                   value="{{ $data['app_id'] or ''}}" placeholder="">
                                        </div>

                                    </div>
                                    <div class="form-group re_nav" style="display: none">

                                        <label class="control-label col-sm-3">页面</label>

                                        <div class="col-sm-5">
                                            <select name="data[url]" id="data_select_url" class="form-control nav">

                                                @foreach($page as $k=>$v)
                                                    <option value="{{$v['value']}}"
                                                            @if($v['value'] == (isset($data['url'])? $data['url']: '' ))selected @endif>{{$v['name']}}</option>
                                                @endforeach
                                            </select>
                                        </div>

                                    </div>-+

                                    <div class="form-group re_xcx" style="display: none">

                                        <label class="control-label col-sm-3">路径配置</label>

                                        <div class="col-sm-5">
                                            <input id="data_url" name="data[url]" class="form-control re_xcx"
                                                   value="{{ $data['url'] or ''}}" placeholder="">
                                        </div>

                                    </div>

                                    <div class="form-group re_call" style="display: none">

                                        <label class="control-label col-sm-3">拨号电话</label>

                                        <div class="col-sm-5">
                                            <input id="data_mobile" name="data[mobile]" class="form-control call"
                                                   value="{{ $data['mobile'] or ''}}" placeholder="">
                                        </div>

                                    </div>


                                    <script type="text/javascript"
                                            src="https://webapi.amap.com/maps?v=1.3&key=533117702a063eae1dc26c183a1ac7bb&plugin=AMap.Autocomplete"></script>
                                    <script type="text/javascript"
                                            src="https://cache.amap.com/lbs/static/addToolbar.js"></script>
                                    <div class="form-group re_address" style="display: none">

                                        <label class="control-label col-sm-3">导航地址</label>

                                        <div class="col-sm-5">
                                            <input n name="data[address]" readonly="true"
                                                   value="{{ (isset($data['address'])?$data['address'] : "")}}"
                                                   class="form-control address" id="add" data-toggle="modal"
                                                   data-target="#myModal">
                                            {{--<input id="data_store_id" name="data[address]" class="form-control address" value="{{ $data['address'] or ''}}"  placeholder="">--}}
                                            <input type="hidden" name="data[lon]" id="lon"
                                                   value="{{$data['lon'] or ''}}">
                                            <input type="hidden" name="data[lat]" id="lat"
                                                   value="{{$data['lat'] or ''}}">
                                        </div>

                                    </div>



                                    <div class="form-group re_nav_zhi" style="display: none">

                                        <label class="control-label col-sm-3 nav_zhi_name">路径参数</label>

                                        <div class="col-sm-5">
                                            <input id="data_zhi" name="zhi" class="form-control nav_zhi"
                                                   value="{{$data['zhi'] or ''}}" placeholder="">
                                        </div>

                                    </div>

                                    <div class="form-group">

                                        <label class="control-label col-sm-3">排序</label>

                                        <div class="col-sm-5">
                                            <input id="data_sort" name="data[sort]" class="form-control"
                                                   value="{{ $data['sort'] or '0'}}" required="" aria-required="true"
                                                   placeholder="数值越大越靠前">
                                            <span class="help-block m-b-none">数值越大越靠前</span>
                                        </div>

                                    </div>
                                    <div class="form-group">
                                        <label class="control-label col-sm-3">&nbsp;</label>
                                        <div class="col-sm-9">
                                            <input type="hidden" name="_referer"
                                                   value="<?php echo urlencode(request()->server('HTTP_REFERER'));?>"/>
                                            <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>"/>
                                            <input type="submit" class="btn btn-success" style="margin-right:20px;">
                                            <input type="reset" class="btn btn-default">
                                        </div>
                                    </div>

                                </form>
                            </div>
                            <!-- /.col-lg-10 -->
                        </div>
                        <!-- /.row -->
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="modal fade" id="myModal" tabindex="-1" role="dialog"
         aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog" style="min-width:60%;">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title" id="myModalLabel">
                        选择坐标
                    </h4>
                </div>
                <div class="modal-body">
                    <div id="container" style="width: 100%;min-height: 400px;"></div>
                    <div id="myPageTop">
                        <table>
                            <tr>
                                <td>
                                    <label>按关键字搜索：</label>
                                </td>
                                <td>

                                </td>

                            </tr>
                            <tr>
                                <td>
                                    <input style="margin-right: 10px;min-width:300px"
                                           value="{{ (isset($data['address'])?$data['address'] : "") }}" type="text"
                                           placeholder="请输入关键字进行搜索" id="tipinput">
                                </td>
                                <td>
                                    <div style="margin-right: 10px;" class="btn btn-sm btn-primary"
                                         id="confirm">显示搜索点
                                    </div>
                                </td>
                                <td class="column2">
                                    <input type="hidden" readonly="true" id="lnglat">
                                </td>
                            </tr>
                        </table>
                        <br>
                        <button type="button" id="modal-close" class="btn btn-sm btn-primary text-center"
                                data-dismiss="modal">确认位置信息
                        </button>
                    </div>
                    <div id="panel"></div>
                </div>

            </div><!-- /.modal-content -->
        </div><!-- /.modal -->
    </div>
    </div>
    <script type="text/javascript">
        var map = new AMap.Map("container", {
            resizeEnable: true,
            zoom: 10
        });
        $("#confirm").click(function () {
            var val = $('#tipinput').val();
            AMap.service(["AMap.PlaceSearch"], function () {
                var placeSearch = new AMap.PlaceSearch({ //构造地点查询类
                    pageSize: 5,
                    pageIndex: 1,
                    map: map,
                    panel: "panel"
                });
                placeSearch.search(val);
            });
        });

        //为地图注册click事件获取鼠标点击出的经纬度坐标
        var clickEventListener = map.on('click', function (e) {
            document.getElementById("lnglat").value = e.lnglat.getLng() + ',' + e.lnglat.getLat();
        });

        AMap.plugin('AMap.Geocoder', function () {
            var geocoder = new AMap.Geocoder({});
            var marker = new AMap.Marker({
                map: map,
                bubble: true
            })
            map.on('click', function (e) {
                console.log(e);
                marker.setPosition(e.lnglat);
                $('#lon').val(e.lnglat.getLng());
                $('#lat').val(e.lnglat.getLat());
                geocoder.getAddress(e.lnglat, function (status, result) {
                    if (status == 'complete') {
                        console.log(result);
                        document.getElementById('tipinput').value = result.regeocode.formattedAddress
                    }
                })
            })

        });
        $("#modal-close").click(function () {
            var b = ($('#tipinput').val());
            console.log(b);
            $('#map').val(b);
            $('#add').val(b);
        });
        /*  $('.sg-datepicker').datepicker({
              format: 'yyyy-mm-dd',
              language: 'zh-CN'
          });*/
    </script>

    <script>
        setUrl();
        $(document).on("change", "select[name='data[open_type]']", function () {
            setOpenType();
        });
        $(document).on("change", "select[name='data[url]']", function () {
            setUrl();
        });
        var setting = ['xcx', 'call', 'address', 'nav'];
        setOpenType();

        function setOpenType() {
            if ($("select[name='data[open_type]']").val() == 1) {
                setRequire('call');
            } else if ($("select[name='data[open_type]']").val() == 2) {
                setRequire('address');
            } else if ($("select[name='data[open_type]']").val() == 3) {
                setRequire('xcx');
            } else if ($("select[name='data[open_type]']").val() == 4) {
                setRequire('nav');
            }
        }

        function setRequire(name) {
            $.each(setting, function (i, item) {
                if(name == 'xcx'){
                    $('#data_select_url').attr('name');
                    $('#data_url').removeAttr('name','data[url]');
                }else if(name == 'nav'){
                    $('#data_url').removeAttr('name');
                    $('#data_select_url').attr('name','data[url]');
                }
                if (item == name) {
                    $('.re_' + item).show();
                    $('input.' + item).attr('required', '');
                    $("input." + item).attr('aria-required', 'true');
                } else {
                    $('.re_' + item).hide();
                    $('input.' + item).removeAttr('required');
                    $("input." + item).removeAttr('aria-required');
                }
            })
        }

        function setUrl() {
            var url = $("select[name='data[url]']").val();
            var data = url.split("/")
            //console.log(data)
            if (data[2] == 'news' || data[2] == 'index') {
                zhiShow('hide');
            } else if (data[2] == 'info_list') {
                zhiShow('hide');
            } else if (data[2] == 'info_detail') {
                $(".nav_zhi_name").text('').text('文章ID');
                zhiShow('show');
            }
        }

        function zhiShow(e) {
            if (e == 'show') {
                $('.re_nav_zhi').show();
                $('.re_nav_zhi').attr('required', '');
                $('.re_nav_zhi').attr('name', 'zhi');
                $('.re_nav_zhi').attr('aria-required', 'true');
            } else {
                $('.re_nav_zhi').hide();
                $('#data_zhi').val('');
                $('.re_nav_zhi').removeAttr('required');
                $('.re_nav_zhi').removeAttr('name');
                $('.re_nav_zhi').removeAttr('aria-required');
            }
        }
    </script>
@endsection