<style type="text/css">
    .footer{display: none;}
    .recommend_info_popover{left: inherit !important;right:0px;top:0 !important;}
    #dialog-header{height: 30px;}
    .ad_link, .sop_link {font-size: 16px;text-decoration: underline;padding-right: 12px;color:#000;font-weight: bold;}
</style>
<script type="text/javascript" src="/wysiwyg/ckeditor.js"></script>
<link type="text/css" rel="stylesheet" href="/js/lyydate/skins/default/laydate.css?1" id="LayDateSkin">
<div class="container-fluid" style="background:#f8f8f8;">
    <div class="row">
        <div class="col-sm-5 col-md-4 nopadding hidden-xs">
            <div class="well" style="margin-bottom:0;padding: 19px 8px;border-bottom:none;">
                <ul id="treeDemo" name="treeDemo" class="ztree" style="min-height:685px;overflow-y:scroll;"></ul>
            </div>
        </div>

        <div class="col-sm-19 col-md-20 nopadding">
            <div class="col-xs-24 well" style="height:60px;background:#f8f8f8;margin-bottom:0;">
                <div class="col-xs-19">
                    <div class="pull-left">
                        <a href="<?php echo site_url('thirdparty/advertise/index/index/news'); ?>" class="btn btn-default btn-sm <?php if ($sub_nav == 'news') echo 'active'; ?>">最新编辑广告</a>
                        <a href="<?php echo site_url('thirdparty/advertise/index/index/will_expire'); ?>" class="btn btn-default btn-sm <?php if ($sub_nav == 'will_expire') echo 'active'; ?>">将到期的广告</a>
                        <a href="<?php echo site_url('thirdparty/advertise/index/index/expired'); ?>" class="btn btn-default btn-sm <?php if ($sub_nav == 'expired') echo 'active'; ?>">已过期的广告</a>
                    </div>
                </div>
                <div class="col-xs-5"></div>
            </div>

            <form id="form_advertise_edit" name="form_advertise_edit" method="post" action="<?php echo site_url('thirdparty/advertise/index/ad_edit'); ?>">
                <div class="col-sm-19 center-col" style="background:#fff;">
                    <div class="col-xs-24">
                        <div class="row">
                            <div class="col-xs-24 btn-sm"></div>
                            <div class="col-xs-7"><label>广告标题</label></div><div class="col-xs-10 pull-right text-right text-muted"><?php if (!empty($click_rate)) echo '广告点击次数:' . $click_rate; ?></div>
                            <div class="col-xs-24">
                                <input class="form-control" type="text" id="ad_title" name="ad_title" value="<?php echo $advertise->ad_title; ?>">
                            </div>
                            <div class="col-xs-24 btn-lg"></div>
                            <div class="col-xs-24 btn-lg"></div>
                        </div>

                        <div class="col-xs-8 form-group nopadding">
                            <div class="col-xs-24 nopadding">广告位置</div>
                            <select class="form-control col-xs-18" name="ad_place" id="ad_place" style="display:block !important;">
                                <option value="right_row" <?php echo $advertise->ad_place == 'right_row' ? 'selected' : ''; ?>>页面右侧</option>
                                <option value="left_nav_top" <?php echo $advertise->ad_place == 'left_nav_top' ? 'selected' : ''; ?>>左导航顶部</option>
                                <option value="left_nav_bottom" <?php echo $advertise->ad_place == 'left_nav_bottom' ? 'selected' : ''; ?>>左导航底部</option>
                                <option value="right_nav_top" <?php echo $advertise->ad_place == 'right_nav_top' ? 'selected' : ''; ?>>右导航顶部</option>
                                <option value="right_nav_bottom" <?php echo $advertise->ad_place == 'right_nav_bottom' ? 'selected' : ''; ?>>右导航底部</option>
                                <option value="centent_bottom" <?php echo $advertise->ad_place == 'centent_bottom' ? 'selected' : ''; ?>>正文底部</option>
                            </select>
                        </div>

                        <div class="col-xs-8">
                            <div class="col-xs-24 nopadding">是否发布</div>
                            <select class="form-control col-xs-24" name="ad_status" id="ad_status">
                                <option value="0" <?php echo $advertise->ad_status == 0 ? 'selected' : ''; ?>>不发布</option>
                                <option value="1" <?php echo $advertise->ad_status == 1 ? 'selected' : ''; ?>>发布</option>
                            </select>
                        </div>

                        <div class="col-xs-8 nopadding-R">
                            <div class="form-group">
                                <div class="col-xs-24 nopadding">过期时间</div>
                                <input class="form-control col-xs-24" type="text" id="ad_expire" name="ad_expire" value="<?php echo date('Y-m-d', $advertise->ad_expire); ?>">
                            </div>
                        </div>
                    </div>
                    <div class="col-xs-24 editor-box">
                        <div class="col-xs-24 btn-lg">
                            <a class="ad_link" href="javascript:;" data-ad-template="ad-img">加载图片广告模板</a>
                            <a class="sop_link" href="http://share.chtcdn.com/info.php/infoshare/detail/240001834" target="_blank">广告操作SOP</a>
                        </div>
                        <textarea id="ad_content" name="ad_content"><?php echo $advertise->ad_content; ?></textarea> 
                    </div>
                </div>
                <input type="hidden" name="ad_is_id" id="ad_is_id" value="<?php echo $ad_is_id; ?>">
                <input type="hidden" name="ad_id" value="<?php echo $advertise->ad_id; ?>">
            </form>

            <div class="col-sm-5 well" style="margin-top:-50px;min-height:712px;margin-bottom:0;border-bottom:none;">
                <h3>关联信息</h3>
                <div class="webpage_list">
                    <div id="webpage_list">
                        <?php foreach ($webpage_list as $key => $p) { ?>
                            <span class="col-xs-24" id="<?php echo 'page_info_box' . $key; ?>" style="border:1px dashed #666;height:100px;margin-bottom:20px;padding:0px">
                                                            <a href="<?php echo site_url('/welcome/create_infomation_urls/0/mobile-first?ic_url=').$p->adp_ic_url; ?>" target="_blank"> <em title="更新静态页面" class=" glyphicon glyphicon-refresh text-danger pull-right"></em></a>
                                    <em data-adpid="<?php echo $p->adp_id; ?>" title="删除" class="delete-page glyphicon glyphicon-minus-sign text-danger pull-left"></em><br/>
                                <div class="col-xs-24 edit_pageinfo" style="height:100%;padding:10px 10px;overflow:hidden;">

                                    <span class="text-muted urlinfo" data-adpid="<?php echo $p->adp_id; ?>" data-urlinfo="<?php echo $p->adp_ic_url; ?>"><?php echo $p->adp_ic_url; ?></span>
                                    <span class="col-xs-24 nopadding text-muted forselfinfo" style="font-size:12px;" data-forself="<?php echo $p->adp_forself; ?>">
                                        <?php
                                        switch ($p->adp_forself) {
                                            case 1:
                                                $text = '可同时运用于子节点';
                                                break;
                                            case 2:
                                                $text = '只用于当前页面';
                                                break;
                                            case 3:
                                                $text = '只用于子节点，不含当前页面';
                                                break;
                                            default :
                                                $text = '错误，请联系YCC';
                                                break;
                                        }
                                        echo $text;
                                        ?>
                                    </span>
                                </div>
                            </span>
                        <?php } ?>
                    </div>
                </div>

                <div class="col-xs-24 text-center" id="page_info_box" style="border:1px dashed #666;height:100px;margin-bottom:20px;">
                    <a href="javascript:void(0);" class="col-xs-24 glyphicon glyphicon-plus text-muted edit_pageinfo" style="line-height: 100px;font-size: 32px;"></a>
                </div>

                <div class="col-xs-24 nopadding">
                    <div class="col-xs-24 btn-lg"></div>
                    <div class="col-xs-24 btn-lg"></div>
                    <div class="btn-group col-xs-24 nopadding pull-right">
                        <button form="form_advertise_edit" type="button" class="btn btn-primary col-xs-19 btn_advertise_edit">保存修改</button>
                        <button class="btn dropdown-toggle btn-primary col-xs-5" data-toggle="dropdown">
                            <span class="caret"></span>
                        </button>
                        <ul class="dropdown-menu">
                            <li><a href="javascript:void(0);" onclick="deleteInfo();">删除信息</a></li>
                        </ul>
                    </div>

                </div>
            </div>

        </div>

    </div>
</div>

<div class="pageinfo_box hidden">
    <form class="col-xs-24" data-flag="form_pageinfo_edit" method="post" action="<?php echo site_url('thirdparty/advertise/index/page_edit'); ?>">
        <div class="col-xs-24 btn-lg"></div>
        <div class="form-group">
            <label for="exampleInputEmail1">页面链接URL</label>
            <input type="text" class="form-control adp_ic_url" name="adp_ic_url" placeholder="要关联的信息页面url 或信息标题ID,如:/beijing/">
        </div>
        <div class="col-xs-24 btn-sm"></div>

        <div class="form-group">
            <select class="form-control adp_forself" name="adp_forself">
                <option value="1">可同时在子节点启用</option>
                <option value="2">只用于当前页面</option>
                <option value="3">只用于子节点，不含当前页面</option>
            </select>
        </div>
        <input type="hidden" class="adp_id" name="adp_id">
        <input type="hidden" name="adp_ad_id" value="<?php echo $advertise->ad_id; ?>">
        <div class="row" style="margin:50px -30px -10px -30px;"><legend></legend></div>
        <button type="button" class="btn btn-sm btn-primary btn_pageinfo_edit pull-right">确定</button>
        <button type="button" class="btn btn-sm btn- pull-left" onclick="$('.edit_pageinfo').popover('destroy');">返回</button>
    </form>
</div>

<script type="text/javascript">


  var editor;

	
    $(document).ready(function() {
        //编辑器初始化

        editor =  CKEDITOR.replace("ad_content", {
			  width: '100%',
			  height: '560px',
			  filebrowserBrowseUrl:'/wysiwyg/ckfinder/ckfinder.php?sitecode=<?php echo $this->config->item('site_code'); ?>',
			  filebrowserUploadUrl: '/wysiwyg/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=image',
		});



        $(".center-col").css({"min-height": $(document).height()});

        //关联信息对话框及数据初始化
        $('body').on('click', '.edit_pageinfo', function() {
            $('.edit_pageinfo').popover('destroy');
            var $elem = $(this);
            var container = '#' + $elem.parent().attr('id');
            $elem.popover({
                'container': container,
                'title': 'title',
                'content': function() {
                },
                'placement': 'left',
                'trigger': 'click',
                'template': '<div class="popover recommend_info_popover" role="tooltip"><div class="arrow"></div><h3 class="popover-title hidden"></h3><div class="popover-content" style="width:500px;"></div><div class="col-xs-24" style="background:#eee;padding: 8px 10px;margin-top: -19px;border-bottom:1px solid #ddd;"><div class="col-xs-24" style="padding-left:0;"><div class="input-group"><h4 style="line-height:30px;margin:0;">关联要启用该广告的页面</h4></div></div></div><div class="col-xs-24" id="pageinfo_list"></div></div>'
            });
            $elem.popover('show');
            $elem.on('shown.bs.popover', function() {
                if ($elem.children('.urlinfo').length > 0) {
                    var urlinfo = $elem.children('.urlinfo').attr('data-urlinfo');
                    $(".pageinfo_box").find('.adp_ic_url').attr('value', urlinfo);
                    $(".pageinfo_box").find('.adp_id').attr('value', $elem.children('.urlinfo').attr('data-adpid'));
                } else {
                    $(".pageinfo_box").find('.adp_ic_url').attr('value', '');
                    $(".pageinfo_box").find('.adp_id').attr('value', '');
                }
                if ($elem.children('.forselfinfo').length > 0) {
                    var forselfinfo = $elem.children('.forselfinfo').attr('data-forself');
                    $(".pageinfo_box").find('.adp_forself').children('option').each(function() {
                        $(this).attr('value') == forselfinfo ? $(this).attr('selected', 'selected') : $(this).removeAttr('selected');
                    });
                }
                $("#pageinfo_list").html($(".pageinfo_box").html());
            });
        });
        //编辑广告内容
        $('.btn_advertise_edit').on('click', function() {
           editor.updateElement();
            submitForm('form_advertise_edit');
        });
        //添加关联信息
        $('body').on('click', '.btn_pageinfo_edit', function() {
            var $form = $(this).parent('form');
            if ($form.find('.adp_ic_url').val() == '') {
                $('.edit_pageinfo').popover('destroy');
                return false;
            }
            ;
            var formId = $form.attr('data-flag');
            $form.attr('id', formId);
            submitForm(formId);
            var ad_is_id = $("#ad_is_id").val();
            setTimeout(function() {
                $('.webpage_list').load('<?php echo site_url("thirdparty/advertise/index/detail"); ?>' + '/' + ad_is_id + ' #webpage_list');
            }, 0);
            $('.edit_pageinfo').popover('destroy');
        });
        //移除关联信息
        $(".webpage_list").on('click', '.delete-page', function(event) {
            if (!confirm("确认要移除该信息关联？")) {
                return false;
            }
            var adp_id = $(this).attr('data-adpid');
            var url = '<?php echo site_url("thirdparty/advertise/index/delete_page"); ?>' + '/' + adp_id;
            var that = $(this);
            $.post(url, '', function(res) {
                if (res == 1) {
                    that.parent().parent('a').remove();
                } else {
                    $.modaldialog.error('删除失败,请稍后再试！');
                }
            });
            return false;
        });

    });


 
    //选择内容图片
    function openKCFinder_Content() {
        var site_url = '<?php echo $this->config->item('site_url') ?>';
        var weburl = encodeURI(site_url);
        window.CallBack = openKCFinder_Content_callback;
        window.open('/media/popselectpicture.php?site_code=<?php echo $this->config->item('site_code'); ?>&site_lgc=<?php echo $this->config->item('site_lgc'); ?>&WebUrl=' + weburl, 'kcfinder_textbox', 'status=0, toolbar=0, location=0, menubar=0, directories=0,resizable=1, scrollbars=0, width=800, height=600');
    }
    function openKCFinder_Content_callback(result) {
        var site_image_url = '<?php echo $this->config->item('media_image_url') ?>';
        if (result != null) {
            for (var key in result.Pinfo) {
                editor.insertHtml('<img src="' + site_image_url + result.Pinfo[key].PUrl + '" alt="' + result.Pinfo[key].PName + '" width="' + result.Pinfo[key].Width + '" height="' + result.Pinfo[key].Height + '" />');
            }
        }
    }
</script>

<!-- 生成节点树结构 -->
<script type="text/javascript">
    zNodes =<?php echo json_encode($structure_list); ?>;
    var current_node = "<?php echo $ad_is_id; ?>"
    var indexpage = "<?php echo site_url('thirdparty/advertise/index/detail'); ?>"
    var addpage = "<?php echo site_url('thirdparty/advertise/index/add'); ?>"
    var movepage = "<?php echo site_url('thirdparty/advertise/index/tree_move'); ?>"
    var deletepage = "<?php echo site_url('thirdparty/advertise/index/delete'); ?>"
    var form_id = "form_advertise_edit";
</script>
<script type="text/javascript" src="/js/init_ztree.js"></script>
<!--日期选择器-->
<script src="/js/lyydate/jquery.lyydate.min.js?1"></script>
<script type="text/javascript">
    $('#ad_expire').lyydatepicker({
        format: "yyyy-mm-dd",
        language: "zh",
        max_width: "350",
        festival: true
    });
</script>